]> git.basschouten.com Git - openhab-addons.git/blob
2532e3e9d3e879ec8c1a02d08f3ed5e6eb0e76ea
[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.ChannelUIDConstants.*;
17
18 import java.io.IOException;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.params.ParameterizedTest;
24 import org.junit.jupiter.params.provider.MethodSource;
25 import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
29
30 /**
31  * Test class for {@link EnergySwitchDevice} class.
32  *
33  * @author Hilbrand Bouwkamp - Initial contribution
34  */
35 @NonNullByDefault
36 public class EnergySwitchDeviceTest {
37
38     private static final List<Object[]> TESTS = Arrays
39             .asList(new Object[][] { { "plug_get_realtime_response", }, { "plug_get_realtime_response_v2", } });
40
41     private final EnergySwitchDevice device = new EnergySwitchDevice();
42
43     public static List<Object[]> data() {
44         return TESTS;
45     }
46
47     @ParameterizedTest
48     @MethodSource("data")
49     public void testUpdateChannelEnergyCurrent(String name) throws IOException {
50         DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
51         assertEquals(new QuantityType<>(1 + " A"), device.updateChannel(CHANNEL_UID_ENERGY_CURRENT, deviceState),
52                 "Energy current should have valid state value");
53     }
54
55     @ParameterizedTest
56     @MethodSource("data")
57     public void testUpdateChannelEnergyTotal(String name) throws IOException {
58         DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
59         assertEquals(new QuantityType<>(10 + " kWh"), device.updateChannel(CHANNEL_UID_ENERGY_TOTAL, deviceState),
60                 "Energy total should have valid state value");
61     }
62
63     @ParameterizedTest
64     @MethodSource("data")
65     public void testUpdateChannelEnergyVoltage(String name) throws IOException {
66         DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
67         State state = device.updateChannel(CHANNEL_UID_ENERGY_VOLTAGE, deviceState);
68         assertEquals(230, ((QuantityType<?>) state).intValue(), "Energy voltage should have valid state value");
69         assertEquals("230 V", state.format("%.0f %unit%"), "Channel patten to format voltage correctly");
70     }
71
72     @ParameterizedTest
73     @MethodSource("data")
74     public void testUpdateChanneEnergyPower(String name) throws IOException {
75         DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
76         assertEquals(new QuantityType<>(20 + " W"), device.updateChannel(CHANNEL_UID_ENERGY_POWER, deviceState),
77                 "Energy power should have valid state value");
78     }
79
80     @ParameterizedTest
81     @MethodSource("data")
82     public void testUpdateChannelOther(String name) throws IOException {
83         DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
84         assertSame(UnDefType.UNDEF, device.updateChannel(CHANNEL_UID_OTHER, deviceState),
85                 "Unknown channel should return UNDEF");
86     }
87 }