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