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.mqtt.homeassistant.internal;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.util.Arrays;
23 import java.util.List;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.binding.mqtt.homeassistant.internal.BaseChannelConfiguration.Connection;
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
33 * @author Jochen Klein - Initial contribution
35 public class HAConfigurationTests {
37 private Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory())
40 private static String readTestJson(final String name) {
41 StringBuilder result = new StringBuilder();
43 try (BufferedReader in = new BufferedReader(
44 new InputStreamReader(HAConfigurationTests.class.getResourceAsStream(name), "UTF-8"))) {
47 while ((line = in.readLine()) != null) {
48 result.append(line).append('\n');
50 return result.toString();
51 } catch (IOException e) {
52 throw new RuntimeException(e);
57 public void testAbbreviations() {
58 String json = readTestJson("configA.json");
60 BaseChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson);
62 assertThat(config.name, is("A"));
63 assertThat(config.icon, is("2"));
64 assertThat(config.qos, is(1));
65 assertThat(config.retain, is(true));
66 assertThat(config.value_template, is("B"));
67 assertThat(config.unique_id, is("C"));
68 assertThat(config.availability_topic, is("D/E"));
69 assertThat(config.payload_available, is("F"));
70 assertThat(config.payload_not_available, is("G"));
72 assertThat(config.device, is(notNullValue()));
74 BaseChannelConfiguration.Device device = config.device;
76 assertThat(device.identifiers, contains("H"));
77 assertThat(device.connections, is(notNullValue()));
78 List<@NonNull Connection> connections = device.connections;
79 if (connections != null) {
80 assertThat(connections.get(0).type, is("I1"));
81 assertThat(connections.get(0).identifier, is("I2"));
83 assertThat(device.name, is("J"));
84 assertThat(device.model, is("K"));
85 assertThat(device.sw_version, is("L"));
86 assertThat(device.manufacturer, is("M"));
91 public void testTildeSubstritution() {
92 String json = readTestJson("configB.json");
94 ComponentSwitch.ChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson,
95 ComponentSwitch.ChannelConfiguration.class);
97 assertThat(config.availability_topic, is("D/E"));
98 assertThat(config.state_topic, is("O/D/"));
99 assertThat(config.command_topic, is("P~Q"));
100 assertThat(config.device, is(notNullValue()));
102 BaseChannelConfiguration.Device device = config.device;
103 if (device != null) {
104 assertThat(device.identifiers, contains("H"));
109 public void testSampleFanConfig() {
110 String json = readTestJson("configFan.json");
112 ComponentFan.ChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson,
113 ComponentFan.ChannelConfiguration.class);
114 assertThat(config.name, is("Bedroom Fan"));
118 public void testDeviceListConfig() {
119 String json = readTestJson("configDeviceList.json");
121 ComponentFan.ChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson,
122 ComponentFan.ChannelConfiguration.class);
123 assertThat(config.device, is(notNullValue()));
125 BaseChannelConfiguration.Device device = config.device;
126 if (device != null) {
127 assertThat(device.identifiers, is(Arrays.asList("A", "B", "C")));
132 public void testDeviceSingleStringConfig() {
133 String json = readTestJson("configDeviceSingleString.json");
135 ComponentFan.ChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson,
136 ComponentFan.ChannelConfiguration.class);
137 assertThat(config.device, is(notNullValue()));
139 BaseChannelConfiguration.Device device = config.device;
140 if (device != null) {
141 assertThat(device.identifiers, is(Arrays.asList("A")));