2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.dmx.internal.handler;
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;
23 import java.util.HashMap;
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;
43 * Tests cases for {@link org.openhab.binding.dmx.internal.handler.ChaserThingHandler}.
45 * @author Jan N. Klug - Initial contribution
47 public class ChaserThingHandlerTest extends AbstractDmxThingTestParent {
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";
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);
56 Map<String, Object> bridgeProperties;
57 Map<String, Object> thingProperties;
59 private Thing chaserThing;
61 private TestBridgeHandler dmxBridgeHandler;
62 private ChaserThingHandler chaserThingHandler;
68 thingProperties = new HashMap<>();
69 thingProperties.put(CONFIG_DMX_ID, TEST_CHANNEL);
73 public void testThingStatus() {
74 thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_INFINITE);
76 assertThingStatus(chaserThing);
80 public void testThingStatus_noBridge() {
81 thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_INFINITE);
83 // check that thing is offline if no bridge found
84 ChaserThingHandler chaserHandlerWithoutBridge = new ChaserThingHandler(chaserThing) {
86 protected @Nullable Bridge getBridge() {
90 assertThingStatusWithoutBridge(chaserHandlerWithoutBridge);
94 public void holdInfiniteChaser() {
95 initializeTestBridge();
96 thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_INFINITE);
99 long currentTime = System.currentTimeMillis();
101 chaserThingHandler.handleCommand(new ChannelUID(chaserThing.getUID(), CHANNEL_SWITCH), OnOffType.ON);
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))));
115 public void runningChaser() {
116 initializeTestBridge();
117 thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_REPEAT);
120 long currentTime = System.currentTimeMillis();
122 chaserThingHandler.handleCommand(new ChannelUID(chaserThing.getUID(), CHANNEL_SWITCH), OnOffType.ON);
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))));
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))));
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))));
141 public void runningChaserWithResume() {
142 initializeTestBridge();
143 thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_REPEAT);
144 thingProperties.put(CONFIG_CHASER_RESUME_AFTER, true);
147 dmxBridgeHandler.setDmxChannelValue(100, 193);
149 long currentTime = System.currentTimeMillis();
150 currentTime = dmxBridgeHandler.calcBuffer(currentTime, 0);
152 waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(193))));
154 chaserThingHandler.handleCommand(new ChannelUID(chaserThing.getUID(), CHANNEL_SWITCH), OnOffType.ON);
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))));
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))));
167 currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
168 waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(193))));
171 private void initialize() {
172 chaserThing = ThingBuilder.create(THING_TYPE_CHASER, "testchaser").withLabel("Chaser Thing")
173 .withBridge(bridge.getUID()).withConfiguration(new Configuration(thingProperties))
175 ChannelBuilder.create(CHANNEL_UID_SWITCH, "Switch").withType(SWITCH_CHANNEL_TYPEUID).build())
178 chaserThingHandler = new ChaserThingHandler(chaserThing) {
180 protected @Nullable Bridge getBridge() {
184 initializeHandler(chaserThingHandler);
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();
192 dmxBridgeHandler = new TestBridgeHandler(bridge);
193 bridge.setHandler(dmxBridgeHandler);
194 ThingHandlerCallback bridgeHandler = mock(ThingHandlerCallback.class);
196 ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
198 }).when(bridgeHandler).statusUpdated(any(), any());
199 dmxBridgeHandler.setCallback(bridgeHandler);
200 dmxBridgeHandler.initialize();