Ignore illegal thing status updates from REMOVING (#2313)

* Ignore illegal thing status updates from REMOVING

Ignore illegal thing status transitions from REMOVING instead of throwing IllegalArgumentException

Signed-off-by: Björn Lange <blange@itemis.com>
This commit is contained in:
Björn Lange 2021-04-26 08:42:16 +02:00 committed by GitHub
parent 0fe6b31f19
commit 6258268031
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 4 deletions

View File

@ -122,6 +122,7 @@ import org.slf4j.LoggerFactory;
* @author Henning Sudbrock - Consider thing type properties when migrating to new thing type
* @author Christoph Weitkamp - Added preconfigured ChannelGroupBuilder
* @author Yordan Zhelev - Added thing disabling mechanism
* @author Björn Lange - Ignore illegal thing status transitions instead of throwing IllegalArgumentException
*/
@NonNullByDefault
@Component(immediate = true, service = { ThingTypeMigrationService.class, ThingManager.class })
@ -193,10 +194,11 @@ public class ThingManagerImpl
if (ThingStatus.REMOVING.equals(oldStatusInfo.getStatus())
&& !ThingStatus.REMOVED.equals(statusInfo.getStatus())) {
// only allow REMOVING -> REMOVED transition, all others are illegal
throw new IllegalArgumentException(MessageFormat.format(
"Illegal status transition from REMOVING to {0}, only REMOVED would have been allowed.",
statusInfo.getStatus()));
// only allow REMOVING -> REMOVED transition, all others are ignored because they are illegal
logger.debug(
"Ignoring illegal status transition for thing {} from REMOVING to {}, only REMOVED would have been allowed.",
thing.getUID(), statusInfo.getStatus());
return;
}
// update thing status and send event about new status

View File

@ -111,6 +111,7 @@ import org.osgi.service.component.ComponentContext;
*
* @author Dennis Nobel - Initial contribution
* @author Wouter Born - Migrate tests from Groovy to Java
* @author Björn Lange - Ignore illegal thing status transitions instead of throwing IllegalArgumentException
*/
public class ThingManagerOSGiTest extends JavaOSGiTest {
@ -968,6 +969,45 @@ public class ThingManagerOSGiTest extends JavaOSGiTest {
expectException(() -> state.callback.statusUpdated(thing, removedNone), IllegalArgumentException.class);
}
@Test
public void thingManagerIgnoresRestoringOfThingStatusFromRemoving() {
class ThingHandlerState {
ThingHandlerCallback callback;
}
final ThingHandlerState state = new ThingHandlerState();
ThingHandler thingHandler = mock(ThingHandler.class);
managedThingProvider.add(thing);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
state.callback = (ThingHandlerCallback) invocation.getArgument(0);
return null;
}
}).when(thingHandler).setCallback(any(ThingHandlerCallback.class));
when(thingHandler.getThing()).thenReturn(thing);
ThingHandlerFactory thingHandlerFactory = mock(ThingHandlerFactory.class);
when(thingHandlerFactory.supportsThingType(any(ThingTypeUID.class))).thenReturn(true);
when(thingHandlerFactory.registerHandler(any(Thing.class))).thenReturn(thingHandler);
registerService(thingHandlerFactory);
final ThingStatusInfo removingNone = ThingStatusInfoBuilder.create(ThingStatus.REMOVING, ThingStatusDetail.NONE)
.build();
final ThingStatusInfo onlineNone = ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE)
.build();
thing.setStatusInfo(removingNone);
state.callback.statusUpdated(thing, onlineNone);
assertThat(thing.getStatusInfo(), is(removingNone));
}
private void expectException(Runnable runnable, Class<? extends Exception> exceptionType) {
try {
runnable.run();