2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.innogysmarthome.internal.client.entity.device;
15 import static org.junit.jupiter.api.Assertions.*;
17 import java.util.Arrays;
18 import java.util.Collections;
19 import java.util.List;
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;
26 * @author Sven Strohschein - Initial contribution
28 public class DeviceTest {
31 public void testSetMessageListLowBatteryMessage() {
32 Device device = createDevice();
34 assertTrue(device.isReachable());
35 assertFalse(device.hasLowBattery());
37 device.setMessageList(Collections.singletonList(createMessage(Message.TYPE_DEVICE_LOW_BATTERY)));
39 assertTrue(device.isReachable());
40 assertTrue(device.hasLowBattery());
44 public void testSetMessageListUnreachableMessage() {
45 Device device = createDevice();
47 assertTrue(device.isReachable());
48 assertFalse(device.hasLowBattery());
50 device.setMessageList(Collections.singletonList(createMessage(Message.TYPE_DEVICE_UNREACHABLE)));
52 assertFalse(device.isReachable());
53 assertFalse(device.hasLowBattery());
57 public void testSetMessageListResetByEmpty() {
58 Device device = createDevice();
60 assertNull(device.getMessageList());
61 assertTrue(device.isReachable());
62 assertFalse(device.hasLowBattery());
64 List<Message> messages = Arrays.asList(createMessage(Message.TYPE_DEVICE_LOW_BATTERY),
65 createMessage(Message.TYPE_DEVICE_UNREACHABLE));
66 device.setMessageList(messages);
68 assertEquals(messages, device.getMessageList());
69 assertFalse(device.isReachable());
70 assertTrue(device.hasLowBattery());
72 device.setMessageList(Collections.emptyList());
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());
83 public void testSetMessageListResetByNULL() {
84 Device device = createDevice();
86 assertNull(device.getMessageList());
87 assertTrue(device.isReachable());
88 assertFalse(device.hasLowBattery());
90 List<Message> messages = Arrays.asList(createMessage(Message.TYPE_DEVICE_LOW_BATTERY),
91 createMessage(Message.TYPE_DEVICE_UNREACHABLE));
92 device.setMessageList(messages);
94 assertEquals(messages, device.getMessageList());
95 assertFalse(device.isReachable());
96 assertTrue(device.hasLowBattery());
98 device.setMessageList(null);
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());
109 public void testSetMessageListResetByUnimportantMessage() {
110 Device device = createDevice();
112 assertNull(device.getMessageList());
113 assertTrue(device.isReachable());
114 assertFalse(device.hasLowBattery());
116 List<Message> messages = Arrays.asList(createMessage(Message.TYPE_DEVICE_LOW_BATTERY),
117 createMessage(Message.TYPE_DEVICE_UNREACHABLE));
118 device.setMessageList(messages);
120 assertEquals(messages, device.getMessageList());
121 assertFalse(device.isReachable());
122 assertTrue(device.hasLowBattery());
124 messages = Collections.singletonList(createMessage("UNKNOWN"));
125 device.setMessageList(messages);
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());
136 public void testSetMessageListUnimportantMessage() {
137 Device device = createDevice();
139 assertTrue(device.isReachable());
140 assertFalse(device.hasLowBattery());
142 device.setMessageList(Collections.singletonList(createMessage("UNKNOWN")));
144 assertTrue(device.isReachable());
145 assertFalse(device.hasLowBattery());
148 private Message createMessage(String messageType) {
149 Message message = new Message();
150 message.setType(messageType);
155 public void testSetMessageListNULL() {
156 Device device = createDevice();
158 assertTrue(device.isReachable());
159 assertFalse(device.hasLowBattery());
161 device.setMessageList(null);
163 assertTrue(device.isReachable());
164 assertFalse(device.hasLowBattery());
168 public void testSetMessageListEmpty() {
169 Device device = createDevice();
171 assertTrue(device.isReachable());
172 assertFalse(device.hasLowBattery());
174 device.setMessageList(Collections.emptyList());
176 assertTrue(device.isReachable());
177 assertFalse(device.hasLowBattery());
180 private static Device createDevice() {
181 BooleanState isReachableState = new BooleanState();
182 isReachableState.setValue(true);
184 State state = new State();
185 state.setIsReachable(isReachableState);
187 DeviceState deviceState = new DeviceState();
188 deviceState.setState(state);
190 Device device = new Device();
191 device.setDeviceState(deviceState);