]> git.basschouten.com Git - openhab-addons.git/blob
49afff39f6dd62dbc8b72a9cc09d89268ef180d1
[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.bsblan.internal.api;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.junit.jupiter.api.Test;
19 import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterDTO;
20 import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterQueryResponseDTO;
21 import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterSetRequestDTO;
22 import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterSetRequestDTO.Type;
23
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonParser;
26
27 /**
28  * The {@link BsbLanApiContentConverterTests} class implements tests
29  * for {@link BsbLanApiContentConverter}.
30  *
31  * @author Peter Schraffl - Initial contribution
32  */
33 @NonNullByDefault
34 public class BsbLanApiContentConverterTests {
35
36     @Test
37     public void parseBsbLanApiParameterQueryResponse() {
38         String content = """
39                 {
40                 "700": {
41                 "name": "Betriebsart",
42                 "value": "0",
43                 "unit": "",
44                 "desc": "Schutzbetrieb",
45                 "dataType": 1
46                 }
47                 }\
48                 """;
49
50         BsbLanApiParameterQueryResponseDTO r = BsbLanApiContentConverter.fromJson(content,
51                 BsbLanApiParameterQueryResponseDTO.class);
52         assertNotNull(r);
53         assertTrue(r.containsKey(700));
54
55         BsbLanApiParameterDTO p = r.get(700);
56         assertEquals("Betriebsart", p.name);
57         assertEquals("0", p.value);
58         assertEquals("", p.unit);
59         assertEquals("Schutzbetrieb", p.description);
60         assertEquals(BsbLanApiParameterDTO.DataType.DT_ENUM, p.dataType);
61     }
62
63     @Test
64     public void serializeBsbLanApiParameterSetRequest() {
65         BsbLanApiParameterSetRequestDTO request = new BsbLanApiParameterSetRequestDTO();
66         request.parameter = "1234";
67         request.value = "Hello World";
68         request.type = Type.SET;
69
70         String serializedRequest = BsbLanApiContentConverter.toJson(request);
71
72         // verify serialized content
73         JsonObject json = JsonParser.parseString(serializedRequest).getAsJsonObject();
74
75         // Although specifying the parameter as int (which would be nicer) also seems to work,
76         // we use a String here as this is the way it is noted in the documentation.
77         // So ensure there is a 'Parameter' and it is serialized as string.
78         assertEquals("1234", json.get("Parameter").getAsString());
79
80         // ensure there is a 'Value' and it is serialized as string
81         assertEquals("Hello World", json.get("Value").getAsString());
82
83         // ensure there is a 'Type' and it is serialized as number
84         assertEquals(1, json.get("Type").getAsInt());
85     }
86 }