2 * Copyright (c) 2010-2021 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.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.core.library.types.DateTimeType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.PercentType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.test.java.JavaTest;
32 * This class provides test cases for {@link DanfossAirUnit}
34 * @author Jacob Laursen - Refactoring, bugfixes and enhancements
36 public class DanfossAirUnitTest extends JavaTest {
38 private CommunicationController communicationController;
41 private void setUp() {
42 this.communicationController = mock(CommunicationController.class);
46 public void getUnitNameIsReturned() throws IOException {
47 byte[] response = new byte[] { (byte) 0x05, (byte) 'w', (byte) '2', (byte) '/', (byte) 'a', (byte) '2' };
48 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, UNIT_NAME)).thenReturn(response);
49 var airUnit = new DanfossAirUnit(communicationController);
50 assertEquals("w2/a2", airUnit.getUnitName());
54 public void getHumidityWhenNearestNeighborIsBelowRoundsDown() throws IOException {
55 byte[] response = new byte[] { (byte) 0x64 };
56 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, HUMIDITY)).thenReturn(response);
57 var airUnit = new DanfossAirUnit(communicationController);
58 assertEquals(new QuantityType<>("39.2 %"), airUnit.getHumidity());
62 public void getHumidityWhenNearestNeighborIsAboveRoundsUp() throws IOException {
63 byte[] response = new byte[] { (byte) 0x67 };
64 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, HUMIDITY)).thenReturn(response);
65 var airUnit = new DanfossAirUnit(communicationController);
66 assertEquals(new QuantityType<>("40.4 %"), airUnit.getHumidity());
70 public void getSupplyTemperatureWhenNearestNeighborIsBelowRoundsDown()
71 throws IOException, UnexpectedResponseValueException {
72 byte[] response = new byte[] { (byte) 0x09, (byte) 0xf0 }; // 0x09f0 = 2544 => 25.44
73 when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response);
74 var airUnit = new DanfossAirUnit(communicationController);
75 assertEquals(new QuantityType<>("25.4 °C"), airUnit.getSupplyTemperature());
79 public void getSupplyTemperatureWhenBothNeighborsAreEquidistantRoundsUp()
80 throws IOException, UnexpectedResponseValueException {
81 byte[] response = new byte[] { (byte) 0x09, (byte) 0xf1 }; // 0x09f1 = 2545 => 25.45
82 when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response);
83 var airUnit = new DanfossAirUnit(communicationController);
84 assertEquals(new QuantityType<>("25.5 °C"), airUnit.getSupplyTemperature());
88 public void getSupplyTemperatureWhenBelowValidRangeThrows() throws IOException {
89 byte[] response = new byte[] { (byte) 0x94, (byte) 0xf8 }; // 0x94f8 = -27400 => -274
90 when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response);
91 var airUnit = new DanfossAirUnit(communicationController);
92 assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getSupplyTemperature());
96 public void getSupplyTemperatureWhenAboveValidRangeThrows() throws IOException {
97 byte[] response = new byte[] { (byte) 0x27, (byte) 0x11 }; // 0x2711 = 10001 => 100,01
98 when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response);
99 var airUnit = new DanfossAirUnit(communicationController);
100 assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getSupplyTemperature());
104 public void getCurrentTimeWhenWellFormattedIsParsed() throws IOException, UnexpectedResponseValueException {
105 byte[] response = new byte[] { (byte) 0x03, (byte) 0x02, (byte) 0x0f, (byte) 0x1d, (byte) 0x08, (byte) 0x15 }; // 29.08.21
107 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, CURRENT_TIME)).thenReturn(response);
108 var airUnit = new DanfossAirUnit(communicationController);
109 assertEquals(new DateTimeType(ZonedDateTime.of(2021, 8, 29, 15, 2, 3, 0, ZoneId.systemDefault())),
110 airUnit.getCurrentTime());
114 public void getCurrentTimeWhenInvalidDateThrows() throws IOException {
115 byte[] response = new byte[] { (byte) 0x03, (byte) 0x02, (byte) 0x0f, (byte) 0x20, (byte) 0x08, (byte) 0x15 }; // 32.08.21
117 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, CURRENT_TIME)).thenReturn(response);
118 var airUnit = new DanfossAirUnit(communicationController);
119 assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getCurrentTime());
123 public void getBoostWhenZeroIsOff() throws IOException {
124 byte[] response = new byte[] { (byte) 0x00 };
125 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, BOOST)).thenReturn(response);
126 var airUnit = new DanfossAirUnit(communicationController);
127 assertEquals(OnOffType.OFF, airUnit.getBoost());
131 public void getBoostWhenNonZeroIsOn() throws IOException {
132 byte[] response = new byte[] { (byte) 0x66 };
133 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, BOOST)).thenReturn(response);
134 var airUnit = new DanfossAirUnit(communicationController);
135 assertEquals(OnOffType.ON, airUnit.getBoost());
139 public void getManualFanStepWhenWithinValidRangeIsConvertedIntoPercent()
140 throws IOException, UnexpectedResponseValueException {
141 byte[] response = new byte[] { (byte) 0x05 };
142 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, MANUAL_FAN_SPEED_STEP))
143 .thenReturn(response);
144 var airUnit = new DanfossAirUnit(communicationController);
145 assertEquals(new PercentType(50), airUnit.getManualFanStep());
149 public void getManualFanStepWhenOutOfRangeThrows() throws IOException {
150 byte[] response = new byte[] { (byte) 0x0b };
151 when(this.communicationController.sendRobustRequest(REGISTER_1_READ, MANUAL_FAN_SPEED_STEP))
152 .thenReturn(response);
153 var airUnit = new DanfossAirUnit(communicationController);
154 assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getManualFanStep());