2 * Copyright (c) 2010-2022 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.MatcherAssert.assertThat;
16 import static org.hamcrest.core.Is.is;
17 import static org.junit.jupiter.api.Assertions.assertEquals;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.*;
20 import static org.openhab.binding.dmx.internal.DmxBindingConstants.*;
22 import java.util.HashMap;
25 import org.junit.jupiter.api.AfterEach;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Disabled;
28 import org.junit.jupiter.api.Test;
29 import org.mockito.Mockito;
30 import org.openhab.binding.dmx.internal.DmxBridgeHandler;
31 import org.openhab.binding.dmx.internal.multiverse.BaseDmxChannel;
32 import org.openhab.binding.dmx.internal.multiverse.Universe;
33 import org.openhab.core.config.core.Configuration;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.test.java.JavaTest;
36 import org.openhab.core.thing.Bridge;
37 import org.openhab.core.thing.ChannelUID;
38 import org.openhab.core.thing.Thing;
39 import org.openhab.core.thing.ThingStatus;
40 import org.openhab.core.thing.ThingTypeUID;
41 import org.openhab.core.thing.ThingUID;
42 import org.openhab.core.thing.binding.ThingHandlerCallback;
43 import org.openhab.core.thing.binding.builder.BridgeBuilder;
44 import org.openhab.core.thing.binding.builder.ChannelBuilder;
45 import org.openhab.core.thing.binding.builder.ThingBuilder;
48 * Tests cases for {@link DmxBridgeHandler}.
50 * @author Jan N. Klug - Initial contribution
52 public class DmxBridgeHandlerTest extends JavaTest {
55 * simple DmxBridgeHandlerImplementation
60 public class DmxBridgeHandlerImpl extends DmxBridgeHandler {
61 public DmxBridgeHandlerImpl(Bridge dmxBridge) {
66 public void openConnection() {
70 protected void sendDmxData() {
74 protected void closeConnection() {
78 public void initialize() {
79 universe = new Universe(TEST_UNIVERSE);
80 super.updateConfiguration();
81 updateStatus(ThingStatus.ONLINE);
85 private static final int TEST_UNIVERSE = 1;
86 private static final int TEST_CHANNEL = 100;
88 private final ThingTypeUID THING_TYPE_TEST_BRIDGE = new ThingTypeUID(BINDING_ID, "testbridge");
89 private final ThingUID BRIDGE_UID_TEST = new ThingUID(THING_TYPE_TEST_BRIDGE, "testbridge");
90 private final ChannelUID CHANNEL_UID_MUTE = new ChannelUID(BRIDGE_UID_TEST, CHANNEL_MUTE);
92 Map<String, Object> bridgeProperties;
94 private Bridge bridge;
95 private DmxBridgeHandlerImpl bridgeHandler;
99 bridgeProperties = new HashMap<>();
100 bridge = BridgeBuilder.create(THING_TYPE_TEST_BRIDGE, "testbridge").withLabel("Test Bridge")
101 .withChannel(ChannelBuilder.create(CHANNEL_UID_MUTE, "Switch").withType(MUTE_CHANNEL_TYPEUID).build())
102 .withConfiguration(new Configuration(bridgeProperties)).build();
104 ThingHandlerCallback mockCallback = mock(ThingHandlerCallback.class);
106 ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
108 }).when(mockCallback).statusUpdated(any(), any());
110 bridgeHandler = Mockito.spy(new DmxBridgeHandlerImpl(bridge));
111 bridgeHandler.getThing().setHandler(bridgeHandler);
112 bridgeHandler.setCallback(mockCallback);
113 bridgeHandler.initialize();
117 public void tearDown() {
118 bridgeHandler.dispose();
122 public void assertBridgeStatus() {
123 waitForAssert(() -> assertEquals(ThingStatus.ONLINE, bridge.getStatusInfo().getStatus()));
126 @Disabled("https://github.com/eclipse/smarthome/issues/6015#issuecomment-411313627")
128 public void assertSendDmxDataIsCalled() {
129 Mockito.verify(bridgeHandler, after(500).atLeast(9)).sendDmxData();
132 @Disabled("https://github.com/eclipse/smarthome/issues/6015")
134 public void assertMuteChannelMutesOutput() {
135 bridgeHandler.handleCommand(CHANNEL_UID_MUTE, OnOffType.ON);
136 Mockito.verify(bridgeHandler, after(500).atMost(1)).sendDmxData();
138 bridgeHandler.handleCommand(CHANNEL_UID_MUTE, OnOffType.OFF);
139 Mockito.verify(bridgeHandler, after(500).atLeast(9)).sendDmxData();
143 public void assertRetrievingOfChannels() {
144 BaseDmxChannel channel = new BaseDmxChannel(TEST_UNIVERSE, TEST_CHANNEL);
145 BaseDmxChannel returnedChannel = bridgeHandler.getDmxChannel(channel,
146 ThingBuilder.create(THING_TYPE_DIMMER, "testthing").build());
148 Integer channelId = returnedChannel.getChannelId();
149 assertThat(channelId, is(TEST_CHANNEL));