]> git.basschouten.com Git - openhab-addons.git/blob
d7ad5c4425a03b34277b615b844afdef9b931f05
[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.tplinksmarthome.internal.device;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
17 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingType.HS300;
18
19 import java.io.IOException;
20 import java.util.concurrent.atomic.AtomicInteger;
21 import java.util.function.Function;
22 import java.util.stream.Collectors;
23 import java.util.stream.IntStream;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants;
29 import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
30 import org.openhab.binding.tplinksmarthome.internal.model.SetRelayState;
31 import org.openhab.core.library.types.OnOffType;
32 import org.openhab.core.library.types.QuantityType;
33 import org.openhab.core.thing.ChannelUID;
34
35 /**
36  * Test class for {@link PowerStripDevice} class.
37  *
38  * @author Hilbrand Bouwkamp - Initial contribution
39  */
40 @NonNullByDefault
41 public class PowerStripDeviceTest extends DeviceTestBase<PowerStripDevice> {
42
43     private static final ChannelUID CHANNEL_OUTLET_1 = ChannelUIDConstants.createChannel(HS300,
44             CHANNEL_OUTLET_GROUP_PREFIX + '1', CHANNEL_SWITCH);
45     private static final ChannelUID CHANNEL_OUTLET_2 = ChannelUIDConstants.createChannel(HS300,
46             CHANNEL_OUTLET_GROUP_PREFIX + '2', CHANNEL_SWITCH);
47     private static final ChannelUID CHANNEL_ENERGY_CURRENT_OUTLET_2 = ChannelUIDConstants.createChannel(HS300,
48             CHANNEL_OUTLET_GROUP_PREFIX + '2', CHANNEL_ENERGY_CURRENT);
49     private static final String[] REALTIME_INPUTS = IntStream.range(0, 6).mapToObj(i -> "hs300_get_realtime")
50             .collect(Collectors.toList()).toArray(new String[0]);
51     private static final String[] REALTIME_RESPONSES = IntStream.range(0, 6).mapToObj(i -> "plug_get_realtime_response")
52             .collect(Collectors.toList()).toArray(new String[0]);
53
54     public PowerStripDeviceTest() throws IOException {
55         super(new PowerStripDevice(HS300), "hs300_get_sysinfo_response");
56     }
57
58     @Override
59     @BeforeEach
60     public void setUp() throws IOException {
61         super.setUp();
62         final AtomicInteger inputCounter = new AtomicInteger(0);
63         final Function<String, String> inputWrapper = s -> s.replace("001", "00" + inputCounter.incrementAndGet());
64
65         assertInput(inputWrapper, Function.identity(), REALTIME_INPUTS);
66         setSocketReturnAssert(REALTIME_RESPONSES);
67         device.refreshedDeviceState(deviceState);
68     }
69
70     @Test
71     public void testHandleCommandSwitchChannel2() throws IOException {
72         Function<String, String> normalize = s -> ModelTestUtil.GSON
73                 .toJson(ModelTestUtil.GSON.fromJson(s, SetRelayState.class));
74         assertInput(normalize, normalize, "hs300_set_relay_state");
75         setSocketReturnAssert("hs300_set_relay_state_response");
76         assertTrue(device.handleCommand(CHANNEL_OUTLET_2, OnOffType.ON), "Outlet channel 2 should be handled");
77     }
78
79     @Test
80     public void testUpdateChannelOutlet1() {
81         assertSame(OnOffType.ON, device.updateChannel(CHANNEL_OUTLET_1, deviceState), "Outlet 1 should be on");
82     }
83
84     @Test
85     public void testUpdateChannelOutlet2() {
86         assertSame(OnOffType.OFF, device.updateChannel(CHANNEL_OUTLET_2, deviceState), "Outlet 2 should be off");
87     }
88
89     @Test
90     public void testUpdateChannelEnergyCurrent() {
91         assertEquals(1,
92                 ((QuantityType<?>) device.updateChannel(CHANNEL_ENERGY_CURRENT_OUTLET_2, deviceState)).intValue(),
93                 "Energy current should have valid state value");
94     }
95 }