2 * Copyright (c) 2010-2023 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.mielecloud.internal.webservice.api.json;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.mielecloud.internal.util.ResourceUtil.getResourceAsString;
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
27 * @author Björn Lange - Initial contribution
28 * @author Benjamin Bolte - Add plate step
31 public class DeviceCollectionTest {
33 public void testCreateDeviceCollection() throws IOException {
35 String json = getResourceAsString(
36 "/org/openhab/binding/mielecloud/internal/webservice/api/json/deviceCollection.json");
39 DeviceCollection collection = DeviceCollection.fromJson(json);
42 assertEquals(1, collection.getDeviceIdentifiers().size());
43 Device device = collection.getDevice(collection.getDeviceIdentifiers().iterator().next());
45 Ident ident = device.getIdent().get();
46 Type type = ident.getType().get();
47 assertEquals("Devicetype", type.getKeyLocalized().get());
48 assertEquals(DeviceType.HOOD, type.getValueRaw());
49 assertEquals("Ventilation Hood", type.getValueLocalized().get());
51 assertEquals("My Hood", ident.getDeviceName().get());
53 DeviceIdentLabel deviceIdentLabel = ident.getDeviceIdentLabel().get();
54 assertEquals("000124430017", deviceIdentLabel.getFabNumber().get());
55 assertEquals("00", deviceIdentLabel.getFabIndex().get());
56 assertEquals("DA-6996", deviceIdentLabel.getTechType().get());
57 assertEquals("10101010", deviceIdentLabel.getMatNumber().get());
58 assertEquals(Arrays.asList("4164", "20380", "25226"), deviceIdentLabel.getSwids());
60 XkmIdentLabel xkmIdentLabel = ident.getXkmIdentLabel().get();
61 assertEquals("EK039W", xkmIdentLabel.getTechType().get());
62 assertEquals("02.31", xkmIdentLabel.getReleaseVersion().get());
64 State state = device.getState().get();
65 Status status = state.getStatus().get();
66 assertEquals(Integer.valueOf(StateType.RUNNING.getCode()), status.getValueRaw().get());
67 assertEquals("In use", status.getValueLocalized().get());
68 assertEquals("State", status.getKeyLocalized().get());
70 ProgramType programType = state.getProgramType().get();
71 assertEquals(Integer.valueOf(0), programType.getValueRaw().get());
72 assertEquals("", programType.getValueLocalized().get());
73 assertEquals("Programme", programType.getKeyLocalized().get());
75 ProgramPhase programPhase = state.getProgramPhase().get();
76 assertEquals(Integer.valueOf(4609), programPhase.getValueRaw().get());
77 assertEquals("", programPhase.getValueLocalized().get());
78 assertEquals("Phase", programPhase.getKeyLocalized().get());
80 assertEquals(Arrays.asList(0, 0), state.getRemainingTime().get());
81 assertEquals(Arrays.asList(0, 0), state.getStartTime().get());
83 assertEquals(1, state.getTargetTemperature().size());
84 Temperature targetTemperature = state.getTargetTemperature().get(0);
85 assertNotNull(targetTemperature);
86 assertEquals(Integer.valueOf(-32768), targetTemperature.getValueRaw().get());
87 assertFalse(targetTemperature.getValueLocalized().isPresent());
88 assertEquals("Celsius", targetTemperature.getUnit().get());
90 assertEquals(3, state.getTemperature().size());
91 Temperature temperature0 = state.getTemperature().get(0);
92 assertNotNull(temperature0);
93 assertEquals(Integer.valueOf(-32768), temperature0.getValueRaw().get());
94 assertFalse(temperature0.getValueLocalized().isPresent());
95 assertEquals("Celsius", temperature0.getUnit().get());
96 Temperature temperature1 = state.getTemperature().get(1);
97 assertNotNull(temperature1);
98 assertEquals(Integer.valueOf(-32768), temperature1.getValueRaw().get());
99 assertFalse(temperature1.getValueLocalized().isPresent());
100 assertEquals("Celsius", temperature1.getUnit().get());
101 Temperature temperature2 = state.getTemperature().get(2);
102 assertNotNull(temperature2);
103 assertEquals(Integer.valueOf(-32768), temperature2.getValueRaw().get());
104 assertFalse(temperature2.getValueLocalized().isPresent());
105 assertEquals("Celsius", temperature2.getUnit().get());
107 assertEquals(false, state.getSignalInfo().get());
108 assertEquals(false, state.getSignalFailure().get());
109 assertEquals(false, state.getSignalDoor().get());
111 RemoteEnable remoteEnable = state.getRemoteEnable().get();
112 assertEquals(false, remoteEnable.getFullRemoteControl().get());
113 assertEquals(false, remoteEnable.getSmartGrid().get());
115 assertEquals(Light.ENABLE, state.getLight());
116 assertEquals(new ArrayList<Object>(), state.getElapsedTime().get());
118 SpinningSpeed spinningSpeed = state.getSpinningSpeed().get();
119 assertEquals(Integer.valueOf(1200), spinningSpeed.getValueRaw().get());
120 assertEquals("1200", spinningSpeed.getValueLocalized().get());
121 assertEquals("rpm", spinningSpeed.getUnit().get());
123 DryingStep dryingStep = state.getDryingStep().get();
124 assertFalse(dryingStep.getValueRaw().isPresent());
125 assertEquals("", dryingStep.getValueLocalized().get());
126 assertEquals("Drying level", dryingStep.getKeyLocalized().get());
128 VentilationStep ventilationStep = state.getVentilationStep().get();
129 assertEquals(Integer.valueOf(2), ventilationStep.getValueRaw().get());
130 assertEquals("2", ventilationStep.getValueLocalized().get());
131 assertEquals("Power Level", ventilationStep.getKeyLocalized().get());
133 List<PlateStep> plateStep = state.getPlateStep();
134 assertEquals(4, plateStep.size());
135 assertEquals(Integer.valueOf(0), plateStep.get(0).getValueRaw().get());
136 assertEquals("0", plateStep.get(0).getValueLocalized().get());
137 assertEquals("Plate Step", plateStep.get(0).getKeyLocalized().get());
138 assertEquals(Integer.valueOf(1), plateStep.get(1).getValueRaw().get());
139 assertEquals("1", plateStep.get(1).getValueLocalized().get());
140 assertEquals("Plate Step", plateStep.get(1).getKeyLocalized().get());
141 assertEquals(Integer.valueOf(2), plateStep.get(2).getValueRaw().get());
142 assertEquals("1.", plateStep.get(2).getValueLocalized().get());
143 assertEquals("Plate Step", plateStep.get(2).getKeyLocalized().get());
144 assertEquals(Integer.valueOf(3), plateStep.get(3).getValueRaw().get());
145 assertEquals("2", plateStep.get(3).getValueLocalized().get());
146 assertEquals("Plate Step", plateStep.get(3).getKeyLocalized().get());
148 assertEquals(Integer.valueOf(20), state.getBatteryLevel().get());
152 public void testCreateDeviceCollectionFromInvalidJsonThrowsMieleSyntaxException() throws IOException {
154 String invalidJson = getResourceAsString(
155 "/org/openhab/binding/mielecloud/internal/webservice/api/json/invalidDeviceCollection.json");
158 assertThrows(MieleSyntaxException.class, () -> {
159 DeviceCollection.fromJson(invalidJson);
164 public void testCreateDeviceCollectionWithLargeProgramID() throws IOException {
166 String json = getResourceAsString(
167 "/org/openhab/binding/mielecloud/internal/webservice/api/json/deviceCollectionWithLargeProgramID.json");
170 DeviceCollection collection = DeviceCollection.fromJson(json);
173 assertEquals(1, collection.getDeviceIdentifiers().size());
174 Device device = collection.getDevice(collection.getDeviceIdentifiers().iterator().next());
176 Ident ident = device.getIdent().get();
177 Type type = ident.getType().get();
178 assertEquals("Devicetype", type.getKeyLocalized().get());
179 assertEquals(DeviceType.UNKNOWN, type.getValueRaw());
180 assertEquals("", type.getValueLocalized().get());
182 assertEquals("Some Devicename", ident.getDeviceName().get());
184 DeviceIdentLabel deviceIdentLabel = ident.getDeviceIdentLabel().get();
185 assertEquals("", deviceIdentLabel.getFabNumber().get());
186 assertEquals("", deviceIdentLabel.getFabIndex().get());
187 assertEquals("", deviceIdentLabel.getTechType().get());
188 assertEquals("", deviceIdentLabel.getMatNumber().get());
189 assertEquals(Arrays.asList(), deviceIdentLabel.getSwids());
191 XkmIdentLabel xkmIdentLabel = ident.getXkmIdentLabel().get();
192 assertEquals("", xkmIdentLabel.getTechType().get());
193 assertEquals("", xkmIdentLabel.getReleaseVersion().get());
195 State state = device.getState().get();
196 ProgramId programId = state.getProgramId().get();
197 assertEquals(Long.valueOf(2499805184L), programId.getValueRaw().get());
198 assertEquals("", programId.getValueLocalized().get());
199 assertEquals("Program Id", programId.getKeyLocalized().get());
201 Status status = state.getStatus().get();
202 assertEquals(Integer.valueOf(StateType.RUNNING.getCode()), status.getValueRaw().get());
203 assertEquals("In use", status.getValueLocalized().get());
204 assertEquals("State", status.getKeyLocalized().get());
206 ProgramType programType = state.getProgramType().get();
207 assertEquals(Integer.valueOf(0), programType.getValueRaw().get());
208 assertEquals("Operation mode", programType.getValueLocalized().get());
209 assertEquals("Program type", programType.getKeyLocalized().get());
211 ProgramPhase programPhase = state.getProgramPhase().get();
212 assertEquals(Integer.valueOf(0), programPhase.getValueRaw().get());
213 assertEquals("", programPhase.getValueLocalized().get());
214 assertEquals("Phase", programPhase.getKeyLocalized().get());
216 assertEquals(Arrays.asList(0, 0), state.getRemainingTime().get());
217 assertEquals(Arrays.asList(0, 0), state.getStartTime().get());
219 assertTrue(state.getTargetTemperature().isEmpty());
220 assertTrue(state.getTemperature().isEmpty());
222 assertEquals(false, state.getSignalInfo().get());
223 assertEquals(false, state.getSignalFailure().get());
224 assertEquals(false, state.getSignalDoor().get());
226 RemoteEnable remoteEnable = state.getRemoteEnable().get();
227 assertEquals(true, remoteEnable.getFullRemoteControl().get());
228 assertEquals(false, remoteEnable.getSmartGrid().get());
230 assertEquals(Light.NOT_SUPPORTED, state.getLight());
231 assertEquals(new ArrayList<Object>(), state.getElapsedTime().get());
233 DryingStep dryingStep = state.getDryingStep().get();
234 assertFalse(dryingStep.getValueRaw().isPresent());
235 assertEquals("", dryingStep.getValueLocalized().get());
236 assertEquals("Drying level", dryingStep.getKeyLocalized().get());
238 VentilationStep ventilationStep = state.getVentilationStep().get();
239 assertFalse(ventilationStep.getValueRaw().isPresent());
240 assertEquals("", ventilationStep.getValueLocalized().get());
241 assertEquals("Power Level", ventilationStep.getKeyLocalized().get());
243 List<PlateStep> plateStep = state.getPlateStep();
244 assertEquals(0, plateStep.size());
248 public void testCreateDeviceCollectionWithSpinningSpeedObject() throws IOException {
250 String json = getResourceAsString(
251 "/org/openhab/binding/mielecloud/internal/webservice/api/json/deviceCollectionWithSpinningSpeedObject.json");
254 DeviceCollection collection = DeviceCollection.fromJson(json);
257 assertEquals(1, collection.getDeviceIdentifiers().size());
258 Device device = collection.getDevice(collection.getDeviceIdentifiers().iterator().next());
260 State state = device.getState().get();
261 SpinningSpeed spinningSpeed = state.getSpinningSpeed().get();
262 assertNotNull(spinningSpeed);
263 assertEquals(Integer.valueOf(1600), spinningSpeed.getValueRaw().get());
264 assertEquals("1600", spinningSpeed.getValueLocalized().get());
265 assertEquals("U/min", spinningSpeed.getUnit().get());
269 public void testCreateDeviceCollectionWithFloatingPointTemperature() throws IOException {
271 String json = getResourceAsString(
272 "/org/openhab/binding/mielecloud/internal/webservice/api/json/deviceCollectionWithFloatingPointTargetTemperature.json");
275 DeviceCollection collection = DeviceCollection.fromJson(json);
278 assertEquals(1, collection.getDeviceIdentifiers().size());
279 Device device = collection.getDevice(collection.getDeviceIdentifiers().iterator().next());
281 State state = device.getState().get();
282 List<Temperature> targetTemperatures = state.getTargetTemperature();
283 assertEquals(1, targetTemperatures.size());
285 Temperature targetTemperature = targetTemperatures.get(0);
286 assertEquals(Integer.valueOf(80), targetTemperature.getValueRaw().get());
287 assertEquals(Integer.valueOf(0), targetTemperature.getValueLocalized().get());
288 assertEquals("Celsius", targetTemperature.getUnit().get());