]> git.basschouten.com Git - openhab-addons.git/blob
75972f93b78dbf54ba63c191e1acbc5ff3e1eda7
[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.livisismarthome.internal.client.entity.device;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.util.Arrays;
18 import java.util.Collections;
19 import java.util.List;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.livisismarthome.internal.client.api.entity.device.DeviceDTO;
24 import org.openhab.binding.livisismarthome.internal.client.api.entity.device.DeviceStateDTO;
25 import org.openhab.binding.livisismarthome.internal.client.api.entity.device.StateDTO;
26 import org.openhab.binding.livisismarthome.internal.client.api.entity.message.MessageDTO;
27 import org.openhab.binding.livisismarthome.internal.client.api.entity.state.BooleanStateDTO;
28
29 /**
30  * @author Sven Strohschein - Initial contribution
31  */
32 @NonNullByDefault
33 public class DeviceDTOTest {
34
35     @Test
36     public void testSetMessageListLowBatteryMessage() {
37         DeviceDTO device = createDevice();
38
39         assertTrue(device.isReachable());
40         assertFalse(device.hasLowBattery());
41
42         device.setMessageList(List.of(createMessage(MessageDTO.TYPE_DEVICE_LOW_BATTERY)));
43
44         assertTrue(device.isReachable());
45         assertTrue(device.hasLowBattery());
46     }
47
48     @Test
49     public void testSetMessageListUnreachableMessage() {
50         DeviceDTO device = createDevice();
51
52         assertTrue(device.isReachable());
53         assertFalse(device.hasLowBattery());
54
55         device.setMessageList(List.of(createMessage(MessageDTO.TYPE_DEVICE_UNREACHABLE)));
56
57         assertFalse(device.isReachable());
58         assertFalse(device.hasLowBattery());
59     }
60
61     @Test
62     public void testSetMessageListResetByEmpty() {
63         DeviceDTO device = createDevice();
64
65         assertTrue(device.getMessageList().isEmpty());
66         assertTrue(device.isReachable());
67         assertFalse(device.hasLowBattery());
68
69         List<MessageDTO> messages = Arrays.asList(createMessage(MessageDTO.TYPE_DEVICE_LOW_BATTERY),
70                 createMessage(MessageDTO.TYPE_DEVICE_UNREACHABLE));
71         device.setMessageList(messages);
72
73         assertEquals(messages, device.getMessageList());
74         assertFalse(device.isReachable());
75         assertTrue(device.hasLowBattery());
76
77         device.setMessageList(Collections.emptyList());
78
79         // Nothing should get changed.
80         // New messages are only set in real-life when the device is refreshed with new data of the API.
81         // Therefore the data of the API should be kept / not overwritten when no corresponding messages are available.
82         assertEquals(Collections.emptyList(), device.getMessageList());
83         assertFalse(device.isReachable());
84         assertTrue(device.hasLowBattery());
85     }
86
87     @Test
88     public void testSetMessageListResetByNULL() {
89         DeviceDTO device = createDevice();
90
91         assertTrue(device.getMessageList().isEmpty());
92         assertTrue(device.isReachable());
93         assertFalse(device.hasLowBattery());
94
95         List<MessageDTO> messages = Arrays.asList(createMessage(MessageDTO.TYPE_DEVICE_LOW_BATTERY),
96                 createMessage(MessageDTO.TYPE_DEVICE_UNREACHABLE));
97         device.setMessageList(messages);
98
99         assertEquals(messages, device.getMessageList());
100         assertFalse(device.isReachable());
101         assertTrue(device.hasLowBattery());
102
103         device.setMessageList(null);
104
105         // Nothing should get changed.
106         // New messages are only set in real-life when the device is refreshed with new data of the API.
107         // Therefore the data of the API should be kept / not overwritten when no corresponding messages are available.
108         assertTrue(device.getMessageList().isEmpty());
109         assertFalse(device.isReachable());
110         assertTrue(device.hasLowBattery());
111     }
112
113     @Test
114     public void testSetMessageListResetByUnimportantMessage() {
115         DeviceDTO device = createDevice();
116
117         assertTrue(device.getMessageList().isEmpty());
118         assertTrue(device.isReachable());
119         assertFalse(device.hasLowBattery());
120
121         List<MessageDTO> messages = Arrays.asList(createMessage(MessageDTO.TYPE_DEVICE_LOW_BATTERY),
122                 createMessage(MessageDTO.TYPE_DEVICE_UNREACHABLE));
123         device.setMessageList(messages);
124
125         assertEquals(messages, device.getMessageList());
126         assertFalse(device.isReachable());
127         assertTrue(device.hasLowBattery());
128
129         messages = List.of(createMessage("UNKNOWN"));
130         device.setMessageList(messages);
131
132         // Nothing should get changed.
133         // New messages are only set in real-life when the device is refreshed with new data of the API.
134         // Therefore the data of the API should be kept / not overwritten when no corresponding messages are available.
135         assertEquals(messages, device.getMessageList());
136         assertFalse(device.isReachable());
137         assertTrue(device.hasLowBattery());
138     }
139
140     @Test
141     public void testSetMessageListUnimportantMessage() {
142         DeviceDTO device = createDevice();
143
144         assertTrue(device.isReachable());
145         assertFalse(device.hasLowBattery());
146
147         device.setMessageList(List.of(createMessage("UNKNOWN")));
148
149         assertTrue(device.isReachable());
150         assertFalse(device.hasLowBattery());
151     }
152
153     private MessageDTO createMessage(String messageType) {
154         MessageDTO message = new MessageDTO();
155         message.setType(messageType);
156         return message;
157     }
158
159     @Test
160     public void testSetMessageListNULL() {
161         DeviceDTO device = createDevice();
162
163         assertTrue(device.isReachable());
164         assertFalse(device.hasLowBattery());
165
166         device.setMessageList(null);
167
168         assertTrue(device.isReachable());
169         assertFalse(device.hasLowBattery());
170     }
171
172     @Test
173     public void testSetMessageListEmpty() {
174         DeviceDTO device = createDevice();
175
176         assertTrue(device.isReachable());
177         assertFalse(device.hasLowBattery());
178
179         device.setMessageList(Collections.emptyList());
180
181         assertTrue(device.isReachable());
182         assertFalse(device.hasLowBattery());
183     }
184
185     private static DeviceDTO createDevice() {
186         BooleanStateDTO isReachableState = new BooleanStateDTO();
187         isReachableState.setValue(true);
188
189         StateDTO state = new StateDTO();
190         state.setIsReachable(isReachableState);
191
192         DeviceStateDTO deviceState = new DeviceStateDTO();
193         deviceState.setState(state);
194
195         DeviceDTO device = new DeviceDTO();
196         device.setDeviceState(deviceState);
197         return device;
198     }
199 }