]> git.basschouten.com Git - openhab-addons.git/blob
e25ccf92a49efd98552f8a3d2d89b79af81f8a9e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.mqtt.homeassistant.internal;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
18
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;
24
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.binding.mqtt.homeassistant.internal.BaseChannelConfiguration.Connection;
28
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31
32 /**
33  * @author Jochen Klein - Initial contribution
34  */
35 public class HAConfigurationTests {
36
37     private Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory())
38             .create();
39
40     private static String readTestJson(final String name) {
41         StringBuilder result = new StringBuilder();
42
43         try (BufferedReader in = new BufferedReader(
44                 new InputStreamReader(HAConfigurationTests.class.getResourceAsStream(name), "UTF-8"))) {
45             String line;
46
47             while ((line = in.readLine()) != null) {
48                 result.append(line).append('\n');
49             }
50             return result.toString();
51         } catch (IOException e) {
52             throw new RuntimeException(e);
53         }
54     }
55
56     @Test
57     public void testAbbreviations() {
58         String json = readTestJson("configA.json");
59
60         BaseChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson);
61
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"));
71
72         assertThat(config.device, is(notNullValue()));
73
74         BaseChannelConfiguration.Device device = config.device;
75         if (device != null) {
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"));
82             }
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"));
87         }
88     }
89
90     @Test
91     public void testTildeSubstritution() {
92         String json = readTestJson("configB.json");
93
94         ComponentSwitch.ChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson,
95                 ComponentSwitch.ChannelConfiguration.class);
96
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()));
101
102         BaseChannelConfiguration.Device device = config.device;
103         if (device != null) {
104             assertThat(device.identifiers, contains("H"));
105         }
106     }
107
108     @Test
109     public void testSampleFanConfig() {
110         String json = readTestJson("configFan.json");
111
112         ComponentFan.ChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson,
113                 ComponentFan.ChannelConfiguration.class);
114         assertThat(config.name, is("Bedroom Fan"));
115     }
116
117     @Test
118     public void testDeviceListConfig() {
119         String json = readTestJson("configDeviceList.json");
120
121         ComponentFan.ChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson,
122                 ComponentFan.ChannelConfiguration.class);
123         assertThat(config.device, is(notNullValue()));
124
125         BaseChannelConfiguration.Device device = config.device;
126         if (device != null) {
127             assertThat(device.identifiers, is(Arrays.asList("A", "B", "C")));
128         }
129     }
130
131     @Test
132     public void testDeviceSingleStringConfig() {
133         String json = readTestJson("configDeviceSingleString.json");
134
135         ComponentFan.ChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson,
136                 ComponentFan.ChannelConfiguration.class);
137         assertThat(config.device, is(notNullValue()));
138
139         BaseChannelConfiguration.Device device = config.device;
140         if (device != null) {
141             assertThat(device.identifiers, is(Arrays.asList("A")));
142         }
143     }
144 }