use of org.openhab.core.thing.ThingStatusInfo in project openhab-core by openhab.
the class ThingManagerOSGiJavaTest method testSetEnabledWithoutHandlerFactory.
@Test
public void testSetEnabledWithoutHandlerFactory() throws Exception {
registerThingTypeProvider();
ThingStatusInfo thingStatusInfo = ThingStatusInfoBuilder.create(ThingStatus.UNINITIALIZED, ThingStatusDetail.NONE).build();
thing.setStatusInfo(thingStatusInfo);
new Thread((Runnable) () -> managedThingProvider.add(thing)).start();
waitForAssert(() -> {
assertThat(thingRegistry.get(THING_UID), is(notNullValue()));
assertThat(thing.getStatus(), is(ThingStatus.UNINITIALIZED));
});
Thread.sleep(1000);
thingManager.setEnabled(THING_UID, false);
waitForAssert(() -> {
assertThat(storage.containsKey(THING_UID.getAsString()), is(true));
assertThat(thing.getStatus(), is(ThingStatus.UNINITIALIZED));
assertThat(thing.getStatusInfo().getStatusDetail(), is(ThingStatusDetail.DISABLED));
}, SafeCaller.DEFAULT_TIMEOUT - 100, 50);
thingManager.setEnabled(THING_UID, true);
waitForAssert(() -> {
assertThat(storage.containsKey(THING_UID.getAsString()), is(false));
assertThat(thing.getStatus(), is(ThingStatus.UNINITIALIZED));
assertThat(thing.getStatusInfo().getStatusDetail(), is(ThingStatusDetail.HANDLER_MISSING_ERROR));
}, SafeCaller.DEFAULT_TIMEOUT - 100, 50);
}
use of org.openhab.core.thing.ThingStatusInfo in project openhab-core by openhab.
the class ThingManagerOSGiJavaTest method testInitializeNotInvokedOnAlreadyEnabledThing.
@Test
public void testInitializeNotInvokedOnAlreadyEnabledThing() {
AtomicReference<ThingHandlerCallback> thingHandlerCallback = new AtomicReference<>();
AtomicReference<Boolean> initializeInvoked = new AtomicReference<>(false);
AtomicReference<Boolean> disposeInvoked = new AtomicReference<>(false);
registerThingHandlerFactory(THING_TYPE_UID, thing -> {
ThingHandler mockHandler = mock(ThingHandler.class);
doAnswer(a -> {
thingHandlerCallback.set((ThingHandlerCallback) a.getArguments()[0]);
return null;
}).when(mockHandler).setCallback(ArgumentMatchers.isA(ThingHandlerCallback.class));
doAnswer(a -> {
initializeInvoked.set(true);
// call thingUpdated() from within initialize()
thingHandlerCallback.get().thingUpdated(thing);
// hang on a little to provoke a potential dead-lock
Thread.sleep(1000);
ThingStatusInfo thingStatusInfo = ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).build();
thing.setStatusInfo(thingStatusInfo);
return null;
}).when(mockHandler).initialize();
doAnswer(a -> {
disposeInvoked.set(true);
return null;
}).when(mockHandler).dispose();
when(mockHandler.getThing()).thenReturn(thing);
return mockHandler;
});
ThingStatusInfo enabledStatusInfo = ThingStatusInfoBuilder.create(ThingStatus.UNINITIALIZED, ThingStatusDetail.NONE).build();
thing.setStatusInfo(enabledStatusInfo);
new Thread((Runnable) () -> managedThingProvider.add(thing)).start();
waitForAssert(() -> {
assertThat(initializeInvoked.get(), is(true));
assertThat(thing.getStatus(), is(ThingStatus.INITIALIZING));
}, SafeCaller.DEFAULT_TIMEOUT - 100, 50);
initializeInvoked.set(false);
// enable the thing
new Thread((Runnable) () -> thingManager.setEnabled(thing.getUID(), true)).start();
waitForAssert(() -> {
assertThat(thing.getStatus(), is(ThingStatus.ONLINE));
assertThat(initializeInvoked.get(), is(false));
}, SafeCaller.DEFAULT_TIMEOUT - 100, 50);
}
use of org.openhab.core.thing.ThingStatusInfo in project openhab-core by openhab.
the class ThingManagerOSGiTest method thingManagerPostsLocalizedThingStatusInfoAndThingStatusInfoChangedEvents.
@Test
@SuppressWarnings("null")
public void thingManagerPostsLocalizedThingStatusInfoAndThingStatusInfoChangedEvents() throws Exception {
registerThingTypeProvider();
class ThingHandlerState {
@Nullable
ThingHandlerCallback callback;
}
final ThingHandlerState state = new ThingHandlerState();
ThingHandler thingHandler = mock(ThingHandler.class);
doAnswer(new Answer<Void>() {
@Override
@Nullable
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);
BundleResolver bundleResolver = mock(BundleResolver.class);
when(bundleResolver.resolveBundle(any())).thenReturn(bundleContext.getBundle());
ThingStatusInfoI18nLocalizationService thingStatusInfoI18nLocalizationService = getService(ThingStatusInfoI18nLocalizationService.class);
thingStatusInfoI18nLocalizationService.setBundleResolver(bundleResolver);
final List<ThingStatusInfoEvent> infoEvents = new ArrayList<>();
EventSubscriber thingStatusInfoEventSubscriber = new EventSubscriber() {
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(ThingStatusInfoEvent.TYPE);
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
@Override
public void receive(Event event) {
infoEvents.add((ThingStatusInfoEvent) event);
}
};
registerService(thingStatusInfoEventSubscriber);
final List<ThingStatusInfoChangedEvent> infoChangedEvents = new ArrayList<>();
EventSubscriber thingStatusInfoChangedEventSubscriber = new EventSubscriber() {
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(ThingStatusInfoChangedEvent.TYPE);
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
@Override
public void receive(Event event) {
infoChangedEvents.add((ThingStatusInfoChangedEvent) event);
}
};
registerService(thingStatusInfoChangedEventSubscriber);
// add thing (UNINITIALIZED -> INITIALIZING)
managedThingProvider.add(thing);
waitForAssert(() -> {
assertThat(infoEvents.size(), is(1));
assertThat(infoChangedEvents.size(), is(1));
});
assertThat(infoEvents.get(0).getType(), is(ThingStatusInfoEvent.TYPE));
assertThat(infoEvents.get(0).getTopic(), is("openhab/things/binding:type:id/status"));
assertThat(infoEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
assertThat(infoEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
assertThat(infoEvents.get(0).getStatusInfo().getDescription(), is(nullValue()));
assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
assertThat(infoChangedEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
assertThat(infoChangedEvents.get(0).getStatusInfo().getDescription(), is(nullValue()));
assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.UNINITIALIZED));
assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
assertThat(infoChangedEvents.get(0).getOldStatusInfo().getDescription(), is(nullValue()));
infoEvents.clear();
infoChangedEvents.clear();
LocaleProvider localeProvider = getService(LocaleProvider.class);
assertThat(localeProvider, is(notNullValue()));
Locale defaultLocale = localeProvider.getLocale();
// set status to ONLINE (INITIALIZING -> ONLINE)
new DefaultLocaleSetter(configurationAdmin).setDefaultLocale(Locale.ENGLISH);
waitForAssert(() -> assertThat(localeProvider.getLocale(), is(Locale.ENGLISH)));
ThingStatusInfo onlineNone = ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).withDescription("@text/online").build();
state.callback.statusUpdated(thing, onlineNone);
waitForAssert(() -> {
assertThat(infoEvents.size(), is(1));
assertThat(infoChangedEvents.size(), is(1));
});
assertThat(infoEvents.get(0).getType(), is(ThingStatusInfoEvent.TYPE));
assertThat(infoEvents.get(0).getTopic(), is("openhab/things/binding:type:id/status"));
assertThat(infoEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.ONLINE));
assertThat(infoEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
assertThat(infoEvents.get(0).getStatusInfo().getDescription(), is("Thing is online."));
assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.ONLINE));
assertThat(infoChangedEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
assertThat(infoChangedEvents.get(0).getStatusInfo().getDescription(), is("Thing is online."));
assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
assertThat(infoChangedEvents.get(0).getOldStatusInfo().getDescription(), is(nullValue()));
infoEvents.clear();
infoChangedEvents.clear();
// set status to OFFLINE (ONLINE -> OFFLINE)
new DefaultLocaleSetter(configurationAdmin).setDefaultLocale(Locale.GERMAN);
waitForAssert(() -> assertThat(localeProvider.getLocale(), is(Locale.GERMAN)));
ThingStatusInfo offlineNone = ThingStatusInfoBuilder.create(ThingStatus.OFFLINE, ThingStatusDetail.NONE).withDescription("@text/offline.without-param").build();
state.callback.statusUpdated(thing, offlineNone);
waitForAssert(() -> {
assertThat(infoEvents.size(), is(1));
assertThat(infoChangedEvents.size(), is(1));
});
assertThat(infoEvents.get(0).getType(), is(ThingStatusInfoEvent.TYPE));
assertThat(infoEvents.get(0).getTopic(), is("openhab/things/binding:type:id/status"));
assertThat(infoEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.OFFLINE));
assertThat(infoEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
assertThat(infoEvents.get(0).getStatusInfo().getDescription(), is("Thing ist offline."));
assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.OFFLINE));
assertThat(infoChangedEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
assertThat(infoChangedEvents.get(0).getStatusInfo().getDescription(), is("Thing ist offline."));
assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.ONLINE));
assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
assertThat(infoChangedEvents.get(0).getOldStatusInfo().getDescription(), is("Thing ist online."));
new DefaultLocaleSetter(configurationAdmin).setDefaultLocale(defaultLocale);
waitForAssert(() -> assertThat(localeProvider.getLocale(), is(defaultLocale)));
}
use of org.openhab.core.thing.ThingStatusInfo in project openhab-core by openhab.
the class ThingManagerOSGiTest method thingManagerHandlesThingStatusUpdateUninitializedWithAnExceptionCorrectly.
@Test
public void thingManagerHandlesThingStatusUpdateUninitializedWithAnExceptionCorrectly() {
String exceptionMessage = "Some runtime exception occurred!";
ThingHandler thingHandler = mock(ThingHandler.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))).thenThrow(new RuntimeException(exceptionMessage));
registerService(thingHandlerFactory);
managedThingProvider.add(thing);
ThingStatusInfo statusInfo = ThingStatusInfoBuilder.create(ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_REGISTERING_ERROR).withDescription(exceptionMessage).build();
assertThat(thing.getStatusInfo(), is(statusInfo));
}
use of org.openhab.core.thing.ThingStatusInfo in project openhab-core by openhab.
the class ThingManagerOSGiTest method thingManagerPostsThingStatusChangedEventsIfTheStatusOfAThingIsChanged.
@Test
public void thingManagerPostsThingStatusChangedEventsIfTheStatusOfAThingIsChanged() throws Exception {
registerThingTypeProvider();
class ThingHandlerState {
@Nullable
ThingHandlerCallback callback;
}
final ThingHandlerState state = new ThingHandlerState();
ThingHandler thingHandler = mock(ThingHandler.class);
doAnswer(new Answer<Void>() {
@Override
@Nullable
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 List<ThingStatusInfoChangedEvent> infoChangedEvents = new ArrayList<>();
EventSubscriber thingStatusEventSubscriber = new EventSubscriber() {
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(ThingStatusInfoChangedEvent.TYPE);
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
@Override
public void receive(Event event) {
infoChangedEvents.add((ThingStatusInfoChangedEvent) event);
}
};
registerService(thingStatusEventSubscriber);
// add thing (UNINITIALIZED -> INITIALIZING)
managedThingProvider.add(thing);
waitForAssert(() -> assertThat(infoChangedEvents.size(), is(1)));
assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.UNINITIALIZED));
infoChangedEvents.clear();
// set status to ONLINE (INITIALIZING -> ONLINE)
ThingStatusInfo onlineNone = ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).build();
state.callback.statusUpdated(thing, onlineNone);
waitForAssert(() -> assertThat(infoChangedEvents.size(), is(1)));
assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.ONLINE));
assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
infoChangedEvents.clear();
// set status to ONLINE again
state.callback.statusUpdated(thing, onlineNone);
// make sure no event has been sent
Thread.sleep(500);
assertThat(infoChangedEvents.size(), is(0));
}
Aggregations