2 * Copyright (c) 2010-2023 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.jupiter.api.Assertions.*;
16 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
17 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingType.HS300;
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;
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;
36 * Test class for {@link PowerStripDevice} class.
38 * @author Hilbrand Bouwkamp - Initial contribution
41 public class PowerStripDeviceTest extends DeviceTestBase<PowerStripDevice> {
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]);
54 public PowerStripDeviceTest() throws IOException {
55 super(new PowerStripDevice(HS300), "hs300_get_sysinfo_response");
60 public void setUp() throws IOException {
62 final AtomicInteger inputCounter = new AtomicInteger(0);
63 final Function<String, String> inputWrapper = s -> s.replace("001", "00" + inputCounter.incrementAndGet());
65 assertInput(inputWrapper, Function.identity(), REALTIME_INPUTS);
66 setSocketReturnAssert(REALTIME_RESPONSES);
67 device.refreshedDeviceState(deviceState);
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");
80 public void testUpdateChannelOutlet1() {
81 assertSame(OnOffType.ON, device.updateChannel(CHANNEL_OUTLET_1, deviceState), "Outlet 1 should be on");
85 public void testUpdateChannelOutlet2() {
86 assertSame(OnOffType.OFF, device.updateChannel(CHANNEL_OUTLET_2, deviceState), "Outlet 2 should be off");
90 public void testUpdateChannelEnergyCurrent() {
92 ((QuantityType<?>) device.updateChannel(CHANNEL_ENERGY_CURRENT_OUTLET_2, deviceState)).intValue(),
93 "Energy current should have valid state value");