2 * Copyright (c) 2010-2020 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.core.Is.is;
16 import static org.junit.Assert.*;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19 import static org.openhab.binding.dmx.internal.DmxBindingConstants.*;
21 import java.util.HashMap;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.openhab.binding.dmx.internal.DmxBridgeHandler;
30 import org.openhab.binding.dmx.internal.multiverse.BaseDmxChannel;
31 import org.openhab.binding.dmx.internal.multiverse.Universe;
32 import org.openhab.core.config.core.Configuration;
33 import org.openhab.core.library.types.OnOffType;
34 import org.openhab.core.test.java.JavaTest;
35 import org.openhab.core.thing.Bridge;
36 import org.openhab.core.thing.ChannelUID;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingStatus;
39 import org.openhab.core.thing.ThingTypeUID;
40 import org.openhab.core.thing.ThingUID;
41 import org.openhab.core.thing.binding.ThingHandlerCallback;
42 import org.openhab.core.thing.binding.builder.BridgeBuilder;
43 import org.openhab.core.thing.binding.builder.ChannelBuilder;
44 import org.openhab.core.thing.binding.builder.ThingBuilder;
47 * Tests cases for {@link DmxBridgeHandler}.
49 * @author Jan N. Klug - Initial contribution
51 public class DmxBridgeHandlerTest extends JavaTest {
54 * simple DmxBridgeHandlerImplementation
59 public class DmxBridgeHandlerImpl extends DmxBridgeHandler {
60 public DmxBridgeHandlerImpl(Bridge dmxBridge) {
65 public void openConnection() {
69 protected void sendDmxData() {
73 protected void closeConnection() {
77 public void initialize() {
78 universe = new Universe(TEST_UNIVERSE);
79 super.updateConfiguration();
80 updateStatus(ThingStatus.ONLINE);
84 private static final int TEST_UNIVERSE = 1;
85 private static final int TEST_CHANNEL = 100;
87 private final ThingTypeUID THING_TYPE_TEST_BRIDGE = new ThingTypeUID(BINDING_ID, "testbridge");
88 private final ThingUID BRIDGE_UID_TEST = new ThingUID(THING_TYPE_TEST_BRIDGE, "testbridge");
89 private final ChannelUID CHANNEL_UID_MUTE = new ChannelUID(BRIDGE_UID_TEST, CHANNEL_MUTE);
91 Map<String, Object> bridgeProperties;
93 private Bridge bridge;
94 private DmxBridgeHandlerImpl bridgeHandler;
98 bridgeProperties = new HashMap<>();
99 bridge = BridgeBuilder.create(THING_TYPE_TEST_BRIDGE, "testbridge").withLabel("Test Bridge")
100 .withChannel(ChannelBuilder.create(CHANNEL_UID_MUTE, "Switch").withType(MUTE_CHANNEL_TYPEUID).build())
101 .withConfiguration(new Configuration(bridgeProperties)).build();
103 ThingHandlerCallback mockCallback = mock(ThingHandlerCallback.class);
105 ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
107 }).when(mockCallback).statusUpdated(any(), any());
109 bridgeHandler = Mockito.spy(new DmxBridgeHandlerImpl(bridge));
110 bridgeHandler.getThing().setHandler(bridgeHandler);
111 bridgeHandler.setCallback(mockCallback);
112 bridgeHandler.initialize();
116 public void tearDown() {
117 bridgeHandler.dispose();
121 public void assertBridgeStatus() {
122 waitForAssert(() -> assertEquals(ThingStatus.ONLINE, bridge.getStatusInfo().getStatus()));
125 @Ignore("https://github.com/eclipse/smarthome/issues/6015#issuecomment-411313627")
127 public void assertSendDmxDataIsCalled() {
128 Mockito.verify(bridgeHandler, after(500).atLeast(9)).sendDmxData();
131 @Ignore("https://github.com/eclipse/smarthome/issues/6015")
133 public void assertMuteChannelMutesOutput() {
134 bridgeHandler.handleCommand(CHANNEL_UID_MUTE, OnOffType.ON);
135 Mockito.verify(bridgeHandler, after(500).atMost(1)).sendDmxData();
137 bridgeHandler.handleCommand(CHANNEL_UID_MUTE, OnOffType.OFF);
138 Mockito.verify(bridgeHandler, after(500).atLeast(9)).sendDmxData();
142 public void assertRetrievingOfChannels() {
143 BaseDmxChannel channel = new BaseDmxChannel(TEST_UNIVERSE, TEST_CHANNEL);
144 BaseDmxChannel returnedChannel = bridgeHandler.getDmxChannel(channel,
145 ThingBuilder.create(THING_TYPE_DIMMER, "testthing").build());
147 Integer channelId = returnedChannel.getChannelId();
148 assertThat(channelId, is(TEST_CHANNEL));