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