Search in sources :

Example 31 with Event

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());
    });
}
Also used : EventPublisher(org.openhab.core.events.EventPublisher) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Event(org.openhab.core.events.Event) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest) Disabled(org.junit.jupiter.api.Disabled)

Example 32 with Event

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");
}
Also used : Condition(org.openhab.core.automation.Condition) Configuration(org.openhab.core.config.core.Configuration) RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Event(org.openhab.core.events.Event) CompareConditionHandler(org.openhab.core.automation.internal.module.handler.CompareConditionHandler) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 33 with Event

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);
    }
}
Also used : Addon(org.openhab.core.addon.Addon) Event(org.openhab.core.events.Event)

Example 34 with Event

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);
}
Also used : Event(org.openhab.core.events.Event)

Example 35 with 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);
}
Also used : Locale(java.util.Locale) EventSubscriber(org.openhab.core.events.EventSubscriber) ConfigStatusService(org.openhab.core.config.core.status.ConfigStatusService) Bundle(org.osgi.framework.Bundle) Field(java.lang.reflect.Field) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ThingUID(org.openhab.core.thing.ThingUID) TranslationProvider(org.openhab.core.i18n.TranslationProvider) Event(org.openhab.core.events.Event) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Thing(org.openhab.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

Event (org.openhab.core.events.Event)120 Test (org.junit.jupiter.api.Test)93 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)58 EventSubscriber (org.openhab.core.events.EventSubscriber)46 ItemCommandEvent (org.openhab.core.items.events.ItemCommandEvent)38 Nullable (org.eclipse.jdt.annotation.Nullable)28 SwitchItem (org.openhab.core.library.items.SwitchItem)28 EventPublisher (org.openhab.core.events.EventPublisher)27 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)25 BeforeEach (org.junit.jupiter.api.BeforeEach)22 Item (org.openhab.core.items.Item)22 Set (java.util.Set)21 Rule (org.openhab.core.automation.Rule)20 RuleStatusInfoEvent (org.openhab.core.automation.events.RuleStatusInfoEvent)20 EventFilter (org.openhab.core.events.EventFilter)20 NumberItem (org.openhab.core.library.items.NumberItem)18 LinkedList (java.util.LinkedList)17 List (java.util.List)17 ArrayList (java.util.ArrayList)16 Configuration (org.openhab.core.config.core.Configuration)16