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