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.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;
44 * Tests cases for {@link org.openhab.binding.dmx.internal.handler.ChaserThingHandler}.
46 * @author Jan N. Klug - Initial contribution
49 public class ChaserThingHandlerTest extends AbstractDmxThingTestParent {
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);
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;
67 thingProperties = new HashMap<>();
68 thingProperties.put(CONFIG_DMX_ID, TEST_CHANNEL);
72 public void testThingStatus() {
73 thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_INFINITE);
75 assertThingStatus(chaserThing);
79 public void testThingStatusNoBridge() {
80 thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_INFINITE);
82 // check that thing is offline if no bridge found
83 ChaserThingHandler chaserHandlerWithoutBridge = new ChaserThingHandler(chaserThing) {
85 protected @Nullable Bridge getBridge() {
89 assertThingStatusWithoutBridge(chaserHandlerWithoutBridge);
93 public void holdInfiniteChaser() {
94 initializeTestBridge();
95 thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_INFINITE);
98 long currentTime = System.currentTimeMillis();
100 chaserThingHandler.handleCommand(new ChannelUID(chaserThing.getUID(), CHANNEL_SWITCH), OnOffType.ON);
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))));
114 public void runningChaser() {
115 initializeTestBridge();
116 thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_REPEAT);
119 long currentTime = System.currentTimeMillis();
121 chaserThingHandler.handleCommand(new ChannelUID(chaserThing.getUID(), CHANNEL_SWITCH), OnOffType.ON);
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))));
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))));
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))));
140 public void runningChaserWithResume() {
141 initializeTestBridge();
142 thingProperties.put(CONFIG_CHASER_STEPS, TEST_STEPS_REPEAT);
143 thingProperties.put(CONFIG_CHASER_RESUME_AFTER, true);
146 dmxBridgeHandler.setDmxChannelValue(100, 193);
148 long currentTime = System.currentTimeMillis();
149 currentTime = dmxBridgeHandler.calcBuffer(currentTime, 0);
151 waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(193))));
153 chaserThingHandler.handleCommand(new ChannelUID(chaserThing.getUID(), CHANNEL_SWITCH), OnOffType.ON);
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))));
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))));
166 currentTime = dmxBridgeHandler.calcBuffer(currentTime, 1000);
167 waitForAssert(() -> assertThat(dmxBridgeHandler.getDmxChannelValue(100), is(equalTo(193))));
170 private void initialize() {
171 chaserThing = ThingBuilder.create(THING_TYPE_CHASER, "testchaser").withLabel("Chaser Thing")
172 .withBridge(bridge.getUID()).withConfiguration(new Configuration(thingProperties))
174 ChannelBuilder.create(CHANNEL_UID_SWITCH, "Switch").withType(SWITCH_CHANNEL_TYPEUID).build())
177 chaserThingHandler = new ChaserThingHandler(chaserThing) {
179 protected @Nullable Bridge getBridge() {
183 initializeHandler(chaserThingHandler);
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();
191 dmxBridgeHandler = new TestBridgeHandler(bridge);
192 bridge.setHandler(dmxBridgeHandler);
193 ThingHandlerCallback bridgeHandler = mock(ThingHandlerCallback.class);
195 ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
197 }).when(bridgeHandler).statusUpdated(any(), any());
198 dmxBridgeHandler.setCallback(bridgeHandler);
199 dmxBridgeHandler.initialize();