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.wemo.internal.handler.test;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.binding.wemo.internal.WemoBindingConstants.*;
20 import org.junit.jupiter.api.AfterEach;
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.wemo.internal.WemoBindingConstants;
24 import org.openhab.binding.wemo.internal.handler.WemoInsightHandler;
25 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.unit.Units;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.types.State;
38 * Tests for {@link WemoInsightHandler}.
40 * @author Svilen Valkanov - Initial contribution
41 * @author Stefan Triller - Ported Tests from Groovy to Java
43 public class WemoInsightHandlerTest {
45 private static final ThingTypeUID THING_TYPE = WemoBindingConstants.THING_TYPE_INSIGHT;
46 private static final String THING_ID = "test";
48 private MockWemoInsightHandler handler;
50 private static final String SERVICE_ID = "insight";
51 private static final String PARAMS_NAME = "InsightParams";
52 private WemoInsightParams insightParams;
54 /** Used for all tests, where expected value is time in seconds **/
55 private static final int TIME_PARAM = 4702;
57 /** Represents a state parameter, where 1 stays for ON and 0 stays for OFF **/
58 private static final int STATE_PARAM = 1;
60 /** Represents power in Wats **/
61 private static final int POWER_PARAM = 54;
63 private final Thing thing = mock(Thing.class);
67 insightParams = new WemoInsightParams();
68 when(thing.getUID()).thenReturn(new ThingUID(THING_TYPE, THING_ID));
69 when(thing.getThingTypeUID()).thenReturn(THING_TYPE);
70 when(thing.getStatus()).thenReturn(ThingStatus.ONLINE);
75 handler.channelState = null;
76 handler.channelToWatch = null;
80 public void assertThatChannelSTATEisUpdatedOnReceivedValue() {
81 insightParams.state = STATE_PARAM;
82 State expectedStateType = OnOffType.ON;
83 String expectedChannel = CHANNEL_STATE;
85 testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
89 public void assertThatChannelLASTONFORIsUpdatedOnReceivedValue() {
90 insightParams.lastOnFor = TIME_PARAM;
91 State expectedStateType = new DecimalType(TIME_PARAM);
92 String expectedChannel = CHANNEL_LAST_ON_FOR;
94 testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
98 public void assertThatChannelONTODAYIsUpdatedOnReceivedValue() {
99 insightParams.onToday = TIME_PARAM;
100 State expectedStateType = new DecimalType(TIME_PARAM);
101 String expectedChannel = CHANNEL_ON_TODAY;
103 testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
107 public void assertThatChannelONTOTALIsUpdatedOnReceivedValue() {
108 insightParams.onTotal = TIME_PARAM;
109 State expectedStateType = new DecimalType(TIME_PARAM);
110 String expectedChannel = CHANNEL_ON_TOTAL;
112 testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
116 public void assertThatChannelTIMESPANIsUpdatedOnReceivedValue() {
117 insightParams.timespan = TIME_PARAM;
118 State expectedStateType = new DecimalType(TIME_PARAM);
119 String expectedChannel = CHANNEL_TIMESPAN;
121 testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
125 public void assertThatChannelAVERAGEPOWERIsUpdatedOnReceivedValue() {
126 insightParams.avgPower = POWER_PARAM;
127 State expectedStateType = new QuantityType<>(POWER_PARAM, Units.WATT);
128 String expectedChannel = CHANNEL_AVERAGE_POWER;
130 testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
133 private void testOnValueReceived(String expectedChannel, State expectedState, String insightParams) {
134 handler = new MockWemoInsightHandler(thing, expectedChannel);
136 handler.onValueReceived(PARAMS_NAME, insightParams, SERVICE_ID);
137 assertThat(handler.channelState, is(notNullValue()));
138 assertThat(handler.channelState, is(expectedState));
141 class MockWemoInsightHandler extends WemoInsightHandler {
143 String channelToWatch;
145 public MockWemoInsightHandler(Thing thing, String channelToWatch) {
146 super(thing, null, null, new WemoHttpCall());
147 this.channelToWatch = channelToWatch;
151 protected void updateState(String channelID, State channelState) {
152 if (channelID.equals(channelToWatch)) {
153 this.channelState = channelState;
158 protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail, String description) {
162 protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail) {
166 protected void updateStatus(ThingStatus status) {
170 class WemoInsightParams {
171 int state, lastChangedAt, lastOnFor, onToday, onTotal, timespan, avgPower, currPower, todayEnergy, totalEnergy,
175 public String toString() {
176 // Example string looks like "1|1427230660|4702|25528|82406|1209600|39|40880|15620649|54450534.000000|8000"
177 return state + "|" + lastChangedAt + "|" + lastOnFor + "|" + onToday + "|" + onTotal + "|" + timespan + "|"
178 + avgPower + "|" + currPower + "|" + todayEnergy + "|" + totalEnergy + "|" + standbyLimit;