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.test;
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.core.Is.is;
17 import static org.hamcrest.number.IsCloseTo.closeTo;
18 import static org.junit.jupiter.api.Assertions.*;
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.*;
21 import static org.openhab.binding.dmx.test.TestBridgeHandler.THING_TYPE_TEST_BRIDGE;
23 import java.util.HashMap;
25 import java.util.function.Consumer;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.ArgumentMatchers;
29 import org.openhab.core.config.core.Configuration;
30 import org.openhab.core.library.types.PercentType;
31 import org.openhab.core.test.java.JavaTest;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingStatus;
36 import org.openhab.core.thing.ThingStatusDetail;
37 import org.openhab.core.thing.binding.ThingHandler;
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.ThingStatusInfoBuilder;
41 import org.openhab.core.types.State;
44 * Common utilities for DMX thing handler tests.
46 * @author Simon Kaufmann - initial contribution and API
49 public class AbstractDmxThingTestParent extends JavaTest {
51 private Map<String, Object> bridgeProperties;
53 protected Bridge bridge;
54 protected TestBridgeHandler dmxBridgeHandler;
55 protected ThingHandlerCallback mockCallback;
57 protected void setup() {
60 mockCallback = mock(ThingHandlerCallback.class);
62 ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
64 }).when(mockCallback).statusUpdated(any(), any());
67 private void initializeBridge() {
68 bridgeProperties = new HashMap<>();
69 bridge = BridgeBuilder.create(THING_TYPE_TEST_BRIDGE, "testbridge").withLabel("Test Bridge")
70 .withConfiguration(new Configuration(bridgeProperties)).build();
71 dmxBridgeHandler = new TestBridgeHandler(bridge);
72 bridge.setHandler(dmxBridgeHandler);
73 ThingHandlerCallback bridgeHandler = mock(ThingHandlerCallback.class);
75 ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
77 }).when(bridgeHandler).statusUpdated(any(), any());
78 dmxBridgeHandler.setCallback(bridgeHandler);
79 dmxBridgeHandler.initialize();
82 protected void initializeHandler(ThingHandler handler) {
83 handler.getThing().setHandler(handler);
84 handler.setCallback(mockCallback);
88 protected void assertThingStatus(Thing thing) {
89 // check that thing turns online if properly configured
90 waitForAssert(() -> assertEquals(ThingStatus.ONLINE, thing.getStatusInfo().getStatus()));
92 // check that thing properly follows bridge status
93 ThingHandler handler = thing.getHandler();
94 assertNotNull(handler);
95 handler.bridgeStatusChanged(ThingStatusInfoBuilder.create(ThingStatus.OFFLINE).build());
96 waitForAssert(() -> assertEquals(ThingStatus.OFFLINE, thing.getStatusInfo().getStatus()));
97 handler.bridgeStatusChanged(ThingStatusInfoBuilder.create(ThingStatus.ONLINE).build());
98 waitForAssert(() -> assertEquals(ThingStatus.ONLINE, thing.getStatusInfo().getStatus()));
101 protected void assertThingStatusWithoutBridge(ThingHandler handler) {
102 handler.setCallback(mockCallback);
103 handler.initialize();
104 waitForAssert(() -> {
105 assertEquals(ThingStatus.OFFLINE, handler.getThing().getStatus());
106 assertEquals(ThingStatusDetail.CONFIGURATION_ERROR, handler.getThing().getStatusInfo().getStatusDetail());
110 public void assertPercentTypeCommands(ThingHandler handler, ChannelUID channelUID, int fadeTime) {
111 long currentTime = System.currentTimeMillis();
114 handler.handleCommand(channelUID, new PercentType(30));
115 currentTime = dmxBridgeHandler.calcBuffer(currentTime, fadeTime);
117 waitForAssert(() -> {
118 assertChannelStateUpdate(channelUID,
119 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(30.0, 1.0))));
123 handler.handleCommand(channelUID, PercentType.ZERO);
124 currentTime = dmxBridgeHandler.calcBuffer(currentTime, fadeTime);
126 waitForAssert(() -> {
127 assertChannelStateUpdate(channelUID, state -> assertEquals(PercentType.ZERO, state));
131 handler.handleCommand(channelUID, PercentType.HUNDRED);
132 currentTime = dmxBridgeHandler.calcBuffer(currentTime, fadeTime);
134 waitForAssert(() -> {
135 assertChannelStateUpdate(channelUID,
136 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
140 protected void assertChannelStateUpdate(ChannelUID channelUID, Consumer<State> stateAssertion) {
141 ArgumentCaptor<State> captor = ArgumentCaptor.forClass(State.class);
142 verify(mockCallback, atLeastOnce()).stateUpdated(ArgumentMatchers.eq(channelUID), captor.capture());
143 State value = captor.getValue();
144 stateAssertion.accept(value);