]> git.basschouten.com Git - openhab-addons.git/blob
438055477afb9fff53370141558157ed0dad8f46
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.hdpowerview;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.hdpowerview.internal.api.CoordinateSystem.*;
17
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;
23
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;
38
39 import com.google.gson.Gson;
40
41 /**
42  * Unit tests for HD PowerView binding.
43  *
44  * @author Andrew Fiddian-Green - Initial contribution
45  * @author Jacob Laursen - Add support for scene groups
46  */
47 @NonNullByDefault
48 public class HDPowerViewJUnitTests {
49
50     private Gson gson = new Gson();
51
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");
56             }
57             byte[] bytes = inputStream.readAllBytes();
58             if (bytes == null) {
59                 throw new IOException("Resulting byte-array empty");
60             }
61             String json = new String(bytes, StandardCharsets.UTF_8);
62             return Objects.requireNonNull(gson.fromJson(json, clazz));
63         }
64     }
65
66     /**
67      * Test generic JSON shades response.
68      */
69     @Test
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());
77     }
78
79     /**
80      * Test generic JSON scene response.
81      */
82     @Test
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());
90     }
91
92     /**
93      * Test generic JSON scene collection response.
94      */
95     @Test
96     public void sceneCollectionNameIsDecoded() throws IOException {
97         SceneCollections sceneCollections = getObjectFromJson("sceneCollections.json", SceneCollections.class);
98
99         List<SceneCollection> sceneCollectionData = sceneCollections.sceneCollectionData;
100         assertNotNull(sceneCollectionData);
101         assertEquals(1, sceneCollectionData.size());
102
103         SceneCollection sceneCollection = sceneCollectionData.get(0);
104         assertEquals("Børn op", sceneCollection.getName());
105     }
106
107     /**
108      * Test the JSON parsing for a duette top down bottom up shade.
109      */
110     @Test
111     public void duetteTopDownBottomUpShadeIsParsedCorrectly() throws IOException {
112         Shades shades = getObjectFromJson("duette.json", Shades.class);
113         List<ShadeData> shadesData = shades.shadeData;
114         assertNotNull(shadesData);
115
116         assertEquals(1, shadesData.size());
117         ShadeData shadeData = shadesData.get(0);
118         assertNotNull(shadeData);
119
120         assertEquals("Gardin 1", shadeData.getName());
121         assertEquals(63778, shadeData.id);
122
123         ShadePosition shadePos = shadeData.positions;
124         assertNotNull(shadePos);
125
126         Integer capabilitiesValue = shadeData.capabilities;
127         assertNotNull(capabilitiesValue);
128         assertEquals(7, capabilitiesValue.intValue());
129         ShadeCapabilitiesDatabase db = new ShadeCapabilitiesDatabase();
130         Capabilities capabilities = db.getCapabilities(capabilitiesValue);
131
132         State pos = shadePos.getState(capabilities, PRIMARY_POSITION);
133         assertEquals(PercentType.class, pos.getClass());
134         assertEquals(59, ((PercentType) pos).intValue());
135
136         pos = shadePos.getState(capabilities, SECONDARY_POSITION);
137         assertEquals(PercentType.class, pos.getClass());
138         assertEquals(35, ((PercentType) pos).intValue());
139
140         pos = shadePos.getState(capabilities, VANE_TILT_POSITION);
141         assertEquals(UnDefType.class, pos.getClass());
142
143         assertEquals(3, shadeData.batteryStatus);
144
145         assertEquals(4, shadeData.signalStrength);
146
147         assertEquals(8, shadeData.type);
148
149         assertTrue(db.isTypeInDatabase(shadeData.type));
150         assertTrue(db.isCapabilitiesInDatabase(capabilitiesValue.intValue()));
151
152         assertEquals(db.getType(shadeData.type).getCapabilities(), capabilitiesValue.intValue());
153
154         assertTrue(db.getCapabilities(capabilitiesValue.intValue()).supportsSecondary());
155         assertNotEquals(db.getType(shadeData.type).getCapabilities(), capabilitiesValue.intValue() + 1);
156
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());
167
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());
175     }
176 }