Search in sources :

Example 1 with RuleRemovedEvent

use of org.eclipse.smarthome.automation.events.RuleRemovedEvent in project smarthome by eclipse.

the class RuleEventFactory method createRuleRemovedEvent.

/**
 * Creates a rule removed event.
 *
 * @param rule   the rule for which this event is created.
 * @param source the source of the event.
 * @return {@link RuleRemovedEvent} instance.
 */
public static RuleRemovedEvent createRuleRemovedEvent(Rule rule, String source) {
    String topic = buildTopic(RULE_REMOVED_EVENT_TOPIC, rule);
    final RuleDTO ruleDto = RuleDTOMapper.map(rule);
    String payload = serializePayload(ruleDto);
    return new RuleRemovedEvent(topic, payload, source, ruleDto);
}
Also used : RuleDTO(org.eclipse.smarthome.automation.dto.RuleDTO) RuleRemovedEvent(org.eclipse.smarthome.automation.events.RuleRemovedEvent)

Example 2 with RuleRemovedEvent

use of org.eclipse.smarthome.automation.events.RuleRemovedEvent in project smarthome by eclipse.

the class AutomationIntegrationTest method assertThatARuleCanBeAddedUpdatedAndRemovedByTheApi.

@Test
public void assertThatARuleCanBeAddedUpdatedAndRemovedByTheApi() {
    logger.info("assert that a rule can be added, updated and removed by the api");
    EventSubscriber ruleEventHandler = new EventSubscriber() {

        @Override
        @NonNull
        public Set<@NonNull String> getSubscribedEventTypes() {
            return Stream.of(RuleAddedEvent.TYPE, RuleRemovedEvent.TYPE, RuleUpdatedEvent.TYPE).collect(toSet());
        }

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(@NonNull Event e) {
            logger.info("RuleEvent: {}", e.getTopic());
            ruleEvent = e;
        }
    };
    registerService(ruleEventHandler);
    // ADD
    Rule rule = createSimpleRule();
    ruleRegistry.add(rule);
    waitForAssert(() -> {
        assertThat(ruleEvent, is(notNullValue()));
        assertThat(ruleEvent, is(instanceOf(RuleAddedEvent.class)));
        RuleAddedEvent ruleAddedEvent = (RuleAddedEvent) ruleEvent;
        assertThat(ruleAddedEvent.getRule().uid, is(rule.getUID()));
    }, 5000, 500);
    Rule ruleAdded = ruleRegistry.get(rule.getUID());
    assertThat(ruleAdded, is(notNullValue()));
    assertThat(ruleEngine.getStatusInfo(rule.getUID()).getStatus(), is(RuleStatus.IDLE));
    // UPDATE
    ruleEvent = null;
    if (ruleAdded == null) {
        throw new NullPointerException();
    }
    Rule updatedRule = RuleBuilder.create(ruleAdded).withDescription("TestDescription").build();
    Rule oldRule = ruleRegistry.update(updatedRule);
    waitForAssert(() -> {
        assertThat(ruleEvent, is(notNullValue()));
        assertThat(ruleEvent, is(instanceOf(RuleUpdatedEvent.class)));
        RuleUpdatedEvent ruEvent = (RuleUpdatedEvent) ruleEvent;
        assertThat(ruEvent.getRule().uid, is(rule.getUID()));
        assertThat(ruEvent.getOldRule().uid, is(rule.getUID()));
        assertThat(ruEvent.getRule().description, is("TestDescription"));
        assertThat(ruEvent.getOldRule().description, is(nullValue()));
    });
    assertThat(oldRule, is(notNullValue()));
    assertThat(oldRule, is(rule));
    // REMOVE
    ruleEvent = null;
    Rule removed = ruleRegistry.remove(rule.getUID());
    waitForAssert(() -> {
        assertThat(ruleEvent, is(notNullValue()));
        assertThat(ruleEvent, is(instanceOf(RuleRemovedEvent.class)));
        RuleRemovedEvent reEvent = (RuleRemovedEvent) ruleEvent;
        assertThat(reEvent.getRule().uid, is(removed.getUID()));
    });
    assertThat(removed, is(notNullValue()));
    assertThat(removed, is(ruleAdded));
    assertThat(ruleRegistry.get(removed.getUID()), is(nullValue()));
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) NonNull(org.eclipse.jdt.annotation.NonNull) RuleRemovedEvent(org.eclipse.smarthome.automation.events.RuleRemovedEvent) RuleRemovedEvent(org.eclipse.smarthome.automation.events.RuleRemovedEvent) RuleAddedEvent(org.eclipse.smarthome.automation.events.RuleAddedEvent) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) RuleUpdatedEvent(org.eclipse.smarthome.automation.events.RuleUpdatedEvent) Event(org.eclipse.smarthome.core.events.Event) RuleAddedEvent(org.eclipse.smarthome.automation.events.RuleAddedEvent) RuleUpdatedEvent(org.eclipse.smarthome.automation.events.RuleUpdatedEvent) Rule(org.eclipse.smarthome.automation.Rule) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 3 with RuleRemovedEvent

use of org.eclipse.smarthome.automation.events.RuleRemovedEvent in project smarthome by eclipse.

the class RuleEventTest method testRuleEvents.

@Test
public void testRuleEvents() throws ItemNotFoundException {
    // Registering eventSubscriber
    List<Event> ruleEvents = new ArrayList<>();
    EventSubscriber ruleEventHandler = new EventSubscriber() {

        @Override
        public Set<String> getSubscribedEventTypes() {
            Set<String> types = new HashSet<>();
            types.add(RuleAddedEvent.TYPE);
            types.add(RuleRemovedEvent.TYPE);
            types.add(RuleStatusInfoEvent.TYPE);
            types.add(RuleUpdatedEvent.TYPE);
            return types;
        }

        @Override
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event event) {
            logger.info("RuleEvent: {}", event.getTopic());
            ruleEvents.add(event);
        }
    };
    registerService(ruleEventHandler);
    // Creation of RULE
    Map<String, Object> triggerCfgEntries = new HashMap<>();
    triggerCfgEntries.put("eventSource", "myMotionItem2");
    triggerCfgEntries.put("eventTopic", "smarthome/*");
    triggerCfgEntries.put("eventTypes", "ItemStateEvent");
    Configuration triggerConfig = new Configuration(triggerCfgEntries);
    Map<String, Object> actionCfgEntries = new HashMap<>();
    actionCfgEntries.put("itemName", "myLampItem2");
    actionCfgEntries.put("command", "ON");
    Configuration actionConfig = new Configuration(actionCfgEntries);
    List<Trigger> triggers = Collections.singletonList(ModuleBuilder.createTrigger().withId("ItemStateChangeTrigger2").withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build());
    List<Action> actions = Collections.singletonList(ModuleBuilder.createAction().withId("ItemPostCommandAction2").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
    Rule rule = RuleBuilder.create("myRule21").withTriggers(triggers).withActions(actions).withName("RuleEventTestingRule").build();
    logger.info("Rule created: {}", rule.getUID());
    RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    RuleManager ruleEngine = getService(RuleManager.class);
    ruleRegistry.add(rule);
    ruleEngine.setEnabled(rule.getUID(), true);
    waitForAssert(() -> {
        assertThat(ruleEngine.getStatusInfo(rule.getUID()).getStatus(), is(RuleStatus.IDLE));
    });
    // TEST RULE
    EventPublisher eventPublisher = getService(EventPublisher.class);
    ItemRegistry itemRegistry = getService(ItemRegistry.class);
    SwitchItem myMotionItem = (SwitchItem) itemRegistry.getItem("myMotionItem2");
    assertNotNull(myMotionItem);
    eventPublisher.post(ItemEventFactory.createStateEvent("myPresenceItem2", OnOffType.ON));
    EventSubscriber itemEventHandler = new EventSubscriber() {

        @Override
        public void receive(Event event) {
            logger.info("Event: {}", event.getTopic());
            if (event instanceof ItemCommandEvent && event.getTopic().contains("myLampItem2")) {
                itemEvent = event;
            }
        }

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Collections.singleton(ItemCommandEvent.TYPE);
        }

        @Override
        public EventFilter getEventFilter() {
            return null;
        }
    };
    registerService(itemEventHandler);
    eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem2", OnOffType.ON));
    waitForAssert(() -> assertThat(itemEvent, is(notNullValue())));
    assertThat(itemEvent.getTopic(), is(equalTo("smarthome/items/myLampItem2/command")));
    assertThat(((ItemCommandEvent) itemEvent).getItemCommand(), is(OnOffType.ON));
    assertThat(ruleEvents.size(), is(not(0)));
    assertThat(ruleEvents.stream().filter(e -> e.getTopic().equals("smarthome/rules/myRule21/added")).findFirst().isPresent(), is(true));
    assertThat(ruleEvents.stream().filter(e -> e.getTopic().equals("smarthome/rules/myRule21/state")).findFirst().isPresent(), is(true));
    List<Event> stateEvents = ruleEvents.stream().filter(e -> e.getTopic().equals("smarthome/rules/myRule21/state")).collect(Collectors.toList());
    assertThat(stateEvents, is(notNullValue()));
    Optional<Event> runningEvent = stateEvents.stream().filter(e -> ((RuleStatusInfoEvent) e).getStatusInfo().getStatus() == RuleStatus.RUNNING).findFirst();
    assertThat(runningEvent.isPresent(), is(true));
    EventSubscriber ruleRemovedEventHandler = new EventSubscriber() {

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Collections.singleton(RuleRemovedEvent.TYPE);
        }

        @Override
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event event) {
            logger.info("RuleRemovedEvent: {}", event.getTopic());
            ruleRemovedEvent = event;
        }
    };
    registerService(ruleRemovedEventHandler);
    ruleRegistry.remove("myRule21");
    waitForAssert(() -> {
        assertThat(ruleRemovedEvent, is(notNullValue()));
        assertThat(ruleRemovedEvent.getTopic(), is(equalTo("smarthome/rules/myRule21/removed")));
    });
}
Also used : Trigger(org.eclipse.smarthome.automation.Trigger) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleBuilder(org.eclipse.smarthome.automation.core.util.RuleBuilder) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) HashMap(java.util.HashMap) ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RuleManager(org.eclipse.smarthome.automation.RuleManager) RuleRemovedEvent(org.eclipse.smarthome.automation.events.RuleRemovedEvent) Map(java.util.Map) Configuration(org.eclipse.smarthome.config.core.Configuration) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) Before(org.junit.Before) RuleStatus(org.eclipse.smarthome.automation.RuleStatus) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) VolatileStorageService(org.eclipse.smarthome.test.storage.VolatileStorageService) Logger(org.slf4j.Logger) ItemEventFactory(org.eclipse.smarthome.core.items.events.ItemEventFactory) RuleAddedEvent(org.eclipse.smarthome.automation.events.RuleAddedEvent) Collection(java.util.Collection) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) Test(org.junit.Test) Rule(org.eclipse.smarthome.automation.Rule) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) List(java.util.List) Optional(java.util.Optional) Action(org.eclipse.smarthome.automation.Action) ModuleBuilder(org.eclipse.smarthome.automation.core.util.ModuleBuilder) RuleUpdatedEvent(org.eclipse.smarthome.automation.events.RuleUpdatedEvent) Assert(org.junit.Assert) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) NonNull(org.eclipse.jdt.annotation.NonNull) Action(org.eclipse.smarthome.automation.Action) Configuration(org.eclipse.smarthome.config.core.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Trigger(org.eclipse.smarthome.automation.Trigger) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) HashSet(java.util.HashSet) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) RuleManager(org.eclipse.smarthome.automation.RuleManager) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleRemovedEvent(org.eclipse.smarthome.automation.events.RuleRemovedEvent) RuleAddedEvent(org.eclipse.smarthome.automation.events.RuleAddedEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) RuleUpdatedEvent(org.eclipse.smarthome.automation.events.RuleUpdatedEvent) Event(org.eclipse.smarthome.core.events.Event) Rule(org.eclipse.smarthome.automation.Rule) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

RuleRemovedEvent (org.eclipse.smarthome.automation.events.RuleRemovedEvent)3 NonNull (org.eclipse.jdt.annotation.NonNull)2 Rule (org.eclipse.smarthome.automation.Rule)2 RuleAddedEvent (org.eclipse.smarthome.automation.events.RuleAddedEvent)2 RuleStatusInfoEvent (org.eclipse.smarthome.automation.events.RuleStatusInfoEvent)2 RuleUpdatedEvent (org.eclipse.smarthome.automation.events.RuleUpdatedEvent)2 Event (org.eclipse.smarthome.core.events.Event)2 EventSubscriber (org.eclipse.smarthome.core.events.EventSubscriber)2 ItemCommandEvent (org.eclipse.smarthome.core.items.events.ItemCommandEvent)2 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1