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.test;
15 import static org.hamcrest.core.Is.is;
16 import static org.hamcrest.number.IsCloseTo.closeTo;
17 import static org.junit.Assert.*;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.*;
20 import static org.openhab.binding.dmx.test.TestBridgeHandler.THING_TYPE_TEST_BRIDGE;
22 import java.util.HashMap;
24 import java.util.function.Consumer;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.ArgumentMatchers;
28 import org.openhab.core.config.core.Configuration;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.test.java.JavaTest;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.thing.binding.ThingHandler;
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.ThingStatusInfoBuilder;
40 import org.openhab.core.types.State;
43 * Common utilities for DMX thing handler tests.
45 * @author Simon Kaufmann - initial contribution and API
48 public class AbstractDmxThingTestParent extends JavaTest {
50 private Map<String, Object> bridgeProperties;
52 protected Bridge bridge;
53 protected TestBridgeHandler dmxBridgeHandler;
54 protected ThingHandlerCallback mockCallback;
56 protected void setup() {
59 mockCallback = mock(ThingHandlerCallback.class);
61 ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
63 }).when(mockCallback).statusUpdated(any(), any());
66 private void initializeBridge() {
67 bridgeProperties = new HashMap<>();
68 bridge = BridgeBuilder.create(THING_TYPE_TEST_BRIDGE, "testbridge").withLabel("Test Bridge")
69 .withConfiguration(new Configuration(bridgeProperties)).build();
70 dmxBridgeHandler = new TestBridgeHandler(bridge);
71 bridge.setHandler(dmxBridgeHandler);
72 ThingHandlerCallback bridgeHandler = mock(ThingHandlerCallback.class);
74 ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
76 }).when(bridgeHandler).statusUpdated(any(), any());
77 dmxBridgeHandler.setCallback(bridgeHandler);
78 dmxBridgeHandler.initialize();
81 protected void initializeHandler(ThingHandler handler) {
82 handler.getThing().setHandler(handler);
83 handler.setCallback(mockCallback);
87 protected void assertThingStatus(Thing thing) {
88 // check that thing turns online if properly configured
89 waitForAssert(() -> assertEquals(ThingStatus.ONLINE, thing.getStatusInfo().getStatus()));
91 // check that thing properly follows bridge status
92 ThingHandler handler = thing.getHandler();
93 assertNotNull(handler);
94 handler.bridgeStatusChanged(ThingStatusInfoBuilder.create(ThingStatus.OFFLINE).build());
95 waitForAssert(() -> assertEquals(ThingStatus.OFFLINE, thing.getStatusInfo().getStatus()));
96 handler.bridgeStatusChanged(ThingStatusInfoBuilder.create(ThingStatus.ONLINE).build());
97 waitForAssert(() -> assertEquals(ThingStatus.ONLINE, thing.getStatusInfo().getStatus()));
100 protected void assertThingStatusWithoutBridge(ThingHandler handler) {
101 handler.setCallback(mockCallback);
102 handler.initialize();
103 waitForAssert(() -> {
104 assertEquals(ThingStatus.OFFLINE, handler.getThing().getStatus());
105 assertEquals(ThingStatusDetail.CONFIGURATION_ERROR, handler.getThing().getStatusInfo().getStatusDetail());
109 public void assertPercentTypeCommands(ThingHandler handler, ChannelUID channelUID, int fadeTime) {
110 long currentTime = System.currentTimeMillis();
113 handler.handleCommand(channelUID, new PercentType(30));
114 currentTime = dmxBridgeHandler.calcBuffer(currentTime, fadeTime);
116 waitForAssert(() -> {
117 assertChannelStateUpdate(channelUID,
118 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(30.0, 1.0))));
122 handler.handleCommand(channelUID, PercentType.ZERO);
123 currentTime = dmxBridgeHandler.calcBuffer(currentTime, fadeTime);
125 waitForAssert(() -> {
126 assertChannelStateUpdate(channelUID, state -> assertEquals(PercentType.ZERO, state));
130 handler.handleCommand(channelUID, PercentType.HUNDRED);
131 currentTime = dmxBridgeHandler.calcBuffer(currentTime, fadeTime);
133 waitForAssert(() -> {
134 assertChannelStateUpdate(channelUID,
135 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
139 protected void assertChannelStateUpdate(ChannelUID channelUID, Consumer<State> stateAssertion) {
140 ArgumentCaptor<State> captor = ArgumentCaptor.forClass(State.class);
141 verify(mockCallback, atLeastOnce()).stateUpdated(ArgumentMatchers.eq(channelUID), captor.capture());
142 State value = captor.getValue();
143 stateAssertion.accept(value);