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