2 * Copyright (c) 2010-2023 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.jupiter.api.Assertions.*;
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;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonParser;
28 * The {@link BsbLanApiContentConverterTests} class implements tests
29 * for {@link BsbLanApiContentConverter}.
31 * @author Peter Schraffl - Initial contribution
34 public class BsbLanApiContentConverterTests {
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" + "}";
41 BsbLanApiParameterQueryResponseDTO r = BsbLanApiContentConverter.fromJson(content,
42 BsbLanApiParameterQueryResponseDTO.class);
44 assertTrue(r.containsKey(700));
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);
55 public void serializeBsbLanApiParameterSetRequest() {
56 BsbLanApiParameterSetRequestDTO request = new BsbLanApiParameterSetRequestDTO();
57 request.parameter = "1234";
58 request.value = "Hello World";
59 request.type = Type.SET;
61 String serializedRequest = BsbLanApiContentConverter.toJson(request);
63 // verify serialized content
64 JsonObject json = JsonParser.parseString(serializedRequest).getAsJsonObject();
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());
71 // ensure there is a 'Value' and it is serialized as string
72 assertEquals("Hello World", json.get("Value").getAsString());
74 // ensure there is a 'Type' and it is serialized as number
75 assertEquals(1, json.get("Type").getAsInt());