]> git.basschouten.com Git - openhab-addons.git/blob
eedeedcd032a4a5e414201339b1fbc7e564014f7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.dmx.internal.handler;
14
15 import static org.hamcrest.CoreMatchers.equalTo;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.core.Is.is;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.*;
20 import static org.openhab.binding.dmx.internal.DmxBindingConstants.*;
21 import static org.openhab.binding.dmx.test.TestBridgeHandler.THING_TYPE_TEST_BRIDGE;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.openhab.binding.dmx.test.AbstractDmxThingTestParent;
30 import org.openhab.binding.dmx.test.TestBridgeHandler;
31 import org.openhab.core.config.core.Configuration;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.thing.Bridge;
34 import org.openhab.core.thing.ChannelUID;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingUID;
37 import org.openhab.core.thing.binding.ThingHandlerCallback;
38 import org.openhab.core.thing.binding.builder.BridgeBuilder;
39 import org.openhab.core.thing.binding.builder.ChannelBuilder;
40 import org.openhab.core.thing.binding.builder.ThingBuilder;
41
42 /**
43  * Tests cases for {@link org.openhab.binding.dmx.internal.handler.ChaserThingHandler}.
44  *
45  * @author Jan N. Klug - Initial contribution
46  */
47 public class ChaserThingHandlerTest extends AbstractDmxThingTestParent {
48
49     private static final String TEST_CHANNEL = "100";
50     private static final String TEST_STEPS_INFINITE = "1000:100:1000|1000:200:-1";
51     private static final String TEST_STEPS_REPEAT = "1000:115:1000|1000:210:1000";
52
53     private final ThingUID THING_UID_CHASER = new ThingUID(THING_TYPE_CHASER, "testchaser");
54     private final ChannelUID CHANNEL_UID_SWITCH = new ChannelUID(THING_UID_CHASER, CHANNEL_SWITCH);
55
56     Map<String, Object> bridgeProperties;
57     Map<String, Object> thingProperties;
58
59     private Thing chaserThing;
60
61     private TestBridgeHandler dmxBridgeHandler;
62     private ChaserThingHandler chaserThingHandler;
63
64     @BeforeEach
65     public void setUp() {
66         super.setup();
67
68         thingProperties = new HashMap<>();
69         thingProperties.put(CONFIG_DMX_ID, TEST_CHANNEL);
70     }
71
72     @Test
73     public void testThingStatus() {
74         thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_INFINITE);
75         initialize();
76         assertThingStatus(chaserThing);
77     }
78
79     @Test
80     public void testThingStatus_noBridge() {
81         thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_INFINITE);
82         initialize();
83         // check that thing is offline if no bridge found
84         ChaserThingHandler chaserHandlerWithoutBridge = new ChaserThingHandler(chaserThing) {
85             @Override
86             protected @Nullable Bridge getBridge() {
87                 return null;
88             }
89         };
90         assertThingStatusWithoutBridge(chaserHandlerWithoutBridge);
91     }
92
93     @Test
94     public void holdInfiniteChaser() {
95         initializeTestBridge();
96         thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_INFINITE);
97         initialize();
98
99         long currentTime = System.currentTimeMillis();
100
101         chaserThingHandler.handleCommand(new ChannelUID(chaserThing.getUID(), CHANNEL_SWITCH), OnOffType.ON);
102         // step I
103         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
104         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(100))));
105         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
106         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(100))));
107         // step II (holds forever)
108         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
109         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(200))));
110         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 2000);
111         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(200))));
112     }
113
114     @Test
115     public void runningChaser() {
116         initializeTestBridge();
117         thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_REPEAT);
118         initialize();
119
120         long currentTime = System.currentTimeMillis();
121
122         chaserThingHandler.handleCommand(new ChannelUID(chaserThing.getUID(), CHANNEL_SWITCH), OnOffType.ON);
123         // step I
124         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
125         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(115))));
126         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
127         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(115))));
128         // step II
129         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
130         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(210))));
131         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
132         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(210))));
133         // step I (repeated)
134         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
135         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(115))));
136         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
137         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(115))));
138     }
139
140     @Test
141     public void runningChaserWithResume() {
142         initializeTestBridge();
143         thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_REPEAT);
144         thingProperties.put(CONFIG_CHASER_RESUME_AFTER, true);
145         initialize();
146
147         dmxBridgeHandler.setDmxChannelValue(100, 193);
148
149         long currentTime = System.currentTimeMillis();
150         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 0);
151
152         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(193))));
153
154         chaserThingHandler.handleCommand(new ChannelUID(chaserThing.getUID(), CHANNEL_SWITCH), OnOffType.ON);
155
156         // step I
157         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
158         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(115))));
159         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
160         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(115))));
161         // step II
162         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
163         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(210))));
164         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
165         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(210))));
166         // resume old state
167         currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
168         waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(193))));
169     }
170
171     private void initialize() {
172         chaserThing = ThingBuilder.create(THING_TYPE_CHASER, "testchaser").withLabel("Chaser Thing")
173                 .withBridge(bridge.getUID()).withConfiguration(new Configuration(thingProperties))
174                 .withChannel(
175                         ChannelBuilder.create(CHANNEL_UID_SWITCH, "Switch").withType(SWITCH_CHANNEL_TYPEUID).build())
176                 .build();
177
178         chaserThingHandler = new ChaserThingHandler(chaserThing) {
179             @Override
180             protected @Nullable Bridge getBridge() {
181                 return bridge;
182             }
183         };
184         initializeHandler(chaserThingHandler);
185     }
186
187     private void initializeTestBridge() {
188         bridgeProperties = new HashMap<>();
189         bridge = BridgeBuilder.create(THING_TYPE_TEST_BRIDGE, "testbridge").withLabel("Test Bridge")
190                 .withConfiguration(new Configuration(bridgeProperties)).build();
191
192         dmxBridgeHandler = new TestBridgeHandler(bridge);
193         bridge.setHandler(dmxBridgeHandler);
194         ThingHandlerCallback bridgeHandler = mock(ThingHandlerCallback.class);
195         doAnswer(answer -> {
196             ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
197             return null;
198         }).when(bridgeHandler).statusUpdated(any(), any());
199         dmxBridgeHandler.setCallback(bridgeHandler);
200         dmxBridgeHandler.initialize();
201     }
202 }