]> git.basschouten.com Git - openhab-addons.git/blob
b73051127c032438c64a758addf21a030effb1ca
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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  */
30 @NonNullByDefault
31 public class DeviceCollectionTest {
32     @Test
33     public void testCreateDeviceCollection() throws IOException {
34         // given:
35         String json = getResourceAsString(
36                 "/org/openhab/binding/mielecloud/internal/webservice/api/json/deviceCollection.json");
37
38         // when:
39         DeviceCollection collection = DeviceCollection.fromJson(json);
40
41         // then:
42         assertEquals(1, collection.getDeviceIdentifiers().size());
43         Device device = collection.getDevice(collection.getDeviceIdentifiers().iterator().next());
44
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());
50
51         assertEquals("My Hood", ident.getDeviceName().get());
52
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());
59
60         XkmIdentLabel xkmIdentLabel = ident.getXkmIdentLabel().get();
61         assertEquals("EK039W", xkmIdentLabel.getTechType().get());
62         assertEquals("02.31", xkmIdentLabel.getReleaseVersion().get());
63
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());
69
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());
74
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());
79
80         assertEquals(Arrays.asList(0, 0), state.getRemainingTime().get());
81         assertEquals(Arrays.asList(0, 0), state.getStartTime().get());
82
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());
89
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());
106
107         assertEquals(false, state.getSignalInfo().get());
108         assertEquals(false, state.getSignalFailure().get());
109         assertEquals(false, state.getSignalDoor().get());
110
111         RemoteEnable remoteEnable = state.getRemoteEnable().get();
112         assertEquals(false, remoteEnable.getFullRemoteControl().get());
113         assertEquals(false, remoteEnable.getSmartGrid().get());
114
115         assertEquals(Light.ENABLE, state.getLight());
116         assertEquals(new ArrayList<Object>(), state.getElapsedTime().get());
117
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());
122
123         DryingStep dryingStep = state.getDryingStep().get();
124         assertFalse(dryingStep.getValueRaw().isPresent());
125         assertEquals("", dryingStep.getValueLocalized().get());
126         assertEquals("Drying level", dryingStep.getKeyLocalized().get());
127
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());
132
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());
147
148         assertEquals(Integer.valueOf(20), state.getBatteryLevel().get());
149     }
150
151     @Test
152     public void testCreateDeviceCollectionFromInvalidJsonThrowsMieleSyntaxException() throws IOException {
153         // given:
154         String invalidJson = getResourceAsString(
155                 "/org/openhab/binding/mielecloud/internal/webservice/api/json/invalidDeviceCollection.json");
156
157         // when:
158         assertThrows(MieleSyntaxException.class, () -> {
159             DeviceCollection.fromJson(invalidJson);
160         });
161     }
162
163     @Test
164     public void testCreateDeviceCollectionWithLargeProgramID() throws IOException {
165         // given:
166         String json = getResourceAsString(
167                 "/org/openhab/binding/mielecloud/internal/webservice/api/json/deviceCollectionWithLargeProgramID.json");
168
169         // when:
170         DeviceCollection collection = DeviceCollection.fromJson(json);
171
172         // then:
173         assertEquals(1, collection.getDeviceIdentifiers().size());
174         Device device = collection.getDevice(collection.getDeviceIdentifiers().iterator().next());
175
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());
181
182         assertEquals("Some Devicename", ident.getDeviceName().get());
183
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());
190
191         XkmIdentLabel xkmIdentLabel = ident.getXkmIdentLabel().get();
192         assertEquals("", xkmIdentLabel.getTechType().get());
193         assertEquals("", xkmIdentLabel.getReleaseVersion().get());
194
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());
200
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());
205
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());
210
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());
215
216         assertEquals(Arrays.asList(0, 0), state.getRemainingTime().get());
217         assertEquals(Arrays.asList(0, 0), state.getStartTime().get());
218
219         assertTrue(state.getTargetTemperature().isEmpty());
220         assertTrue(state.getTemperature().isEmpty());
221
222         assertEquals(false, state.getSignalInfo().get());
223         assertEquals(false, state.getSignalFailure().get());
224         assertEquals(false, state.getSignalDoor().get());
225
226         RemoteEnable remoteEnable = state.getRemoteEnable().get();
227         assertEquals(true, remoteEnable.getFullRemoteControl().get());
228         assertEquals(false, remoteEnable.getSmartGrid().get());
229
230         assertEquals(Light.NOT_SUPPORTED, state.getLight());
231         assertEquals(new ArrayList<Object>(), state.getElapsedTime().get());
232
233         DryingStep dryingStep = state.getDryingStep().get();
234         assertFalse(dryingStep.getValueRaw().isPresent());
235         assertEquals("", dryingStep.getValueLocalized().get());
236         assertEquals("Drying level", dryingStep.getKeyLocalized().get());
237
238         VentilationStep ventilationStep = state.getVentilationStep().get();
239         assertFalse(ventilationStep.getValueRaw().isPresent());
240         assertEquals("", ventilationStep.getValueLocalized().get());
241         assertEquals("Power Level", ventilationStep.getKeyLocalized().get());
242
243         List<PlateStep> plateStep = state.getPlateStep();
244         assertEquals(0, plateStep.size());
245     }
246
247     @Test
248     public void testCreateDeviceCollectionWithSpinningSpeedObject() throws IOException {
249         // given:
250         String json = getResourceAsString(
251                 "/org/openhab/binding/mielecloud/internal/webservice/api/json/deviceCollectionWithSpinningSpeedObject.json");
252
253         // when:
254         DeviceCollection collection = DeviceCollection.fromJson(json);
255
256         // then:
257         assertEquals(1, collection.getDeviceIdentifiers().size());
258         Device device = collection.getDevice(collection.getDeviceIdentifiers().iterator().next());
259
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());
266     }
267
268     @Test
269     public void testCreateDeviceCollectionWithFloatingPointTemperature() throws IOException {
270         // given:
271         String json = getResourceAsString(
272                 "/org/openhab/binding/mielecloud/internal/webservice/api/json/deviceCollectionWithFloatingPointTargetTemperature.json");
273
274         // when:
275         DeviceCollection collection = DeviceCollection.fromJson(json);
276
277         // then:
278         assertEquals(1, collection.getDeviceIdentifiers().size());
279         Device device = collection.getDevice(collection.getDeviceIdentifiers().iterator().next());
280
281         State state = device.getState().get();
282         List<Temperature> targetTemperatures = state.getTargetTemperature();
283         assertEquals(1, targetTemperatures.size());
284
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());
289     }
290 }