Skip to content

Conversation

querdenker2k
Copy link
Collaborator

This is how i started testing my rules. I need the already done changes in this MR. There are 3 more classes behind JRuleTestBase in my JRule rules project.
Is this something somebody else could use?
Currently there is a test option inside JRule (org.openhab.automation.jrule.internal.test) but i am not using this, it's too 'simple'. Is somebody else using this? Could this be removed.
If somebody could use this doing here as well i would put this in a separate maven project here (to have a multimodule project)

What can be done with this?

  • it reads all items from JRuleItemNames (with the extra annotation, see MR)
  • it can set states for an item
  • it just uses JRule items and values on the using facade.
  • it can fire updates (like change/command/update...)
  • it can assert states of an item
  • it can invoke timers directly to not wait until your timer is invoked (test execution would be very long otherwise)
  • it can do of course some Mockito stuff like assert rule execution.

What do you think? @seime @seaside1

package org.openhab.automation.jrule.rules.user.light;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openhab.automation.jrule.generated.items.JRuleItemNames;
import org.openhab.automation.jrule.rules.user.jrule.JRuleTestBase;
import org.openhab.automation.jrule.rules.value.JRuleDecimalValue;
import org.openhab.automation.jrule.rules.value.JRuleOnOffValue;
import org.openhab.automation.jrule.rules.value.JRulePercentValue;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(MockitoExtension.class)
class LightSceneTest extends JRuleTestBase {
    @Spy
    LightScene lightScene;
    @Spy
    Light light;

    @Test
    void lightSceneDiningroomOff() {
        // preconditions
        setState(JRuleItemNames.Light_Diningroom, new JRulePercentValue(70));

        fireChange(JRuleItemNames.Scene_Light_Diningroom, new JRuleDecimalValue(LightScene.LIGHT_SCENE_NONE));
        Mockito.verify(lightScene, Mockito.times(1)).lightSceneDiningroom();
        assertState(JRuleItemNames.Light_Automatic_Diningroom, JRuleOnOffValue.ON);
    }

    @Test
    void lightSceneDiningroom() {
        // preconditions
        setState(JRuleItemNames.Light_Diningroom, new JRulePercentValue(70));
        setState(JRuleItemNames.Light_Automatic_Diningroom, JRuleOnOffValue.ON);

        // set a scene
        fireChange(JRuleItemNames.Scene_Light_Diningroom, new JRuleDecimalValue(LightScene.LIGHT_SCENE_DINGINGROOM_PLAYING));
        Mockito.verify(lightScene, Mockito.times(1)).lightSceneDiningroom();
        assertState(JRuleItemNames.Light_Proxy_Diningroom, new JRulePercentValue(79));
        assertState(JRuleItemNames.Light_Temperature_Diningroom, new JRulePercentValue(66));

        // no presence anymore -> reset scene
        fireChange(JRuleItemNames.Presence_Virtual_Diningroom, JRuleOnOffValue.OFF);
        Mockito.verify(light, Mockito.times(1)).lightAutomatic(Mockito.any());
        assertState(JRuleItemNames.Light_Diningroom, new JRulePercentValue(35));

        // trigger the timer (can't wait until it's fired by jRule)
        getTimers("light-automatic-Diningroom").get(0).invoke();
        assertState(JRuleItemNames.Light_Diningroom, new JRulePercentValue(0));
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant