]> git.basschouten.com Git - openhab-addons.git/blob
252983adbe543f6e7808729e0462670b973e34fd
[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.boschshc.internal.serialization;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.junit.jupiter.api.Assertions.assertNotNull;
17 import static org.junit.jupiter.api.Assertions.assertTrue;
18
19 import java.util.HashSet;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.boschshc.internal.devices.bridge.dto.DeviceServiceData;
24 import org.openhab.binding.boschshc.internal.devices.bridge.dto.LongPollResult;
25 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Scenario;
26
27 /**
28  * Unit tests for {@link BoschServiceDataDeserializer}.
29  *
30  * @author Patrick Gell - Initial contribution
31  *
32  */
33 @NonNullByDefault
34 class BoschServiceDataDeserializerTest {
35
36     @Test
37     void deserializationOfLongPollingResult() {
38         var resultJson = """
39                 {
40                     "result": [
41                         {
42                             "@type": "scenarioTriggered",
43                             "name": "MyTriggeredScenario",
44                             "id": "509bd737-eed0-40b7-8caa-e8686a714399",
45                             "lastTimeTriggered": "1689417526720"
46                         },
47                         {
48                             "path":"/devices/hdm:HomeMaticIP:3014F711A0001916D859A8A9/services/PowerSwitch",
49                             "@type":"DeviceServiceData",
50                             "id":"PowerSwitch",
51                             "state":{
52                                 "@type":"powerSwitchState",
53                                 "switchState":"ON"
54                             },
55                             "deviceId":"hdm:HomeMaticIP:3014F711A0001916D859A8A9"
56                         }
57                     ],
58                     "jsonrpc": "2.0"
59                 }
60                 """;
61
62         var longPollResult = GsonUtils.DEFAULT_GSON_INSTANCE.fromJson(resultJson, LongPollResult.class);
63         assertNotNull(longPollResult);
64         assertEquals(2, longPollResult.result.size());
65
66         var resultClasses = new HashSet<>(longPollResult.result.stream().map(e -> e.getClass().getName()).toList());
67         assertEquals(2, resultClasses.size());
68         assertTrue(resultClasses.contains(DeviceServiceData.class.getName()));
69         assertTrue(resultClasses.contains(Scenario.class.getName()));
70     }
71 }