Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
eclipse-archived#6577 Fix NPE in ThingManager.setEnabled() (eclipse-a…
Browse files Browse the repository at this point in the history
…rchived#6578)

The methods setEnabled() and isEnabled() of the ThingManager now throw
an IllegalArgumentException if there is no Thing for the given ThingUID.
This is now also documented in the ThingManager Javadoc.

Signed-off-by: Florian Stolte <fstolte@itemis.de>
  • Loading branch information
FStolte authored and htreu committed Nov 26, 2018
1 parent f1c0403 commit 259b1c4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Expand Up @@ -76,4 +76,20 @@ public void testThingHandlerFactoryLifecycle() {
verify(mockFactory2, atLeastOnce()).supportsThingType(any());
}

@Test(expected = IllegalArgumentException.class)
public void testCallSetEnabledWithUnknownThingUID() throws Exception {
ThingUID unknownUID = new ThingUID("someBundle", "someType", "someID");
ThingManagerImpl thingManager = new ThingManagerImpl();

thingManager.setEnabled(unknownUID, true);
}

@Test(expected = IllegalArgumentException.class)
public void testCallIsEnabledWithUnknownThingUID() throws Exception {
ThingUID unknownUID = new ThingUID("someBundle", "someType", "someID");
ThingManagerImpl thingManager = new ThingManagerImpl();

thingManager.isEnabled(unknownUID);
}

}
Expand Up @@ -27,6 +27,7 @@ public interface ThingManager {
* @param thingUID UID of the {@link Thing}.
* @return {@code false} when the {@link Thing} has {@link ThingStatus} with {@link ThingStatusDetail#DISABLED}.
* Returns {@code true} in all other cases.
* @throws IllegalArgumentException if there is no Thing with thingUID as its UID.
*/
public boolean isEnabled(ThingUID thingUID);

Expand All @@ -37,6 +38,7 @@ public interface ThingManager {
*
* @param thingUID UID of the {@link Thing}.
* @param isEnabled a new <b>enabled / disabled</b> state of the {@link Thing}.
* @throws IllegalArgumentException if there is no Thing with thingUID as its UID.
*/
public void setEnabled(ThingUID thingUID, boolean isEnabled);

Expand Down
Expand Up @@ -1208,6 +1208,11 @@ private void setThingStatus(Thing thing, ThingStatusInfo thingStatusInfo) {
@Override
public void setEnabled(ThingUID thingUID, boolean enabled) {
Thing thing = getThing(thingUID);

if (thing == null) {
throw new IllegalArgumentException(String.format("Thing with the UID '%s' is unknown, cannot set its enabled status.", thingUID));
}

if (enabled) {
// Enable a thing
// Clear the disabled thing storage. Otherwise the handler will NOT be initialized later.
Expand Down Expand Up @@ -1256,6 +1261,11 @@ public void setEnabled(ThingUID thingUID, boolean enabled) {
@Override
public boolean isEnabled(ThingUID thingUID) {
Thing thing = getThing(thingUID);

if (thing == null) {
throw new IllegalArgumentException(String.format("Thing with the UID '%s' is unknown, cannot get its enabled status.", thingUID));
}

return thing.isEnabled();
}

Expand Down

0 comments on commit 259b1c4

Please sign in to comment.