]> git.basschouten.com Git - openhab-addons.git/blob
0a69c71f99755c39663c402cf5185067b7ea5850
[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.mielecloud.internal.webservice.api;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
17
18 import java.util.Arrays;
19 import java.util.Collections;
20 import java.util.LinkedList;
21 import java.util.List;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.mielecloud.internal.webservice.api.json.Actions;
26 import org.openhab.binding.mielecloud.internal.webservice.api.json.Light;
27 import org.openhab.binding.mielecloud.internal.webservice.api.json.ProcessAction;
28
29 /**
30  * @author Björn Lange - Initial contribution
31  */
32 @NonNullByDefault
33 public class ActionsStateTest {
34     private static final String DEVICE_IDENTIFIER = "003458276345";
35
36     @Test
37     public void testGetDeviceIdentifierReturnsDeviceIdentifier() {
38         // given:
39         Actions actions = mock(Actions.class);
40         ActionsState actionsState = new ActionsState(DEVICE_IDENTIFIER, actions);
41
42         // when:
43         String deviceId = actionsState.getDeviceIdentifier();
44
45         // then:
46         assertEquals(DEVICE_IDENTIFIER, deviceId);
47     }
48
49     @Test
50     public void testReturnValuesWhenActionsIsNull() {
51         // given:
52         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, null);
53
54         // when:
55         boolean canBeStarted = actionState.canBeStarted();
56         boolean canBeStopped = actionState.canBeStopped();
57         boolean canBePaused = actionState.canBePaused();
58         boolean canStartSupercooling = actionState.canStartSupercooling();
59         boolean canStopSupercooling = actionState.canStopSupercooling();
60         boolean canContolSupercooling = actionState.canContolSupercooling();
61         boolean canStartSuperfreezing = actionState.canStartSuperfreezing();
62         boolean canStopSuperfreezing = actionState.canStopSuperfreezing();
63         boolean canControlSuperfreezing = actionState.canControlSuperfreezing();
64         boolean canEnableLight = actionState.canEnableLight();
65         boolean canDisableLight = actionState.canDisableLight();
66
67         // then:
68         assertFalse(canBeStarted);
69         assertFalse(canBeStopped);
70         assertFalse(canBePaused);
71         assertFalse(canStartSupercooling);
72         assertFalse(canStopSupercooling);
73         assertFalse(canContolSupercooling);
74         assertFalse(canStartSuperfreezing);
75         assertFalse(canStopSuperfreezing);
76         assertFalse(canControlSuperfreezing);
77         assertFalse(canEnableLight);
78         assertFalse(canDisableLight);
79     }
80
81     @Test
82     public void testReturnValuesWhenProcessActionIsEmpty() {
83         // given:
84         Actions actions = mock(Actions.class);
85         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, null);
86         when(actions.getProcessAction()).thenReturn(Collections.emptyList());
87
88         // when:
89         boolean canBeStarted = actionState.canBeStarted();
90         boolean canBeStopped = actionState.canBeStopped();
91         boolean canBePaused = actionState.canBePaused();
92         boolean canStartSupercooling = actionState.canStartSupercooling();
93         boolean canStopSupercooling = actionState.canStopSupercooling();
94         boolean canStartSuperfreezing = actionState.canStartSuperfreezing();
95         boolean canStopSuperfreezing = actionState.canStopSuperfreezing();
96
97         // then:
98         assertFalse(canBeStarted);
99         assertFalse(canBeStopped);
100         assertFalse(canBePaused);
101         assertFalse(canStartSupercooling);
102         assertFalse(canStopSupercooling);
103         assertFalse(canStartSuperfreezing);
104         assertFalse(canStopSuperfreezing);
105     }
106
107     @Test
108     public void testReturnValuesWhenLightIsEmpty() {
109         // given:
110         Actions actions = mock(Actions.class);
111         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, null);
112         when(actions.getLight()).thenReturn(Collections.emptyList());
113
114         // when:
115         boolean canEnableLight = actionState.canEnableLight();
116         boolean canDisableLight = actionState.canDisableLight();
117
118         // then:
119         assertFalse(canEnableLight);
120         assertFalse(canDisableLight);
121     }
122
123     @Test
124     public void testReturnValueWhenProcessActionStartIsAvailable() {
125         // given:
126         Actions actions = mock(Actions.class);
127         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, actions);
128         when(actions.getProcessAction()).thenReturn(Collections.singletonList(ProcessAction.START));
129
130         // when:
131         boolean canBeStarted = actionState.canBeStarted();
132         boolean canBeStopped = actionState.canBeStopped();
133
134         // then:
135         assertTrue(canBeStarted);
136         assertFalse(canBeStopped);
137     }
138
139     @Test
140     public void testReturnValueWhenProcessActionStopIsAvailable() {
141         // given:
142         Actions actions = mock(Actions.class);
143         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, actions);
144         when(actions.getProcessAction()).thenReturn(Collections.singletonList(ProcessAction.STOP));
145
146         // when:
147         boolean canBeStarted = actionState.canBeStarted();
148         boolean canBeStopped = actionState.canBeStopped();
149
150         // then:
151         assertFalse(canBeStarted);
152         assertTrue(canBeStopped);
153     }
154
155     @Test
156     public void testReturnValueWhenProcessActionStartAndStopAreAvailable() {
157         // given:
158         Actions actions = mock(Actions.class);
159         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, actions);
160         when(actions.getProcessAction()).thenReturn(List.of(ProcessAction.START, ProcessAction.STOP));
161
162         // when:
163         boolean canBeStarted = actionState.canBeStarted();
164         boolean canBeStopped = actionState.canBeStopped();
165
166         // then:
167         assertTrue(canBeStarted);
168         assertTrue(canBeStopped);
169     }
170
171     @Test
172     public void testReturnValueWhenProcessActionStartSupercoolIsAvailable() {
173         // given:
174         Actions actions = mock(Actions.class);
175         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, actions);
176         when(actions.getProcessAction()).thenReturn(Collections.singletonList(ProcessAction.START_SUPERCOOLING));
177
178         // when:
179         boolean canStartSupercooling = actionState.canStartSupercooling();
180         boolean canContolSupercooling = actionState.canContolSupercooling();
181
182         // then:
183         assertTrue(canStartSupercooling);
184         assertTrue(canContolSupercooling);
185     }
186
187     @Test
188     public void testReturnValueWhenProcessActionStartSuperfreezeIsAvailable() {
189         // given:
190         Actions actions = mock(Actions.class);
191         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, actions);
192         when(actions.getProcessAction()).thenReturn(Collections.singletonList(ProcessAction.START_SUPERFREEZING));
193
194         // when:
195         boolean canStartSuperfreezing = actionState.canStartSuperfreezing();
196         boolean canControlSuperfreezing = actionState.canControlSuperfreezing();
197
198         // then:
199         assertTrue(canStartSuperfreezing);
200         assertTrue(canControlSuperfreezing);
201     }
202
203     @Test
204     public void testReturnValueWhenLightEnableIsAvailable() {
205         // given:
206         Actions actions = mock(Actions.class);
207         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, actions);
208         when(actions.getLight()).thenReturn(Collections.singletonList(Light.ENABLE));
209
210         // when:
211         boolean canEnableLight = actionState.canEnableLight();
212
213         // then:
214         assertTrue(canEnableLight);
215     }
216
217     @Test
218     public void testReturnValueWhenLightDisableIsAvailable() {
219         // given:
220         Actions actions = mock(Actions.class);
221         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, actions);
222         when(actions.getLight()).thenReturn(Collections.singletonList(Light.DISABLE));
223
224         // when:
225         boolean canDisableLight = actionState.canDisableLight();
226
227         // then:
228         assertTrue(canDisableLight);
229     }
230
231     @Test
232     public void testCanControlLightReturnsTrueWhenLightCanBeEnabled() {
233         // given:
234         Actions actions = mock(Actions.class);
235         when(actions.getLight()).thenReturn(Collections.singletonList(Light.ENABLE));
236
237         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, actions);
238
239         // when:
240         boolean canControlLight = actionState.canControlLight();
241
242         // then:
243         assertTrue(canControlLight);
244     }
245
246     @Test
247     public void testCanControlLightReturnsTrueWhenLightCanBeDisabled() {
248         // given:
249         Actions actions = mock(Actions.class);
250         when(actions.getLight()).thenReturn(Collections.singletonList(Light.DISABLE));
251
252         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, actions);
253
254         // when:
255         boolean canControlLight = actionState.canControlLight();
256
257         // then:
258         assertTrue(canControlLight);
259     }
260
261     @Test
262     public void testCanControlLightReturnsTrueWhenLightCanBeEnabledAndDisabled() {
263         // given:
264         Actions actions = mock(Actions.class);
265         when(actions.getLight()).thenReturn(Arrays.asList(Light.ENABLE, Light.DISABLE));
266
267         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, actions);
268
269         // when:
270         boolean canControlLight = actionState.canControlLight();
271
272         // then:
273         assertTrue(canControlLight);
274     }
275
276     @Test
277     public void testCanControlLightReturnsFalseWhenNoLightOptionIsAvailable() {
278         // given:
279         Actions actions = mock(Actions.class);
280         when(actions.getLight()).thenReturn(new LinkedList<Light>());
281
282         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, actions);
283
284         // when:
285         boolean canControlLight = actionState.canControlLight();
286
287         // then:
288         assertFalse(canControlLight);
289     }
290
291     @Test
292     public void testNoProgramCanBeSetWhenNoProgramIdIsPresent() {
293         // given:
294         Actions actions = mock(Actions.class);
295         when(actions.getProgramId()).thenReturn(Collections.emptyList());
296
297         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, actions);
298
299         // when:
300         boolean canSetActiveProgram = actionState.canSetActiveProgramId();
301
302         // then:
303         assertFalse(canSetActiveProgram);
304     }
305
306     @Test
307     public void testProgramIdCanBeSetWhenProgramIdIsPresent() {
308         // given:
309         Actions actions = mock(Actions.class);
310         when(actions.getProgramId()).thenReturn(Collections.singletonList(1));
311
312         ActionsState actionState = new ActionsState(DEVICE_IDENTIFIER, actions);
313
314         // when:
315         boolean canSetActiveProgram = actionState.canSetActiveProgramId();
316
317         // then:
318         assertTrue(canSetActiveProgram);
319     }
320 }