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