]> git.basschouten.com Git - openhab-addons.git/blob
032e346e6362d22c58a872e3c0c84edd09f56104
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.mielecloud.internal.webservice.api.json;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.mielecloud.internal.util.ResourceUtil.getResourceAsString;
17
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25
26 /**
27  * @author Björn Lange - Initial contribution
28  * @author Benjamin Bolte - Add plate step
29  * @author Björn Lange - Add eco feedback
30  */
31 @NonNullByDefault
32 public class DeviceCollectionTest {
33     @Test
34     public void testCreateDeviceCollection() throws IOException {
35         // given:
36         String json = getResourceAsString(
37                 "/org/openhab/binding/mielecloud/internal/webservice/api/json/deviceCollection.json");
38
39         // when:
40         DeviceCollection collection = DeviceCollection.fromJson(json);
41
42         // then:
43         assertEquals(1, collection.getDeviceIdentifiers().size());
44         Device device = collection.getDevice(collection.getDeviceIdentifiers().iterator().next());
45
46         Ident ident = device.getIdent().get();
47         Type type = ident.getType().get();
48         assertEquals("Devicetype", type.getKeyLocalized().get());
49         assertEquals(DeviceType.HOOD, type.getValueRaw());
50         assertEquals("Ventilation Hood", type.getValueLocalized().get());
51
52         assertEquals("My Hood", ident.getDeviceName().get());
53
54         DeviceIdentLabel deviceIdentLabel = ident.getDeviceIdentLabel().get();
55         assertEquals("000124430017", deviceIdentLabel.getFabNumber().get());
56         assertEquals("00", deviceIdentLabel.getFabIndex().get());
57         assertEquals("DA-6996", deviceIdentLabel.getTechType().get());
58         assertEquals("10101010", deviceIdentLabel.getMatNumber().get());
59         assertEquals(Arrays.asList("4164", "20380", "25226"), deviceIdentLabel.getSwids());
60
61         XkmIdentLabel xkmIdentLabel = ident.getXkmIdentLabel().get();
62         assertEquals("EK039W", xkmIdentLabel.getTechType().get());
63         assertEquals("02.31", xkmIdentLabel.getReleaseVersion().get());
64
65         State state = device.getState().get();
66         Status status = state.getStatus().get();
67         assertEquals(Integer.valueOf(StateType.RUNNING.getCode()), status.getValueRaw().get());
68         assertEquals("In use", status.getValueLocalized().get());
69         assertEquals("State", status.getKeyLocalized().get());
70
71         ProgramType programType = state.getProgramType().get();
72         assertEquals(Integer.valueOf(0), programType.getValueRaw().get());
73         assertEquals("", programType.getValueLocalized().get());
74         assertEquals("Programme", programType.getKeyLocalized().get());
75
76         ProgramPhase programPhase = state.getProgramPhase().get();
77         assertEquals(Integer.valueOf(4609), programPhase.getValueRaw().get());
78         assertEquals("", programPhase.getValueLocalized().get());
79         assertEquals("Phase", programPhase.getKeyLocalized().get());
80
81         assertEquals(Arrays.asList(0, 0), state.getRemainingTime().get());
82         assertEquals(Arrays.asList(0, 0), state.getStartTime().get());
83
84         assertEquals(1, state.getTargetTemperature().size());
85         Temperature targetTemperature = state.getTargetTemperature().get(0);
86         assertNotNull(targetTemperature);
87         assertEquals(Integer.valueOf(-32768), targetTemperature.getValueRaw().get());
88         assertFalse(targetTemperature.getValueLocalized().isPresent());
89         assertEquals("Celsius", targetTemperature.getUnit().get());
90
91         assertEquals(3, state.getTemperature().size());
92         Temperature temperature0 = state.getTemperature().get(0);
93         assertNotNull(temperature0);
94         assertEquals(Integer.valueOf(-32768), temperature0.getValueRaw().get());
95         assertFalse(temperature0.getValueLocalized().isPresent());
96         assertEquals("Celsius", temperature0.getUnit().get());
97         Temperature temperature1 = state.getTemperature().get(1);
98         assertNotNull(temperature1);
99         assertEquals(Integer.valueOf(-32768), temperature1.getValueRaw().get());
100         assertFalse(temperature1.getValueLocalized().isPresent());
101         assertEquals("Celsius", temperature1.getUnit().get());
102         Temperature temperature2 = state.getTemperature().get(2);
103         assertNotNull(temperature2);
104         assertEquals(Integer.valueOf(-32768), temperature2.getValueRaw().get());
105         assertFalse(temperature2.getValueLocalized().isPresent());
106         assertEquals("Celsius", temperature2.getUnit().get());
107
108         assertEquals(false, state.getSignalInfo().get());
109         assertEquals(false, state.getSignalFailure().get());
110         assertEquals(false, state.getSignalDoor().get());
111
112         RemoteEnable remoteEnable = state.getRemoteEnable().get();
113         assertEquals(false, remoteEnable.getFullRemoteControl().get());
114         assertEquals(false, remoteEnable.getSmartGrid().get());
115
116         assertEquals(Light.ENABLE, state.getLight());
117         assertEquals(new ArrayList<Object>(), state.getElapsedTime().get());
118
119         SpinningSpeed spinningSpeed = state.getSpinningSpeed().get();
120         assertEquals(Integer.valueOf(1200), spinningSpeed.getValueRaw().get());
121         assertEquals("1200", spinningSpeed.getValueLocalized().get());
122         assertEquals("rpm", spinningSpeed.getUnit().get());
123
124         DryingStep dryingStep = state.getDryingStep().get();
125         assertFalse(dryingStep.getValueRaw().isPresent());
126         assertEquals("", dryingStep.getValueLocalized().get());
127         assertEquals("Drying level", dryingStep.getKeyLocalized().get());
128
129         VentilationStep ventilationStep = state.getVentilationStep().get();
130         assertEquals(Integer.valueOf(2), ventilationStep.getValueRaw().get());
131         assertEquals("2", ventilationStep.getValueLocalized().get());
132         assertEquals("Power Level", ventilationStep.getKeyLocalized().get());
133
134         List<PlateStep> plateStep = state.getPlateStep();
135         assertEquals(4, plateStep.size());
136         assertEquals(Integer.valueOf(0), plateStep.get(0).getValueRaw().get());
137         assertEquals("0", plateStep.get(0).getValueLocalized().get());
138         assertEquals("Plate Step", plateStep.get(0).getKeyLocalized().get());
139         assertEquals(Integer.valueOf(1), plateStep.get(1).getValueRaw().get());
140         assertEquals("1", plateStep.get(1).getValueLocalized().get());
141         assertEquals("Plate Step", plateStep.get(1).getKeyLocalized().get());
142         assertEquals(Integer.valueOf(2), plateStep.get(2).getValueRaw().get());
143         assertEquals("1.", plateStep.get(2).getValueLocalized().get());
144         assertEquals("Plate Step", plateStep.get(2).getKeyLocalized().get());
145         assertEquals(Integer.valueOf(3), plateStep.get(3).getValueRaw().get());
146         assertEquals("2", plateStep.get(3).getValueLocalized().get());
147         assertEquals("Plate Step", plateStep.get(3).getKeyLocalized().get());
148
149         assertEquals(Integer.valueOf(20), state.getBatteryLevel().get());
150     }
151
152     @Test
153     public void testCreateDeviceCollectionFromInvalidJsonThrowsMieleSyntaxException() throws IOException {
154         // given:
155         String invalidJson = getResourceAsString(
156                 "/org/openhab/binding/mielecloud/internal/webservice/api/json/invalidDeviceCollection.json");
157
158         // when:
159         assertThrows(MieleSyntaxException.class, () -> {
160             DeviceCollection.fromJson(invalidJson);
161         });
162     }
163
164     @Test
165     public void testCreateDeviceCollectionWithLargeProgramID() throws IOException {
166         // given:
167         String json = getResourceAsString(
168                 "/org/openhab/binding/mielecloud/internal/webservice/api/json/deviceCollectionWithLargeProgramID.json");
169
170         // when:
171         DeviceCollection collection = DeviceCollection.fromJson(json);
172
173         // then:
174         assertEquals(1, collection.getDeviceIdentifiers().size());
175         Device device = collection.getDevice(collection.getDeviceIdentifiers().iterator().next());
176
177         Ident ident = device.getIdent().get();
178         Type type = ident.getType().get();
179         assertEquals("Devicetype", type.getKeyLocalized().get());
180         assertEquals(DeviceType.UNKNOWN, type.getValueRaw());
181         assertEquals("", type.getValueLocalized().get());
182
183         assertEquals("Some Devicename", ident.getDeviceName().get());
184
185         DeviceIdentLabel deviceIdentLabel = ident.getDeviceIdentLabel().get();
186         assertEquals("", deviceIdentLabel.getFabNumber().get());
187         assertEquals("", deviceIdentLabel.getFabIndex().get());
188         assertEquals("", deviceIdentLabel.getTechType().get());
189         assertEquals("", deviceIdentLabel.getMatNumber().get());
190         assertEquals(Arrays.asList(), deviceIdentLabel.getSwids());
191
192         XkmIdentLabel xkmIdentLabel = ident.getXkmIdentLabel().get();
193         assertEquals("", xkmIdentLabel.getTechType().get());
194         assertEquals("", xkmIdentLabel.getReleaseVersion().get());
195
196         State state = device.getState().get();
197         ProgramId programId = state.getProgramId().get();
198         assertEquals(Long.valueOf(2499805184L), programId.getValueRaw().get());
199         assertEquals("", programId.getValueLocalized().get());
200         assertEquals("Program Id", programId.getKeyLocalized().get());
201
202         Status status = state.getStatus().get();
203         assertEquals(Integer.valueOf(StateType.RUNNING.getCode()), status.getValueRaw().get());
204         assertEquals("In use", status.getValueLocalized().get());
205         assertEquals("State", status.getKeyLocalized().get());
206
207         ProgramType programType = state.getProgramType().get();
208         assertEquals(Integer.valueOf(0), programType.getValueRaw().get());
209         assertEquals("Operation mode", programType.getValueLocalized().get());
210         assertEquals("Program type", programType.getKeyLocalized().get());
211
212         ProgramPhase programPhase = state.getProgramPhase().get();
213         assertEquals(Integer.valueOf(0), programPhase.getValueRaw().get());
214         assertEquals("", programPhase.getValueLocalized().get());
215         assertEquals("Phase", programPhase.getKeyLocalized().get());
216
217         assertEquals(Arrays.asList(0, 0), state.getRemainingTime().get());
218         assertEquals(Arrays.asList(0, 0), state.getStartTime().get());
219
220         assertTrue(state.getTargetTemperature().isEmpty());
221         assertTrue(state.getTemperature().isEmpty());
222
223         assertEquals(false, state.getSignalInfo().get());
224         assertEquals(false, state.getSignalFailure().get());
225         assertEquals(false, state.getSignalDoor().get());
226
227         RemoteEnable remoteEnable = state.getRemoteEnable().get();
228         assertEquals(true, remoteEnable.getFullRemoteControl().get());
229         assertEquals(false, remoteEnable.getSmartGrid().get());
230
231         assertEquals(Light.NOT_SUPPORTED, state.getLight());
232         assertEquals(new ArrayList<Object>(), state.getElapsedTime().get());
233
234         DryingStep dryingStep = state.getDryingStep().get();
235         assertFalse(dryingStep.getValueRaw().isPresent());
236         assertEquals("", dryingStep.getValueLocalized().get());
237         assertEquals("Drying level", dryingStep.getKeyLocalized().get());
238
239         VentilationStep ventilationStep = state.getVentilationStep().get();
240         assertFalse(ventilationStep.getValueRaw().isPresent());
241         assertEquals("", ventilationStep.getValueLocalized().get());
242         assertEquals("Power Level", ventilationStep.getKeyLocalized().get());
243
244         List<PlateStep> plateStep = state.getPlateStep();
245         assertEquals(0, plateStep.size());
246     }
247
248     @Test
249     public void testCreateDeviceCollectionWithSpinningSpeedObject() throws IOException {
250         // given:
251         String json = getResourceAsString(
252                 "/org/openhab/binding/mielecloud/internal/webservice/api/json/deviceCollectionWithSpinningSpeedObject.json");
253
254         // when:
255         DeviceCollection collection = DeviceCollection.fromJson(json);
256
257         // then:
258         assertEquals(1, collection.getDeviceIdentifiers().size());
259         Device device = collection.getDevice(collection.getDeviceIdentifiers().iterator().next());
260
261         State state = device.getState().get();
262         SpinningSpeed spinningSpeed = state.getSpinningSpeed().get();
263         assertNotNull(spinningSpeed);
264         assertEquals(Integer.valueOf(1600), spinningSpeed.getValueRaw().get());
265         assertEquals("1600", spinningSpeed.getValueLocalized().get());
266         assertEquals("U/min", spinningSpeed.getUnit().get());
267     }
268
269     @Test
270     public void testCreateDeviceCollectionWithFloatingPointTemperature() throws IOException {
271         // given:
272         String json = getResourceAsString(
273                 "/org/openhab/binding/mielecloud/internal/webservice/api/json/deviceCollectionWithFloatingPointTargetTemperature.json");
274
275         // when:
276         DeviceCollection collection = DeviceCollection.fromJson(json);
277
278         // then:
279         assertEquals(1, collection.getDeviceIdentifiers().size());
280         Device device = collection.getDevice(collection.getDeviceIdentifiers().iterator().next());
281
282         State state = device.getState().get();
283         List<Temperature> targetTemperatures = state.getTargetTemperature();
284         assertEquals(1, targetTemperatures.size());
285
286         Temperature targetTemperature = targetTemperatures.get(0);
287         assertEquals(Integer.valueOf(80), targetTemperature.getValueRaw().get());
288         assertEquals(Integer.valueOf(0), targetTemperature.getValueLocalized().get());
289         assertEquals("Celsius", targetTemperature.getUnit().get());
290     }
291
292     @Test
293     public void testCreateDeviceCollectionWithEcoFeedback() throws IOException {
294         // given:
295         String json = getResourceAsString(
296                 "/org/openhab/binding/mielecloud/internal/webservice/api/json/deviceCollectionWithEcoFeedback.json");
297
298         // when:
299         DeviceCollection collection = DeviceCollection.fromJson(json);
300
301         // then:
302         assertEquals(1, collection.getDeviceIdentifiers().size());
303         Device device = collection.getDevice(collection.getDeviceIdentifiers().iterator().next());
304
305         State state = device.getState().get();
306         EcoFeedback ecoFeedback = state.getEcoFeedback().get();
307
308         WaterConsumption currentWaterConsumption = ecoFeedback.getCurrentWaterConsumption().get();
309         assertEquals("l", currentWaterConsumption.getUnit().get());
310         assertEquals(0.0, currentWaterConsumption.getValue().get());
311
312         EnergyConsumption currentEnergyConsumption = ecoFeedback.getCurrentEnergyConsumption().get();
313         assertEquals("kWh", currentEnergyConsumption.getUnit().get());
314         assertEquals(0.5, currentEnergyConsumption.getValue().get());
315
316         assertEquals(0.0, ecoFeedback.getWaterForecast().get());
317         assertEquals(0.6, ecoFeedback.getEnergyForecast().get());
318     }
319 }