]> git.basschouten.com Git - openhab-addons.git/blob
e2a4592e0adf43a03434884fc9d0b51407a7aac3
[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 import java.util.Optional;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.mielecloud.internal.webservice.api.json.Device;
27 import org.openhab.binding.mielecloud.internal.webservice.api.json.DeviceIdentLabel;
28 import org.openhab.binding.mielecloud.internal.webservice.api.json.DeviceType;
29 import org.openhab.binding.mielecloud.internal.webservice.api.json.DryingStep;
30 import org.openhab.binding.mielecloud.internal.webservice.api.json.EcoFeedback;
31 import org.openhab.binding.mielecloud.internal.webservice.api.json.EnergyConsumption;
32 import org.openhab.binding.mielecloud.internal.webservice.api.json.Ident;
33 import org.openhab.binding.mielecloud.internal.webservice.api.json.Light;
34 import org.openhab.binding.mielecloud.internal.webservice.api.json.PlateStep;
35 import org.openhab.binding.mielecloud.internal.webservice.api.json.ProgramId;
36 import org.openhab.binding.mielecloud.internal.webservice.api.json.ProgramPhase;
37 import org.openhab.binding.mielecloud.internal.webservice.api.json.ProgramType;
38 import org.openhab.binding.mielecloud.internal.webservice.api.json.RemoteEnable;
39 import org.openhab.binding.mielecloud.internal.webservice.api.json.SpinningSpeed;
40 import org.openhab.binding.mielecloud.internal.webservice.api.json.State;
41 import org.openhab.binding.mielecloud.internal.webservice.api.json.StateType;
42 import org.openhab.binding.mielecloud.internal.webservice.api.json.Status;
43 import org.openhab.binding.mielecloud.internal.webservice.api.json.Temperature;
44 import org.openhab.binding.mielecloud.internal.webservice.api.json.Type;
45 import org.openhab.binding.mielecloud.internal.webservice.api.json.VentilationStep;
46 import org.openhab.binding.mielecloud.internal.webservice.api.json.WaterConsumption;
47
48 /**
49  * @author Björn Lange - Initial contribution
50  * @author Benjamin Bolte - Add pre-heat finished, plate step, door state, door alarm and info state channels and map
51  *         signal flags from API
52  * @author Björn Lange - Add elapsed time channel, robotic vacuum cleaner, eco feedback
53  */
54 @NonNullByDefault
55 public class DeviceStateTest {
56     private static final String DEVICE_IDENTIFIER = "mac-f83001f37d45ffff";
57
58     @Test
59     public void testGetDeviceIdentifierReturnsDeviceIdentifier() {
60         // given:
61         Device device = mock(Device.class);
62         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
63
64         // when:
65         String deviceId = deviceState.getDeviceIdentifier();
66
67         // then:
68         assertEquals(DEVICE_IDENTIFIER, deviceId);
69     }
70
71     @Test
72     public void testReturnValuesWhenDeviceIsNull() {
73         // given:
74         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, null);
75
76         // when:
77         Optional<String> status = deviceState.getStatus();
78         Optional<Integer> statusRaw = deviceState.getStatusRaw();
79         Optional<StateType> stateType = deviceState.getStateType();
80         Optional<String> selectedProgram = deviceState.getSelectedProgram();
81         Optional<Long> selectedProgramId = deviceState.getSelectedProgramId();
82         Optional<String> programPhase = deviceState.getProgramPhase();
83         Optional<Integer> programPhaseRaw = deviceState.getProgramPhaseRaw();
84         Optional<String> dryingTarget = deviceState.getDryingTarget();
85         Optional<Integer> dryingTargetRaw = deviceState.getDryingTargetRaw();
86         Optional<Boolean> hasPreHeatFinished = deviceState.hasPreHeatFinished();
87         Optional<Integer> targetTemperature = deviceState.getTargetTemperature(0);
88         Optional<Integer> temperature = deviceState.getTemperature(0);
89         Optional<Boolean> remoteControlEnabled = deviceState.isRemoteControlEnabled();
90         Optional<String> ventilationStep = deviceState.getVentilationStep();
91         Optional<Integer> ventilationStepRaw = deviceState.getVentilationStepRaw();
92         Optional<Integer> plateStepCount = deviceState.getPlateStepCount();
93         Optional<String> plateStep = deviceState.getPlateStep(0);
94         Optional<Integer> plateStepRaw = deviceState.getPlateStepRaw(0);
95         boolean hasError = deviceState.hasError();
96         boolean hasInfo = deviceState.hasInfo();
97         Optional<Boolean> doorState = deviceState.getDoorState();
98         Optional<Boolean> doorAlarm = deviceState.getDoorAlarm();
99         Optional<String> type = deviceState.getType();
100         DeviceType rawType = deviceState.getRawType();
101         Optional<Integer> batteryLevel = deviceState.getBatteryLevel();
102
103         Optional<String> deviceName = deviceState.getDeviceName();
104         Optional<String> fabNumber = deviceState.getFabNumber();
105         Optional<String> techType = deviceState.getTechType();
106         Optional<Integer> progress = deviceState.getProgress();
107
108         // then:
109         assertFalse(status.isPresent());
110         assertFalse(statusRaw.isPresent());
111         assertFalse(stateType.isPresent());
112         assertFalse(selectedProgram.isPresent());
113         assertFalse(selectedProgramId.isPresent());
114         assertFalse(programPhase.isPresent());
115         assertFalse(programPhaseRaw.isPresent());
116         assertFalse(dryingTarget.isPresent());
117         assertFalse(dryingTargetRaw.isPresent());
118         assertFalse(hasPreHeatFinished.isPresent());
119         assertFalse(targetTemperature.isPresent());
120         assertFalse(temperature.isPresent());
121         assertFalse(remoteControlEnabled.isPresent());
122         assertFalse(ventilationStep.isPresent());
123         assertFalse(ventilationStepRaw.isPresent());
124         assertFalse(plateStepCount.isPresent());
125         assertFalse(plateStep.isPresent());
126         assertFalse(plateStepRaw.isPresent());
127         assertFalse(hasError);
128         assertFalse(hasInfo);
129         assertFalse(doorState.isPresent());
130         assertFalse(doorAlarm.isPresent());
131         assertFalse(type.isPresent());
132         assertEquals(DeviceType.UNKNOWN, rawType);
133         assertFalse(deviceName.isPresent());
134         assertFalse(fabNumber.isPresent());
135         assertFalse(techType.isPresent());
136         assertFalse(progress.isPresent());
137         assertFalse(batteryLevel.isPresent());
138     }
139
140     @Test
141     public void testReturnValuesWhenStateIsNull() {
142         // given:
143         Device device = mock(Device.class);
144         when(device.getState()).thenReturn(Optional.empty());
145         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
146
147         // when:
148         Optional<String> status = deviceState.getStatus();
149         Optional<Integer> statusRaw = deviceState.getStatusRaw();
150         Optional<StateType> stateType = deviceState.getStateType();
151         Optional<String> selectedProgram = deviceState.getSelectedProgram();
152         Optional<Long> selectedProgramId = deviceState.getSelectedProgramId();
153         Optional<String> programPhase = deviceState.getProgramPhase();
154         Optional<Integer> programPhaseRaw = deviceState.getProgramPhaseRaw();
155         Optional<String> dryingTarget = deviceState.getDryingTarget();
156         Optional<Integer> dryingTargetRaw = deviceState.getDryingTargetRaw();
157         Optional<Boolean> hasPreHeatFinished = deviceState.hasPreHeatFinished();
158         Optional<Integer> targetTemperature = deviceState.getTargetTemperature(0);
159         Optional<Integer> temperature = deviceState.getTemperature(0);
160         Optional<Boolean> remoteControlEnabled = deviceState.isRemoteControlEnabled();
161         Optional<Integer> progress = deviceState.getProgress();
162         Optional<String> ventilationStep = deviceState.getVentilationStep();
163         Optional<Integer> ventilationStepRaw = deviceState.getVentilationStepRaw();
164         Optional<Integer> plateStepCount = deviceState.getPlateStepCount();
165         Optional<String> plateStep = deviceState.getPlateStep(0);
166         Optional<Integer> plateStepRaw = deviceState.getPlateStepRaw(0);
167         Boolean hasError = deviceState.hasError();
168         Optional<Boolean> doorState = deviceState.getDoorState();
169         Optional<Boolean> doorAlarm = deviceState.getDoorAlarm();
170         Optional<Integer> batteryLevel = deviceState.getBatteryLevel();
171
172         // then:
173         assertFalse(status.isPresent());
174         assertFalse(statusRaw.isPresent());
175         assertFalse(stateType.isPresent());
176         assertFalse(selectedProgram.isPresent());
177         assertFalse(selectedProgramId.isPresent());
178         assertFalse(programPhase.isPresent());
179         assertFalse(programPhaseRaw.isPresent());
180         assertFalse(dryingTarget.isPresent());
181         assertFalse(dryingTargetRaw.isPresent());
182         assertFalse(hasPreHeatFinished.isPresent());
183         assertFalse(targetTemperature.isPresent());
184         assertFalse(temperature.isPresent());
185         assertFalse(remoteControlEnabled.isPresent());
186         assertFalse(progress.isPresent());
187         assertFalse(ventilationStep.isPresent());
188         assertFalse(ventilationStepRaw.isPresent());
189         assertFalse(plateStepCount.isPresent());
190         assertFalse(plateStep.isPresent());
191         assertFalse(plateStepRaw.isPresent());
192         assertFalse(hasError);
193         assertFalse(doorState.isPresent());
194         assertFalse(doorAlarm.isPresent());
195         assertFalse(batteryLevel.isPresent());
196     }
197
198     @Test
199     public void testReturnValuesWhenStatusIsEmpty() {
200         // given:
201         State state = mock(State.class);
202         when(state.getStatus()).thenReturn(Optional.empty());
203
204         Device device = mock(Device.class);
205         when(device.getState()).thenReturn(Optional.of(state));
206         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
207
208         // when:
209         Optional<String> status = deviceState.getStatus();
210         Optional<Integer> statusRaw = deviceState.getStatusRaw();
211         Optional<StateType> stateType = deviceState.getStateType();
212
213         // then:
214         assertFalse(status.isPresent());
215         assertFalse(statusRaw.isPresent());
216         assertFalse(stateType.isPresent());
217     }
218
219     @Test
220     public void testReturnValuesWhenStatusValuesAreEmpty() {
221         // given:
222         Status statusMock = mock(Status.class);
223         when(statusMock.getValueLocalized()).thenReturn(Optional.empty());
224         when(statusMock.getValueRaw()).thenReturn(Optional.empty());
225
226         State state = mock(State.class);
227         when(state.getStatus()).thenReturn(Optional.of(statusMock));
228
229         Device device = mock(Device.class);
230         when(device.getState()).thenReturn(Optional.of(state));
231         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
232
233         // when:
234         Optional<String> status = deviceState.getStatus();
235         Optional<Integer> statusRaw = deviceState.getStatusRaw();
236         Optional<StateType> stateType = deviceState.getStateType();
237
238         // then:
239         assertFalse(status.isPresent());
240         assertFalse(statusRaw.isPresent());
241         assertFalse(stateType.isPresent());
242     }
243
244     @Test
245     public void testReturnValuesWhenStatusValueLocalizedIsNotNull() {
246         // given:
247         Status statusMock = mock(Status.class);
248         when(statusMock.getValueLocalized()).thenReturn(Optional.of("Not connected"));
249         when(statusMock.getValueRaw()).thenReturn(Optional.of(StateType.NOT_CONNECTED.getCode()));
250
251         State state = mock(State.class);
252         when(state.getStatus()).thenReturn(Optional.of(statusMock));
253
254         Device device = mock(Device.class);
255         when(device.getState()).thenReturn(Optional.of(state));
256         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
257
258         // when:
259         String status = deviceState.getStatus().get();
260         int statusRaw = deviceState.getStatusRaw().get();
261         StateType stateType = deviceState.getStateType().get();
262
263         // then:
264         assertEquals("Not connected", status);
265         assertEquals(StateType.NOT_CONNECTED.getCode(), statusRaw);
266         assertEquals(StateType.NOT_CONNECTED, stateType);
267     }
268
269     @Test
270     public void testReturnValuesWhenStatusValueRawIsNotNull() {
271         // given:
272         Status statusMock = mock(Status.class);
273         when(statusMock.getValueRaw()).thenReturn(Optional.of(StateType.END_PROGRAMMED.getCode()));
274
275         State state = mock(State.class);
276         when(state.getStatus()).thenReturn(Optional.of(statusMock));
277
278         Device device = mock(Device.class);
279         when(device.getState()).thenReturn(Optional.of(state));
280         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
281
282         // when:
283         StateType stateType = deviceState.getStateType().get();
284
285         // then:
286         assertEquals(StateType.END_PROGRAMMED, stateType);
287     }
288
289     @Test
290     public void testReturnValuesWhenProgramTypeIsEmpty() {
291         // given:
292         State state = mock(State.class);
293         when(state.getProgramType()).thenReturn(Optional.empty());
294
295         Device device = mock(Device.class);
296         when(device.getState()).thenReturn(Optional.of(state));
297         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
298
299         // when:
300         Optional<String> selectedProgram = deviceState.getSelectedProgram();
301         Optional<Long> selectedProgramId = deviceState.getSelectedProgramId();
302
303         // then:
304         assertFalse(selectedProgram.isPresent());
305         assertFalse(selectedProgramId.isPresent());
306     }
307
308     @Test
309     public void testReturnValuesWhenProgramTypeValueLocalizedIsEmpty() {
310         // given:
311         ProgramType programType = mock(ProgramType.class);
312         when(programType.getValueLocalized()).thenReturn(Optional.empty());
313
314         State state = mock(State.class);
315         when(state.getProgramType()).thenReturn(Optional.of(programType));
316
317         Device device = mock(Device.class);
318         when(device.getState()).thenReturn(Optional.of(state));
319         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
320
321         // when:
322         Optional<String> selectedProgram = deviceState.getSelectedProgram();
323         Optional<Long> selectedProgramId = deviceState.getSelectedProgramId();
324
325         // then:
326         assertFalse(selectedProgram.isPresent());
327         assertFalse(selectedProgramId.isPresent());
328     }
329
330     @Test
331     public void testReturnValuesWhenProgramTypeValueLocalizedIsNotNull() {
332         // given:
333         ProgramId programId = mock(ProgramId.class);
334         when(programId.getValueRaw()).thenReturn(Optional.of(3L));
335         when(programId.getValueLocalized()).thenReturn(Optional.of("Washing"));
336
337         State state = mock(State.class);
338         Status status = mock(Status.class);
339         Device device = mock(Device.class);
340         when(state.getStatus()).thenReturn(Optional.of(status));
341         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
342         when(state.getProgramId()).thenReturn(Optional.of(programId));
343         when(device.getState()).thenReturn(Optional.of(state));
344         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
345
346         // when:
347         String selectedProgram = deviceState.getSelectedProgram().get();
348         long selectedProgramId = deviceState.getSelectedProgramId().get();
349
350         // then:
351         assertEquals("Washing", selectedProgram);
352         assertEquals(3L, selectedProgramId);
353     }
354
355     @Test
356     public void testReturnValuesWhenProgramPhaseIsEmpty() {
357         // given:
358         State state = mock(State.class);
359         when(state.getProgramPhase()).thenReturn(Optional.empty());
360
361         Device device = mock(Device.class);
362         when(device.getState()).thenReturn(Optional.of(state));
363         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
364
365         // when:
366         Optional<String> programPhase = deviceState.getProgramPhase();
367         Optional<Integer> programPhaseRaw = deviceState.getProgramPhaseRaw();
368
369         // then:
370         assertFalse(programPhase.isPresent());
371         assertFalse(programPhaseRaw.isPresent());
372     }
373
374     @Test
375     public void testReturnValuesWhenProgramPhaseValueLocalizedIsEmpty() {
376         // given:
377         ProgramPhase programPhaseMock = mock(ProgramPhase.class);
378         when(programPhaseMock.getValueLocalized()).thenReturn(Optional.empty());
379
380         State state = mock(State.class);
381         when(state.getProgramPhase()).thenReturn(Optional.of(programPhaseMock));
382
383         Device device = mock(Device.class);
384         when(device.getState()).thenReturn(Optional.of(state));
385         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
386
387         // when:
388         Optional<String> programPhase = deviceState.getProgramPhase();
389         Optional<Integer> programPhaseRaw = deviceState.getProgramPhaseRaw();
390
391         // then:
392         assertFalse(programPhase.isPresent());
393         assertFalse(programPhaseRaw.isPresent());
394     }
395
396     @Test
397     public void testReturnValuesWhenProgramPhaseValueLocalizedIsNotNull() {
398         // given:
399         ProgramPhase programPhaseMock = mock(ProgramPhase.class);
400         when(programPhaseMock.getValueLocalized()).thenReturn(Optional.of("Spülen"));
401         when(programPhaseMock.getValueRaw()).thenReturn(Optional.of(4));
402
403         State state = mock(State.class);
404         Status status = mock(Status.class);
405         Device device = mock(Device.class);
406         when(state.getStatus()).thenReturn(Optional.of(status));
407         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
408         when(state.getProgramPhase()).thenReturn(Optional.of(programPhaseMock));
409         when(device.getState()).thenReturn(Optional.of(state));
410         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
411
412         // when:
413         String programPhase = deviceState.getProgramPhase().get();
414         int programPhaseRaw = deviceState.getProgramPhaseRaw().get();
415
416         // then:
417         assertEquals("Spülen", programPhase);
418         assertEquals(4, programPhaseRaw);
419     }
420
421     @Test
422     public void testReturnValuesWhenDryingStepIsEmpty() {
423         // given:
424         State state = mock(State.class);
425         when(state.getDryingStep()).thenReturn(Optional.empty());
426
427         Device device = mock(Device.class);
428         when(device.getState()).thenReturn(Optional.of(state));
429         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
430
431         // when:
432         Optional<String> dryingTarget = deviceState.getDryingTarget();
433         Optional<Integer> dryingTargetRaw = deviceState.getDryingTargetRaw();
434
435         // then:
436         assertFalse(dryingTarget.isPresent());
437         assertFalse(dryingTargetRaw.isPresent());
438     }
439
440     @Test
441     public void testReturnValuesWhenDryingStepValueLocalizedIsEmpty() {
442         // given:
443         DryingStep dryingStep = mock(DryingStep.class);
444         when(dryingStep.getValueLocalized()).thenReturn(Optional.empty());
445         when(dryingStep.getValueRaw()).thenReturn(Optional.empty());
446
447         State state = mock(State.class);
448         when(state.getDryingStep()).thenReturn(Optional.of(dryingStep));
449
450         Device device = mock(Device.class);
451         when(device.getState()).thenReturn(Optional.of(state));
452         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
453
454         // when:
455         Optional<String> dryingTarget = deviceState.getDryingTarget();
456         Optional<Integer> dryingTargetRaw = deviceState.getDryingTargetRaw();
457
458         // then:
459         assertFalse(dryingTarget.isPresent());
460         assertFalse(dryingTargetRaw.isPresent());
461     }
462
463     @Test
464     public void testReturnValuesWhenDryingStepValueLocalizedIsNotNull() {
465         // given:
466         DryingStep dryingStep = mock(DryingStep.class);
467         when(dryingStep.getValueLocalized()).thenReturn(Optional.of("Hot"));
468         when(dryingStep.getValueRaw()).thenReturn(Optional.of(5));
469
470         State state = mock(State.class);
471         Device device = mock(Device.class);
472         Status status = mock(Status.class);
473         when(state.getDryingStep()).thenReturn(Optional.of(dryingStep));
474         when(state.getStatus()).thenReturn(Optional.of(status));
475         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
476         when(device.getState()).thenReturn(Optional.of(state));
477         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
478
479         // when:
480         String dryingTarget = deviceState.getDryingTarget().get();
481         int dryingTargetRaw = deviceState.getDryingTargetRaw().get();
482
483         // then:
484         assertEquals("Hot", dryingTarget);
485         assertEquals(5, dryingTargetRaw);
486     }
487
488     @Test
489     public void testReturnValuesPreHeatFinishedWhenStateIsNotRunning() {
490         // given:
491         Temperature targetTemperature = mock(Temperature.class);
492         when(targetTemperature.getValueLocalized()).thenReturn(Optional.of(0));
493         Temperature currentTemperature = mock(Temperature.class);
494         when(currentTemperature.getValueLocalized()).thenReturn(Optional.of(0));
495
496         Status status = mock(Status.class);
497         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
498
499         State state = mock(State.class);
500         when(state.getTargetTemperature()).thenReturn(Arrays.asList(targetTemperature));
501         when(state.getTemperature()).thenReturn(Arrays.asList(currentTemperature));
502         when(state.getStatus()).thenReturn(Optional.of(status));
503
504         Device device = mock(Device.class);
505         when(device.getState()).thenReturn(Optional.of(state));
506
507         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
508
509         // when:
510         Optional<Boolean> hasPreHeatFinished = deviceState.hasPreHeatFinished();
511
512         // then:
513         assertFalse(hasPreHeatFinished.get());
514     }
515
516     @Test
517     public void testReturnValuesPreHeatFinishedWhenTargetTemperatureIsEmpty() {
518         // given:
519         Temperature targetTemperature = mock(Temperature.class);
520         when(targetTemperature.getValueLocalized()).thenReturn(Optional.empty());
521         Temperature currentTemperature = mock(Temperature.class);
522         when(currentTemperature.getValueLocalized()).thenReturn(Optional.of(180));
523
524         Status status = mock(Status.class);
525         when(status.getValueRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
526
527         State state = mock(State.class);
528         when(state.getTargetTemperature()).thenReturn(Arrays.asList(targetTemperature));
529         when(state.getTemperature()).thenReturn(Arrays.asList(currentTemperature));
530         when(state.getStatus()).thenReturn(Optional.of(status));
531
532         Device device = mock(Device.class);
533         when(device.getState()).thenReturn(Optional.of(state));
534
535         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
536
537         // when:
538         Optional<Boolean> hasPreHeatFinished = deviceState.hasPreHeatFinished();
539
540         // then:
541         assertFalse(hasPreHeatFinished.isPresent());
542     }
543
544     @Test
545     public void testReturnValuesPreHeatFinishedWhenCurrentTemperatureIsEmpty() {
546         // given:
547         Temperature targetTemperature = mock(Temperature.class);
548         when(targetTemperature.getValueLocalized()).thenReturn(Optional.of(180));
549         Temperature currentTemperature = mock(Temperature.class);
550         when(currentTemperature.getValueLocalized()).thenReturn(Optional.empty());
551
552         Status status = mock(Status.class);
553         when(status.getValueRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
554
555         State state = mock(State.class);
556         when(state.getTargetTemperature()).thenReturn(Arrays.asList(targetTemperature));
557         when(state.getTemperature()).thenReturn(Arrays.asList(currentTemperature));
558         when(state.getStatus()).thenReturn(Optional.of(status));
559
560         Device device = mock(Device.class);
561         when(device.getState()).thenReturn(Optional.of(state));
562
563         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
564
565         // when:
566         Optional<Boolean> hasPreHeatFinished = deviceState.hasPreHeatFinished();
567
568         // then:
569         assertFalse(hasPreHeatFinished.isPresent());
570     }
571
572     @Test
573     public void testReturnValuesPreHeatFinishedWhenPreHeatingHasFinished() {
574         // given:
575         Temperature targetTemperature = mock(Temperature.class);
576         when(targetTemperature.getValueLocalized()).thenReturn(Optional.of(180));
577         Temperature currentTemperature = mock(Temperature.class);
578         when(currentTemperature.getValueLocalized()).thenReturn(Optional.of(180));
579
580         Status status = mock(Status.class);
581         when(status.getValueRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
582
583         State state = mock(State.class);
584         when(state.getTargetTemperature()).thenReturn(Arrays.asList(targetTemperature));
585         when(state.getTemperature()).thenReturn(Arrays.asList(currentTemperature));
586         when(state.getStatus()).thenReturn(Optional.of(status));
587
588         Device device = mock(Device.class);
589         when(device.getState()).thenReturn(Optional.of(state));
590
591         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
592
593         // when:
594         Optional<Boolean> hasPreHeatFinished = deviceState.hasPreHeatFinished();
595
596         // then:
597         assertTrue(hasPreHeatFinished.get());
598     }
599
600     @Test
601     public void testReturnValuesPreHeatFinishedWhenPreHeatingHasNotFinished() {
602         // given:
603         Temperature targetTemperature = mock(Temperature.class);
604         when(targetTemperature.getValueLocalized()).thenReturn(Optional.of(180));
605         Temperature currentTemperature = mock(Temperature.class);
606         when(currentTemperature.getValueLocalized()).thenReturn(Optional.of(179));
607
608         Status status = mock(Status.class);
609         when(status.getValueRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
610
611         State state = mock(State.class);
612         when(state.getTargetTemperature()).thenReturn(Arrays.asList(targetTemperature));
613         when(state.getTemperature()).thenReturn(Arrays.asList(currentTemperature));
614         when(state.getStatus()).thenReturn(Optional.of(status));
615
616         Device device = mock(Device.class);
617         when(device.getState()).thenReturn(Optional.of(state));
618
619         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
620
621         // when:
622         Optional<Boolean> hasPreHeatFinished = deviceState.hasPreHeatFinished();
623
624         // then:
625         assertFalse(hasPreHeatFinished.get());
626     }
627
628     @Test
629     public void testReturnValuesWhenTargetTemperatureIsEmpty() {
630         // given:
631         State state = mock(State.class);
632         when(state.getTargetTemperature()).thenReturn(Collections.emptyList());
633
634         Device device = mock(Device.class);
635         when(device.getState()).thenReturn(Optional.of(state));
636         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
637
638         // when:
639         Optional<Integer> targetTemperature = deviceState.getTargetTemperature(0);
640
641         // then:
642         assertFalse(targetTemperature.isPresent());
643     }
644
645     @Test
646     public void testReturnValuesWhenTargetTemperatureIndexIsOutOfRange() {
647         // given:
648         Status status = mock(Status.class);
649         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
650
651         State state = mock(State.class);
652         when(state.getTargetTemperature()).thenReturn(new LinkedList<>());
653         when(state.getStatus()).thenReturn(Optional.of(status));
654
655         Device device = mock(Device.class);
656         when(device.getState()).thenReturn(Optional.of(state));
657         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
658
659         // when:
660         Optional<Integer> targetTemperature = deviceState.getTargetTemperature(0);
661
662         // then:
663         assertFalse(targetTemperature.isPresent());
664     }
665
666     @Test
667     public void testReturnValuesWhenTargetTemperatureValueLocalizedIsEmpty() {
668         // given:
669         Status status = mock(Status.class);
670         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
671
672         Temperature temperature = mock(Temperature.class);
673         when(temperature.getValueLocalized()).thenReturn(Optional.empty());
674
675         State state = mock(State.class);
676         when(state.getTargetTemperature()).thenReturn(Arrays.asList(temperature));
677         when(state.getStatus()).thenReturn(Optional.of(status));
678
679         Device device = mock(Device.class);
680         when(device.getState()).thenReturn(Optional.of(state));
681         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
682
683         // when:
684         Optional<Integer> targetTemperature = deviceState.getTargetTemperature(0);
685
686         // then:
687         assertFalse(targetTemperature.isPresent());
688     }
689
690     @Test
691     public void testReturnValuesWhenTargetTemperatureValueLocalizedIsValid() {
692         // given:
693         Temperature temperature = mock(Temperature.class);
694         when(temperature.getValueLocalized()).thenReturn(Optional.of(20));
695
696         State state = mock(State.class);
697         Status status = mock(Status.class);
698         Device device = mock(Device.class);
699         when(state.getStatus()).thenReturn(Optional.of(status));
700         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
701         when(state.getTargetTemperature()).thenReturn(Arrays.asList(temperature));
702         when(device.getState()).thenReturn(Optional.of(state));
703         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
704
705         // when:
706         Integer targetTemperature = deviceState.getTargetTemperature(0).get();
707
708         // then:
709         assertEquals(Integer.valueOf(20), targetTemperature);
710     }
711
712     @Test
713     public void testReturnValuesWhenTemperatureIsEmpty() {
714         // given:
715         Status status = mock(Status.class);
716         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
717
718         State state = mock(State.class);
719         when(state.getTemperature()).thenReturn(Collections.emptyList());
720         when(state.getStatus()).thenReturn(Optional.of(status));
721
722         Device device = mock(Device.class);
723         when(device.getState()).thenReturn(Optional.of(state));
724         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
725
726         // when:
727         Optional<Integer> temperature = deviceState.getTemperature(0);
728
729         // then:
730         assertFalse(temperature.isPresent());
731     }
732
733     @Test
734     public void testReturnValuesWhenVentilationStepIsEmpty() {
735         // given:
736         State state = mock(State.class);
737         when(state.getVentilationStep()).thenReturn(Optional.empty());
738
739         Device device = mock(Device.class);
740         when(device.getState()).thenReturn(Optional.of(state));
741         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
742
743         // when:
744         Optional<String> ventilationStep = deviceState.getVentilationStep();
745         Optional<Integer> ventilationStepRaw = deviceState.getVentilationStepRaw();
746
747         // then:
748         assertFalse(ventilationStep.isPresent());
749         assertFalse(ventilationStepRaw.isPresent());
750     }
751
752     @Test
753     public void testReturnValuesWhenTemperatureIndexIsOutOfRange() {
754         // given:
755         Status status = mock(Status.class);
756         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
757
758         State state = mock(State.class);
759         when(state.getTemperature()).thenReturn(new LinkedList<>());
760         when(state.getStatus()).thenReturn(Optional.of(status));
761
762         Device device = mock(Device.class);
763         when(device.getState()).thenReturn(Optional.of(state));
764         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
765
766         // when:
767         Optional<Integer> temperature = deviceState.getTemperature(-1);
768
769         // then:
770         assertFalse(temperature.isPresent());
771     }
772
773     @Test
774     public void testReturnValuesWhenTemperatureValueLocalizedIsEmpty() {
775         // given:
776         Status status = mock(Status.class);
777         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
778
779         Temperature temperatureMock = mock(Temperature.class);
780         when(temperatureMock.getValueLocalized()).thenReturn(Optional.empty());
781
782         State state = mock(State.class);
783         when(state.getTemperature()).thenReturn(Arrays.asList(temperatureMock));
784         when(state.getStatus()).thenReturn(Optional.of(status));
785
786         Device device = mock(Device.class);
787         when(device.getState()).thenReturn(Optional.of(state));
788         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
789
790         // when:
791         Optional<Integer> temperature = deviceState.getTemperature(0);
792
793         // then:
794         assertFalse(temperature.isPresent());
795     }
796
797     @Test
798     public void testReturnValuesWhenTemperatureValueLocalizedIsValid() {
799         // given:
800         Temperature temperatureMock = mock(Temperature.class);
801         when(temperatureMock.getValueLocalized()).thenReturn(Optional.of(10));
802
803         State state = mock(State.class);
804         Device device = mock(Device.class);
805         Status status = mock(Status.class);
806         when(state.getTemperature()).thenReturn(Arrays.asList(temperatureMock));
807         when(state.getStatus()).thenReturn(Optional.of(status));
808         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
809         when(device.getState()).thenReturn(Optional.of(state));
810         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
811
812         // when:
813         Integer temperature = deviceState.getTemperature(0).get();
814
815         // then:
816         assertEquals(Integer.valueOf(10), temperature);
817     }
818
819     @Test
820     public void testReturnValuesWhenPlatStepIndexIsOutOfRange() {
821         // given:
822         Status status = mock(Status.class);
823         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
824
825         State state = mock(State.class);
826         when(state.getPlateStep()).thenReturn(Collections.emptyList());
827         when(state.getStatus()).thenReturn(Optional.of(status));
828
829         Device device = mock(Device.class);
830         when(device.getState()).thenReturn(Optional.of(state));
831         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
832
833         // when:
834         int plateStepCount = deviceState.getPlateStepCount().get();
835         Optional<String> plateStep = deviceState.getPlateStep(0);
836         Optional<Integer> plateStepRaw = deviceState.getPlateStepRaw(0);
837
838         // then:
839         assertEquals(0, plateStepCount);
840         assertFalse(plateStep.isPresent());
841         assertFalse(plateStepRaw.isPresent());
842     }
843
844     @Test
845     public void testReturnValuesWhenPlateStepValueIsEmpty() {
846         // given:
847         Status status = mock(Status.class);
848         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
849
850         PlateStep plateStepMock = mock(PlateStep.class);
851         when(plateStepMock.getValueRaw()).thenReturn(Optional.empty());
852         when(plateStepMock.getValueLocalized()).thenReturn(Optional.empty());
853
854         State state = mock(State.class);
855         when(state.getPlateStep()).thenReturn(List.of(plateStepMock));
856         when(state.getStatus()).thenReturn(Optional.of(status));
857
858         Device device = mock(Device.class);
859         when(device.getState()).thenReturn(Optional.of(state));
860         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
861
862         // when:
863         int plateStepCount = deviceState.getPlateStepCount().get();
864         Optional<String> plateStep = deviceState.getPlateStep(0);
865         Optional<Integer> plateStepRaw = deviceState.getPlateStepRaw(0);
866
867         // then:
868         assertEquals(1, plateStepCount);
869         assertFalse(plateStep.isPresent());
870         assertFalse(plateStepRaw.isPresent());
871     }
872
873     @Test
874     public void testReturnValuesWhenPlateStepValueIsValid() {
875         // given:
876         PlateStep plateStepMock = mock(PlateStep.class);
877         when(plateStepMock.getValueRaw()).thenReturn(Optional.of(2));
878         when(plateStepMock.getValueLocalized()).thenReturn(Optional.of("1."));
879
880         State state = mock(State.class);
881         Status status = mock(Status.class);
882         Device device = mock(Device.class);
883         when(state.getStatus()).thenReturn(Optional.of(status));
884         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
885         when(state.getPlateStep()).thenReturn(Arrays.asList(plateStepMock));
886         when(device.getState()).thenReturn(Optional.of(state));
887         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
888
889         // when:
890         int plateStepCount = deviceState.getPlateStepCount().get();
891         String plateStep = deviceState.getPlateStep(0).get();
892         int plateStepRaw = deviceState.getPlateStepRaw(0).get();
893
894         // then:
895         assertEquals(1, plateStepCount);
896         assertEquals("1.", plateStep);
897         assertEquals(2, plateStepRaw);
898     }
899
900     @Test
901     public void testReturnValuesWhenRemainingTimeIsEmpty() {
902         // given:
903         State state = mock(State.class);
904         when(state.getRemainingTime()).thenReturn(Optional.empty());
905         when(state.getElapsedTime()).thenReturn(Optional.of(Arrays.asList(0, 0)));
906
907         Device device = mock(Device.class);
908         when(device.getState()).thenReturn(Optional.of(state));
909         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
910
911         // when:
912         Optional<Integer> progress = deviceState.getProgress();
913
914         // then:
915         assertFalse(progress.isPresent());
916     }
917
918     @Test
919     public void testReturnValuesWhenRemainingTimeSizeIsNotTwo() {
920         // given:
921         Status status = mock(Status.class);
922         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
923
924         State state = mock(State.class);
925         when(state.getRemainingTime()).thenReturn(Optional.of(Arrays.asList(2)));
926         when(state.getElapsedTime()).thenReturn(Optional.of(Arrays.asList(0, 0)));
927         when(state.getStatus()).thenReturn(Optional.of(status));
928
929         Device device = mock(Device.class);
930         when(device.getState()).thenReturn(Optional.of(state));
931         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
932
933         // when:
934         Optional<Integer> progress = deviceState.getProgress();
935
936         // then:
937         assertFalse(progress.isPresent());
938     }
939
940     @Test
941     public void testReturnValuesWhenRemoteEnableIsEmpty() {
942         // given:
943         State state = mock(State.class);
944         when(state.getRemoteEnable()).thenReturn(Optional.empty());
945
946         Device device = mock(Device.class);
947         when(device.getState()).thenReturn(Optional.of(state));
948         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
949
950         // when:
951         Optional<Boolean> remoteControlEnabled = deviceState.isRemoteControlEnabled();
952
953         // then:
954         assertFalse(remoteControlEnabled.isPresent());
955     }
956
957     @Test
958     public void testReturnValuesWhenFullRemoteControlIsEmpty() {
959         // given:
960         RemoteEnable remoteEnable = mock(RemoteEnable.class);
961         when(remoteEnable.getFullRemoteControl()).thenReturn(Optional.empty());
962
963         State state = mock(State.class);
964         when(state.getRemoteEnable()).thenReturn(Optional.of(remoteEnable));
965
966         Device device = mock(Device.class);
967         when(device.getState()).thenReturn(Optional.of(state));
968         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
969
970         // when:
971         Optional<Boolean> remoteControlEnabled = deviceState.isRemoteControlEnabled();
972
973         // then:
974         assertFalse(remoteControlEnabled.isPresent());
975     }
976
977     @Test
978     public void testReturnValuesWhenFullRemoteControlIsNotNull() {
979         // given:
980         RemoteEnable remoteEnable = mock(RemoteEnable.class);
981         when(remoteEnable.getFullRemoteControl()).thenReturn(Optional.of(true));
982
983         State state = mock(State.class);
984         when(state.getRemoteEnable()).thenReturn(Optional.of(remoteEnable));
985
986         Device device = mock(Device.class);
987         when(device.getState()).thenReturn(Optional.of(state));
988         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
989
990         // when:
991         Boolean remoteControlEnabled = deviceState.isRemoteControlEnabled().get();
992
993         // then:
994         assertTrue(remoteControlEnabled);
995     }
996
997     @Test
998     public void testReturnValuesWhenElapsedTimeIsEmpty() {
999         // given:
1000         Status status = mock(Status.class);
1001         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1002
1003         State state = mock(State.class);
1004         when(state.getElapsedTime()).thenReturn(Optional.empty());
1005         when(state.getRemainingTime()).thenReturn(Optional.of(Arrays.asList(0, 0)));
1006         when(state.getStatus()).thenReturn(Optional.of(status));
1007
1008         Device device = mock(Device.class);
1009         when(device.getState()).thenReturn(Optional.of(state));
1010         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1011
1012         // when:
1013         Optional<Integer> progress = deviceState.getProgress();
1014
1015         // then:
1016         assertFalse(progress.isPresent());
1017     }
1018
1019     @Test
1020     public void testReturnValuesWhenElapsedTimeSizeIsNotTwo() {
1021         // given:
1022         Status status = mock(Status.class);
1023         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1024
1025         State state = mock(State.class);
1026         when(state.getElapsedTime()).thenReturn(Optional.of(Arrays.asList(0)));
1027         when(state.getRemainingTime()).thenReturn(Optional.of(Arrays.asList(0, 0)));
1028         when(state.getStatus()).thenReturn(Optional.of(status));
1029
1030         Device device = mock(Device.class);
1031         when(device.getState()).thenReturn(Optional.of(state));
1032         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1033
1034         // when:
1035         Optional<Integer> progress = deviceState.getProgress();
1036
1037         // then:
1038         assertFalse(progress.isPresent());
1039     }
1040
1041     @Test
1042     public void testReturnValuesWhenElapsedTimeAndRemainingTimeIsZero() {
1043         // given:
1044         Status status = mock(Status.class);
1045         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1046
1047         State state = mock(State.class);
1048         when(state.getElapsedTime()).thenReturn(Optional.of(Arrays.asList(0, 0)));
1049         when(state.getRemainingTime()).thenReturn(Optional.of(Arrays.asList(0, 0)));
1050         when(state.getStatus()).thenReturn(Optional.of(status));
1051
1052         Device device = mock(Device.class);
1053         when(device.getState()).thenReturn(Optional.of(state));
1054         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1055
1056         // when:
1057         Optional<Integer> progress = deviceState.getProgress();
1058
1059         // then:
1060         assertFalse(progress.isPresent());
1061     }
1062
1063     @Test
1064     public void whenElapsedTimeIsNotPresentThenEmptyIsReturned() {
1065         // given:
1066         Status status = mock(Status.class);
1067         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1068
1069         State state = mock(State.class);
1070         when(state.getStatus()).thenReturn(Optional.of(status));
1071         when(state.getElapsedTime()).thenReturn(Optional.empty());
1072
1073         Device device = mock(Device.class);
1074         when(device.getState()).thenReturn(Optional.of(state));
1075
1076         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1077
1078         // when:
1079         Optional<Integer> elapsedTime = deviceState.getElapsedTime();
1080
1081         // then:
1082         assertFalse(elapsedTime.isPresent());
1083     }
1084
1085     @Test
1086     public void whenElapsedTimeIsAnEmptyListThenEmptyIsReturned() {
1087         // given:
1088         Status status = mock(Status.class);
1089         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1090
1091         State state = mock(State.class);
1092         when(state.getStatus()).thenReturn(Optional.of(status));
1093         when(state.getElapsedTime()).thenReturn(Optional.of(Collections.emptyList()));
1094
1095         Device device = mock(Device.class);
1096         when(device.getState()).thenReturn(Optional.of(state));
1097
1098         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1099
1100         // when:
1101         Optional<Integer> elapsedTime = deviceState.getElapsedTime();
1102
1103         // then:
1104         assertFalse(elapsedTime.isPresent());
1105     }
1106
1107     @Test
1108     public void whenElapsedTimeHasOnlyOneElementThenEmptyIsReturned() {
1109         // given:
1110         Status status = mock(Status.class);
1111         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1112
1113         State state = mock(State.class);
1114         when(state.getStatus()).thenReturn(Optional.of(status));
1115         when(state.getElapsedTime()).thenReturn(Optional.of(List.of(2)));
1116
1117         Device device = mock(Device.class);
1118         when(device.getState()).thenReturn(Optional.of(state));
1119
1120         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1121
1122         // when:
1123         Optional<Integer> elapsedTime = deviceState.getElapsedTime();
1124
1125         // then:
1126         assertFalse(elapsedTime.isPresent());
1127     }
1128
1129     @Test
1130     public void whenElapsedTimeHasThreeElementsThenEmptyIsReturned() {
1131         // given:
1132         Status status = mock(Status.class);
1133         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1134
1135         State state = mock(State.class);
1136         when(state.getStatus()).thenReturn(Optional.of(status));
1137         when(state.getElapsedTime()).thenReturn(Optional.of(Arrays.asList(1, 2, 3)));
1138
1139         Device device = mock(Device.class);
1140         when(device.getState()).thenReturn(Optional.of(state));
1141
1142         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1143
1144         // when:
1145         Optional<Integer> elapsedTime = deviceState.getElapsedTime();
1146
1147         // then:
1148         assertFalse(elapsedTime.isPresent());
1149     }
1150
1151     @Test
1152     public void whenElapsedTimeHasTwoElementsThenTheTotalNumberOfSecondsIsReturned() {
1153         // given:
1154         Status status = mock(Status.class);
1155         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1156
1157         State state = mock(State.class);
1158         when(state.getStatus()).thenReturn(Optional.of(status));
1159         when(state.getElapsedTime()).thenReturn(Optional.of(Arrays.asList(1, 2)));
1160
1161         Device device = mock(Device.class);
1162         when(device.getState()).thenReturn(Optional.of(state));
1163
1164         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1165
1166         // when:
1167         Optional<Integer> elapsedTime = deviceState.getElapsedTime();
1168
1169         // then:
1170         assertTrue(elapsedTime.isPresent());
1171         assertEquals(Integer.valueOf((60 + 2) * 60), elapsedTime.get());
1172     }
1173
1174     @Test
1175     public void whenDeviceIsInOffStateThenElapsedTimeIsEmpty() {
1176         // given:
1177         Status status = mock(Status.class);
1178         when(status.getValueRaw()).thenReturn(Optional.of(StateType.OFF.getCode()));
1179
1180         State state = mock(State.class);
1181         when(state.getStatus()).thenReturn(Optional.of(status));
1182         when(state.getElapsedTime()).thenReturn(Optional.of(Arrays.asList(1, 2)));
1183
1184         Device device = mock(Device.class);
1185         when(device.getState()).thenReturn(Optional.of(state));
1186
1187         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1188
1189         // when:
1190         Optional<Integer> elapsedTime = deviceState.getElapsedTime();
1191
1192         // then:
1193         assertFalse(elapsedTime.isPresent());
1194     }
1195
1196     @Test
1197     public void testReturnValuesWhenProgressIs50Percent() {
1198         // given:
1199         State state = mock(State.class);
1200         when(state.getElapsedTime()).thenReturn(Optional.of(Arrays.asList(0, 45)));
1201         when(state.getRemainingTime()).thenReturn(Optional.of(Arrays.asList(0, 45)));
1202
1203         Device device = mock(Device.class);
1204         Status status = mock(Status.class);
1205         when(state.getStatus()).thenReturn(Optional.of(status));
1206         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1207         when(device.getState()).thenReturn(Optional.of(state));
1208         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1209
1210         // when:
1211         Integer progress = deviceState.getProgress().get();
1212
1213         // then:
1214         assertEquals(Integer.valueOf(50), progress);
1215     }
1216
1217     @Test
1218     public void testReturnValuesWhenProgressIs25Percent() {
1219         // given:
1220         State state = mock(State.class);
1221         when(state.getElapsedTime()).thenReturn(Optional.of(Arrays.asList(0, 15)));
1222         when(state.getRemainingTime()).thenReturn(Optional.of(Arrays.asList(0, 45)));
1223
1224         Device device = mock(Device.class);
1225         Status status = mock(Status.class);
1226         when(state.getStatus()).thenReturn(Optional.of(status));
1227         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1228         when(device.getState()).thenReturn(Optional.of(state));
1229         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1230
1231         // when:
1232         Integer progress = deviceState.getProgress().get();
1233
1234         // then:
1235         assertEquals(Integer.valueOf(25), progress);
1236     }
1237
1238     @Test
1239     public void testReturnValuesWhenProgressIs0Percent() {
1240         // given:
1241         State state = mock(State.class);
1242         when(state.getElapsedTime()).thenReturn(Optional.of(Arrays.asList(0, 0)));
1243         when(state.getRemainingTime()).thenReturn(Optional.of(Arrays.asList(0, 45)));
1244
1245         Device device = mock(Device.class);
1246         Status status = mock(Status.class);
1247         when(state.getStatus()).thenReturn(Optional.of(status));
1248         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1249         when(device.getState()).thenReturn(Optional.of(state));
1250         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1251
1252         // when:
1253         Integer progress = deviceState.getProgress().get();
1254
1255         // then:
1256         assertEquals(Integer.valueOf(0), progress);
1257     }
1258
1259     @Test
1260     public void testReturnValuesWhenSignalDoorIsEmpty() {
1261         // given:
1262         State state = mock(State.class);
1263         when(state.getSignalDoor()).thenReturn(Optional.empty());
1264
1265         Status status = mock(Status.class);
1266         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1267         when(state.getStatus()).thenReturn(Optional.of(status));
1268
1269         Device device = mock(Device.class);
1270         when(device.getState()).thenReturn(Optional.of(state));
1271
1272         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1273
1274         // when:
1275         Optional<Boolean> doorState = deviceState.getDoorState();
1276         Optional<Boolean> doorAlarm = deviceState.getDoorAlarm();
1277
1278         // then:
1279         assertFalse(doorState.isPresent());
1280         assertFalse(doorAlarm.isPresent());
1281     }
1282
1283     @Test
1284     public void testReturnValuesWhenSignalDoorIsTrue() {
1285         // given:
1286         State state = mock(State.class);
1287         when(state.getSignalDoor()).thenReturn(Optional.of(true));
1288
1289         Status status = mock(Status.class);
1290         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1291         when(state.getStatus()).thenReturn(Optional.of(status));
1292
1293         Device device = mock(Device.class);
1294         when(device.getState()).thenReturn(Optional.of(state));
1295
1296         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1297
1298         // when:
1299         Optional<Boolean> doorState = deviceState.getDoorState();
1300
1301         // then:
1302         assertTrue(doorState.get());
1303     }
1304
1305     @Test
1306     public void testReturnValuesWhenSignalDoorIsFalse() {
1307         // given:
1308         State state = mock(State.class);
1309         when(state.getSignalDoor()).thenReturn(Optional.of(false));
1310
1311         Status status = mock(Status.class);
1312         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1313         when(state.getStatus()).thenReturn(Optional.of(status));
1314
1315         Device device = mock(Device.class);
1316         when(device.getState()).thenReturn(Optional.of(state));
1317
1318         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1319
1320         // when:
1321         Optional<Boolean> doorState = deviceState.getDoorState();
1322
1323         // then:
1324         assertFalse(doorState.get());
1325     }
1326
1327     @Test
1328     public void testReturnValuesWhenSignalFailureIsEmpty() {
1329         // given:
1330         State state = mock(State.class);
1331         when(state.getSignalDoor()).thenReturn(Optional.of(true));
1332         when(state.getSignalFailure()).thenReturn(Optional.empty());
1333
1334         Status status = mock(Status.class);
1335         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1336         when(state.getStatus()).thenReturn(Optional.of(status));
1337
1338         Device device = mock(Device.class);
1339         when(device.getState()).thenReturn(Optional.of(state));
1340
1341         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1342
1343         // when:
1344         Optional<Boolean> doorAlarm = deviceState.getDoorAlarm();
1345
1346         // then:
1347         assertFalse(doorAlarm.isPresent());
1348     }
1349
1350     @Test
1351     public void testReturnValuesWhenDoorAlarmIsActive() {
1352         // given:
1353         State state = mock(State.class);
1354         when(state.getSignalDoor()).thenReturn(Optional.of(true));
1355         when(state.getSignalFailure()).thenReturn(Optional.of(true));
1356
1357         Status status = mock(Status.class);
1358         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1359         when(state.getStatus()).thenReturn(Optional.of(status));
1360
1361         Device device = mock(Device.class);
1362         when(device.getState()).thenReturn(Optional.of(state));
1363
1364         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1365
1366         // when:
1367         Optional<Boolean> doorAlarm = deviceState.getDoorAlarm();
1368
1369         // then:
1370         assertTrue(doorAlarm.get());
1371     }
1372
1373     @Test
1374     public void testReturnValuesWhenDoorAlarmIsNotActiveBecauseOfNoDoorSignal() {
1375         // given:
1376         State state = mock(State.class);
1377         when(state.getSignalDoor()).thenReturn(Optional.of(false));
1378         when(state.getSignalFailure()).thenReturn(Optional.of(true));
1379
1380         Status status = mock(Status.class);
1381         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1382         when(state.getStatus()).thenReturn(Optional.of(status));
1383
1384         Device device = mock(Device.class);
1385         when(device.getState()).thenReturn(Optional.of(state));
1386
1387         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1388
1389         // when:
1390         Optional<Boolean> doorAlarm = deviceState.getDoorAlarm();
1391
1392         // then:
1393         assertFalse(doorAlarm.get());
1394     }
1395
1396     @Test
1397     public void testReturnValuesWhenDoorAlarmIsNotActiveBecauseOfNoFailureSignal() {
1398         // given:
1399         State state = mock(State.class);
1400         when(state.getSignalDoor()).thenReturn(Optional.of(true));
1401         when(state.getSignalFailure()).thenReturn(Optional.of(false));
1402
1403         Status status = mock(Status.class);
1404         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1405         when(state.getStatus()).thenReturn(Optional.of(status));
1406
1407         Device device = mock(Device.class);
1408         when(device.getState()).thenReturn(Optional.of(state));
1409
1410         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1411
1412         // when:
1413         Optional<Boolean> doorAlarm = deviceState.getDoorAlarm();
1414
1415         // then:
1416         assertFalse(doorAlarm.get());
1417     }
1418
1419     @Test
1420     public void testReturnValuesWhenIdentIsEmpty() {
1421         // given:
1422         Device device = mock(Device.class);
1423         when(device.getIdent()).thenReturn(Optional.empty());
1424         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1425
1426         // when:
1427         Optional<String> type = deviceState.getType();
1428         DeviceType rawType = deviceState.getRawType();
1429         Optional<String> deviceName = deviceState.getDeviceName();
1430         Optional<String> fabNumber = deviceState.getFabNumber();
1431         Optional<String> techType = deviceState.getTechType();
1432
1433         // then:
1434         assertFalse(type.isPresent());
1435         assertEquals(DeviceType.UNKNOWN, rawType);
1436         assertFalse(deviceName.isPresent());
1437         assertFalse(fabNumber.isPresent());
1438         assertFalse(techType.isPresent());
1439     }
1440
1441     @Test
1442     public void testReturnValuesWhenTypeIsEmpty() {
1443         // given:
1444         Ident ident = mock(Ident.class);
1445         when(ident.getType()).thenReturn(Optional.empty());
1446
1447         Device device = mock(Device.class);
1448         when(device.getIdent()).thenReturn(Optional.of(ident));
1449         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1450
1451         // when:
1452         Optional<String> type = deviceState.getType();
1453         DeviceType rawType = deviceState.getRawType();
1454
1455         // then:
1456         assertFalse(type.isPresent());
1457         assertEquals(DeviceType.UNKNOWN, rawType);
1458     }
1459
1460     @Test
1461     public void testReturnValuesWhenTypeValueLocalizedIsEmpty() {
1462         // given:
1463         Type typeMock = mock(Type.class);
1464         when(typeMock.getValueLocalized()).thenReturn(Optional.empty());
1465
1466         Ident ident = mock(Ident.class);
1467         when(ident.getType()).thenReturn(Optional.of(typeMock));
1468
1469         Device device = mock(Device.class);
1470         when(device.getIdent()).thenReturn(Optional.of(ident));
1471         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1472
1473         // when:
1474         Optional<String> type = deviceState.getType();
1475
1476         // then:
1477         assertFalse(type.isPresent());
1478     }
1479
1480     @Test
1481     public void testReturnValuesWhenTypeValueLocalizedIsNotNull() {
1482         // given:
1483         Type typeMock = mock(Type.class);
1484         when(typeMock.getValueLocalized()).thenReturn(Optional.of("Hood"));
1485
1486         Ident ident = mock(Ident.class);
1487         when(ident.getType()).thenReturn(Optional.of(typeMock));
1488
1489         Device device = mock(Device.class);
1490         when(device.getIdent()).thenReturn(Optional.of(ident));
1491         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1492
1493         // when:
1494         String type = deviceState.getType().get();
1495
1496         // then:
1497         assertEquals("Hood", type);
1498     }
1499
1500     @Test
1501     public void testReturnValuesWhenTypeValueRawIsNotNull() {
1502         // given:
1503         Type typeMock = mock(Type.class);
1504         when(typeMock.getValueRaw()).thenReturn(DeviceType.COFFEE_SYSTEM);
1505
1506         Ident ident = mock(Ident.class);
1507         when(ident.getType()).thenReturn(Optional.of(typeMock));
1508
1509         Device device = mock(Device.class);
1510         when(device.getIdent()).thenReturn(Optional.of(ident));
1511         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1512
1513         // when:
1514         DeviceType rawType = deviceState.getRawType();
1515
1516         // then:
1517         assertEquals(DeviceType.COFFEE_SYSTEM, rawType);
1518     }
1519
1520     @Test
1521     public void testReturnValuesWhenDeviceNameIsEmpty() {
1522         // given:
1523         Ident ident = mock(Ident.class);
1524         when(ident.getDeviceName()).thenReturn(Optional.empty());
1525
1526         Device device = mock(Device.class);
1527         when(device.getIdent()).thenReturn(Optional.of(ident));
1528         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1529
1530         // when:
1531         Optional<String> deviceName = deviceState.getDeviceName();
1532
1533         // then:
1534         assertFalse(deviceName.isPresent());
1535     }
1536
1537     @Test
1538     public void testReturnValuesWhenDeviceNameIsEmptyString() {
1539         // given:
1540         Ident ident = mock(Ident.class);
1541         when(ident.getDeviceName()).thenReturn(Optional.of(""));
1542
1543         Device device = mock(Device.class);
1544         when(device.getIdent()).thenReturn(Optional.of(ident));
1545         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1546
1547         // when:
1548         Optional<String> deviceName = deviceState.getDeviceName();
1549
1550         // then:
1551         assertFalse(deviceName.isPresent());
1552     }
1553
1554     @Test
1555     public void testReturnValuesWhenDeviceNameIsValid() {
1556         // given:
1557         Ident ident = mock(Ident.class);
1558         when(ident.getDeviceName()).thenReturn(Optional.of("MyWashingMachine"));
1559
1560         Device device = mock(Device.class);
1561         when(device.getIdent()).thenReturn(Optional.of(ident));
1562         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1563
1564         // when:
1565         Optional<String> deviceName = deviceState.getDeviceName();
1566
1567         // then:
1568         assertEquals(Optional.of("MyWashingMachine"), deviceName);
1569     }
1570
1571     @Test
1572     public void testReturnValuesWhenFabNumberIsNotNull() {
1573         // given:
1574         DeviceIdentLabel deviceIdentLabel = mock(DeviceIdentLabel.class);
1575         when(deviceIdentLabel.getFabNumber()).thenReturn(Optional.of("000061431659"));
1576
1577         Ident ident = mock(Ident.class);
1578         when(ident.getDeviceIdentLabel()).thenReturn(Optional.of(deviceIdentLabel));
1579
1580         Device device = mock(Device.class);
1581         when(device.getIdent()).thenReturn(Optional.of(ident));
1582         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1583
1584         // when:
1585         String fabNumber = deviceState.getFabNumber().get();
1586
1587         // then:
1588         assertEquals("000061431659", fabNumber);
1589     }
1590
1591     @Test
1592     public void testReturnValuesWhenTechTypeIsNotNull() {
1593         // given:
1594         DeviceIdentLabel deviceIdentLabel = mock(DeviceIdentLabel.class);
1595         when(deviceIdentLabel.getTechType()).thenReturn(Optional.of("XKM3100WEC"));
1596
1597         Ident ident = mock(Ident.class);
1598         when(ident.getDeviceIdentLabel()).thenReturn(Optional.of(deviceIdentLabel));
1599
1600         Device device = mock(Device.class);
1601         when(device.getIdent()).thenReturn(Optional.of(ident));
1602         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1603
1604         // when:
1605         String techType = deviceState.getTechType().get();
1606
1607         // then:
1608         assertEquals("XKM3100WEC", techType);
1609     }
1610
1611     @Test
1612     public void whenDeviceIsInFailureStateThenItHasAnError() {
1613         // given:
1614         Status status = mock(Status.class);
1615         when(status.getValueRaw()).thenReturn(Optional.of(StateType.FAILURE.getCode()));
1616
1617         State state = mock(State.class);
1618         when(state.getStatus()).thenReturn(Optional.of(status));
1619
1620         Device device = mock(Device.class);
1621         when(device.getState()).thenReturn(Optional.of(state));
1622         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1623
1624         // when:
1625         boolean hasError = deviceState.hasError();
1626
1627         // then:
1628         assertTrue(hasError);
1629     }
1630
1631     @Test
1632     public void whenDeviceIsInRunningStateAndDoesNotSignalAFailureThenItHasNoError() {
1633         // given:
1634         Status status = mock(Status.class);
1635         when(status.getValueRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
1636
1637         State state = mock(State.class);
1638         when(state.getStatus()).thenReturn(Optional.of(status));
1639
1640         Device device = mock(Device.class);
1641         when(device.getState()).thenReturn(Optional.of(state));
1642         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1643
1644         // when:
1645         boolean hasError = deviceState.hasError();
1646
1647         // then:
1648         assertFalse(hasError);
1649     }
1650
1651     @Test
1652     public void whenDeviceSignalsAFailureThenItHasAnError() {
1653         // given:
1654         Status status = mock(Status.class);
1655         when(status.getValueRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
1656
1657         State state = mock(State.class);
1658         when(state.getStatus()).thenReturn(Optional.of(status));
1659         when(state.getSignalFailure()).thenReturn(Optional.of(true));
1660
1661         Device device = mock(Device.class);
1662         when(device.getState()).thenReturn(Optional.of(state));
1663         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1664
1665         // when:
1666         boolean hasError = deviceState.hasError();
1667
1668         // then:
1669         assertTrue(hasError);
1670     }
1671
1672     @Test
1673     public void testReturnValuesForHasInfoWhenSignalInfoIsEmpty() {
1674         // given:
1675         State state = mock(State.class);
1676         when(state.getSignalInfo()).thenReturn(Optional.empty());
1677
1678         Status status = mock(Status.class);
1679         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1680         when(state.getStatus()).thenReturn(Optional.of(status));
1681
1682         Device device = mock(Device.class);
1683         when(device.getState()).thenReturn(Optional.of(state));
1684         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1685
1686         // when:
1687         boolean hasInfo = deviceState.hasInfo();
1688
1689         // then:
1690         assertFalse(hasInfo);
1691     }
1692
1693     @Test
1694     public void whenDeviceSignalsAnInfoThenItHasAnInfo() {
1695         // given:
1696         State state = mock(State.class);
1697         when(state.getSignalInfo()).thenReturn(Optional.of(true));
1698
1699         Status status = mock(Status.class);
1700         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1701         when(state.getStatus()).thenReturn(Optional.of(status));
1702
1703         Device device = mock(Device.class);
1704         when(device.getState()).thenReturn(Optional.of(state));
1705         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1706
1707         // when:
1708         boolean hasInfo = deviceState.hasInfo();
1709
1710         // then:
1711         assertTrue(hasInfo);
1712     }
1713
1714     @Test
1715     public void whenDeviceSignalsNoInfoThenItHasNoInfo() {
1716         // given:
1717         State state = mock(State.class);
1718         when(state.getSignalInfo()).thenReturn(Optional.of(false));
1719
1720         Status status = mock(Status.class);
1721         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1722         when(state.getStatus()).thenReturn(Optional.of(status));
1723
1724         Device device = mock(Device.class);
1725         when(device.getState()).thenReturn(Optional.of(state));
1726         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1727
1728         // when:
1729         boolean hasInfo = deviceState.hasInfo();
1730
1731         // then:
1732         assertFalse(hasInfo);
1733     }
1734
1735     @Test
1736     public void testReturnValuesForVentilationStep() {
1737         // given:
1738         VentilationStep ventilationStepMock = mock(VentilationStep.class);
1739         when(ventilationStepMock.getValueLocalized()).thenReturn(Optional.of("Step 1"));
1740         when(ventilationStepMock.getValueRaw()).thenReturn(Optional.of(1));
1741
1742         Status status = mock(Status.class);
1743         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1744
1745         State state = mock(State.class);
1746         when(state.getVentilationStep()).thenReturn(Optional.of(ventilationStepMock));
1747         when(state.getStatus()).thenReturn(Optional.of(status));
1748
1749         Device device = mock(Device.class);
1750         when(device.getState()).thenReturn(Optional.of(state));
1751
1752         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1753
1754         // when:
1755         String ventilationStep = deviceState.getVentilationStep().get();
1756         int ventilationStepRaw = deviceState.getVentilationStepRaw().get();
1757
1758         // then:
1759         assertEquals("Step 1", ventilationStep);
1760         assertEquals(1, ventilationStepRaw);
1761     }
1762
1763     @Test
1764     public void testProgramPhaseWhenDeviceIsInOffState() {
1765         // given:
1766         ProgramPhase programPhase = mock(ProgramPhase.class);
1767         when(programPhase.getValueLocalized()).thenReturn(Optional.of("Washing"));
1768         when(programPhase.getValueRaw()).thenReturn(Optional.of(3));
1769
1770         Status status = mock(Status.class);
1771         when(status.getValueRaw()).thenReturn(Optional.of(StateType.OFF.getCode()));
1772
1773         State state = mock(State.class);
1774         when(state.getProgramPhase()).thenReturn(Optional.of(programPhase));
1775         when(state.getStatus()).thenReturn(Optional.of(status));
1776
1777         Device device = mock(Device.class);
1778         when(device.getState()).thenReturn(Optional.of(state));
1779
1780         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1781
1782         // when:
1783         Optional<String> phase = deviceState.getProgramPhase();
1784         Optional<Integer> phaseRaw = deviceState.getProgramPhaseRaw();
1785
1786         // then:
1787         assertFalse(phase.isPresent());
1788         assertFalse(phaseRaw.isPresent());
1789     }
1790
1791     @Test
1792     public void testDryingTargetWhenDeviceIsInOffState() {
1793         // given:
1794         DryingStep dryingStep = mock(DryingStep.class);
1795         when(dryingStep.getValueLocalized()).thenReturn(Optional.of("Schranktrocken"));
1796         when(dryingStep.getValueRaw()).thenReturn(Optional.of(3));
1797
1798         Status status = mock(Status.class);
1799         when(status.getValueRaw()).thenReturn(Optional.of(StateType.OFF.getCode()));
1800
1801         State state = mock(State.class);
1802         when(state.getDryingStep()).thenReturn(Optional.of(dryingStep));
1803         when(state.getStatus()).thenReturn(Optional.of(status));
1804
1805         Device device = mock(Device.class);
1806         when(device.getState()).thenReturn(Optional.of(state));
1807
1808         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1809
1810         // when:
1811         Optional<String> dryingTarget = deviceState.getDryingTarget();
1812         Optional<Integer> dryingTargetRaw = deviceState.getDryingTargetRaw();
1813
1814         // then:
1815         assertFalse(dryingTarget.isPresent());
1816         assertFalse(dryingTargetRaw.isPresent());
1817     }
1818
1819     @Test
1820     public void testVentilationStepWhenDeviceIsInOffState() {
1821         // given:
1822         VentilationStep ventilationStep = mock(VentilationStep.class);
1823         when(ventilationStep.getValueLocalized()).thenReturn(Optional.of("Stufe 1"));
1824         when(ventilationStep.getValueRaw()).thenReturn(Optional.of(1));
1825
1826         Status status = mock(Status.class);
1827         when(status.getValueRaw()).thenReturn(Optional.of(StateType.OFF.getCode()));
1828
1829         State state = mock(State.class);
1830         when(state.getVentilationStep()).thenReturn(Optional.of(ventilationStep));
1831         when(state.getStatus()).thenReturn(Optional.of(status));
1832
1833         Device device = mock(Device.class);
1834         when(device.getState()).thenReturn(Optional.of(state));
1835
1836         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1837
1838         // when:
1839         Optional<String> step = deviceState.getVentilationStep();
1840         Optional<Integer> stepRaw = deviceState.getVentilationStepRaw();
1841
1842         // then:
1843         assertFalse(step.isPresent());
1844         assertFalse(stepRaw.isPresent());
1845     }
1846
1847     @Test
1848     public void testReturnValuesWhenDeviceIsInOffState() {
1849         // given:
1850         Device device = mock(Device.class);
1851         State state = mock(State.class);
1852         Status status = mock(Status.class);
1853
1854         when(device.getState()).thenReturn(Optional.of(state));
1855         when(state.getStatus()).thenReturn(Optional.of(status));
1856         when(status.getValueRaw()).thenReturn(Optional.of(StateType.OFF.getCode()));
1857         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1858
1859         // Test SelectedProgram:
1860         ProgramId programId = mock(ProgramId.class);
1861         when(state.getProgramId()).thenReturn(Optional.of(programId));
1862         when(programId.getValueLocalized()).thenReturn(Optional.of("Washing"));
1863         // when:
1864         Optional<String> selectedProgram = deviceState.getSelectedProgram();
1865         // then:
1866         assertFalse(selectedProgram.isPresent());
1867
1868         // Test TargetTemperature:
1869         Temperature targetTemperatureMock = mock(Temperature.class);
1870         when(state.getTargetTemperature()).thenReturn(List.of(targetTemperatureMock));
1871         when(targetTemperatureMock.getValueLocalized()).thenReturn(Optional.of(200));
1872         // when:
1873         Optional<Integer> targetTemperature = deviceState.getTargetTemperature(0);
1874         // then:
1875         assertFalse(targetTemperature.isPresent());
1876
1877         // Test Temperature:
1878         Temperature temperature = mock(Temperature.class);
1879         when(state.getTemperature()).thenReturn(List.of(temperature));
1880         when(temperature.getValueLocalized()).thenReturn(Optional.of(200));
1881         // when:
1882         Optional<Integer> t = deviceState.getTemperature(0);
1883         // then:
1884         assertFalse(t.isPresent());
1885
1886         // Test Progress:
1887         when(state.getElapsedTime()).thenReturn(Optional.of(Arrays.asList(0, 5)));
1888         when(state.getRemainingTime()).thenReturn(Optional.of(Arrays.asList(1, 5)));
1889         // when:
1890         Optional<Integer> progress = deviceState.getProgress();
1891         // then:
1892         assertFalse(progress.isPresent());
1893     }
1894
1895     @Test
1896     public void testWhenDeviceIsInOffStateThenGetSpinningSpeedReturnsNull() {
1897         // given:
1898         Status status = mock(Status.class);
1899         when(status.getValueRaw()).thenReturn(Optional.of(StateType.OFF.getCode()));
1900
1901         SpinningSpeed spinningSpeed = mock(SpinningSpeed.class);
1902         when(spinningSpeed.getValueRaw()).thenReturn(Optional.of(800));
1903         when(spinningSpeed.getValueLocalized()).thenReturn(Optional.of("800"));
1904         when(spinningSpeed.getUnit()).thenReturn(Optional.of("rpm"));
1905
1906         State state = mock(State.class);
1907         when(state.getStatus()).thenReturn(Optional.of(status));
1908         when(state.getSpinningSpeed()).thenReturn(Optional.of(spinningSpeed));
1909
1910         Device device = mock(Device.class);
1911         when(device.getState()).thenReturn(Optional.of(state));
1912
1913         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1914
1915         // when:
1916         Optional<String> speed = deviceState.getSpinningSpeed();
1917         Optional<Integer> speedRaw = deviceState.getSpinningSpeedRaw();
1918
1919         // then:
1920         assertFalse(speed.isPresent());
1921         assertFalse(speedRaw.isPresent());
1922     }
1923
1924     @Test
1925     public void testGetSpinningSpeedReturnsNullWhenSpinningSpeedIsEmpty() {
1926         // given:
1927         Status status = mock(Status.class);
1928         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1929
1930         State state = mock(State.class);
1931         when(state.getStatus()).thenReturn(Optional.of(status));
1932         when(state.getSpinningSpeed()).thenReturn(Optional.empty());
1933
1934         Device device = mock(Device.class);
1935         when(device.getState()).thenReturn(Optional.of(state));
1936
1937         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1938
1939         // when:
1940         Optional<String> spinningSpeed = deviceState.getSpinningSpeed();
1941         Optional<Integer> spinningSpeedRaw = deviceState.getSpinningSpeedRaw();
1942
1943         // then:
1944         assertFalse(spinningSpeed.isPresent());
1945         assertFalse(spinningSpeedRaw.isPresent());
1946     }
1947
1948     @Test
1949     public void testGetSpinningSpeedReturnsNullWhenSpinningSpeedRawValueIsEmpty() {
1950         // given:
1951         Status status = mock(Status.class);
1952         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1953
1954         SpinningSpeed spinningSpeedMock = mock(SpinningSpeed.class);
1955         when(spinningSpeedMock.getValueRaw()).thenReturn(Optional.empty());
1956         when(spinningSpeedMock.getValueLocalized()).thenReturn(Optional.of("1200"));
1957
1958         State state = mock(State.class);
1959         when(state.getStatus()).thenReturn(Optional.of(status));
1960         when(state.getSpinningSpeed()).thenReturn(Optional.of(spinningSpeedMock));
1961
1962         Device device = mock(Device.class);
1963         when(device.getState()).thenReturn(Optional.of(state));
1964
1965         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1966
1967         // when:
1968         Optional<String> spinningSpeed = deviceState.getSpinningSpeed();
1969         Optional<Integer> spinningSpeedRaw = deviceState.getSpinningSpeedRaw();
1970
1971         // then:
1972         assertFalse(spinningSpeed.isPresent());
1973         assertFalse(spinningSpeedRaw.isPresent());
1974     }
1975
1976     @Test
1977     public void testGetSpinningSpeedReturnsValidValueWhenSpinningSpeedRawValueIsNotNull() {
1978         // given:
1979         Status status = mock(Status.class);
1980         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
1981
1982         SpinningSpeed spinningSpeedMock = mock(SpinningSpeed.class);
1983         when(spinningSpeedMock.getValueRaw()).thenReturn(Optional.of(1200));
1984         when(spinningSpeedMock.getValueLocalized()).thenReturn(Optional.of("1200"));
1985
1986         State state = mock(State.class);
1987         when(state.getStatus()).thenReturn(Optional.of(status));
1988         when(state.getSpinningSpeed()).thenReturn(Optional.of(spinningSpeedMock));
1989
1990         Device device = mock(Device.class);
1991         when(device.getState()).thenReturn(Optional.of(state));
1992
1993         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
1994
1995         // when:
1996         String spinningSpeed = deviceState.getSpinningSpeed().get();
1997         int spinningSpeedRaw = deviceState.getSpinningSpeedRaw().get();
1998
1999         // then:
2000         assertEquals("1200", spinningSpeed);
2001         assertEquals(1200, spinningSpeedRaw);
2002     }
2003
2004     @Test
2005     public void testGetLightStateWhenDeviceIsOff() {
2006         // given:
2007         Status status = mock(Status.class);
2008         when(status.getValueRaw()).thenReturn(Optional.of(StateType.OFF.getCode()));
2009
2010         State state = mock(State.class);
2011         when(state.getStatus()).thenReturn(Optional.of(status));
2012
2013         Device device = mock(Device.class);
2014         when(device.getState()).thenReturn(Optional.of(state));
2015
2016         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2017
2018         // when:
2019         Optional<Boolean> lightState = deviceState.getLightState();
2020
2021         // then:
2022         assertFalse(lightState.isPresent());
2023     }
2024
2025     @Test
2026     public void testGetLightStateWhenLightIsUnknown() {
2027         // given:
2028         Status status = mock(Status.class);
2029         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2030
2031         State state = mock(State.class);
2032         when(state.getStatus()).thenReturn(Optional.of(status));
2033         when(state.getLight()).thenReturn(Light.UNKNOWN);
2034
2035         Device device = mock(Device.class);
2036         when(device.getState()).thenReturn(Optional.of(state));
2037
2038         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2039
2040         // when:
2041         Optional<Boolean> lightState = deviceState.getLightState();
2042
2043         // then:
2044         assertFalse(lightState.isPresent());
2045     }
2046
2047     @Test
2048     public void testGetLightStateWhenLightIsEnabled() {
2049         // given:
2050         Status status = mock(Status.class);
2051         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2052
2053         State state = mock(State.class);
2054         when(state.getStatus()).thenReturn(Optional.of(status));
2055         when(state.getLight()).thenReturn(Light.ENABLE);
2056
2057         Device device = mock(Device.class);
2058         when(device.getState()).thenReturn(Optional.of(state));
2059
2060         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2061
2062         // when:
2063         Boolean lightState = deviceState.getLightState().get();
2064
2065         // then:
2066         assertEquals(Boolean.valueOf(true), lightState);
2067     }
2068
2069     @Test
2070     public void testGetLightStateWhenLightIsDisabled() {
2071         // given:
2072         Status status = mock(Status.class);
2073         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2074
2075         State state = mock(State.class);
2076         when(state.getStatus()).thenReturn(Optional.of(status));
2077         when(state.getLight()).thenReturn(Light.DISABLE);
2078
2079         Device device = mock(Device.class);
2080         when(device.getState()).thenReturn(Optional.of(state));
2081
2082         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2083
2084         // when:
2085         Boolean lightState = deviceState.getLightState().get();
2086
2087         // then:
2088         assertEquals(Boolean.valueOf(false), lightState);
2089     }
2090
2091     @Test
2092     public void testGetLightStateWhenLightIsNotSupported() {
2093         // given:
2094         Status status = mock(Status.class);
2095         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2096
2097         State state = mock(State.class);
2098         when(state.getStatus()).thenReturn(Optional.of(status));
2099         when(state.getLight()).thenReturn(Light.NOT_SUPPORTED);
2100
2101         Device device = mock(Device.class);
2102         when(device.getState()).thenReturn(Optional.of(state));
2103
2104         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2105
2106         // when:
2107         Optional<Boolean> lightState = deviceState.getLightState();
2108
2109         // then:
2110         assertFalse(lightState.isPresent());
2111     }
2112
2113     @Test
2114     public void testGetCurrentWaterConsumptionWhenEcoFeedbackIsNotPresent() {
2115         // given:
2116         Status status = mock(Status.class);
2117         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2118
2119         State state = mock(State.class);
2120         when(state.getStatus()).thenReturn(Optional.of(status));
2121         when(state.getEcoFeedback()).thenReturn(Optional.empty());
2122
2123         Device device = mock(Device.class);
2124         when(device.getState()).thenReturn(Optional.of(state));
2125
2126         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2127
2128         // when:
2129         Optional<Quantity> waterConsumption = deviceState.getCurrentWaterConsumption();
2130
2131         // then:
2132         assertFalse(waterConsumption.isPresent());
2133     }
2134
2135     @Test
2136     public void testGetCurrentWaterConsumptionWhenCurrentWaterConsumptionIsNotPresent() {
2137         // given:
2138         Status status = mock(Status.class);
2139         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2140
2141         EcoFeedback ecoFeedback = mock(EcoFeedback.class);
2142         when(ecoFeedback.getCurrentWaterConsumption()).thenReturn(Optional.empty());
2143
2144         State state = mock(State.class);
2145         when(state.getStatus()).thenReturn(Optional.of(status));
2146         when(state.getEcoFeedback()).thenReturn(Optional.of(ecoFeedback));
2147
2148         Device device = mock(Device.class);
2149         when(device.getState()).thenReturn(Optional.of(state));
2150
2151         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2152
2153         // when:
2154         Optional<Quantity> waterConsumption = deviceState.getCurrentWaterConsumption();
2155
2156         // then:
2157         assertFalse(waterConsumption.isPresent());
2158     }
2159
2160     @Test
2161     public void testGetCurrentWaterConsumptionWhenCurrentWaterConsumptionIsEmpty() {
2162         // given:
2163         Status status = mock(Status.class);
2164         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2165
2166         WaterConsumption currentWaterConsumption = mock(WaterConsumption.class);
2167         when(currentWaterConsumption.getUnit()).thenReturn(Optional.empty());
2168         when(currentWaterConsumption.getValue()).thenReturn(Optional.empty());
2169
2170         EcoFeedback ecoFeedback = mock(EcoFeedback.class);
2171         when(ecoFeedback.getCurrentWaterConsumption()).thenReturn(Optional.of(currentWaterConsumption));
2172
2173         State state = mock(State.class);
2174         when(state.getStatus()).thenReturn(Optional.of(status));
2175         when(state.getEcoFeedback()).thenReturn(Optional.of(ecoFeedback));
2176
2177         Device device = mock(Device.class);
2178         when(device.getState()).thenReturn(Optional.of(state));
2179
2180         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2181
2182         // when:
2183         Optional<Quantity> waterConsumption = deviceState.getCurrentWaterConsumption();
2184
2185         // then:
2186         assertFalse(waterConsumption.isPresent());
2187     }
2188
2189     @Test
2190     public void testGetCurrentWaterConsumptionWhenValueIsNotPresent() {
2191         // given:
2192         Status status = mock(Status.class);
2193         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2194
2195         WaterConsumption currentWaterConsumption = mock(WaterConsumption.class);
2196         when(currentWaterConsumption.getUnit()).thenReturn(Optional.of("l"));
2197         when(currentWaterConsumption.getValue()).thenReturn(Optional.empty());
2198
2199         EcoFeedback ecoFeedback = mock(EcoFeedback.class);
2200         when(ecoFeedback.getCurrentWaterConsumption()).thenReturn(Optional.of(currentWaterConsumption));
2201
2202         State state = mock(State.class);
2203         when(state.getStatus()).thenReturn(Optional.of(status));
2204         when(state.getEcoFeedback()).thenReturn(Optional.of(ecoFeedback));
2205
2206         Device device = mock(Device.class);
2207         when(device.getState()).thenReturn(Optional.of(state));
2208
2209         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2210
2211         // when:
2212         Optional<Quantity> waterConsumption = deviceState.getCurrentWaterConsumption();
2213
2214         // then:
2215         assertFalse(waterConsumption.isPresent());
2216     }
2217
2218     @Test
2219     public void testGetCurrentWaterConsumptionWhenUnitIsNotPresent() {
2220         // given:
2221         Status status = mock(Status.class);
2222         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2223
2224         WaterConsumption currentWaterConsumption = mock(WaterConsumption.class);
2225         when(currentWaterConsumption.getUnit()).thenReturn(Optional.empty());
2226         when(currentWaterConsumption.getValue()).thenReturn(Optional.of(0.5));
2227
2228         EcoFeedback ecoFeedback = mock(EcoFeedback.class);
2229         when(ecoFeedback.getCurrentWaterConsumption()).thenReturn(Optional.of(currentWaterConsumption));
2230
2231         State state = mock(State.class);
2232         when(state.getStatus()).thenReturn(Optional.of(status));
2233         when(state.getEcoFeedback()).thenReturn(Optional.of(ecoFeedback));
2234
2235         Device device = mock(Device.class);
2236         when(device.getState()).thenReturn(Optional.of(state));
2237
2238         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2239
2240         // when:
2241         Quantity waterConsumption = deviceState.getCurrentWaterConsumption().get();
2242
2243         // then:
2244         assertEquals(0.5, waterConsumption.getValue());
2245         assertFalse(waterConsumption.getUnit().isPresent());
2246     }
2247
2248     @Test
2249     public void testGetCurrentWaterConsumption() {
2250         // given:
2251         Status status = mock(Status.class);
2252         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2253
2254         WaterConsumption currentWaterConsumption = mock(WaterConsumption.class);
2255         when(currentWaterConsumption.getUnit()).thenReturn(Optional.of("l"));
2256         when(currentWaterConsumption.getValue()).thenReturn(Optional.of(0.5));
2257
2258         EcoFeedback ecoFeedback = mock(EcoFeedback.class);
2259         when(ecoFeedback.getCurrentWaterConsumption()).thenReturn(Optional.of(currentWaterConsumption));
2260
2261         State state = mock(State.class);
2262         when(state.getStatus()).thenReturn(Optional.of(status));
2263         when(state.getEcoFeedback()).thenReturn(Optional.of(ecoFeedback));
2264
2265         Device device = mock(Device.class);
2266         when(device.getState()).thenReturn(Optional.of(state));
2267
2268         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2269
2270         // when:
2271         Quantity waterConsumption = deviceState.getCurrentWaterConsumption().get();
2272
2273         // then:
2274         assertEquals(0.5, waterConsumption.getValue());
2275         assertEquals(Optional.of("l"), waterConsumption.getUnit());
2276     }
2277
2278     @Test
2279     public void testGetCurrentEnergyConsumptionWhenEcoFeedbackIsNotPresent() {
2280         // given:
2281         Status status = mock(Status.class);
2282         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2283
2284         State state = mock(State.class);
2285         when(state.getStatus()).thenReturn(Optional.of(status));
2286         when(state.getEcoFeedback()).thenReturn(Optional.empty());
2287
2288         Device device = mock(Device.class);
2289         when(device.getState()).thenReturn(Optional.of(state));
2290
2291         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2292
2293         // when:
2294         Optional<Quantity> energyConsumption = deviceState.getCurrentEnergyConsumption();
2295
2296         // then:
2297         assertFalse(energyConsumption.isPresent());
2298     }
2299
2300     @Test
2301     public void testGetCurrentEnergyConsumptionWhenCurrentEnergyConsumptionIsNotPresent() {
2302         // given:
2303         Status status = mock(Status.class);
2304         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2305
2306         EcoFeedback ecoFeedback = mock(EcoFeedback.class);
2307         when(ecoFeedback.getCurrentEnergyConsumption()).thenReturn(Optional.empty());
2308
2309         State state = mock(State.class);
2310         when(state.getStatus()).thenReturn(Optional.of(status));
2311         when(state.getEcoFeedback()).thenReturn(Optional.of(ecoFeedback));
2312
2313         Device device = mock(Device.class);
2314         when(device.getState()).thenReturn(Optional.of(state));
2315
2316         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2317
2318         // when:
2319         Optional<Quantity> energyConsumption = deviceState.getCurrentEnergyConsumption();
2320
2321         // then:
2322         assertFalse(energyConsumption.isPresent());
2323     }
2324
2325     @Test
2326     public void testGetCurrentEnergyConsumptionWhenCurrentEnergyConsumptionIsEmpty() {
2327         // given:
2328         Status status = mock(Status.class);
2329         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2330
2331         EnergyConsumption currentEnergyConsumption = mock(EnergyConsumption.class);
2332         when(currentEnergyConsumption.getUnit()).thenReturn(Optional.empty());
2333         when(currentEnergyConsumption.getValue()).thenReturn(Optional.empty());
2334
2335         EcoFeedback ecoFeedback = mock(EcoFeedback.class);
2336         when(ecoFeedback.getCurrentEnergyConsumption()).thenReturn(Optional.of(currentEnergyConsumption));
2337
2338         State state = mock(State.class);
2339         when(state.getStatus()).thenReturn(Optional.of(status));
2340         when(state.getEcoFeedback()).thenReturn(Optional.of(ecoFeedback));
2341
2342         Device device = mock(Device.class);
2343         when(device.getState()).thenReturn(Optional.of(state));
2344
2345         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2346
2347         // when:
2348         Optional<Quantity> energyConsumption = deviceState.getCurrentEnergyConsumption();
2349
2350         // then:
2351         assertFalse(energyConsumption.isPresent());
2352     }
2353
2354     @Test
2355     public void testGetCurrentEnergyConsumptionWhenValueIsNotPresent() {
2356         // given:
2357         Status status = mock(Status.class);
2358         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2359
2360         EnergyConsumption currentEnergyConsumption = mock(EnergyConsumption.class);
2361         when(currentEnergyConsumption.getUnit()).thenReturn(Optional.of("kWh"));
2362         when(currentEnergyConsumption.getValue()).thenReturn(Optional.empty());
2363
2364         EcoFeedback ecoFeedback = mock(EcoFeedback.class);
2365         when(ecoFeedback.getCurrentEnergyConsumption()).thenReturn(Optional.of(currentEnergyConsumption));
2366
2367         State state = mock(State.class);
2368         when(state.getStatus()).thenReturn(Optional.of(status));
2369         when(state.getEcoFeedback()).thenReturn(Optional.of(ecoFeedback));
2370
2371         Device device = mock(Device.class);
2372         when(device.getState()).thenReturn(Optional.of(state));
2373
2374         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2375
2376         // when:
2377         Optional<Quantity> energyConsumption = deviceState.getCurrentEnergyConsumption();
2378
2379         // then:
2380         assertFalse(energyConsumption.isPresent());
2381     }
2382
2383     @Test
2384     public void testGetCurrentEnergyConsumptionWhenUnitIsNotPresent() {
2385         // given:
2386         Status status = mock(Status.class);
2387         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2388
2389         EnergyConsumption currentEnergyConsumption = mock(EnergyConsumption.class);
2390         when(currentEnergyConsumption.getUnit()).thenReturn(Optional.empty());
2391         when(currentEnergyConsumption.getValue()).thenReturn(Optional.of(0.5));
2392
2393         EcoFeedback ecoFeedback = mock(EcoFeedback.class);
2394         when(ecoFeedback.getCurrentEnergyConsumption()).thenReturn(Optional.of(currentEnergyConsumption));
2395
2396         State state = mock(State.class);
2397         when(state.getStatus()).thenReturn(Optional.of(status));
2398         when(state.getEcoFeedback()).thenReturn(Optional.of(ecoFeedback));
2399
2400         Device device = mock(Device.class);
2401         when(device.getState()).thenReturn(Optional.of(state));
2402
2403         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2404
2405         // when:
2406         Quantity energyConsumption = deviceState.getCurrentEnergyConsumption().get();
2407
2408         // then:
2409         assertEquals(0.5, energyConsumption.getValue());
2410         assertFalse(energyConsumption.getUnit().isPresent());
2411     }
2412
2413     @Test
2414     public void testGetCurrentEnergyConsumption() {
2415         // given:
2416         Status status = mock(Status.class);
2417         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2418
2419         EnergyConsumption currentEnergyConsumption = mock(EnergyConsumption.class);
2420         when(currentEnergyConsumption.getUnit()).thenReturn(Optional.of("kWh"));
2421         when(currentEnergyConsumption.getValue()).thenReturn(Optional.of(0.5));
2422
2423         EcoFeedback ecoFeedback = mock(EcoFeedback.class);
2424         when(ecoFeedback.getCurrentEnergyConsumption()).thenReturn(Optional.of(currentEnergyConsumption));
2425
2426         State state = mock(State.class);
2427         when(state.getStatus()).thenReturn(Optional.of(status));
2428         when(state.getEcoFeedback()).thenReturn(Optional.of(ecoFeedback));
2429
2430         Device device = mock(Device.class);
2431         when(device.getState()).thenReturn(Optional.of(state));
2432
2433         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2434
2435         // when:
2436         Quantity energyConsumption = deviceState.getCurrentEnergyConsumption().get();
2437
2438         // then:
2439         assertEquals(0.5, energyConsumption.getValue());
2440         assertEquals(Optional.of("kWh"), energyConsumption.getUnit());
2441     }
2442
2443     @Test
2444     public void testGetBatteryLevel() {
2445         // given:
2446         Status status = mock(Status.class);
2447         when(status.getValueRaw()).thenReturn(Optional.of(StateType.ON.getCode()));
2448
2449         State state = mock(State.class);
2450         when(state.getStatus()).thenReturn(Optional.of(status));
2451         when(state.getBatteryLevel()).thenReturn(Optional.of(4));
2452
2453         Device device = mock(Device.class);
2454         when(device.getState()).thenReturn(Optional.of(state));
2455
2456         DeviceState deviceState = new DeviceState(DEVICE_IDENTIFIER, device);
2457
2458         // when:
2459         Integer batteryLevel = deviceState.getBatteryLevel().get();
2460
2461         // then:
2462         assertEquals(Integer.valueOf(4), batteryLevel);
2463     }
2464 }