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.library.unit.Units;
34 import org.openhab.core.test.java.JavaTest;
37 * This class provides test cases for {@link DanfossAirUnit}
39 * @author Jacob Laursen - Initial contribution
42 @ExtendWith(MockitoExtension.class)
43 public class DanfossAirUnitTest extends JavaTest {
45 private @NonNullByDefault({}) @Mock CommunicationController communicationController;
48 public void getUnitNameIsReturned() throws IOException {
49 byte[] response = new byte[] { (byte) 0x05, (byte) 'w', (byte) '2', (byte) '/', (byte) 'a', (byte) '2' };
50 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, UNIT_NAME)).thenReturn(response);
51 var airUnit = new DanfossAirUnit(communicationController);
52 assertEquals("w2/a2", airUnit.getUnitName());
56 public void getHumidityWhenNearestNeighborIsBelowRoundsDown() throws IOException {
57 byte[] response = new byte[] { (byte) 0x64 };
58 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, HUMIDITY)).thenReturn(response);
59 var airUnit = new DanfossAirUnit(communicationController);
60 assertEquals(new QuantityType<>("39.2 %"), airUnit.getHumidity());
64 public void getHumidityWhenNearestNeighborIsAboveRoundsUp() throws IOException {
65 byte[] response = new byte[] { (byte) 0x67 };
66 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, HUMIDITY)).thenReturn(response);
67 var airUnit = new DanfossAirUnit(communicationController);
68 assertEquals(new QuantityType<>("40.4 %"), airUnit.getHumidity());
72 public void getSupplyTemperatureWhenNearestNeighborIsBelowRoundsDown()
73 throws IOException, UnexpectedResponseValueException {
74 byte[] response = new byte[] { (byte) 0x09, (byte) 0xf0 }; // 0x09f0 = 2544 => 25.44
75 when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response);
76 var airUnit = new DanfossAirUnit(communicationController);
77 assertEquals(new QuantityType<>("25.4 °C"), airUnit.getSupplyTemperature());
81 public void getSupplyTemperatureWhenBothNeighborsAreEquidistantRoundsUp()
82 throws IOException, UnexpectedResponseValueException {
83 byte[] response = new byte[] { (byte) 0x09, (byte) 0xf1 }; // 0x09f1 = 2545 => 25.45
84 when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response);
85 var airUnit = new DanfossAirUnit(communicationController);
86 assertEquals(new QuantityType<>("25.5 °C"), airUnit.getSupplyTemperature());
90 public void getSupplyTemperatureWhenBelowValidRangeThrows() throws IOException {
91 byte[] response = new byte[] { (byte) 0x94, (byte) 0xf8 }; // 0x94f8 = -27400 => -274
92 when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response);
93 var airUnit = new DanfossAirUnit(communicationController);
94 assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getSupplyTemperature());
98 public void getSupplyTemperatureWhenAboveValidRangeThrows() throws IOException {
99 byte[] response = new byte[] { (byte) 0x27, (byte) 0x11 }; // 0x2711 = 10001 => 100,01
100 when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response);
101 var airUnit = new DanfossAirUnit(communicationController);
102 assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getSupplyTemperature());
106 public void getCurrentTimeWhenWellFormattedIsParsed() throws IOException, UnexpectedResponseValueException {
107 byte[] response = new byte[] { (byte) 0x03, (byte) 0x02, (byte) 0x0f, (byte) 0x1d, (byte) 0x08, (byte) 0x15 }; // 29.08.21
109 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, CURRENT_TIME)).thenReturn(response);
110 var airUnit = new DanfossAirUnit(communicationController);
111 assertEquals(new DateTimeType(ZonedDateTime.of(2021, 8, 29, 15, 2, 3, 0, ZoneId.systemDefault())),
112 airUnit.getCurrentTime());
116 public void getCurrentTimeWhenInvalidDateThrows() throws IOException {
117 byte[] response = new byte[] { (byte) 0x03, (byte) 0x02, (byte) 0x0f, (byte) 0x20, (byte) 0x08, (byte) 0x15 }; // 32.08.21
119 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, CURRENT_TIME)).thenReturn(response);
120 var airUnit = new DanfossAirUnit(communicationController);
121 assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getCurrentTime());
125 public void getBoostWhenZeroIsOff() throws IOException {
126 byte[] response = new byte[] { (byte) 0x00 };
127 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, BOOST)).thenReturn(response);
128 var airUnit = new DanfossAirUnit(communicationController);
129 assertEquals(OnOffType.OFF, airUnit.getBoost());
133 public void getBoostWhenNonZeroIsOn() throws IOException {
134 byte[] response = new byte[] { (byte) 0x66 };
135 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, BOOST)).thenReturn(response);
136 var airUnit = new DanfossAirUnit(communicationController);
137 assertEquals(OnOffType.ON, airUnit.getBoost());
141 public void getManualFanStepWhenWithinValidRangeIsConvertedIntoPercent()
142 throws IOException, UnexpectedResponseValueException {
143 byte[] response = new byte[] { (byte) 0x05 };
144 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, MANUAL_FAN_SPEED_STEP))
145 .thenReturn(response);
146 var airUnit = new DanfossAirUnit(communicationController);
147 assertEquals(new PercentType(50), airUnit.getManualFanStep());
151 public void getSupplyFanSpeedIsReturnedAsRPM() throws IOException {
152 byte[] response = new byte[] { (byte) 0x04, (byte) 0xda };
153 when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_FAN_SPEED)).thenReturn(response);
154 var airUnit = new DanfossAirUnit(communicationController);
155 assertEquals(new QuantityType<>(1242, Units.RPM), airUnit.getSupplyFanSpeed());
159 public void getManualFanStepWhenOutOfRangeThrows() throws IOException {
160 byte[] response = new byte[] { (byte) 0x0b };
161 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, MANUAL_FAN_SPEED_STEP))
162 .thenReturn(response);
163 var airUnit = new DanfossAirUnit(communicationController);
164 assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getManualFanStep());
168 public void getFilterLifeWhenNearestNeighborIsBelowRoundsDown() throws IOException {
169 byte[] response = new byte[] { (byte) 0xf0 };
170 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, FILTER_LIFE)).thenReturn(response);
171 var airUnit = new DanfossAirUnit(communicationController);
172 assertEquals(new DecimalType("94.1"), airUnit.getFilterLife());