use of org.openhab.core.thing.binding.ThingHandlerFactory in project openhab-core by openhab.
the class ThingManagerOSGiTest method thingManagerChangesTheThingTypeCorrectlyEvenIfInitializeTakesLongAndCalledFromThere.
@Test
public void thingManagerChangesTheThingTypeCorrectlyEvenIfInitializeTakesLongAndCalledFromThere() {
registerThingTypeProvider();
ThingTypeUID newThingTypeUID = new ThingTypeUID("binding:type2");
class ThingHandlerState {
boolean initializeRunning;
boolean raceCondition;
boolean migrateBlocked;
@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));
doAnswer(new Answer<Void>() {
@Override
@Nullable
public Void answer(InvocationOnMock invocation) throws Throwable {
if (state.initializeRunning) {
state.raceCondition = true;
}
state.initializeRunning = true;
long start = System.nanoTime();
state.callback.migrateThingType(thing, newThingTypeUID, thing.getConfiguration());
if (System.nanoTime() - start > TimeUnit.SECONDS.toNanos(1)) {
state.migrateBlocked = true;
}
Thread.sleep(3000);
state.initializeRunning = false;
return null;
}
}).when(thingHandler).initialize();
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);
managedThingProvider.add(thing);
waitForAssert(() -> assertThat(thing.getThingTypeUID().getAsString(), is(equalTo(newThingTypeUID.getAsString()))));
assertThat(state.migrateBlocked, is(false));
assertThat(state.raceCondition, is(false));
}
use of org.openhab.core.thing.binding.ThingHandlerFactory in project openhab-core by openhab.
the class ThingManagerOSGiTest method thingManagerDoesNotDelegateUpdateEventsToItsSource.
@Test
public void thingManagerDoesNotDelegateUpdateEventsToItsSource() {
registerThingTypeProvider();
class ThingHandlerState {
boolean handleCommandWasCalled;
@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));
doAnswer(new Answer<Void>() {
@Override
@Nullable
public Void answer(InvocationOnMock invocation) throws Throwable {
state.handleCommandWasCalled = true;
state.callback.statusUpdated(thing, ThingStatusInfoBuilder.create(ThingStatus.ONLINE).build());
return null;
}
}).when(thingHandler).handleCommand(any(ChannelUID.class), any(Command.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);
String itemName = "name";
managedThingProvider.add(thing);
managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, CHANNEL_UID));
registerService(thingHandlerFactory);
Item item = new StringItem(itemName);
itemRegistry.add(item);
waitForAssert(() -> assertThat(itemRegistry.get(itemName), is(notNullValue())));
state.callback.statusUpdated(thing, ThingStatusInfoBuilder.create(ThingStatus.ONLINE).build());
// event should be delivered
eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, OnOffType.ON));
waitForAssert(() -> assertThat(state.handleCommandWasCalled, is(true)));
state.handleCommandWasCalled = false;
// event should not be delivered, because the source is the same
eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, OnOffType.ON, CHANNEL_UID.toString()));
waitFor(() -> state.handleCommandWasCalled);
assertThat(state.handleCommandWasCalled, is(false));
}
use of org.openhab.core.thing.binding.ThingHandlerFactory 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.binding.ThingHandlerFactory in project openhab-core by openhab.
the class ThingManagerOSGiTest method thingManagerWaitsWithInitializeUntilBundleProcessingIsFinished.
@Test
@SuppressWarnings("null")
public void thingManagerWaitsWithInitializeUntilBundleProcessingIsFinished() throws Exception {
Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
class ThingHandlerState {
@SuppressWarnings("unused")
@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 ReadyMarker marker = new ReadyMarker(ThingManagerImpl.XML_THING_TYPE, ReadyMarkerUtils.getIdentifier(FrameworkUtil.getBundle(this.getClass())));
waitForAssert(() -> {
// wait for the XML processing to be finished, then remove the ready marker again
assertThat(readyService.isReady(marker), is(true));
readyService.unmarkReady(marker);
});
ThingStatusInfo uninitializedNone = ThingStatusInfoBuilder.create(ThingStatus.UNINITIALIZED, ThingStatusDetail.NONE).build();
assertThat(thing.getStatusInfo(), is(uninitializedNone));
managedThingProvider.add(thing);
// just wait a little to make sure really nothing happens
Thread.sleep(1000);
verify(thingHandler, never()).initialize();
assertThat(thing.getStatusInfo(), is(uninitializedNone));
readyService.markReady(marker);
// ThingHandler.initialize() called, thing status is INITIALIZING.NONE
ThingStatusInfo initializingNone = ThingStatusInfoBuilder.create(ThingStatus.INITIALIZING, ThingStatusDetail.NONE).build();
waitForAssert(() -> {
verify(thingHandler, times(1)).initialize();
assertThat(thing.getStatusInfo(), is(initializingNone));
});
}
use of org.openhab.core.thing.binding.ThingHandlerFactory in project openhab-core by openhab.
the class GenericThingProviderMultipleBundlesTest method setup.
@BeforeEach
public void setup() {
thingProvider = new GenericThingProvider();
Bundle bundle = mock(Bundle.class);
when(bundle.getSymbolicName()).thenReturn(BUNDLE_NAME);
BundleResolver bundleResolver = mock(BundleResolver.class);
when(bundleResolver.resolveBundle(any(Class.class))).thenReturn(bundle);
thingProvider.setBundleResolver(bundleResolver);
ModelRepository modelRepository = mock(ModelRepository.class);
ThingModel thingModel = mock(ThingModel.class);
EList<ModelThing> dslThings = createModelBridge();
when(thingModel.getThings()).thenReturn(dslThings);
when(modelRepository.getModel(TEST_MODEL_THINGS)).thenReturn(thingModel);
thingProvider.setModelRepository(modelRepository);
// configure bridgeHandlerFactory to accept the bridge type UID and create a bridge:
bridgeHandlerFactory = mock(ThingHandlerFactory.class);
when(bridgeHandlerFactory.supportsThingType(BRIDGE_TYPE_UID)).thenReturn(true);
when(bridgeHandlerFactory.createThing(eq(BRIDGE_TYPE_UID), any(Configuration.class), eq(BRIDGE_UID), eq(null))).thenReturn(BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_ID).build());
thingProvider.addThingHandlerFactory(bridgeHandlerFactory);
// configure thingHandlerFactory to accept the thing type UID and create a thing:
thingHandlerFactory = mock(ThingHandlerFactory.class);
when(thingHandlerFactory.supportsThingType(THING_TYPE_UID)).thenReturn(true);
when(thingHandlerFactory.createThing(eq(THING_TYPE_UID), any(Configuration.class), eq(THING_UID), eq(BRIDGE_UID))).thenReturn(ThingBuilder.create(THING_TYPE_UID, THING_ID).build());
thingProvider.addThingHandlerFactory(thingHandlerFactory);
}
Aggregations