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.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.WemoHandler;
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.thing.Thing;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingStatusDetail;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.types.State;
36 * Tests for {@link WemoHandler}.
38 * @author Svilen Valkanov - Initial contribution
39 * @author Stefan Triller - Ported Tests from Groovy to Java
41 public class WemoHandlerTest {
43 private final ThingTypeUID THING_TYPE = WemoBindingConstants.THING_TYPE_INSIGHT;
44 private final String THING_ID = "test";
46 private MockWemoHandler handler;
48 private final String SERVICE_ID = "insight";
49 private final String PARAMS_NAME = "InsightParams";
50 private WemoInsightParams insightParams;
52 /** Used for all tests, where expected value is time in seconds **/
53 private final int TIME_PARAM = 4702;
55 /** Represents a state parameter, where 1 stays for ON and 0 stays for OFF **/
56 private final int STATE_PARAM = 1;
58 /** Represents power in Wats **/
59 private final int POWER_PARAM = 54;
61 private final Thing thing = mock(Thing.class);
65 insightParams = new WemoInsightParams();
66 when(thing.getUID()).thenReturn(new ThingUID(THING_TYPE, THING_ID));
67 when(thing.getThingTypeUID()).thenReturn(THING_TYPE);
68 when(thing.getStatus()).thenReturn(ThingStatus.ONLINE);
73 handler.channelState = null;
74 handler.channelToWatch = null;
78 public void assertThatChannelSTATEisUpdatedOnReceivedValue() {
79 insightParams.state = STATE_PARAM;
80 State expectedStateType = OnOffType.ON;
81 String expectedChannel = CHANNEL_STATE;
83 testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
87 public void assertThatChannelLASTONFORIsUpdatedOnReceivedValue() {
88 insightParams.lastOnFor = TIME_PARAM;
89 State expectedStateType = new DecimalType(TIME_PARAM);
90 String expectedChannel = CHANNEL_LASTONFOR;
92 testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
96 public void assertThatChannelONTODAYIsUpdatedOnReceivedValue() {
97 insightParams.onToday = TIME_PARAM;
98 State expectedStateType = new DecimalType(TIME_PARAM);
99 String expectedChannel = CHANNEL_ONTODAY;
101 testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
105 public void assertThatChannelONTOTALIsUpdatedOnReceivedValue() {
106 insightParams.onTotal = TIME_PARAM;
107 State expectedStateType = new DecimalType(TIME_PARAM);
108 String expectedChannel = CHANNEL_ONTOTAL;
110 testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
114 public void assertThatChannelTIMESPANIsUpdatedOnReceivedValue() {
115 insightParams.timespan = TIME_PARAM;
116 State expectedStateType = new DecimalType(TIME_PARAM);
117 String expectedChannel = CHANNEL_TIMESPAN;
119 testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
123 public void assertThatChannelAVERAGEPOWERIsUpdatedOnReceivedValue() {
124 insightParams.avgPower = POWER_PARAM;
125 State expectedStateType = new DecimalType(POWER_PARAM);
126 String expectedChannel = CHANNEL_AVERAGEPOWER;
128 testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
131 private void testOnValueReceived(String expectedChannel, State expectedState, String insightParams) {
132 handler = new MockWemoHandler(thing, expectedChannel);
134 handler.onValueReceived(PARAMS_NAME, insightParams, SERVICE_ID);
135 assertThat(handler.channelState, is(notNullValue()));
136 assertThat(handler.channelState, is(expectedState));
139 class MockWemoHandler extends WemoHandler {
141 String channelToWatch;
143 public MockWemoHandler(Thing thing, String channelToWatch) {
144 super(thing, null, new WemoHttpCall());
145 this.channelToWatch = channelToWatch;
149 protected void updateState(String channelID, State channelState) {
150 if (channelID.equals(channelToWatch)) {
151 this.channelState = channelState;
156 protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail, String description) {
160 protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail) {
164 protected void updateStatus(ThingStatus status) {
168 class WemoInsightParams {
169 int state, lastChangedAt, lastOnFor, onToday, onTotal, timespan, avgPower, currPower, todayEnergy, totalEnergy,
173 public String toString() {
174 // Example string looks like "1|1427230660|4702|25528|82406|1209600|39|40880|15620649|54450534.000000|8000"
175 return state + "|" + lastChangedAt + "|" + lastOnFor + "|" + onToday + "|" + onTotal + "|" + timespan + "|"
176 + avgPower + "|" + currPower + "|" + todayEnergy + "|" + totalEnergy + "|" + standbyLimit;