forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract interface from DanfossAirUnitCommunicationController.
Inject CommunicationController through DanfossAirUnit constructor. Add unit test coverage for DanfossAirUnit class. Fixes openhab#11167 Signed-off-by: Jacob Laursen <[email protected]>
- Loading branch information
Showing
5 changed files
with
170 additions
and
14 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...it/src/main/java/org/openhab/binding/danfossairunit/internal/CommunicationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.danfossairunit.internal; | ||
|
||
import java.io.IOException; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
@NonNullByDefault | ||
public interface CommunicationController { | ||
public void connect() throws IOException; | ||
|
||
public void disconnect(); | ||
|
||
public byte[] sendRobustRequest(byte[] operation, byte[] register) throws IOException; | ||
|
||
public byte[] sendRobustRequest(byte[] operation, byte[] register, byte[] value) throws IOException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...airunit/src/test/java/org/openhab/binding/danfossairunit/internal/DanfossAirUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.danfossairunit.internal; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
import static org.openhab.binding.danfossairunit.internal.Commands.*; | ||
|
||
import java.io.IOException; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.openhab.core.library.types.DateTimeType; | ||
import org.openhab.core.library.types.OnOffType; | ||
import org.openhab.core.library.types.QuantityType; | ||
import org.openhab.core.test.java.JavaTest; | ||
|
||
/** | ||
* This class provides test cases for {@link DanfossAirUnit} | ||
* | ||
* @author Ralf Duckstein - Initial contribution | ||
* @author Robert Bach - heavy refactorings | ||
*/ | ||
public class DanfossAirUnitTest extends JavaTest { | ||
|
||
private CommunicationController communicationController; | ||
|
||
@BeforeEach | ||
private void setUp() { | ||
this.communicationController = mock(CommunicationController.class); | ||
} | ||
|
||
@Test | ||
public void getUnitNameIsReturned() throws IOException { | ||
byte[] response = new byte[] { (byte) 0x05, (byte) 'w', (byte) '2', (byte) '/', (byte) 'a', (byte) '2' }; | ||
when(this.communicationController.sendRobustRequest(REGISTER_1_READ, UNIT_NAME)).thenReturn(response); | ||
var airUnit = new DanfossAirUnit(communicationController); | ||
assertEquals("w2/a2", airUnit.getUnitName()); | ||
} | ||
|
||
@Test | ||
public void getHumidityWhenNearestNeighborIsBelowRoundsDown() throws IOException { | ||
byte[] response = new byte[] { (byte) 0x64 }; | ||
when(this.communicationController.sendRobustRequest(REGISTER_1_READ, HUMIDITY)).thenReturn(response); | ||
var airUnit = new DanfossAirUnit(communicationController); | ||
assertEquals(new QuantityType<>("39.2 %"), airUnit.getHumidity()); | ||
} | ||
|
||
@Test | ||
public void getHumidityWhenNearestNeighborIsAboveRoundsUp() throws IOException { | ||
byte[] response = new byte[] { (byte) 0x67 }; | ||
when(this.communicationController.sendRobustRequest(REGISTER_1_READ, HUMIDITY)).thenReturn(response); | ||
var airUnit = new DanfossAirUnit(communicationController); | ||
assertEquals(new QuantityType<>("40.4 %"), airUnit.getHumidity()); | ||
} | ||
|
||
@Test | ||
public void getSupplyTemperatureWhenNearestNeighborIsBelowRoundsDown() | ||
throws IOException, UnexpectedResponseValueException { | ||
byte[] response = new byte[] { (byte) 0x09, (byte) 0xf0 }; // 0x09f0 = 2544 => 25.44 | ||
when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response); | ||
var airUnit = new DanfossAirUnit(communicationController); | ||
assertEquals(new QuantityType<>("25.4 °C"), airUnit.getSupplyTemperature()); | ||
} | ||
|
||
@Test | ||
public void getSupplyTemperatureWhenBothNeighborsAreEquidistantRoundsUp() | ||
throws IOException, UnexpectedResponseValueException { | ||
byte[] response = new byte[] { (byte) 0x09, (byte) 0xf1 }; // 0x09f1 = 2545 => 25.45 | ||
when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response); | ||
var airUnit = new DanfossAirUnit(communicationController); | ||
assertEquals(new QuantityType<>("25.5 °C"), airUnit.getSupplyTemperature()); | ||
} | ||
|
||
@Test | ||
public void getSupplyTemperatureWhenBelowValidRangeThrows() throws IOException { | ||
byte[] response = new byte[] { (byte) 0x94, (byte) 0xf8 }; // 0x94f8 = -27400 => -274 | ||
when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response); | ||
var airUnit = new DanfossAirUnit(communicationController); | ||
assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getSupplyTemperature()); | ||
} | ||
|
||
@Test | ||
public void getSupplyTemperatureWhenAboveValidRangeThrows() throws IOException { | ||
byte[] response = new byte[] { (byte) 0x27, (byte) 0x11 }; // 0x2711 = 10001 => 100,01 | ||
when(this.communicationController.sendRobustRequest(REGISTER_4_READ, SUPPLY_TEMPERATURE)).thenReturn(response); | ||
var airUnit = new DanfossAirUnit(communicationController); | ||
assertThrows(UnexpectedResponseValueException.class, () -> airUnit.getSupplyTemperature()); | ||
} | ||
|
||
@Test | ||
public void getCurrentTimeWhenWellFormattedIsParsed() throws IOException, UnexpectedResponseValueException { | ||
byte[] response = new byte[] { (byte) 0x03, (byte) 0x02, (byte) 0x0f, (byte) 0x1d, (byte) 0x08, (byte) 0x15 }; // 29.08.21 | ||
// 15:02:03 | ||
when(this.communicationController.sendRobustRequest(REGISTER_1_READ, CURRENT_TIME)).thenReturn(response); | ||
var airUnit = new DanfossAirUnit(communicationController); | ||
assertEquals(new DateTimeType(ZonedDateTime.of(2021, 8, 29, 15, 2, 3, 0, ZoneId.systemDefault())), | ||
airUnit.getCurrentTime()); | ||
} | ||
|
||
@Test | ||
public void getBootstWhenZeroIsOff() throws IOException { | ||
byte[] response = new byte[] { (byte) 0x00 }; | ||
when(this.communicationController.sendRobustRequest(REGISTER_1_READ, BOOST)).thenReturn(response); | ||
var airUnit = new DanfossAirUnit(communicationController); | ||
assertEquals(OnOffType.OFF, airUnit.getBoost()); | ||
} | ||
|
||
@Test | ||
public void getBootstWhenNonZeroIsOn() throws IOException { | ||
byte[] response = new byte[] { (byte) 0x66 }; | ||
when(this.communicationController.sendRobustRequest(REGISTER_1_READ, BOOST)).thenReturn(response); | ||
var airUnit = new DanfossAirUnit(communicationController); | ||
assertEquals(OnOffType.ON, airUnit.getBoost()); | ||
} | ||
} |