]> git.basschouten.com Git - openhab-addons.git/blob
070318dfed530bb0a6b55131252618b5d329f9c5
[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.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18
19 import java.util.Optional;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.mielecloud.internal.webservice.api.json.StateType;
24
25 /**
26  * @author Björn Lange - Initial contribution
27  */
28 @NonNullByDefault
29 public class TransitionStateTest {
30     private final DeviceState historic = mock(DeviceState.class);
31     private final DeviceState previous = mock(DeviceState.class);
32     private final DeviceState next = mock(DeviceState.class);
33
34     @Test
35     public void testHasFinishedChangedReturnsTrueWhenPreviousStateIsNull() {
36         // given:
37         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
38
39         TransitionState transitionState = new TransitionState(null, next);
40
41         // when:
42         boolean hasFinishedChanged = transitionState.hasFinishedChanged();
43
44         // then:
45         assertTrue(hasFinishedChanged);
46     }
47
48     @Test
49     public void testHasFinishedChangedReturnsTrueWhenPreviousStateIsUnknown() {
50         // given:
51         when(previous.getStateType()).thenReturn(Optional.empty());
52         when(previous.isInState(any())).thenCallRealMethod();
53         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
54         when(next.isInState(any())).thenCallRealMethod();
55
56         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
57
58         // when:
59         boolean hasFinishedChanged = transitionState.hasFinishedChanged();
60
61         // then:
62         assertTrue(hasFinishedChanged);
63     }
64
65     @Test
66     public void testHasFinishedChangedReturnsFalseWhenNoStateTransitionOccurred() {
67         // given:
68         when(previous.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
69         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
70
71         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
72
73         // when:
74         boolean hasFinishedChanged = transitionState.hasFinishedChanged();
75
76         // then:
77         assertFalse(hasFinishedChanged);
78     }
79
80     @Test
81     public void testHasFinishedChangedReturnsTrueWhenStateChangedFromRunningToEndProgrammed() {
82         // given:
83         when(previous.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
84         when(previous.isInState(any())).thenCallRealMethod();
85         when(next.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
86         when(next.isInState(any())).thenCallRealMethod();
87
88         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
89
90         // when:
91         boolean hasFinishedChanged = transitionState.hasFinishedChanged();
92
93         // then:
94         assertTrue(hasFinishedChanged);
95     }
96
97     @Test
98     public void testHasFinishedChangedReturnsTrueWhenStateChangedFromRunningToProgrammed() {
99         // given:
100         when(previous.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
101         when(previous.isInState(any())).thenCallRealMethod();
102         when(next.getStateType()).thenReturn(Optional.of(StateType.PROGRAMMED));
103         when(next.isInState(any())).thenCallRealMethod();
104
105         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
106
107         // when:
108         boolean hasFinishedChanged = transitionState.hasFinishedChanged();
109
110         // then:
111         assertTrue(hasFinishedChanged);
112     }
113
114     @Test
115     public void testHasFinishedChangedReturnsFalseWhenStateChangedFromRunningToPause() {
116         // given:
117         when(previous.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
118         when(next.getStateType()).thenReturn(Optional.of(StateType.PAUSE));
119
120         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
121
122         // when:
123         boolean hasFinishedChanged = transitionState.hasFinishedChanged();
124
125         // then:
126         assertFalse(hasFinishedChanged);
127     }
128
129     @Test
130     public void testHasFinishedChangedReturnsTrueWhenStateChangedFromProgrammedWaitingToStartToRunning() {
131         // given:
132         when(previous.getStateType()).thenReturn(Optional.of(StateType.PROGRAMMED_WAITING_TO_START));
133         when(previous.isInState(any())).thenCallRealMethod();
134         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
135         when(next.isInState(any())).thenCallRealMethod();
136
137         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
138
139         // when:
140         boolean hasFinishedChanged = transitionState.hasFinishedChanged();
141
142         // then:
143         assertTrue(hasFinishedChanged);
144     }
145
146     @Test
147     public void testHasFinishedChangedReturnsFalseWhenStateRemainsProgrammedWaitingToStart() {
148         // given:
149         when(previous.getStateType()).thenReturn(Optional.of(StateType.PROGRAMMED_WAITING_TO_START));
150         when(next.getStateType()).thenReturn(Optional.of(StateType.PROGRAMMED_WAITING_TO_START));
151
152         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
153
154         // when:
155         boolean hasFinishedChanged = transitionState.hasFinishedChanged();
156
157         // then:
158         assertFalse(hasFinishedChanged);
159     }
160
161     @Test
162     public void testHasFinishedChangedReturnsFalseWhenStateChangedFromPauseToRunning() {
163         // given:
164         when(previous.getStateType()).thenReturn(Optional.of(StateType.PAUSE));
165         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
166
167         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
168
169         // when:
170         boolean hasFinishedChanged = transitionState.hasFinishedChanged();
171
172         // then:
173         assertFalse(hasFinishedChanged);
174     }
175
176     @Test
177     public void testHasFinishedChangedReturnsTrueWhenStateChangedFromEndProgrammedToOff() {
178         // given:
179         when(previous.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
180         when(previous.isInState(any())).thenCallRealMethod();
181         when(next.getStateType()).thenReturn(Optional.of(StateType.OFF));
182         when(next.isInState(any())).thenCallRealMethod();
183
184         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
185
186         // when:
187         boolean hasFinishedChanged = transitionState.hasFinishedChanged();
188
189         // then:
190         assertTrue(hasFinishedChanged);
191     }
192
193     @Test
194     public void testHasFinishedChangedReturnsFalseWhenStateChangedFromRunningToFailure() {
195         // given:
196         when(previous.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
197         when(next.getStateType()).thenReturn(Optional.of(StateType.FAILURE));
198
199         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
200
201         // when:
202         boolean hasFinishedChanged = transitionState.hasFinishedChanged();
203
204         // then:
205         assertFalse(hasFinishedChanged);
206     }
207
208     @Test
209     public void testHasFinishedChangedReturnsFalseWhenStateChangedFromPauseToFailure() {
210         // given:
211         when(previous.getStateType()).thenReturn(Optional.of(StateType.PAUSE));
212         when(next.getStateType()).thenReturn(Optional.of(StateType.FAILURE));
213
214         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
215
216         // when:
217         boolean hasFinishedChanged = transitionState.hasFinishedChanged();
218
219         // then:
220         assertFalse(hasFinishedChanged);
221     }
222
223     @Test
224     public void testIsFinishedReturnsTrueWhenStateChangedFromRunningToEndProgrammed() {
225         // given:
226         when(previous.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
227         when(next.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
228
229         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
230
231         // when:
232         Boolean isFinished = transitionState.isFinished().get();
233
234         // then:
235         assertTrue(isFinished);
236     }
237
238     @Test
239     public void testIsFinishedReturnsTrueWhenStateChangedFromRunningToProgrammed() {
240         // given:
241         when(previous.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
242         when(next.getStateType()).thenReturn(Optional.of(StateType.PROGRAMMED));
243
244         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
245
246         // when:
247         Boolean isFinished = transitionState.isFinished().get();
248
249         // then:
250         assertTrue(isFinished);
251     }
252
253     @Test
254     public void testIsFinishedReturnsFalseWhenStateChangedFromProgrammedWaitingToStartToRunning() {
255         // given:
256         when(previous.getStateType()).thenReturn(Optional.of(StateType.PROGRAMMED_WAITING_TO_START));
257         when(previous.isInState(any())).thenCallRealMethod();
258         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
259         when(next.isInState(any())).thenCallRealMethod();
260
261         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
262
263         // when:
264         Boolean isFinished = transitionState.isFinished().get();
265
266         // then:
267         assertFalse(isFinished);
268     }
269
270     @Test
271     public void testIsFinishedReturnsFalseWhenStateChangedFromRunningToFailure() {
272         // given:
273         when(previous.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
274         when(previous.isInState(any())).thenCallRealMethod();
275         when(next.getStateType()).thenReturn(Optional.of(StateType.FAILURE));
276         when(next.isInState(any())).thenCallRealMethod();
277
278         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
279
280         // when:
281         Boolean isFinished = transitionState.isFinished().get();
282
283         // then:
284         assertFalse(isFinished);
285     }
286
287     @Test
288     public void testIsFinishedReturnsFalseWhenStateChangedFromPauseToFailure() {
289         // given:
290         when(previous.getStateType()).thenReturn(Optional.of(StateType.PAUSE));
291         when(previous.isInState(any())).thenCallRealMethod();
292         when(next.getStateType()).thenReturn(Optional.of(StateType.FAILURE));
293         when(next.isInState(any())).thenCallRealMethod();
294
295         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
296
297         // when:
298         Boolean isFinished = transitionState.isFinished().get();
299
300         // then:
301         assertFalse(isFinished);
302     }
303
304     @Test
305     public void testIsFinishedReturnsTrueWhenStateChangedFromEndProgrammedToOff() {
306         // given:
307         when(previous.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
308         when(previous.isInState(any())).thenCallRealMethod();
309         when(next.getStateType()).thenReturn(Optional.of(StateType.OFF));
310         when(next.isInState(any())).thenCallRealMethod();
311
312         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
313
314         // when:
315         Boolean isFinished = transitionState.isFinished().get();
316
317         // then:
318         assertFalse(isFinished);
319     }
320
321     @Test
322     public void testIsFinishedReturnsNullWhenPreviousStateIsNull() {
323         // given:
324         when(next.getStateType()).thenReturn(Optional.of(StateType.IDLE));
325
326         TransitionState transitionState = new TransitionState(null, next);
327
328         // when:
329         Optional<Boolean> isFinished = transitionState.isFinished();
330
331         // then:
332         assertFalse(isFinished.isPresent());
333     }
334
335     @Test
336     public void testIsFinishedReturnsNullWhenPreviousStateIsUnknown() {
337         // given:
338         when(previous.getStateType()).thenReturn(Optional.empty());
339         when(next.getStateType()).thenReturn(Optional.of(StateType.IDLE));
340
341         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
342
343         // when:
344         Optional<Boolean> isFinished = transitionState.isFinished();
345
346         // then:
347         assertFalse(isFinished.isPresent());
348     }
349
350     @Test
351     public void testProgramStartedWithZeroRemainingTimeShowsNoRemainingTimeAndProgress() {
352         // given:
353         when(previous.isInState(any())).thenCallRealMethod();
354         when(previous.getStateType()).thenReturn(Optional.of(StateType.PROGRAMMED_WAITING_TO_START));
355
356         when(next.isInState(any())).thenCallRealMethod();
357         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
358         when(next.getRemainingTime()).thenReturn(Optional.of(0));
359         when(next.getProgress()).thenReturn(Optional.of(100));
360
361         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
362
363         // when:
364         Optional<Integer> remainingTime = transitionState.getRemainingTime();
365         Optional<Integer> progress = transitionState.getProgress();
366
367         // then:
368         assertFalse(remainingTime.isPresent());
369         assertFalse(progress.isPresent());
370     }
371
372     @Test
373     public void testProgramStartetdWithRemainingTimeShowsRemainingTimeAndProgress() {
374         // given:
375         when(previous.isInState(any())).thenCallRealMethod();
376         when(previous.getStateType()).thenReturn(Optional.of(StateType.PROGRAMMED_WAITING_TO_START));
377
378         when(next.isInState(any())).thenCallRealMethod();
379         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
380         when(next.getRemainingTime()).thenReturn(Optional.of(2));
381         when(next.getProgress()).thenReturn(Optional.of(50));
382
383         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
384
385         // when:
386         int remainingTime = transitionState.getRemainingTime().get();
387         int progress = transitionState.getProgress().get();
388
389         // then:
390         assertEquals(2, remainingTime);
391         assertEquals(50, progress);
392     }
393
394     @Test
395     public void testProgramCountingDownRemainingTimeToZeroShowsRemainingTimeAndProgress() {
396         // given:
397         when(previous.isInState(any())).thenCallRealMethod();
398         when(previous.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
399         when(previous.getRemainingTime()).thenReturn(Optional.of(1));
400
401         when(next.isInState(any())).thenCallRealMethod();
402         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
403         when(next.getRemainingTime()).thenReturn(Optional.of(0));
404         when(next.getProgress()).thenReturn(Optional.of(100));
405
406         TransitionState transitionState = new TransitionState(new TransitionState(null, previous), next);
407
408         // when:
409         int remainingTime = transitionState.getRemainingTime().get();
410         int progress = transitionState.getProgress().get();
411
412         // then:
413         assertEquals(0, remainingTime);
414         assertEquals(100, progress);
415     }
416
417     @Test
418     public void testDevicePairedWhileRunningWithZeroRemainingTimeShowsNoRemainingTimeAndProgress() {
419         // given:
420         when(next.isInState(any())).thenCallRealMethod();
421         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
422         when(next.getRemainingTime()).thenReturn(Optional.of(0));
423         when(next.getProgress()).thenReturn(Optional.of(100));
424
425         TransitionState transitionState = new TransitionState(null, next);
426
427         // when:
428         Optional<Integer> remainingTime = transitionState.getRemainingTime();
429         Optional<Integer> progress = transitionState.getProgress();
430
431         // then:
432         assertFalse(remainingTime.isPresent());
433         assertFalse(progress.isPresent());
434     }
435
436     @Test
437     public void testDevicePairedWhileRunningWithRemainingTimeShowsRemainingTimeAndProgress() {
438         // given:
439         when(next.isInState(any())).thenCallRealMethod();
440         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
441         when(next.getRemainingTime()).thenReturn(Optional.of(3));
442         when(next.getProgress()).thenReturn(Optional.of(80));
443
444         TransitionState transitionState = new TransitionState(null, next);
445
446         // when:
447         int remainingTime = transitionState.getRemainingTime().get();
448         int progress = transitionState.getProgress().get();
449
450         // then:
451         assertEquals(3, remainingTime);
452         assertEquals(80, progress);
453     }
454
455     @Test
456     public void testWhenNoRemainingTimeIsSetWhileProgramIsRunningThenNoRemainingTimeAndProgressIsShown() {
457         // given:
458         when(historic.isInState(any())).thenCallRealMethod();
459         when(historic.getStateType()).thenReturn(Optional.of(StateType.PROGRAMMED_WAITING_TO_START));
460
461         when(previous.isInState(any())).thenCallRealMethod();
462         when(previous.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
463         when(previous.getRemainingTime()).thenReturn(Optional.of(0));
464
465         when(next.isInState(any())).thenCallRealMethod();
466         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
467         when(next.getRemainingTime()).thenReturn(Optional.of(0));
468         when(next.getProgress()).thenReturn(Optional.of(100));
469
470         TransitionState transitionState = new TransitionState(
471                 new TransitionState(new TransitionState(null, historic), previous), next);
472
473         // when:
474         Optional<Integer> remainingTime = transitionState.getRemainingTime();
475         Optional<Integer> progress = transitionState.getProgress();
476
477         // then:
478         assertFalse(remainingTime.isPresent());
479         assertFalse(progress.isPresent());
480     }
481
482     @Test
483     public void testRemainingTimeIsSetWhileRunningShowsRemainingTimeAndProgress() {
484         // given:
485         when(historic.isInState(any())).thenCallRealMethod();
486         when(historic.getStateType()).thenReturn(Optional.of(StateType.PROGRAMMED_WAITING_TO_START));
487
488         when(previous.isInState(any())).thenCallRealMethod();
489         when(previous.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
490         when(previous.getRemainingTime()).thenReturn(Optional.of(0));
491
492         when(next.isInState(any())).thenCallRealMethod();
493         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
494         when(next.getRemainingTime()).thenReturn(Optional.of(100));
495         when(next.getProgress()).thenReturn(Optional.of(10));
496
497         TransitionState transitionState = new TransitionState(
498                 new TransitionState(new TransitionState(null, historic), previous), next);
499
500         // when:
501         int remainingTime = transitionState.getRemainingTime().get();
502         int progress = transitionState.getProgress().get();
503
504         // then:
505         assertEquals(100, remainingTime);
506         assertEquals(10, progress);
507     }
508
509     @Test
510     public void testPreviousProgramDoesNotAffectHandlingOfRemainingTimeAndProgressForNextProgramCase1() {
511         // given:
512         DeviceState beforeHistoric = mock(DeviceState.class);
513         when(beforeHistoric.isInState(any())).thenCallRealMethod();
514         when(beforeHistoric.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
515         when(beforeHistoric.getRemainingTime()).thenReturn(Optional.of(1));
516
517         when(historic.isInState(any())).thenCallRealMethod();
518         when(historic.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
519         when(historic.getRemainingTime()).thenReturn(Optional.of(0));
520
521         when(previous.isInState(any())).thenCallRealMethod();
522         when(previous.getStateType()).thenReturn(Optional.of(StateType.PROGRAMMED_WAITING_TO_START));
523         when(previous.getRemainingTime()).thenReturn(Optional.of(0));
524
525         when(next.isInState(any())).thenCallRealMethod();
526         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
527         when(next.getRemainingTime()).thenReturn(Optional.of(0));
528         when(next.getProgress()).thenReturn(Optional.of(100));
529
530         TransitionState transitionState = new TransitionState(
531                 new TransitionState(new TransitionState(new TransitionState(null, beforeHistoric), historic), previous),
532                 next);
533
534         // when:
535         Optional<Integer> remainingTime = transitionState.getRemainingTime();
536         Optional<Integer> progress = transitionState.getProgress();
537
538         // then:
539         assertFalse(remainingTime.isPresent());
540         assertFalse(progress.isPresent());
541     }
542
543     @Test
544     public void testPreviousProgramDoesNotAffectHandlingOfRemainingTimeAndProgressForNextProgramCase2() {
545         // given:
546         DeviceState beforeHistoric = mock(DeviceState.class);
547         when(beforeHistoric.isInState(any())).thenCallRealMethod();
548         when(beforeHistoric.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
549         when(beforeHistoric.getRemainingTime()).thenReturn(Optional.of(1));
550
551         when(historic.isInState(any())).thenCallRealMethod();
552         when(historic.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
553         when(historic.getRemainingTime()).thenReturn(Optional.of(0));
554
555         when(previous.isInState(any())).thenCallRealMethod();
556         when(previous.getStateType()).thenReturn(Optional.of(StateType.PROGRAMMED_WAITING_TO_START));
557         when(previous.getRemainingTime()).thenReturn(Optional.of(0));
558
559         when(next.isInState(any())).thenCallRealMethod();
560         when(next.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
561         when(next.getRemainingTime()).thenReturn(Optional.of(10));
562         when(next.getProgress()).thenReturn(Optional.of(60));
563
564         TransitionState transitionState = new TransitionState(
565                 new TransitionState(new TransitionState(new TransitionState(null, beforeHistoric), historic), previous),
566                 next);
567
568         // when:
569         int remainingTime = transitionState.getRemainingTime().get();
570         int progress = transitionState.getProgress().get();
571
572         // then:
573         assertEquals(10, remainingTime);
574         assertEquals(60, progress);
575     }
576 }