]> git.basschouten.com Git - openhab-addons.git/blob
aceda0a7e5e83040bac67bc76cf58c32e20abc09
[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.internal.handler;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.binding.dmx.internal.DmxBindingConstants.*;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.AfterEach;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.test.java.JavaTest;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.ThingHandlerCallback;
35 import org.openhab.core.thing.binding.builder.BridgeBuilder;
36 import org.openhab.core.thing.binding.builder.ChannelBuilder;
37
38 /**
39  * Tests cases for {@link org.openhab.binding.dmx.internal.handler.Lib485BridgeHandler}.
40  *
41  * @author Jan N. Klug - Initial contribution
42  */
43 @NonNullByDefault
44 public class Lib485BridgeHandlerTest extends JavaTest {
45     private static final String TEST_ADDRESS = "localhost";
46     private static final ThingUID BRIDGE_UID_LIB485 = new ThingUID(THING_TYPE_LIB485_BRIDGE, "lib485bridge");
47     private static final ChannelUID CHANNEL_UID_MUTE = new ChannelUID(BRIDGE_UID_LIB485, CHANNEL_MUTE);
48
49     private @NonNullByDefault({}) Map<String, Object> bridgeProperties;
50     private @NonNullByDefault({}) Bridge bridge;
51     private @NonNullByDefault({}) Lib485BridgeHandler bridgeHandler;
52
53     @BeforeEach
54     public void setUp() {
55         bridgeProperties = new HashMap<>();
56         bridgeProperties.put(CONFIG_ADDRESS, TEST_ADDRESS);
57         bridge = BridgeBuilder.create(THING_TYPE_LIB485_BRIDGE, "lib485bridge").withLabel("Lib485 Bridge")
58                 .withChannel(ChannelBuilder.create(CHANNEL_UID_MUTE, "Switch").withType(MUTE_CHANNEL_TYPEUID).build())
59                 .withConfiguration(new Configuration(bridgeProperties)).build();
60
61         ThingHandlerCallback mockCallback = mock(ThingHandlerCallback.class);
62         doAnswer(answer -> {
63             ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
64             return null;
65         }).when(mockCallback).statusUpdated(any(), any());
66
67         bridgeHandler = new Lib485BridgeHandler(bridge) {
68             @Override
69             protected void validateConfigurationParameters(Map<String, Object> configurationParameters) {
70             }
71         };
72
73         bridgeHandler.getThing().setHandler(bridgeHandler);
74         bridgeHandler.setCallback(mockCallback);
75         bridgeHandler.initialize();
76     }
77
78     @AfterEach
79     public void tearDown() {
80         bridgeHandler.dispose();
81     }
82
83     @Test
84     public void assertBridgeStatus() {
85         waitForAssert(() -> assertEquals(ThingStatus.OFFLINE, bridge.getStatusInfo().getStatus()));
86     }
87 }