2 * Copyright (c) 2010-2022 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.hdpowerview;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.hdpowerview.internal.api.CoordinateSystem.*;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.nio.charset.StandardCharsets;
21 import java.util.List;
22 import java.util.Objects;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.hdpowerview.internal.api.ShadePosition;
27 import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections;
28 import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections.SceneCollection;
29 import org.openhab.binding.hdpowerview.internal.api.responses.Scenes;
30 import org.openhab.binding.hdpowerview.internal.api.responses.Scenes.Scene;
31 import org.openhab.binding.hdpowerview.internal.api.responses.Shades;
32 import org.openhab.binding.hdpowerview.internal.api.responses.Shades.ShadeData;
33 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase;
34 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase.Capabilities;
35 import org.openhab.core.library.types.PercentType;
36 import org.openhab.core.types.State;
37 import org.openhab.core.types.UnDefType;
39 import com.google.gson.Gson;
42 * Unit tests for HD PowerView binding.
44 * @author Andrew Fiddian-Green - Initial contribution
45 * @author Jacob Laursen - Add support for scene groups
48 public class HDPowerViewJUnitTests {
50 private Gson gson = new Gson();
52 private <T> T getObjectFromJson(String filename, Class<T> clazz) throws IOException {
53 try (InputStream inputStream = HDPowerViewJUnitTests.class.getResourceAsStream(filename)) {
54 if (inputStream == null) {
55 throw new IOException("inputstream is null");
57 byte[] bytes = inputStream.readAllBytes();
59 throw new IOException("Resulting byte-array empty");
61 String json = new String(bytes, StandardCharsets.UTF_8);
62 return Objects.requireNonNull(gson.fromJson(json, clazz));
67 * Test generic JSON shades response.
70 public void shadeNameIsDecoded() throws IOException {
71 Shades shades = getObjectFromJson("shades.json", Shades.class);
72 List<ShadeData> shadeData = shades.shadeData;
73 assertNotNull(shadeData);
74 assertEquals(3, shadeData.size());
75 ShadeData shade = shadeData.get(0);
76 assertEquals("Shade 2", shade.getName());
80 * Test generic JSON scene response.
83 public void sceneNameIsDecoded() throws IOException {
84 Scenes scenes = getObjectFromJson("scenes.json", Scenes.class);
85 List<Scene> sceneData = scenes.sceneData;
86 assertNotNull(sceneData);
87 assertEquals(4, sceneData.size());
88 Scene scene = sceneData.get(0);
89 assertEquals("Door Open", scene.getName());
93 * Test generic JSON scene collection response.
96 public void sceneCollectionNameIsDecoded() throws IOException {
97 SceneCollections sceneCollections = getObjectFromJson("sceneCollections.json", SceneCollections.class);
99 List<SceneCollection> sceneCollectionData = sceneCollections.sceneCollectionData;
100 assertNotNull(sceneCollectionData);
101 assertEquals(1, sceneCollectionData.size());
103 SceneCollection sceneCollection = sceneCollectionData.get(0);
104 assertEquals("Børn op", sceneCollection.getName());
108 * Test the JSON parsing for a duette top down bottom up shade.
111 public void duetteTopDownBottomUpShadeIsParsedCorrectly() throws IOException {
112 Shades shades = getObjectFromJson("duette.json", Shades.class);
113 List<ShadeData> shadesData = shades.shadeData;
114 assertNotNull(shadesData);
116 assertEquals(1, shadesData.size());
117 ShadeData shadeData = shadesData.get(0);
118 assertNotNull(shadeData);
120 assertEquals("Gardin 1", shadeData.getName());
121 assertEquals(63778, shadeData.id);
123 ShadePosition shadePos = shadeData.positions;
124 assertNotNull(shadePos);
126 Integer capabilitiesValue = shadeData.capabilities;
127 assertNotNull(capabilitiesValue);
128 assertEquals(7, capabilitiesValue.intValue());
129 ShadeCapabilitiesDatabase db = new ShadeCapabilitiesDatabase();
130 Capabilities capabilities = db.getCapabilities(capabilitiesValue);
132 State pos = shadePos.getState(capabilities, PRIMARY_POSITION);
133 assertEquals(PercentType.class, pos.getClass());
134 assertEquals(59, ((PercentType) pos).intValue());
136 pos = shadePos.getState(capabilities, SECONDARY_POSITION);
137 assertEquals(PercentType.class, pos.getClass());
138 assertEquals(35, ((PercentType) pos).intValue());
140 pos = shadePos.getState(capabilities, VANE_TILT_POSITION);
141 assertEquals(UnDefType.class, pos.getClass());
143 assertEquals(3, shadeData.batteryStatus);
145 assertEquals(4, shadeData.signalStrength);
147 assertEquals(8, shadeData.type);
149 assertTrue(db.isTypeInDatabase(shadeData.type));
150 assertTrue(db.isCapabilitiesInDatabase(capabilitiesValue.intValue()));
152 assertEquals(db.getType(shadeData.type).getCapabilities(), capabilitiesValue.intValue());
154 assertTrue(db.getCapabilities(capabilitiesValue.intValue()).supportsSecondary());
155 assertNotEquals(db.getType(shadeData.type).getCapabilities(), capabilitiesValue.intValue() + 1);
157 // ==== when changing position1, position2 value is not changed (vice-versa) ====
158 ShadePosition shadePosition = shadeData.positions;
159 assertNotNull(shadePosition);
160 // ==== position2 ====
161 State position2Old = shadePosition.getState(capabilities, SECONDARY_POSITION);
162 shadePosition.setPosition(capabilities, PRIMARY_POSITION, 99);
163 State position2New = shadePosition.getState(capabilities, SECONDARY_POSITION);
164 assertEquals(PercentType.class, position2Old.getClass());
165 assertEquals(PercentType.class, position2New.getClass());
166 assertEquals(((PercentType) position2Old).intValue(), ((PercentType) position2New).intValue());
168 // ==== position2 ====
169 State position1Old = shadePosition.getState(capabilities, PRIMARY_POSITION);
170 shadePosition.setPosition(capabilities, SECONDARY_POSITION, 99);
171 State position1New = shadePosition.getState(capabilities, PRIMARY_POSITION);
172 assertEquals(PercentType.class, position1Old.getClass());
173 assertEquals(PercentType.class, position1New.getClass());
174 assertEquals(((PercentType) position1Old).intValue(), ((PercentType) position1New).intValue());