use of org.eclipse.smarthome.core.events.EventSubscriber in project smarthome by eclipse.
the class RunRuleModuleTest method sceneActivatedByRule.
@Test
public void sceneActivatedByRule() throws ItemNotFoundException, InterruptedException {
final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
Assert.assertNotNull(ruleRegistry);
// Scene rule
final Rule sceneRule = createSceneRule();
logger.info("SceneRule created: {}", sceneRule.getUID());
ruleRegistry.add(sceneRule);
ruleRegistry.setEnabled(sceneRule.getUID(), true);
waitForAssert(() -> {
Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatusInfo(sceneRule.getUID()).getStatus());
});
// Outer rule
final Rule outerRule = createOuterRule();
logger.info("SceneActivationRule created: {}", outerRule.getUID());
ruleRegistry.add(outerRule);
ruleRegistry.setEnabled(outerRule.getUID(), true);
waitForAssert(() -> {
Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatusInfo(outerRule.getUID()).getStatus());
});
// Test rule
final EventPublisher eventPublisher = getService(EventPublisher.class);
Assert.assertNotNull(eventPublisher);
final ItemRegistry itemRegistry = getService(ItemRegistry.class);
Assert.assertNotNull(itemRegistry);
final Queue<Event> events = new LinkedList<>();
registerService(new EventSubscriber() {
@Override
public void receive(final Event event) {
logger.info("Event: {}", event.getTopic());
events.add(event);
}
@Override
public Set<String> getSubscribedEventTypes() {
return Collections.singleton(ItemCommandEvent.TYPE);
}
@Override
public EventFilter getEventFilter() {
return null;
}
});
// trigger rule by switching triggerItem ON
eventPublisher.post(ItemEventFactory.createStateEvent("ruleTrigger", OnOffType.ON));
waitForAssert(() -> {
assertFalse(events.isEmpty());
ItemCommandEvent event = (ItemCommandEvent) events.remove();
assertEquals("smarthome/items/switch3/command", event.getTopic());
assertEquals(OnOffType.ON, event.getItemCommand());
});
}
use of org.eclipse.smarthome.core.events.EventSubscriber in project smarthome by eclipse.
the class RuntimeRuleTest method testPredefinedRule.
@Test
@Ignore
public void testPredefinedRule() throws ItemNotFoundException, InterruptedException {
final EventPublisher eventPublisher = getService(EventPublisher.class);
// final ItemRegistry itemRegistry = getService(ItemRegistry.class);
// final SwitchItem myMotionItem = (SwitchItem) itemRegistry.getItem("myMotionItem");
// eventPublisher.post(ItemEventFactory.createStateEvent("myPresenceItem", OnOffType.ON));
final Queue<Event> events = new LinkedList<>();
registerService(new EventSubscriber() {
@Override
public void receive(final Event event) {
logger.info("Event: {}", event.getTopic());
events.add(event);
}
@Override
public Set<String> getSubscribedEventTypes() {
return Collections.singleton(ItemCommandEvent.TYPE);
}
@Override
public EventFilter getEventFilter() {
return null;
}
});
eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem", OnOffType.ON));
waitForAssert(() -> {
assertFalse(events.isEmpty());
ItemCommandEvent event = (ItemCommandEvent) events.remove();
assertEquals("smarthome/items/myLampItem/command", event.getTopic());
assertEquals(OnOffType.ON, event.getItemCommand());
});
}
use of org.eclipse.smarthome.core.events.EventSubscriber in project smarthome by eclipse.
the class ThingRegistryOSGiTest method assertThatThingRegistryEventSubscribersReceiveEventsAboutThingChanges.
@Test
public void assertThatThingRegistryEventSubscribersReceiveEventsAboutThingChanges() {
EventSubscriber thingRegistryEventSubscriber = new EventSubscriber() {
@Override
public Set<String> getSubscribedEventTypes() {
Set<String> types = new HashSet<>();
types.add(ThingAddedEvent.TYPE);
types.add(ThingRemovedEvent.TYPE);
types.add(ThingUpdatedEvent.TYPE);
return types;
}
@Override
public EventFilter getEventFilter() {
return null;
}
@Override
public void receive(Event event) {
receivedEvent = event;
}
};
registerService(thingRegistryEventSubscriber);
// add new thing
managedThingProvider.add(THING);
waitForAssert(() -> {
assertThat(receivedEvent, notNullValue());
});
assertThat(receivedEvent, is(instanceOf(ThingAddedEvent.class)));
receivedEvent = null;
// update thing
Thing updatedThing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
managedThingProvider.update(updatedThing);
waitForAssert(() -> {
assertThat(receivedEvent, notNullValue());
});
assertThat(receivedEvent, is(instanceOf(ThingUpdatedEvent.class)));
receivedEvent = null;
// remove thing
managedThingProvider.remove(THING.getUID());
waitForAssert(() -> {
assertThat(receivedEvent, notNullValue());
});
assertThat(receivedEvent, is(instanceOf(ThingRemovedEvent.class)));
receivedEvent = null;
}
use of org.eclipse.smarthome.core.events.EventSubscriber in project smarthome by eclipse.
the class NtpOSGiTest method assertEventIsReceived.
private void assertEventIsReceived(UpdateEventType updateEventType, String channelID, String acceptedItemType) {
Configuration configuration = new Configuration();
initialize(configuration, channelID, acceptedItemType, null, null);
EventSubscriber eventSubscriberMock = mock(EventSubscriber.class);
when(eventSubscriberMock.getSubscribedEventTypes()).thenReturn(Collections.singleton(ItemStateEvent.TYPE));
registerService(eventSubscriberMock);
if (updateEventType.equals(UpdateEventType.HANDLE_COMMAND)) {
ntpHandler.handleCommand(new ChannelUID("ntp:test:chan:1"), new StringType("test"));
} else if (updateEventType.equals(UpdateEventType.CHANNEL_LINKED)) {
ntpHandler.channelLinked(new ChannelUID("ntp:test:chan:1"));
}
waitForAssert(() -> {
verify(eventSubscriberMock, atLeastOnce()).receive(ArgumentMatchers.any(Event.class));
});
}
use of org.eclipse.smarthome.core.events.EventSubscriber in project smarthome by eclipse.
the class GroupItemTest method setUp.
@Before
public void setUp() {
initMocks(this);
registerVolatileStorageService();
publisher = event -> events.add(event);
itemRegistry = getService(ItemRegistry.class);
assertNotNull(itemRegistry);
registerService(new EventSubscriber() {
@Override
public void receive(Event event) {
events.add(event);
}
@Override
public Set<String> getSubscribedEventTypes() {
HashSet<String> hs = new HashSet<>();
hs.add(ItemUpdatedEvent.TYPE);
return hs;
}
@Override
public EventFilter getEventFilter() {
return null;
}
});
when(unitProvider.getUnit(Temperature.class)).thenReturn(Units.CELSIUS);
groupFunctionHelper = new GroupFunctionHelper();
itemStateConverter = new ItemStateConverterImpl();
((ItemStateConverterImpl) itemStateConverter).setUnitProvider(unitProvider);
}
Aggregations