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