]> git.basschouten.com Git - openhab-addons.git/blob
1f785d1cbc975eff1e672b31b22e792f796d16c5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.wlanthermo.internal.api.nano;
14
15 import static org.openhab.binding.wlanthermo.internal.WlanThermoBindingConstants.*;
16
17 import java.awt.Color;
18 import java.io.InputStream;
19 import java.io.InputStreamReader;
20 import java.nio.charset.StandardCharsets;
21 import java.util.Objects;
22 import java.util.stream.Stream;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.junit.jupiter.api.Assertions;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.function.Executable;
29 import org.junit.jupiter.params.ParameterizedTest;
30 import org.junit.jupiter.params.provider.Arguments;
31 import org.junit.jupiter.params.provider.MethodSource;
32 import org.openhab.binding.wlanthermo.internal.WlanThermoException;
33 import org.openhab.binding.wlanthermo.internal.WlanThermoUnknownChannelException;
34 import org.openhab.binding.wlanthermo.internal.WlanThermoUtil;
35 import org.openhab.binding.wlanthermo.internal.api.nano.dto.data.Data;
36 import org.openhab.binding.wlanthermo.internal.api.nano.dto.settings.Settings;
37 import org.openhab.core.library.types.DecimalType;
38 import org.openhab.core.library.types.HSBType;
39 import org.openhab.core.library.types.OnOffType;
40 import org.openhab.core.library.types.QuantityType;
41 import org.openhab.core.library.types.StringType;
42 import org.openhab.core.library.unit.SIUnits;
43 import org.openhab.core.library.unit.Units;
44 import org.openhab.core.thing.ChannelUID;
45 import org.openhab.core.thing.ThingUID;
46 import org.openhab.core.types.Command;
47 import org.openhab.core.types.State;
48
49 import com.google.gson.Gson;
50
51 /**
52  * The {@link WlanThermoNanoV1CommandHandlerTest} class tests the {@link WlanThermoNanoV1CommandHandler}
53  *
54  * @author Christian Schlipp - Initial contribution
55  */
56 @NonNullByDefault
57 class WlanThermoNanoV1CommandHandlerTest {
58     private static final ThingUID THING_UID = new ThingUID("wlanthermo", "nano", "test");
59
60     @Nullable
61     private Data data;
62     @Nullable
63     private Settings settings;
64
65     @BeforeEach
66     void setUp() {
67         Gson gson = new Gson();
68         ClassLoader classLoader = Objects.requireNonNull(WlanThermoNanoV1CommandHandlerTest.class.getClassLoader());
69         InputStream dataStream = Objects.requireNonNull(classLoader.getResourceAsStream("nanov1/data.json"));
70         InputStream settingsStream = Objects.requireNonNull(classLoader.getResourceAsStream("nanov1/settings.json"));
71         data = gson.fromJson(new InputStreamReader(dataStream, StandardCharsets.UTF_8), Data.class);
72         settings = gson.fromJson(new InputStreamReader(settingsStream, StandardCharsets.UTF_8), Settings.class);
73     }
74
75     static Stream<Arguments> getState() {
76         return Stream.of(
77                 // System channels
78                 Arguments.of(SYSTEM, SYSTEM_SOC, new DecimalType(32), null),
79                 Arguments.of(SYSTEM, SYSTEM_CHARGE, OnOffType.OFF, null),
80                 Arguments.of(SYSTEM, SYSTEM_RSSI_SIGNALSTRENGTH, new DecimalType(4), null),
81                 Arguments.of(SYSTEM, SYSTEM_RSSI, new QuantityType<>(-47, Units.DECIBEL_MILLIWATTS), null),
82
83                 // All channels
84                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_NAME, new StringType("Kanal 1"), null),
85                 Arguments.of(CHANNEL_PREFIX + "2", CHANNEL_NAME, new StringType("Kanal 2"), null),
86                 Arguments.of(CHANNEL_PREFIX + "3", CHANNEL_NAME, new StringType("Kanal 3"), null),
87                 Arguments.of(CHANNEL_PREFIX + "4", CHANNEL_NAME, new StringType("Kanal 4"), null),
88                 Arguments.of(CHANNEL_PREFIX + "5", CHANNEL_NAME, new StringType("Kanal 5"), null),
89                 Arguments.of(CHANNEL_PREFIX + "6", CHANNEL_NAME, new StringType("Kanal 6"), null),
90                 Arguments.of(CHANNEL_PREFIX + "7", CHANNEL_NAME, new StringType("Kanal 7"), null),
91                 Arguments.of(CHANNEL_PREFIX + "8", CHANNEL_NAME, new StringType("Kanal 8"), null),
92                 // invalid channel number
93                 Arguments.of(CHANNEL_PREFIX + "9", CHANNEL_NAME, new StringType("Kanal 9"),
94                         WlanThermoUnknownChannelException.class),
95
96                 // all channel values
97                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_NAME, new StringType("Kanal 1"), null),
98                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_TYP, new StringType("1000K/Maverick"), null),
99                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_TEMP, new QuantityType<>(23.7, SIUnits.CELSIUS), null),
100                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_MIN, new QuantityType<>(11, SIUnits.CELSIUS), null),
101                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_MAX, new QuantityType<>(155, SIUnits.CELSIUS), null),
102                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_DEVICE, OnOffType.OFF, null),
103                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_PUSH, OnOffType.ON, null),
104                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_OPENHAB_HIGH, OnOffType.OFF, null),
105                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_OPENHAB_LOW, OnOffType.OFF, null),
106                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_COLOR,
107                         HSBType.fromRGB(Color.decode("#EF562D").getRed(), Color.decode("#EF562D").getGreen(),
108                                 Color.decode("#EF562D").getBlue()),
109                         null),
110                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_COLOR_NAME,
111                         new StringType(WlanThermoNanoV1Util.toColorName("#EF562D")), null),
112
113                 // all pitmaster values
114                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_CHANNEL_ID, new DecimalType(1), null),
115                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_PIDPROFILE, new DecimalType(0), null),
116                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_DUTY_CYCLE, new DecimalType(0), null),
117                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_SETPOINT, new QuantityType<>(50, SIUnits.CELSIUS),
118                         null),
119                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_STATE, new StringType("off"), null));
120     }
121
122     static Stream<Arguments> getTrigger() {
123         return Stream.of(
124                 // all channels
125                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_OPENHAB, TRIGGER_NONE, null),
126                 Arguments.of(CHANNEL_PREFIX + "2", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class),
127                 Arguments.of(CHANNEL_PREFIX + "3", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class),
128                 Arguments.of(CHANNEL_PREFIX + "4", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class),
129                 Arguments.of(CHANNEL_PREFIX + "5", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class),
130                 Arguments.of(CHANNEL_PREFIX + "6", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class),
131                 Arguments.of(CHANNEL_PREFIX + "7", CHANNEL_ALARM_OPENHAB, TRIGGER_NONE, null),
132                 Arguments.of(CHANNEL_PREFIX + "8", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class),
133                 // invalid channel number
134                 Arguments.of(CHANNEL_PREFIX + "9", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class));
135     }
136
137     static Stream<Arguments> setState() {
138         return Stream.of(
139                 // All channels
140                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_NAME, new StringType("Kanal 1"), true),
141                 Arguments.of(CHANNEL_PREFIX + "2", CHANNEL_NAME, new StringType("Kanal 2"), true),
142                 Arguments.of(CHANNEL_PREFIX + "3", CHANNEL_NAME, new StringType("Kanal 3"), true),
143                 Arguments.of(CHANNEL_PREFIX + "4", CHANNEL_NAME, new StringType("Kanal 4"), true),
144                 Arguments.of(CHANNEL_PREFIX + "5", CHANNEL_NAME, new StringType("Kanal 5"), true),
145                 Arguments.of(CHANNEL_PREFIX + "6", CHANNEL_NAME, new StringType("Kanal 6"), true),
146                 Arguments.of(CHANNEL_PREFIX + "7", CHANNEL_NAME, new StringType("Kanal 7"), true),
147                 Arguments.of(CHANNEL_PREFIX + "8", CHANNEL_NAME, new StringType("Kanal 8"), true),
148                 // invalid channel number
149                 Arguments.of(CHANNEL_PREFIX + "9", CHANNEL_NAME, new StringType("Kanal 9"), false),
150
151                 // all channel values
152                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_NAME, new StringType("Kanal 1"), true),
153                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_TYP, new StringType("1000K/Maverick"), false),
154                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_TEMP, new QuantityType<>(23.7, SIUnits.CELSIUS), false),
155                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_MIN, new QuantityType<>(11, SIUnits.CELSIUS), true),
156                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_MAX, new QuantityType<>(155, SIUnits.CELSIUS), true),
157                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_DEVICE, OnOffType.OFF, true),
158                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_PUSH, OnOffType.ON, true),
159                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_OPENHAB_HIGH, OnOffType.OFF, false),
160                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_OPENHAB_LOW, OnOffType.OFF, false),
161                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_COLOR,
162                         HSBType.fromRGB(Color.decode("#EF562D").getRed(), Color.decode("#EF562D").getGreen(),
163                                 Color.decode("#EF562D").getBlue()),
164                         false),
165                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_COLOR_NAME,
166                         new StringType(WlanThermoNanoV1Util.toColorName("#EF562D")), true),
167
168                 // all pitmaster values
169                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_CHANNEL_ID, new DecimalType(1), true),
170                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_PIDPROFILE, new DecimalType(0), true),
171                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_DUTY_CYCLE, new DecimalType(0), false),
172                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_SETPOINT, new QuantityType<>(50, SIUnits.CELSIUS),
173                         true),
174                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_STATE, new StringType("off"), true)
175
176         );
177     }
178
179     @ParameterizedTest
180     @MethodSource("getTrigger")
181     void getTrigger(String groupId, String id, String expectedTrigger,
182             @Nullable Class<WlanThermoException> exceptionClass) {
183         Executable test = () -> Assertions.assertEquals(expectedTrigger, WlanThermoNanoV1CommandHandler
184                 .getTrigger(new ChannelUID(THING_UID, groupId, id), WlanThermoUtil.requireNonNull(data)));
185         if (exceptionClass != null) {
186             Assertions.assertThrows(exceptionClass, test);
187         } else {
188             Assertions.assertDoesNotThrow(test);
189         }
190     }
191
192     @ParameterizedTest
193     @MethodSource("getState")
194     void getState(String groupId, String id, State expectedState, @Nullable Class<WlanThermoException> exceptionClass) {
195         Executable test = () -> Assertions.assertEquals(expectedState,
196                 WlanThermoNanoV1CommandHandler.getState(new ChannelUID(THING_UID, groupId, id),
197                         WlanThermoUtil.requireNonNull(data), WlanThermoUtil.requireNonNull(settings)));
198         if (exceptionClass != null) {
199             Assertions.assertThrows(exceptionClass, test);
200         } else {
201             Assertions.assertDoesNotThrow(test);
202         }
203     }
204
205     @ParameterizedTest
206     @MethodSource("setState")
207     void setState(String groupId, String id, Command command, boolean expectedResult) {
208         Assertions.assertDoesNotThrow(() -> Assertions.assertEquals(expectedResult, WlanThermoNanoV1CommandHandler
209                 .setState(new ChannelUID(THING_UID, groupId, id), command, WlanThermoUtil.requireNonNull(data))));
210     }
211 }