]> git.basschouten.com Git - openhab-addons.git/blob
b9574b72e53e54d7fe862e24ec3a11042636a58f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.salus.internal.rest;
14
15 import static org.assertj.core.api.Assertions.assertThat;
16
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.junit.jupiter.api.DisplayName;
23 import org.junit.jupiter.api.Test;
24
25 /**
26  * @author Martin GrzeĊ›lowski - Initial contribution
27  */
28 @SuppressWarnings("EqualsWithItself")
29 @NonNullByDefault
30 class DeviceTest {
31
32     // Returns true if 'connection_status' property exists and is set to 'online'
33     @Test
34     @DisplayName("Returns true if 'connection_status' property exists and is set to 'online'")
35     public void testReturnsTrueIfConnectionStatusPropertyExistsAndIsSetToOnline() {
36         // Given
37         var properties = new HashMap<String, @Nullable Object>();
38         properties.put("connection_status", "online");
39         var device = new Device("dsn", "name", properties);
40
41         // When
42         var result = device.isConnected();
43
44         // Then
45         assertThat(result).isTrue();
46     }
47
48     // Returns false if 'connection_status' property exists and is not set to 'online'
49     @Test
50     @DisplayName("Returns false if 'connection_status' property exists and is not set to 'online'")
51     public void testReturnsFalseIfConnectionStatusPropertyExistsAndIsNotSetToOnline() {
52         // Given
53         var properties = new HashMap<String, @Nullable Object>();
54         properties.put("connection_status", "offline");
55         var device = new Device("dsn", "name", properties);
56
57         // When
58         var result = device.isConnected();
59
60         // Then
61         assertThat(result).isFalse();
62     }
63
64     // Returns false if 'connection_status' property does not exist
65     @Test
66     @DisplayName("Returns false if 'connection_status' property does not exist")
67     public void testReturnsFalseIfConnectionStatusPropertyDoesNotExist() {
68         // Given
69         var properties = new HashMap<String, @Nullable Object>();
70         var device = new Device("dsn", "name", properties);
71
72         // When
73         var result = device.isConnected();
74
75         // Then
76         assertThat(result).isFalse();
77     }
78
79     // Returns false if 'properties' parameter does not contain 'connection_status' key
80     @Test
81     @DisplayName("Returns false if 'properties' parameter does not contain 'connection_status' key")
82     public void testReturnsFalseIfPropertiesParameterDoesNotContainConnectionStatusKey() {
83         // Given
84         var properties = new HashMap<String, @Nullable Object>();
85         var device = new Device("dsn", "name", properties);
86
87         // When
88         var result = device.isConnected();
89
90         // Then
91         assertThat(result).isFalse();
92     }
93
94     // Returns false if 'connection_status' property is null
95     @Test
96     @DisplayName("Returns false if 'connection_status' property is null")
97     public void testReturnsFalseIfConnectionStatusPropertyIsNull() {
98         // Given
99         var properties = new HashMap<String, @Nullable Object>();
100         properties.put("connection_status", null);
101         var device = new Device("dsn", "name", properties);
102
103         // When
104         var result = device.isConnected();
105
106         // Then
107         assertThat(result).isFalse();
108     }
109
110     // Returns false if 'connection_status' property is not a string
111     @Test
112     @DisplayName("Returns false if 'connection_status' property is not a string")
113     public void testReturnsFalseIfConnectionStatusPropertyIsNotAString() {
114         // Given
115         var properties = new HashMap<String, @Nullable Object>();
116         properties.put("connection_status", 123);
117         var device = new Device("dsn", "name", properties);
118
119         // When
120         var result = device.isConnected();
121
122         // Then
123         assertThat(result).isFalse();
124     }
125
126     // Creating a new Device object with valid parameters should succeed.
127     @Test
128     @DisplayName("Creating a new Device object with valid parameters should succeed")
129     public void testCreatingNewDeviceWithValidParametersShouldSucceed() {
130         // Given
131         String dsn = "123456";
132         String name = "Device 1";
133         Map<String, @Nullable Object> properties = Map.of("connection_status", "online");
134
135         // When
136         Device device = new Device(dsn, name, properties);
137
138         // Then
139         assertThat(device).isNotNull();
140         assertThat(device.dsn()).isEqualTo(dsn);
141         assertThat(device.name()).isEqualTo(name);
142         assertThat(device.properties()).isEqualTo(properties);
143     }
144
145     // Two Device objects with the same DSN should be considered equal.
146     @Test
147     @DisplayName("Two Device objects with the same DSN should be considered equal")
148     public void testTwoDevicesWithSameDsnShouldBeEqual() {
149         // Given
150         String dsn = "123456";
151         String name1 = "Device 1";
152         String name2 = "Device 2";
153         Map<String, @Nullable Object> properties = Map.of("connection_status", "online");
154
155         Device device1 = new Device(dsn, name1, properties);
156         Device device2 = new Device(dsn, name2, properties);
157
158         // When
159         boolean isEqual = device1.equals(device2);
160
161         // Then
162         assertThat(isEqual).isTrue();
163     }
164
165     // The compareTo method should correctly compare two Device objects based on their DSNs.
166     @Test
167     @DisplayName("The compareTo method should correctly compare two Device objects based on their DSNs")
168     public void testCompareToMethodShouldCorrectlyCompareDevicesBasedOnDsn() {
169         // Given
170         String dsn1 = "123456";
171         String dsn2 = "654321";
172         String name = "Device";
173         Map<String, @Nullable Object> properties = Map.of("connection_status", "online");
174
175         Device device1 = new Device(dsn1, name, properties);
176         Device device2 = new Device(dsn2, name, properties);
177
178         // When
179         int result1 = device1.compareTo(device2);
180         int result2 = device2.compareTo(device1);
181         int result3 = device1.compareTo(device1);
182
183         // Then
184         assertThat(result1).isNegative();
185         assertThat(result2).isPositive();
186         assertThat(result3).isZero();
187     }
188
189     // The isConnected method should return true if the connection_status property is "online".
190     @Test
191     @DisplayName("The isConnected method should return true if the connection_status property is \"online\"")
192     public void testIsConnectedMethodShouldReturnTrueIfConnectionStatusIsOnline() {
193         // Given
194         String dsn = "123456";
195         String name = "Device";
196         Map<String, @Nullable Object> properties1 = Map.of("connection_status", "online");
197         Map<String, @Nullable Object> properties2 = Map.of("connection_status", "offline");
198
199         Device device1 = new Device(dsn, name, properties1);
200         Device device2 = new Device(dsn, name, properties2);
201
202         // When
203         boolean isConnected1 = device1.isConnected();
204         boolean isConnected2 = device2.isConnected();
205
206         // Then
207         assertThat(isConnected1).isTrue();
208         assertThat(isConnected2).isFalse();
209     }
210
211     // The toString method should return a string representation of the Device object with its DSN and name.
212     @Test
213     @DisplayName("The toString method should return a string representation of the Device object with its DSN and name")
214     public void testToStringMethodShouldReturnStringRepresentationWithDsnAndName() {
215         // Given
216         String dsn = "123456";
217         String name = "Device";
218         Map<String, @Nullable Object> properties = Map.of("connection_status", "online");
219
220         Device device = new Device(dsn, name, properties);
221
222         // When
223         String result = device.toString();
224
225         // Then
226         assertThat(result).isEqualTo("Device{dsn='123456', name='Device'}");
227     }
228 }