]> git.basschouten.com Git - openhab-addons.git/blob
ab0eaeb961fd7fb31ac16420b1f3ba895c64e804
[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 = "{\r\n" + "\"700\": {\r\n" + "\"name\": \"Betriebsart\",\r\n" + "\"value\": \"0\",\r\n"
39                 + "\"unit\": \"\",\r\n" + "\"desc\": \"Schutzbetrieb\",\r\n" + "\"dataType\": 1\r\n" + "}\r\n" + "}";
40
41         BsbLanApiParameterQueryResponseDTO r = BsbLanApiContentConverter.fromJson(content,
42                 BsbLanApiParameterQueryResponseDTO.class);
43         assertNotNull(r);
44         assertTrue(r.containsKey(700));
45
46         BsbLanApiParameterDTO p = r.get(700);
47         assertEquals("Betriebsart", p.name);
48         assertEquals("0", p.value);
49         assertEquals("", p.unit);
50         assertEquals("Schutzbetrieb", p.description);
51         assertEquals(BsbLanApiParameterDTO.DataType.DT_ENUM, p.dataType);
52     }
53
54     @Test
55     public void serializeBsbLanApiParameterSetRequest() {
56         BsbLanApiParameterSetRequestDTO request = new BsbLanApiParameterSetRequestDTO();
57         request.parameter = "1234";
58         request.value = "Hello World";
59         request.type = Type.SET;
60
61         String serializedRequest = BsbLanApiContentConverter.toJson(request);
62
63         // verify serialized content
64         JsonObject json = JsonParser.parseString(serializedRequest).getAsJsonObject();
65
66         // Although specifying the parameter as int (which would be nicer) also seems to work,
67         // we use a String here as this is the way it is noted in the documentation.
68         // So ensure there is a 'Parameter' and it is serialized as string.
69         assertEquals("1234", json.get("Parameter").getAsString());
70
71         // ensure there is a 'Value' and it is serialized as string
72         assertEquals("Hello World", json.get("Value").getAsString());
73
74         // ensure there is a 'Type' and it is serialized as number
75         assertEquals(1, json.get("Type").getAsInt());
76     }
77 }