]> git.basschouten.com Git - openhab-addons.git/blob
f515f9d323a3f2de05e31ff013cf9baf8f6f17d9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.danfossairunit.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
17 import static org.openhab.binding.danfossairunit.internal.Commands.*;
18
19 import java.io.IOException;
20 import java.time.ZoneId;
21 import java.time.ZonedDateTime;
22
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;
30
31 /**
32  * This class provides test cases for {@link DanfossAirUnit}
33  * 
34  * @author Jacob Laursen - Refactoring, bugfixes and enhancements
35  */
36 public class DanfossAirUnitTest extends JavaTest {
37
38     private CommunicationController communicationController;
39
40     @BeforeEach
41     private void setUp() {
42         this.communicationController = mock(CommunicationController.class);
43     }
44
45     @Test
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());
51     }
52
53     @Test
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());
59     }
60
61     @Test
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());
67     }
68
69     @Test
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());
76     }
77
78     @Test
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());
85     }
86
87     @Test
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());
93     }
94
95     @Test
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());
101     }
102
103     @Test
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
106                                                                                                                        // 15:02:03
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());
111     }
112
113     @Test
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
116                                                                                                                        // 15:02:03
117         when(this.communicationController.sendRobustRequest(REGISTER_1_READ, CURRENT_TIME)).thenReturn(response);
118         var airUnit = new DanfossAirUnit(communicationController);
119         assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getCurrentTime());
120     }
121
122     @Test
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());
128     }
129
130     @Test
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());
136     }
137
138     @Test
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());
146     }
147
148     @Test
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());
155     }
156 }