2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bsblan.internal.api;
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertTrue;
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;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParser;
30 * The {@link BsbLanApiContentConverterTests} class implements tests
31 * for {@link BsbLanApiContentConverter}.
33 * @author Peter Schraffl - Initial contribution
36 public class BsbLanApiContentConverterTests {
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" + "}";
43 BsbLanApiParameterQueryResponseDTO r = BsbLanApiContentConverter.fromJson(content,
44 BsbLanApiParameterQueryResponseDTO.class);
46 assertTrue(r.containsKey(700));
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);
57 public void serializeBsbLanApiParameterSetRequest() {
58 BsbLanApiParameterSetRequestDTO request = new BsbLanApiParameterSetRequestDTO();
59 request.parameter = "1234";
60 request.value = "Hello World";
61 request.type = Type.SET;
63 String serializedRequest = BsbLanApiContentConverter.toJson(request);
65 // verify serialized content
66 JsonParser parser = new JsonParser();
67 JsonObject json = parser.parse(serializedRequest).getAsJsonObject();
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());
74 // ensure there is a 'Value' and it is serialized as string
75 assertEquals("Hello World", json.get("Value").getAsString());
77 // ensure there is a 'Type' and it is serialized as number
78 assertEquals(1, json.get("Type").getAsInt());