2 * Copyright (c) 2010-2023 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.danfossairunit.internal;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
17 import static org.openhab.binding.danfossairunit.internal.Commands.*;
19 import java.io.IOException;
20 import java.time.ZoneId;
21 import java.time.ZonedDateTime;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.openhab.core.library.types.DateTimeType;
29 import org.openhab.core.library.types.DecimalType;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.library.types.PercentType;
32 import org.openhab.core.library.types.QuantityType;
33 import org.openhab.core.test.java.JavaTest;
36 * This class provides test cases for {@link DanfossAirUnit}
38 * @author Jacob Laursen - Initial contribution
41 @ExtendWith(MockitoExtension.class)
42 public class DanfossAirUnitTest extends JavaTest {
44 private @NonNullByDefault({}) @Mock CommunicationController communicationController;
47 public void getUnitNameIsReturned() throws IOException {
48 byte[] response = new byte[] { (byte) 0x05, (byte) 'w', (byte) '2', (byte) '/', (byte) 'a', (byte) '2' };
49 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, UNIT_NAME)).thenReturn(response);
50 var airUnit = new DanfossAirUnit(communicationController);
51 assertEquals("w2/a2", airUnit.getUnitName());
55 public void getHumidityWhenNearestNeighborIsBelowRoundsDown() throws IOException {
56 byte[] response = new byte[] { (byte) 0x64 };
57 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, HUMIDITY)).thenReturn(response);
58 var airUnit = new DanfossAirUnit(communicationController);
59 assertEquals(new QuantityType<>("39.2 %"), airUnit.getHumidity());
63 public void getHumidityWhenNearestNeighborIsAboveRoundsUp() throws IOException {
64 byte[] response = new byte[] { (byte) 0x67 };
65 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, HUMIDITY)).thenReturn(response);
66 var airUnit = new DanfossAirUnit(communicationController);
67 assertEquals(new QuantityType<>("40.4 %"), airUnit.getHumidity());
71 public void getSupplyTemperatureWhenNearestNeighborIsBelowRoundsDown()
72 throws IOException, UnexpectedResponseValueException {
73 byte[] response = new byte[] { (byte) 0x09, (byte) 0xf0 }; // 0x09f0 = 2544 => 25.44
74 when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response);
75 var airUnit = new DanfossAirUnit(communicationController);
76 assertEquals(new QuantityType<>("25.4 °C"), airUnit.getSupplyTemperature());
80 public void getSupplyTemperatureWhenBothNeighborsAreEquidistantRoundsUp()
81 throws IOException, UnexpectedResponseValueException {
82 byte[] response = new byte[] { (byte) 0x09, (byte) 0xf1 }; // 0x09f1 = 2545 => 25.45
83 when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response);
84 var airUnit = new DanfossAirUnit(communicationController);
85 assertEquals(new QuantityType<>("25.5 °C"), airUnit.getSupplyTemperature());
89 public void getSupplyTemperatureWhenBelowValidRangeThrows() throws IOException {
90 byte[] response = new byte[] { (byte) 0x94, (byte) 0xf8 }; // 0x94f8 = -27400 => -274
91 when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response);
92 var airUnit = new DanfossAirUnit(communicationController);
93 assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getSupplyTemperature());
97 public void getSupplyTemperatureWhenAboveValidRangeThrows() throws IOException {
98 byte[] response = new byte[] { (byte) 0x27, (byte) 0x11 }; // 0x2711 = 10001 => 100,01
99 when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response);
100 var airUnit = new DanfossAirUnit(communicationController);
101 assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getSupplyTemperature());
105 public void getCurrentTimeWhenWellFormattedIsParsed() throws IOException, UnexpectedResponseValueException {
106 byte[] response = new byte[] { (byte) 0x03, (byte) 0x02, (byte) 0x0f, (byte) 0x1d, (byte) 0x08, (byte) 0x15 }; // 29.08.21
108 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, CURRENT_TIME)).thenReturn(response);
109 var airUnit = new DanfossAirUnit(communicationController);
110 assertEquals(new DateTimeType(ZonedDateTime.of(2021, 8, 29, 15, 2, 3, 0, ZoneId.systemDefault())),
111 airUnit.getCurrentTime());
115 public void getCurrentTimeWhenInvalidDateThrows() throws IOException {
116 byte[] response = new byte[] { (byte) 0x03, (byte) 0x02, (byte) 0x0f, (byte) 0x20, (byte) 0x08, (byte) 0x15 }; // 32.08.21
118 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, CURRENT_TIME)).thenReturn(response);
119 var airUnit = new DanfossAirUnit(communicationController);
120 assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getCurrentTime());
124 public void getBoostWhenZeroIsOff() throws IOException {
125 byte[] response = new byte[] { (byte) 0x00 };
126 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, BOOST)).thenReturn(response);
127 var airUnit = new DanfossAirUnit(communicationController);
128 assertEquals(OnOffType.OFF, airUnit.getBoost());
132 public void getBoostWhenNonZeroIsOn() throws IOException {
133 byte[] response = new byte[] { (byte) 0x66 };
134 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, BOOST)).thenReturn(response);
135 var airUnit = new DanfossAirUnit(communicationController);
136 assertEquals(OnOffType.ON, airUnit.getBoost());
140 public void getManualFanStepWhenWithinValidRangeIsConvertedIntoPercent()
141 throws IOException, UnexpectedResponseValueException {
142 byte[] response = new byte[] { (byte) 0x05 };
143 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, MANUAL_FAN_SPEED_STEP))
144 .thenReturn(response);
145 var airUnit = new DanfossAirUnit(communicationController);
146 assertEquals(new PercentType(50), airUnit.getManualFanStep());
150 public void getManualFanStepWhenOutOfRangeThrows() throws IOException {
151 byte[] response = new byte[] { (byte) 0x0b };
152 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, MANUAL_FAN_SPEED_STEP))
153 .thenReturn(response);
154 var airUnit = new DanfossAirUnit(communicationController);
155 assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getManualFanStep());
159 public void getFilterLifeWhenNearestNeighborIsBelowRoundsDown() throws IOException {
160 byte[] response = new byte[] { (byte) 0xf0 };
161 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, FILTER_LIFE)).thenReturn(response);
162 var airUnit = new DanfossAirUnit(communicationController);
163 assertEquals(new DecimalType("94.1"), airUnit.getFilterLife());