]> git.basschouten.com Git - openhab-addons.git/blob
55bbebe8ee256f297cbc679a06a5ec9ee017be5b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.wemo.internal.handler.test;
14
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.*;
19
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;
36
37 /**
38  * Tests for {@link WemoInsightHandler}.
39  *
40  * @author Svilen Valkanov - Initial contribution
41  * @author Stefan Triller - Ported Tests from Groovy to Java
42  */
43 public class WemoInsightHandlerTest {
44
45     private static final ThingTypeUID THING_TYPE = WemoBindingConstants.THING_TYPE_INSIGHT;
46     private static final String THING_ID = "test";
47
48     private MockWemoInsightHandler handler;
49
50     private static final String SERVICE_ID = "insight";
51     private static final String PARAMS_NAME = "InsightParams";
52     private WemoInsightParams insightParams;
53
54     /** Used for all tests, where expected value is time in seconds **/
55     private static final int TIME_PARAM = 4702;
56
57     /** Represents a state parameter, where 1 stays for ON and 0 stays for OFF **/
58     private static final int STATE_PARAM = 1;
59
60     /** Represents power in Wats **/
61     private static final int POWER_PARAM = 54;
62
63     private final Thing thing = mock(Thing.class);
64
65     @BeforeEach
66     public void setUp() {
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);
71     }
72
73     @AfterEach
74     public void clear() {
75         handler.channelState = null;
76         handler.channelToWatch = null;
77     }
78
79     @Test
80     public void assertThatChannelSTATEisUpdatedOnReceivedValue() {
81         insightParams.state = STATE_PARAM;
82         State expectedStateType = OnOffType.ON;
83         String expectedChannel = CHANNEL_STATE;
84
85         testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
86     }
87
88     @Test
89     public void assertThatChannelLASTONFORIsUpdatedOnReceivedValue() {
90         insightParams.lastOnFor = TIME_PARAM;
91         State expectedStateType = new DecimalType(TIME_PARAM);
92         String expectedChannel = CHANNEL_LAST_ON_FOR;
93
94         testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
95     }
96
97     @Test
98     public void assertThatChannelONTODAYIsUpdatedOnReceivedValue() {
99         insightParams.onToday = TIME_PARAM;
100         State expectedStateType = new DecimalType(TIME_PARAM);
101         String expectedChannel = CHANNEL_ON_TODAY;
102
103         testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
104     }
105
106     @Test
107     public void assertThatChannelONTOTALIsUpdatedOnReceivedValue() {
108         insightParams.onTotal = TIME_PARAM;
109         State expectedStateType = new DecimalType(TIME_PARAM);
110         String expectedChannel = CHANNEL_ON_TOTAL;
111
112         testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
113     }
114
115     @Test
116     public void assertThatChannelTIMESPANIsUpdatedOnReceivedValue() {
117         insightParams.timespan = TIME_PARAM;
118         State expectedStateType = new DecimalType(TIME_PARAM);
119         String expectedChannel = CHANNEL_TIMESPAN;
120
121         testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
122     }
123
124     @Test
125     public void assertThatChannelAVERAGEPOWERIsUpdatedOnReceivedValue() {
126         insightParams.avgPower = POWER_PARAM;
127         State expectedStateType = new QuantityType<>(POWER_PARAM, Units.WATT);
128         String expectedChannel = CHANNEL_AVERAGE_POWER;
129
130         testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
131     }
132
133     private void testOnValueReceived(String expectedChannel, State expectedState, String insightParams) {
134         handler = new MockWemoInsightHandler(thing, expectedChannel);
135
136         handler.onValueReceived(PARAMS_NAME, insightParams, SERVICE_ID);
137         assertThat(handler.channelState, is(notNullValue()));
138         assertThat(handler.channelState, is(expectedState));
139     }
140
141     class MockWemoInsightHandler extends WemoInsightHandler {
142         State channelState;
143         String channelToWatch;
144
145         public MockWemoInsightHandler(Thing thing, String channelToWatch) {
146             super(thing, null, null, new WemoHttpCall());
147             this.channelToWatch = channelToWatch;
148         }
149
150         @Override
151         protected void updateState(String channelID, State channelState) {
152             if (channelID.equals(channelToWatch)) {
153                 this.channelState = channelState;
154             }
155         }
156
157         @Override
158         protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail, String description) {
159         }
160
161         @Override
162         protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail) {
163         }
164
165         @Override
166         protected void updateStatus(ThingStatus status) {
167         }
168     }
169
170     class WemoInsightParams {
171         int state, lastChangedAt, lastOnFor, onToday, onTotal, timespan, avgPower, currPower, todayEnergy, totalEnergy,
172                 standbyLimit;
173
174         @Override
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;
179         }
180     }
181 }