Search in sources :

Example 1 with RuleProvider

use of org.openhab.core.automation.RuleProvider in project openhab-core by openhab.

the class AutomationIntegrationTest method assertThatARuleCanBeAddedByARuleProvider.

@Test
public void assertThatARuleCanBeAddedByARuleProvider() {
    logger.info("assert that a rule can be added by a ruleProvider");
    Rule rule = createSimpleRule();
    RuleProvider ruleProvider = new RuleProvider() {

        @Override
        public void addProviderChangeListener(ProviderChangeListener<Rule> listener) {
        }

        @Override
        public Collection<Rule> getAll() {
            return Set.of(rule);
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<Rule> listener) {
        }
    };
    registerService(ruleProvider);
    assertThat(ruleRegistry.get(rule.getUID()), is(notNullValue()));
    unregisterService(ruleProvider);
    assertThat(ruleRegistry.get(rule.getUID()), is(nullValue()));
    Rule rule2 = createSimpleRule();
    assertThat(ruleRegistry.get(rule2.getUID()), is(nullValue()));
    managedRuleProvider.add(rule2);
    assertThat(ruleRegistry.get(rule2.getUID()), is(notNullValue()));
    managedRuleProvider.remove(rule2.getUID());
    assertThat(ruleRegistry.get(rule2.getUID()), is(nullValue()));
}
Also used : ProviderChangeListener(org.openhab.core.common.registry.ProviderChangeListener) Rule(org.openhab.core.automation.Rule) ManagedRuleProvider(org.openhab.core.automation.ManagedRuleProvider) RuleProvider(org.openhab.core.automation.RuleProvider) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 2 with RuleProvider

use of org.openhab.core.automation.RuleProvider in project openhab-core by openhab.

the class RuleSimulationTest method testSimulateRuleWithCronTrigger.

@Test
public void testSimulateRuleWithCronTrigger() {
    logger.info("assert that rules are simulated correct");
    final Rule cronRuleWithTimeOfDayCondition = createRuleWithCronExpressionTrigger();
    final Rule disabledRule = createRuleWithCronExpressionTrigger();
    final Rule timeOfDayTriggerWithDayOfWeekCondition = createRuleWithTimeOfDayTrigger();
    final Rule timeOfDayTriggerWithEphemerisCondition = createRuleWithEphemerisCondition();
    RuleProvider ruleProvider = new RuleProvider() {

        @Override
        public void addProviderChangeListener(ProviderChangeListener<Rule> listener) {
        }

        @Override
        public Collection<Rule> getAll() {
            return Set.of(cronRuleWithTimeOfDayCondition, timeOfDayTriggerWithDayOfWeekCondition, timeOfDayTriggerWithEphemerisCondition, disabledRule);
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<Rule> listener) {
        }
    };
    // Register the RuleProvider, so that the rules are registered within the ruleRegistry.
    registerService(ruleProvider);
    assertThat(ruleRegistry.get(cronRuleWithTimeOfDayCondition.getUID()), is(notNullValue()));
    assertThat(ruleRegistry.get(disabledRule.getUID()), is(notNullValue()));
    assertThat(ruleRegistry.get(timeOfDayTriggerWithDayOfWeekCondition.getUID()), is(notNullValue()));
    assertThat(ruleRegistry.get(timeOfDayTriggerWithEphemerisCondition.getUID()), is(notNullValue()));
    // Disable one rule, so it must not be contained within the simulation
    ruleEngine.setEnabled(disabledRule.getUID(), false);
    assertFalse(ruleEngine.isEnabled(disabledRule.getUID()));
    // Simulate for two weeks
    final ZonedDateTime from = ZonedDateTime.of(2021, 1, 4, 0, 0, 0, 0, ZoneId.systemDefault());
    final ZonedDateTime until = ZonedDateTime.of(2021, 1, 17, 23, 59, 59, 0, ZoneId.systemDefault());
    List<RuleExecution> executions = ruleEngine.simulateRuleExecutions(from, until).collect(Collectors.toList());
    // Every rule fires twice a week. We simulate for two weeks so we expect 12 executions
    // TODO: must be 12, but Ephemeris Condition is not yet evaluated in test, because dayset is not configured.
    assertEquals(8, executions.size());
    Iterator<RuleExecution> it = executions.iterator();
    // due the result is sorted, check the results by position.
    // First week
    checkExecution(it.next(), timeOfDayTriggerWithDayOfWeekCondition, 4, 16, 00);
    checkExecution(it.next(), cronRuleWithTimeOfDayCondition, 6, 10, 30);
    checkExecution(it.next(), timeOfDayTriggerWithDayOfWeekCondition, 6, 16, 00);
    checkExecution(it.next(), cronRuleWithTimeOfDayCondition, 8, 10, 30);
    // checkExecution(it.next(), timeOfDayTriggerWithEphemerisCondition, 9, 10, 00);
    // checkExecution(it.next(), timeOfDayTriggerWithEphemerisCondition, 10, 10, 00);
    // Second week
    checkExecution(it.next(), timeOfDayTriggerWithDayOfWeekCondition, 11, 16, 00);
    checkExecution(it.next(), cronRuleWithTimeOfDayCondition, 13, 10, 30);
    checkExecution(it.next(), timeOfDayTriggerWithDayOfWeekCondition, 13, 16, 00);
    checkExecution(it.next(), cronRuleWithTimeOfDayCondition, 15, 10, 30);
    // checkExecution(it.next(), timeOfDayTriggerWithEphemerisCondition, 16, 10, 00);
    // checkExecution(it.next(), timeOfDayTriggerWithEphemerisCondition, 17, 10, 00);
    assertFalse(it.hasNext());
}
Also used : RuleExecution(org.openhab.core.automation.RuleExecution) ZonedDateTime(java.time.ZonedDateTime) ProviderChangeListener(org.openhab.core.common.registry.ProviderChangeListener) Rule(org.openhab.core.automation.Rule) RuleProvider(org.openhab.core.automation.RuleProvider) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

Test (org.junit.jupiter.api.Test)2 Rule (org.openhab.core.automation.Rule)2 RuleProvider (org.openhab.core.automation.RuleProvider)2 ProviderChangeListener (org.openhab.core.common.registry.ProviderChangeListener)2 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)2 ZonedDateTime (java.time.ZonedDateTime)1 ManagedRuleProvider (org.openhab.core.automation.ManagedRuleProvider)1 RuleExecution (org.openhab.core.automation.RuleExecution)1