]> git.basschouten.com Git - openhab-addons.git/blob
f6006b8b2a9cd0865a94705a7cc42e14a5e4b189
[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.eclipse.jdt.annotation.NonNullByDefault;
26 import org.junit.jupiter.api.AfterEach;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.openhab.core.config.core.Configuration;
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.ThingUID;
36 import org.openhab.core.thing.binding.ThingHandlerCallback;
37 import org.openhab.core.thing.binding.builder.BridgeBuilder;
38 import org.openhab.core.thing.binding.builder.ChannelBuilder;
39
40 /**
41  * Tests cases for {@link org.openhab.binding.dmx.internal.handler.ArtnetBridgeHandler}.
42  *
43  * @author Jan N. Klug - Initial contribution
44  */
45 @NonNullByDefault
46 public class ArtnetBridgeHandlerTest extends JavaTest {
47     private static final String TEST_ADDRESS = "localhost";
48     private static final int TEST_UNIVERSE = 1;
49     private static final ThingUID BRIDGE_UID_ARTNET = new ThingUID(THING_TYPE_ARTNET_BRIDGE, "artnetbridge");
50     private static final ChannelUID CHANNEL_UID_MUTE = new ChannelUID(BRIDGE_UID_ARTNET, CHANNEL_MUTE);
51
52     private @NonNullByDefault({}) Map<String, Object> bridgeProperties;
53     private @NonNullByDefault({}) Bridge bridge;
54     private @NonNullByDefault({}) ArtnetBridgeHandler 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         bridge = BridgeBuilder.create(THING_TYPE_ARTNET_BRIDGE, "artnetbridge").withLabel("Artnet 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 ArtnetBridgeHandler(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     @AfterEach
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 }