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