]> git.basschouten.com Git - openhab-addons.git/blob
89499352c1765d87ffb49f5b016f8c0832579c14
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.dmx.test;
14
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;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.function.Consumer;
26
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;
42
43 /**
44  * Common utilities for DMX thing handler tests.
45  *
46  * @author Simon Kaufmann - initial contribution and API
47  *
48  */
49 public class AbstractDmxThingTestParent extends JavaTest {
50
51     private Map<String, Object> bridgeProperties;
52
53     protected Bridge bridge;
54     protected TestBridgeHandler dmxBridgeHandler;
55     protected ThingHandlerCallback mockCallback;
56
57     protected void setup() {
58         initializeBridge();
59
60         mockCallback = mock(ThingHandlerCallback.class);
61         doAnswer(answer -> {
62             ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
63             return null;
64         }).when(mockCallback).statusUpdated(any(), any());
65     }
66
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);
74         doAnswer(answer -> {
75             ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
76             return null;
77         }).when(bridgeHandler).statusUpdated(any(), any());
78         dmxBridgeHandler.setCallback(bridgeHandler);
79         dmxBridgeHandler.initialize();
80     }
81
82     protected void initializeHandler(ThingHandler handler) {
83         handler.getThing().setHandler(handler);
84         handler.setCallback(mockCallback);
85         handler.initialize();
86     }
87
88     protected void assertThingStatus(Thing thing) {
89         // check that thing turns online if properly configured
90         waitForAssert(() -> assertEquals(ThingStatus.ONLINE, thing.getStatusInfo().getStatus()));
91
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()));
99     }
100
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());
107         });
108     }
109
110     public void assertPercentTypeCommands(ThingHandler handler, ChannelUID channelUID, int fadeTime) {
111         long currentTime = System.currentTimeMillis();
112
113         // set 30%
114         handler.handleCommand(channelUID, new PercentType(30));
115         currentTime = dmxBridgeHandler.calcBuffer(currentTime, fadeTime);
116
117         waitForAssert(() -> {
118             assertChannelStateUpdate(channelUID,
119                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(30.0, 1.0))));
120         });
121
122         // set 0%
123         handler.handleCommand(channelUID, PercentType.ZERO);
124         currentTime = dmxBridgeHandler.calcBuffer(currentTime, fadeTime);
125
126         waitForAssert(() -> {
127             assertChannelStateUpdate(channelUID, state -> assertEquals(PercentType.ZERO, state));
128         });
129
130         // set 100%
131         handler.handleCommand(channelUID, PercentType.HUNDRED);
132         currentTime = dmxBridgeHandler.calcBuffer(currentTime, fadeTime);
133
134         waitForAssert(() -> {
135             assertChannelStateUpdate(channelUID,
136                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
137         });
138     }
139
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);
145     }
146 }