]> git.basschouten.com Git - openhab-addons.git/blob
808b126e19cc2ade1f84cc0ad1acf46385283e07
[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.internal.handler;
14
15 import static org.hamcrest.core.Is.is;
16 import static org.junit.Assert.*;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19 import static org.openhab.binding.dmx.internal.DmxBindingConstants.*;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.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.SacnBridgeHandler}.
40  *
41  * @author Jan N. Klug - Initial contribution
42  */
43 public class SacnBridgeHandlerTest extends JavaTest {
44     private static final String TEST_ADDRESS = "localhost";
45     private static final int TEST_UNIVERSE = 1;
46
47     private final ThingUID BRIDGE_UID_SACN = new ThingUID(THING_TYPE_SACN_BRIDGE, "sacnbridge");
48     private final ChannelUID CHANNEL_UID_MUTE = new ChannelUID(BRIDGE_UID_SACN, CHANNEL_MUTE);
49
50     Map<String, Object> bridgeProperties;
51
52     private Bridge bridge;
53     private SacnBridgeHandler bridgeHandler;
54
55     @Before
56     public void setUp() {
57         bridgeProperties = new HashMap<>();
58         bridgeProperties.put(CONFIG_ADDRESS, TEST_ADDRESS);
59         bridgeProperties.put(CONFIG_UNIVERSE, TEST_UNIVERSE);
60         bridgeProperties.put(CONFIG_SACN_MODE, "unicast");
61         bridge = BridgeBuilder.create(THING_TYPE_SACN_BRIDGE, "sacnbridge").withLabel("sACN Bridge")
62                 .withChannel(ChannelBuilder.create(CHANNEL_UID_MUTE, "Switch").withType(MUTE_CHANNEL_TYPEUID).build())
63                 .withConfiguration(new Configuration(bridgeProperties)).build();
64
65         ThingHandlerCallback mockCallback = mock(ThingHandlerCallback.class);
66         doAnswer(answer -> {
67             ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
68             return null;
69         }).when(mockCallback).statusUpdated(any(), any());
70
71         bridgeHandler = new SacnBridgeHandler(bridge) {
72             @Override
73             protected void validateConfigurationParameters(Map<String, Object> configurationParameters) {
74             }
75         };
76
77         bridgeHandler.getThing().setHandler(bridgeHandler);
78         bridgeHandler.setCallback(mockCallback);
79         bridgeHandler.initialize();
80     }
81
82     @After
83     public void tearDown() {
84         bridgeHandler.dispose();
85     }
86
87     @Test
88     public void assertBridgeStatus() {
89         waitForAssert(() -> assertEquals(ThingStatus.ONLINE, bridge.getStatusInfo().getStatus()));
90     }
91
92     @Test
93     public void renamingOfUniverses() {
94         waitForAssert(() -> assertThat(bridgeHandler.getUniverseId(), is(TEST_UNIVERSE)));
95
96         bridgeProperties.replace(CONFIG_UNIVERSE, 2);
97         bridgeHandler.handleConfigurationUpdate(bridgeProperties);
98         waitForAssert(() -> assertThat(bridgeHandler.getUniverseId(), is(2)));
99
100         bridgeProperties.replace(CONFIG_UNIVERSE, TEST_UNIVERSE);
101         bridgeHandler.handleConfigurationUpdate(bridgeProperties);
102         waitForAssert(() -> assertThat(bridgeHandler.getUniverseId(), is(TEST_UNIVERSE)));
103     }
104 }