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.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.eclipse.jdt.annotation.NonNullByDefault;
26 import org.junit.jupiter.api.AfterEach;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Disabled;
29 import org.junit.jupiter.api.Test;
30 import org.mockito.Mockito;
31 import org.openhab.binding.dmx.internal.DmxBridgeHandler;
32 import org.openhab.binding.dmx.internal.multiverse.BaseDmxChannel;
33 import org.openhab.binding.dmx.internal.multiverse.Universe;
34 import org.openhab.core.config.core.Configuration;
35 import org.openhab.core.library.types.OnOffType;
36 import org.openhab.core.test.java.JavaTest;
37 import org.openhab.core.thing.Bridge;
38 import org.openhab.core.thing.ChannelUID;
39 import org.openhab.core.thing.Thing;
40 import org.openhab.core.thing.ThingStatus;
41 import org.openhab.core.thing.ThingTypeUID;
42 import org.openhab.core.thing.ThingUID;
43 import org.openhab.core.thing.binding.ThingHandlerCallback;
44 import org.openhab.core.thing.binding.builder.BridgeBuilder;
45 import org.openhab.core.thing.binding.builder.ChannelBuilder;
46 import org.openhab.core.thing.binding.builder.ThingBuilder;
49 * Tests cases for {@link DmxBridgeHandler}.
51 * @author Jan N. Klug - Initial contribution
54 public class DmxBridgeHandlerTest extends JavaTest {
57 * simple DmxBridgeHandlerImplementation
62 public class DmxBridgeHandlerImpl extends DmxBridgeHandler {
63 public DmxBridgeHandlerImpl(Bridge dmxBridge) {
68 public void openConnection() {
72 protected void sendDmxData() {
76 protected void closeConnection() {
80 public void initialize() {
81 universe = new Universe(TEST_UNIVERSE);
82 super.updateConfiguration();
83 updateStatus(ThingStatus.ONLINE);
87 private static final int TEST_UNIVERSE = 1;
88 private static final int TEST_CHANNEL = 100;
90 private static final ThingTypeUID THING_TYPE_TEST_BRIDGE = new ThingTypeUID(BINDING_ID, "testbridge");
91 private static final ThingUID BRIDGE_UID_TEST = new ThingUID(THING_TYPE_TEST_BRIDGE, "testbridge");
92 private static final ChannelUID CHANNEL_UID_MUTE = new ChannelUID(BRIDGE_UID_TEST, CHANNEL_MUTE);
94 private @NonNullByDefault({}) Map<String, Object> bridgeProperties;
95 private @NonNullByDefault({}) Bridge bridge;
96 private @NonNullByDefault({}) DmxBridgeHandlerImpl bridgeHandler;
100 bridgeProperties = new HashMap<>();
101 bridge = BridgeBuilder.create(THING_TYPE_TEST_BRIDGE, "testbridge").withLabel("Test Bridge")
102 .withChannel(ChannelBuilder.create(CHANNEL_UID_MUTE, "Switch").withType(MUTE_CHANNEL_TYPEUID).build())
103 .withConfiguration(new Configuration(bridgeProperties)).build();
105 ThingHandlerCallback mockCallback = mock(ThingHandlerCallback.class);
107 ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
109 }).when(mockCallback).statusUpdated(any(), any());
111 bridgeHandler = Mockito.spy(new DmxBridgeHandlerImpl(bridge));
112 bridgeHandler.getThing().setHandler(bridgeHandler);
113 bridgeHandler.setCallback(mockCallback);
114 bridgeHandler.initialize();
118 public void tearDown() {
119 bridgeHandler.dispose();
123 public void assertBridgeStatus() {
124 waitForAssert(() -> assertEquals(ThingStatus.ONLINE, bridge.getStatusInfo().getStatus()));
127 @Disabled("https://github.com/eclipse/smarthome/issues/6015#issuecomment-411313627")
129 public void assertSendDmxDataIsCalled() {
130 Mockito.verify(bridgeHandler, after(500).atLeast(9)).sendDmxData();
133 @Disabled("https://github.com/eclipse/smarthome/issues/6015")
135 public void assertMuteChannelMutesOutput() {
136 bridgeHandler.handleCommand(CHANNEL_UID_MUTE, OnOffType.ON);
137 Mockito.verify(bridgeHandler, after(500).atMost(1)).sendDmxData();
139 bridgeHandler.handleCommand(CHANNEL_UID_MUTE, OnOffType.OFF);
140 Mockito.verify(bridgeHandler, after(500).atLeast(9)).sendDmxData();
144 public void assertRetrievingOfChannels() {
145 BaseDmxChannel channel = new BaseDmxChannel(TEST_UNIVERSE, TEST_CHANNEL);
146 BaseDmxChannel returnedChannel = bridgeHandler.getDmxChannel(channel,
147 ThingBuilder.create(THING_TYPE_DIMMER, "testthing").build());
149 Integer channelId = returnedChannel.getChannelId();
150 assertThat(channelId, is(TEST_CHANNEL));