]> git.basschouten.com Git - openhab-addons.git/blob
007ddd08cc5b31a394a5a262c5d33bc2f680036c
[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.handler;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.ArgumentMatchers.anyInt;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants.Channels.*;
19 import static org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants.HOB_DEVICE_THING_UID;
20
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.MieleCloudBindingConstants;
26 import org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants;
27 import org.openhab.binding.mielecloud.internal.webservice.api.DeviceState;
28 import org.openhab.binding.mielecloud.internal.webservice.api.json.StateType;
29 import org.openhab.core.library.types.DecimalType;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.library.types.StringType;
32
33 /**
34  * @author Björn Lange - Initial contribution
35  * @author Benjamin Bolte - Add plate step
36  * @author Benjamin Bolte - Add info state channel and map signal flags from API tests
37  */
38 @NonNullByDefault
39 public class HobDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
40     @Override
41     protected AbstractMieleThingHandler setUpThingHandler() {
42         return createThingHandler(MieleCloudBindingConstants.THING_TYPE_HOB, HOB_DEVICE_THING_UID,
43                 HobDeviceThingHandler.class, MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER, "0");
44     }
45
46     @Test
47     public void testChannelUpdatesForNullValues() throws Exception {
48         // given:
49         setUpBridgeAndThing();
50
51         DeviceState deviceState = mock(DeviceState.class);
52         when(deviceState.getDeviceIdentifier()).thenReturn(HOB_DEVICE_THING_UID.getId());
53         when(deviceState.getStateType()).thenReturn(Optional.empty());
54         when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.empty());
55         when(deviceState.getStatus()).thenReturn(Optional.empty());
56         when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
57         when(deviceState.getPlateStep(anyInt())).thenReturn(Optional.empty());
58         when(deviceState.getPlateStepRaw(anyInt())).thenReturn(Optional.empty());
59
60         // when:
61         getBridgeHandler().onDeviceStateUpdated(deviceState);
62
63         // then:
64         waitForAssert(() -> {
65             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
66             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
67             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_1_POWER_STEP));
68             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_1_POWER_STEP_RAW));
69             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_2_POWER_STEP));
70             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_2_POWER_STEP_RAW));
71             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_3_POWER_STEP));
72             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_3_POWER_STEP_RAW));
73             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_4_POWER_STEP));
74             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_4_POWER_STEP_RAW));
75             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_5_POWER_STEP));
76             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_5_POWER_STEP_RAW));
77             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_6_POWER_STEP));
78             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_6_POWER_STEP_RAW));
79         });
80     }
81
82     @Test
83     public void testChannelUpdatesForValidValues() throws Exception {
84         // given:
85         setUpBridgeAndThing();
86
87         DeviceState deviceState = mock(DeviceState.class);
88         when(deviceState.getDeviceIdentifier()).thenReturn(HOB_DEVICE_THING_UID.getId());
89         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
90         when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.of(false));
91         when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
92         when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
93         when(deviceState.hasError()).thenReturn(false);
94         when(deviceState.hasInfo()).thenReturn(true);
95         when(deviceState.getPlateStep(0)).thenReturn(Optional.of("1."));
96         when(deviceState.getPlateStepRaw(0)).thenReturn(Optional.of(2));
97         when(deviceState.getPlateStep(1)).thenReturn(Optional.empty());
98         when(deviceState.getPlateStep(2)).thenReturn(Optional.empty());
99         when(deviceState.getPlateStep(3)).thenReturn(Optional.empty());
100         when(deviceState.getPlateStep(4)).thenReturn(Optional.empty());
101         when(deviceState.getPlateStep(5)).thenReturn(Optional.empty());
102
103         // when:
104         getBridgeHandler().onDeviceStateUpdated(deviceState);
105
106         // then:
107         waitForAssert(() -> {
108             assertEquals(new StringType("Running"), getChannelState(OPERATION_STATE));
109             assertEquals(new DecimalType(StateType.RUNNING.getCode()), getChannelState(OPERATION_STATE_RAW));
110             assertEquals(OnOffType.OFF, getChannelState(ERROR_STATE));
111             assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
112             assertEquals(new StringType("1."), getChannelState(PLATE_1_POWER_STEP));
113             assertEquals(new DecimalType(2), getChannelState(PLATE_1_POWER_STEP_RAW));
114             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_2_POWER_STEP));
115             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_2_POWER_STEP_RAW));
116             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_3_POWER_STEP));
117             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_3_POWER_STEP_RAW));
118             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_4_POWER_STEP));
119             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_4_POWER_STEP_RAW));
120             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_5_POWER_STEP));
121             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_5_POWER_STEP_RAW));
122             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_6_POWER_STEP));
123             assertEquals(NULL_VALUE_STATE, getChannelState(PLATE_6_POWER_STEP_RAW));
124         });
125     }
126 }