2 * Copyright (c) 2010-2024 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.salus.internal.rest;
15 import static org.assertj.core.api.Assertions.assertThat;
17 import java.util.HashMap;
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;
26 * @author Martin GrzeĊlowski - Initial contribution
28 @SuppressWarnings("EqualsWithItself")
32 // Returns true if 'connection_status' property exists and is set to 'online'
34 @DisplayName("Returns true if 'connection_status' property exists and is set to 'online'")
35 public void testReturnsTrueIfConnectionStatusPropertyExistsAndIsSetToOnline() {
37 var properties = new HashMap<String, @Nullable Object>();
38 properties.put("connection_status", "online");
39 var device = new Device("dsn", "name", properties);
42 var result = device.isConnected();
45 assertThat(result).isTrue();
48 // Returns false if 'connection_status' property exists and is not set to 'online'
50 @DisplayName("Returns false if 'connection_status' property exists and is not set to 'online'")
51 public void testReturnsFalseIfConnectionStatusPropertyExistsAndIsNotSetToOnline() {
53 var properties = new HashMap<String, @Nullable Object>();
54 properties.put("connection_status", "offline");
55 var device = new Device("dsn", "name", properties);
58 var result = device.isConnected();
61 assertThat(result).isFalse();
64 // Returns false if 'connection_status' property does not exist
66 @DisplayName("Returns false if 'connection_status' property does not exist")
67 public void testReturnsFalseIfConnectionStatusPropertyDoesNotExist() {
69 var properties = new HashMap<String, @Nullable Object>();
70 var device = new Device("dsn", "name", properties);
73 var result = device.isConnected();
76 assertThat(result).isFalse();
79 // Returns false if 'properties' parameter does not contain 'connection_status' key
81 @DisplayName("Returns false if 'properties' parameter does not contain 'connection_status' key")
82 public void testReturnsFalseIfPropertiesParameterDoesNotContainConnectionStatusKey() {
84 var properties = new HashMap<String, @Nullable Object>();
85 var device = new Device("dsn", "name", properties);
88 var result = device.isConnected();
91 assertThat(result).isFalse();
94 // Returns false if 'connection_status' property is null
96 @DisplayName("Returns false if 'connection_status' property is null")
97 public void testReturnsFalseIfConnectionStatusPropertyIsNull() {
99 var properties = new HashMap<String, @Nullable Object>();
100 properties.put("connection_status", null);
101 var device = new Device("dsn", "name", properties);
104 var result = device.isConnected();
107 assertThat(result).isFalse();
110 // Returns false if 'connection_status' property is not a string
112 @DisplayName("Returns false if 'connection_status' property is not a string")
113 public void testReturnsFalseIfConnectionStatusPropertyIsNotAString() {
115 var properties = new HashMap<String, @Nullable Object>();
116 properties.put("connection_status", 123);
117 var device = new Device("dsn", "name", properties);
120 var result = device.isConnected();
123 assertThat(result).isFalse();
126 // Creating a new Device object with valid parameters should succeed.
128 @DisplayName("Creating a new Device object with valid parameters should succeed")
129 public void testCreatingNewDeviceWithValidParametersShouldSucceed() {
131 String dsn = "123456";
132 String name = "Device 1";
133 Map<String, @Nullable Object> properties = Map.of("connection_status", "online");
136 Device device = new Device(dsn, name, properties);
139 assertThat(device).isNotNull();
140 assertThat(device.dsn()).isEqualTo(dsn);
141 assertThat(device.name()).isEqualTo(name);
142 assertThat(device.properties()).isEqualTo(properties);
145 // Two Device objects with the same DSN should be considered equal.
147 @DisplayName("Two Device objects with the same DSN should be considered equal")
148 public void testTwoDevicesWithSameDsnShouldBeEqual() {
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");
155 Device device1 = new Device(dsn, name1, properties);
156 Device device2 = new Device(dsn, name2, properties);
159 boolean isEqual = device1.equals(device2);
162 assertThat(isEqual).isTrue();
165 // The compareTo method should correctly compare two Device objects based on their DSNs.
167 @DisplayName("The compareTo method should correctly compare two Device objects based on their DSNs")
168 public void testCompareToMethodShouldCorrectlyCompareDevicesBasedOnDsn() {
170 String dsn1 = "123456";
171 String dsn2 = "654321";
172 String name = "Device";
173 Map<String, @Nullable Object> properties = Map.of("connection_status", "online");
175 Device device1 = new Device(dsn1, name, properties);
176 Device device2 = new Device(dsn2, name, properties);
179 int result1 = device1.compareTo(device2);
180 int result2 = device2.compareTo(device1);
181 int result3 = device1.compareTo(device1);
184 assertThat(result1).isNegative();
185 assertThat(result2).isPositive();
186 assertThat(result3).isZero();
189 // The isConnected method should return true if the connection_status property is "online".
191 @DisplayName("The isConnected method should return true if the connection_status property is \"online\"")
192 public void testIsConnectedMethodShouldReturnTrueIfConnectionStatusIsOnline() {
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");
199 Device device1 = new Device(dsn, name, properties1);
200 Device device2 = new Device(dsn, name, properties2);
203 boolean isConnected1 = device1.isConnected();
204 boolean isConnected2 = device2.isConnected();
207 assertThat(isConnected1).isTrue();
208 assertThat(isConnected2).isFalse();
211 // The toString method should return a string representation of the Device object with its DSN and name.
213 @DisplayName("The toString method should return a string representation of the Device object with its DSN and name")
214 public void testToStringMethodShouldReturnStringRepresentationWithDsnAndName() {
216 String dsn = "123456";
217 String name = "Device";
218 Map<String, @Nullable Object> properties = Map.of("connection_status", "online");
220 Device device = new Device(dsn, name, properties);
223 String result = device.toString();
226 assertThat(result).isEqualTo("Device{dsn='123456', name='Device'}");