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