use of org.openhab.core.events.Event in project openhab-core by openhab.
the class RuntimeRuleTest method testPredefinedRule.
@Test
@Disabled
public void testPredefinedRule() throws ItemNotFoundException, InterruptedException {
final Queue<Event> events = new LinkedList<>();
subscribeToEvents(ItemCommandEvent.TYPE, events);
final EventPublisher eventPublisher = getService(EventPublisher.class);
eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem", OnOffType.ON));
waitForAssert(() -> {
assertFalse(events.isEmpty());
ItemCommandEvent event = (ItemCommandEvent) events.remove();
assertEquals("openhab/items/myLampItem/command", event.getTopic());
assertEquals(OnOffType.ON, event.getItemCommand());
});
}
use of org.openhab.core.events.Event in project openhab-core by openhab.
the class RuntimeRuleTest method compareConditionWorks.
@Test
public void compareConditionWorks() {
final Configuration conditionConfig = newRightOperatorConfig("ON", "=");
final Map<String, String> inputs = Map.of("input", "someTrigger.someoutput");
Condition condition = ModuleBuilder.createCondition().withId("id").withTypeUID("core.GenericCompareCondition").withConfiguration(conditionConfig).withInputs(inputs).build();
CompareConditionHandler handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, true, OnOffType.ON);
assertSatisfiedHandlerInput(handler, true, "ON");
assertSatisfiedHandlerInput(handler, false, OnOffType.OFF);
assertSatisfiedHandlerInput(handler, false, "OFF");
condition = ModuleBuilder.createCondition(condition).withConfiguration(newRightOperatorConfig("21", "=")).build();
handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, true, 21);
assertSatisfiedHandlerInput(handler, false, 22);
condition = ModuleBuilder.createCondition(condition).withConfiguration(newRightOperatorConfig("21", "<")).build();
handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, true, 20);
assertSatisfiedHandlerInput(handler, false, 22);
assertSatisfiedHandlerInput(handler, true, 20l);
assertSatisfiedHandlerInput(handler, false, 22l);
assertSatisfiedHandlerInput(handler, true, 20.9d);
assertSatisfiedHandlerInput(handler, false, 21.1d);
condition = ModuleBuilder.createCondition(condition).withConfiguration(newRightOperatorConfig("21", "<=")).build();
handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, true, 20);
assertSatisfiedHandlerInput(handler, true, 21);
assertSatisfiedHandlerInput(handler, false, 22);
assertSatisfiedHandlerInput(handler, true, 20l);
assertSatisfiedHandlerInput(handler, true, 21l);
assertSatisfiedHandlerInput(handler, false, 22l);
assertSatisfiedHandlerInput(handler, true, 20.9d);
assertSatisfiedHandlerInput(handler, true, 21.0d);
assertSatisfiedHandlerInput(handler, false, 21.1d);
condition = ModuleBuilder.createCondition(condition).withConfiguration(newRightOperatorConfig("21", "<")).build();
handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, true, 20);
assertSatisfiedHandlerInput(handler, false, 22);
assertSatisfiedHandlerInput(handler, true, 20l);
assertSatisfiedHandlerInput(handler, false, 22l);
assertSatisfiedHandlerInput(handler, true, 20.9d);
assertSatisfiedHandlerInput(handler, false, 21.1d);
condition = ModuleBuilder.createCondition(condition).withConfiguration(newRightOperatorConfig("21", "<=")).build();
handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, true, 20);
assertSatisfiedHandlerInput(handler, true, 21);
assertSatisfiedHandlerInput(handler, false, 22);
assertSatisfiedHandlerInput(handler, true, 20l);
assertSatisfiedHandlerInput(handler, true, 21l);
assertSatisfiedHandlerInput(handler, false, 22l);
assertSatisfiedHandlerInput(handler, true, 20.9d);
assertSatisfiedHandlerInput(handler, true, 21.0d);
assertSatisfiedHandlerInput(handler, false, 21.1d);
condition = ModuleBuilder.createCondition(condition).withConfiguration(newRightOperatorConfig(".*anything.*", "matches")).build();
handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, false, "something matches?");
assertSatisfiedHandlerInput(handler, true, "anything matches?");
assertFalse(handler.isSatisfied(Map.of("nothing", "nothing")));
condition = ModuleBuilder.createCondition(condition).withConfiguration(newRightOperatorConfig("ONOFF", "matches")).build();
handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, false, OnOffType.ON);
final Event event = ItemEventFactory.createStateEvent("itemName", OnOffType.OFF, "source");
condition = ModuleBuilder.createCondition(condition).withConfiguration(newRightOperatorInputPropertyConfig(".*ON.*", "matches", "itemName")).build();
handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, false, event);
condition = ModuleBuilder.createCondition(condition).withConfiguration(newRightOperatorInputPropertyConfig("itemName", "matches", "itemName")).build();
handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, true, event);
condition = ModuleBuilder.createCondition(condition).withConfiguration(newRightOperatorConfig("null", "=")).build();
handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, true, null);
condition = ModuleBuilder.createCondition(condition).withConfiguration(newRightOperatorConfig("notnull", "=")).build();
handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, false, null);
condition = ModuleBuilder.createCondition(condition).withConfiguration(newRightOperatorConfig("ON", "<")).build();
handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, false, OnOffType.ON);
condition = ModuleBuilder.createCondition(condition).withConfiguration(newRightOperatorInputPropertyConfig("ON", "<", "nothing")).build();
handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, false, event);
condition = ModuleBuilder.createCondition(condition).withConfiguration(newRightOperatorInputPropertyConfig("ON", "=", "nothing")).build();
handler = new CompareConditionHandler(condition);
assertSatisfiedHandlerInput(handler, true, "ON");
}
use of org.openhab.core.events.Event in project openhab-core by openhab.
the class AbstractRemoteAddonServiceTest method checkResult.
/**
* checks that a proper event is posted, the presence in storage and installation status in handler
*
* @param id add-on id (without service-prefix)
* @param expectedEventTopic the expected event (e.g. installed)
* @param installStatus the expected installation status of the add-on
* @param present if the addon is expected to be present after the test
*/
private void checkResult(String id, String expectedEventTopic, boolean installStatus, boolean present) {
// assert expected event is posted
ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
Mockito.verify(eventPublisher).post(eventCaptor.capture());
Event event = eventCaptor.getValue();
String topic = "openhab/addons/" + expectedEventTopic;
Assertions.assertEquals(topic, event.getTopic());
// assert addon handler was called (by checking it's installed status)
Assertions.assertEquals(installStatus, addonHandler.isInstalled(getFullAddonId(id)));
// assert is present in storage if installed or missing if uninstalled
Assertions.assertEquals(installStatus, storage.containsKey(id));
// assert correct installation status is reported for addon
Addon addon = addonService.getAddon(id, null);
if (present) {
Assertions.assertNotNull(addon);
Objects.requireNonNull(addon);
Assertions.assertEquals(installStatus, addon.isInstalled());
} else {
Assertions.assertNull(addon);
}
}
use of org.openhab.core.events.Event in project openhab-core by openhab.
the class AbstractRemoteAddonService method postUninstalledEvent.
private void postUninstalledEvent(String extensionId) {
Event event = AddonEventFactory.createAddonUninstalledEvent(extensionId);
eventPublisher.post(event);
}
use of org.openhab.core.events.Event in project openhab-core by openhab.
the class BindingBaseClassesOSGiTest method assertConfigStatusIsPropagated.
@Test
public void assertConfigStatusIsPropagated() throws Exception {
ConfigStatusProviderThingHandlerFactory thingHandlerFactory = new ConfigStatusProviderThingHandlerFactory();
thingHandlerFactory.activate(componentContextMock);
registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
ThingTypeUID thingTypeUID = new ThingTypeUID("bindingId:type");
ThingUID thingUID = new ThingUID("bindingId:type:thingId");
Thing thing = ThingBuilder.create(thingTypeUID, thingUID).build();
managedThingProvider.add(thing);
ConfigStatusService service = getService(ConfigStatusService.class);
TranslationProvider translationProvider = mock(TranslationProvider.class);
when(translationProvider.getText(nullable(Bundle.class), nullable(String.class), nullable(String.class), nullable(Locale.class), nullable(Object[].class))).then(new Answer<String>() {
@Override
@Nullable
public String answer(InvocationOnMock invocation) throws Throwable {
String key = (String) invocation.getArgument(1);
return key.endsWith("param.invalid") ? "param invalid" : "param ok";
}
});
Field field = service.getClass().getDeclaredField("translationProvider");
field.setAccessible(true);
field.set(service, translationProvider);
ConfigStatusInfoEventSubscriber eventSubscriber = new ConfigStatusInfoEventSubscriber(thingUID);
registerService(eventSubscriber, EventSubscriber.class.getName());
Thread.sleep(2000);
thing.getHandler().handleConfigurationUpdate(Map.of("param", "invalid"));
waitForAssert(() -> {
Event event = eventSubscriber.getReceivedEvent();
assertThat(event, is(notNullValue()));
assertThat(event.getPayload(), CoreMatchers.containsString("\"parameterName\":\"param\",\"type\":\"ERROR\",\"message\":\"param invalid\"}"));
eventSubscriber.resetReceivedEvent();
}, 2500, DFL_SLEEP_TIME);
thing.getHandler().handleConfigurationUpdate(Map.of("param", "ok"));
waitForAssert(() -> {
Event event = eventSubscriber.getReceivedEvent();
assertThat(event, is(notNullValue()));
assertThat(event.getPayload(), CoreMatchers.containsString("\"parameterName\":\"param\",\"type\":\"INFORMATION\",\"message\":\"param ok\"}"));
}, 2500, DFL_SLEEP_TIME);
}
Aggregations