2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.tplinksmarthome.internal.device;
15 import static org.junit.Assert.*;
16 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.*;
18 import java.io.IOException;
19 import java.util.Arrays;
20 import java.util.List;
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;
33 * Test class for {@link EnergySwitchDevice} class.
35 * @author Hilbrand Bouwkamp - Initial contribution
37 @RunWith(value = Parameterized.class)
39 public class EnergySwitchDeviceTest {
41 private static final List<Object[]> TESTS = Arrays
42 .asList(new Object[][] { { "plug_get_realtime_response", }, { "plug_get_realtime_response_v2", } });
44 private final EnergySwitchDevice device = new EnergySwitchDevice();
45 private final DeviceState deviceState;
47 public EnergySwitchDeviceTest(String name) throws IOException {
48 deviceState = new DeviceState(ModelTestUtil.readJson(name));
51 @Parameters(name = "{0}")
52 public static List<Object[]> data() {
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));
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));
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%"));
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));
82 public void testUpdateChannelOther() {
83 assertSame("Unknown channel should return UNDEF", UnDefType.UNDEF,
84 device.updateChannel(CHANNEL_UID_OTHER, deviceState));