]> git.basschouten.com Git - openhab-addons.git/blob
5c5654f0e60b33c51632e2467a2a711b1f650493
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.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;
34
35 /**
36  * This class provides test cases for {@link DanfossAirUnit}
37  * 
38  * @author Jacob Laursen - Initial contribution
39  */
40 @NonNullByDefault
41 @ExtendWith(MockitoExtension.class)
42 public class DanfossAirUnitTest extends JavaTest {
43
44     private @NonNullByDefault({}) @Mock CommunicationController communicationController;
45
46     @Test
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());
52     }
53
54     @Test
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());
60     }
61
62     @Test
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());
68     }
69
70     @Test
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());
77     }
78
79     @Test
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());
86     }
87
88     @Test
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());
94     }
95
96     @Test
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());
102     }
103
104     @Test
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
107                                                                                                                        // 15:02:03
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());
112     }
113
114     @Test
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
117                                                                                                                        // 15:02:03
118         when(this.communicationController.sendRobustRequest(REGISTER_1_READ, CURRENT_TIME)).thenReturn(response);
119         var airUnit = new DanfossAirUnit(communicationController);
120         assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getCurrentTime());
121     }
122
123     @Test
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());
129     }
130
131     @Test
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());
137     }
138
139     @Test
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());
147     }
148
149     @Test
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());
156     }
157
158     @Test
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());
164     }
165 }