[inbox] Approval with newThingId: only one segment allowed (#2338)

* [inbox] Approval with newThingId: only one segment allowed

Build of the new thing UID corrected in case newThingId is provided

Fix #2331

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
pull/2345/head
lolodomo 2021-05-04 19:11:25 +02:00 committed by GitHub
parent 141e73c85f
commit 6eee84661f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View File

@ -36,6 +36,7 @@ import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.common.AbstractUID;
import org.openhab.core.common.ThreadPoolManager;
import org.openhab.core.config.core.ConfigDescription;
import org.openhab.core.config.core.ConfigDescriptionParameter;
@ -183,6 +184,9 @@ public final class PersistentInbox implements Inbox, DiscoveryListener, ThingReg
if (results.isEmpty()) {
throw new IllegalArgumentException("No Thing with UID " + thingUID.getAsString() + " in inbox");
}
if (newThingId != null && newThingId.contains(AbstractUID.SEPARATOR)) {
throw new IllegalArgumentException("New Thing ID " + newThingId + " must not contain multiple segments");
}
DiscoveryResult result = results.get(0);
final Map<String, String> properties = new HashMap<>();
final Map<String, Object> configParams = new HashMap<>();
@ -191,11 +195,12 @@ public final class PersistentInbox implements Inbox, DiscoveryListener, ThingReg
ThingTypeUID thingTypeUID = result.getThingTypeUID();
ThingUID newThingUID = thingUID;
if (newThingId != null) {
String newUID = thingUID.getAsString().substring(0,
thingUID.getAsString().lastIndexOf(AbstractUID.SEPARATOR) + 1) + newThingId;
try {
newThingUID = new ThingUID(thingTypeUID, newThingId);
newThingUID = new ThingUID(newUID);
} catch (IllegalArgumentException e) {
logger.warn("Cannot create thing: {}", e.getMessage());
return null;
throw new IllegalArgumentException("Invalid thing UID " + newUID, e);
}
}
Thing newThing = ThingFactory.createThing(newThingUID, config, properties, result.getBridgeUID(), thingTypeUID,

View File

@ -172,7 +172,15 @@ public class PersistentInboxTest {
when(storage.getValues()).thenReturn(List.of(result));
inbox.activate();
assertNull(inbox.approve(THING_UID, "Test", "invalid:id"));
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
inbox.approve(THING_UID, "Test", "invalid:id");
});
assertEquals("New Thing ID invalid:id must not contain multiple segments", exception.getMessage());
exception = assertThrows(IllegalArgumentException.class, () -> {
inbox.approve(THING_UID, "Test", "invalid$id");
});
assertEquals("Invalid thing UID test:test:invalid$id", exception.getMessage());
}
@Test