]> git.basschouten.com Git - openhab-addons.git/blob
c65771d3150d65e6f3b8e450a56f2d6b04ebde5c
[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.esp32;
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.esp32.dto.data.Data;
36 import org.openhab.binding.wlanthermo.internal.api.esp32.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 import org.openhab.core.types.UnDefType;
49
50 import com.google.gson.Gson;
51
52 /**
53  * The {@link WlanThermoEsp32CommandHandlerTest} class tests the {@link WlanThermoEsp32CommandHandler}
54  *
55  * @author Christian Schlipp - Initial contribution
56  */
57 @NonNullByDefault
58 class WlanThermoEsp32CommandHandlerTest {
59
60     private static final ThingUID THING_UID = new ThingUID("wlanthermo", "esp32", "test");
61
62     @Nullable
63     private Data data;
64     @Nullable
65     private Settings settings;
66
67     @BeforeEach
68     void setUp() {
69         Gson gson = new Gson();
70         ClassLoader classLoader = Objects.requireNonNull(WlanThermoEsp32CommandHandlerTest.class.getClassLoader());
71         InputStream dataStream = Objects.requireNonNull(classLoader.getResourceAsStream("esp32/data.json"));
72         InputStream settingsStream = Objects.requireNonNull(classLoader.getResourceAsStream("esp32/settings.json"));
73         data = gson.fromJson(new InputStreamReader(dataStream, StandardCharsets.UTF_8), Data.class);
74         settings = gson.fromJson(new InputStreamReader(settingsStream, StandardCharsets.UTF_8), Settings.class);
75     }
76
77     static Stream<Arguments> getState() {
78         return Stream.of(
79                 // System channels
80                 Arguments.of(SYSTEM, SYSTEM_SOC, new DecimalType(89), null),
81                 Arguments.of(SYSTEM, SYSTEM_CHARGE, OnOffType.OFF, null),
82                 Arguments.of(SYSTEM, SYSTEM_RSSI_SIGNALSTRENGTH, new DecimalType(4), null),
83                 Arguments.of(SYSTEM, SYSTEM_RSSI, new QuantityType<>(-32, Units.DECIBEL_MILLIWATTS), null),
84
85                 // All channels
86                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_NAME, new StringType("Kanal Eins"), null),
87                 Arguments.of(CHANNEL_PREFIX + "2", CHANNEL_NAME, new StringType("Kanal 2"), null),
88                 Arguments.of(CHANNEL_PREFIX + "3", CHANNEL_NAME, new StringType("Kanal 3"), null),
89                 Arguments.of(CHANNEL_PREFIX + "4", CHANNEL_NAME, new StringType("Kanal 4"), null),
90                 Arguments.of(CHANNEL_PREFIX + "5", CHANNEL_NAME, new StringType("Kanal 5"), null),
91                 Arguments.of(CHANNEL_PREFIX + "6", CHANNEL_NAME, new StringType("Kanal 6"), null),
92                 Arguments.of(CHANNEL_PREFIX + "7", CHANNEL_NAME, new StringType("Kanal 7"), null),
93                 Arguments.of(CHANNEL_PREFIX + "8", CHANNEL_NAME, new StringType("Kanal 8"), null),
94                 Arguments.of(CHANNEL_PREFIX + "9", CHANNEL_NAME, new StringType("Kanal 9"), null),
95                 Arguments.of(CHANNEL_PREFIX + "10", CHANNEL_NAME, new StringType("Kanal 10"), null),
96                 // invalid channel number
97                 Arguments.of(CHANNEL_PREFIX + "11", CHANNEL_NAME, UnDefType.UNDEF,
98                         WlanThermoUnknownChannelException.class),
99
100                 // all channel values
101                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_NAME, new StringType("Kanal Eins"), null),
102                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_TYP, new StringType("1000K/Maverick"), null),
103                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_TEMP, new QuantityType<>(23.7, SIUnits.CELSIUS), null),
104                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_MIN, new QuantityType<>(17, SIUnits.CELSIUS), null),
105                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_MAX, new QuantityType<>(104, SIUnits.CELSIUS), null),
106                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_DEVICE, OnOffType.OFF, null),
107                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_PUSH, OnOffType.ON, null),
108                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_OPENHAB_HIGH, OnOffType.OFF, null),
109                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_OPENHAB_LOW, OnOffType.OFF, null),
110                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_COLOR,
111                         HSBType.fromRGB(Color.decode("#270000").getRed(), Color.decode("#270000").getGreen(),
112                                 Color.decode("#270000").getBlue()),
113                         null),
114                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_COLOR_NAME,
115                         new StringType(WlanThermoEsp32Util.toColorName("#270000")), null),
116
117                 // all pitmaster
118                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_CHANNEL_ID, new DecimalType(1), null),
119                 Arguments.of(CHANNEL_PITMASTER_2, CHANNEL_PITMASTER_CHANNEL_ID, UnDefType.UNDEF, null),
120
121                 // all pitmaster values
122                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_CHANNEL_ID, new DecimalType(1), null),
123                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_PIDPROFILE, new DecimalType(1), null),
124                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_DUTY_CYCLE, new DecimalType(70), null),
125                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_SETPOINT, new QuantityType<>(50, SIUnits.CELSIUS),
126                         null),
127                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_STATE, new StringType("manual"), null));
128     }
129
130     static Stream<Arguments> getTrigger() {
131         return Stream.of(
132                 // all channels
133                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_OPENHAB, TRIGGER_NONE, null),
134                 Arguments.of(CHANNEL_PREFIX + "2", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class),
135                 Arguments.of(CHANNEL_PREFIX + "3", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class),
136                 Arguments.of(CHANNEL_PREFIX + "4", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class),
137                 Arguments.of(CHANNEL_PREFIX + "5", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class),
138                 Arguments.of(CHANNEL_PREFIX + "6", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class),
139                 Arguments.of(CHANNEL_PREFIX + "7", CHANNEL_ALARM_OPENHAB, TRIGGER_NONE, null),
140                 Arguments.of(CHANNEL_PREFIX + "8", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class),
141                 Arguments.of(CHANNEL_PREFIX + "9", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class),
142                 Arguments.of(CHANNEL_PREFIX + "10", CHANNEL_ALARM_OPENHAB, "", WlanThermoUnknownChannelException.class),
143                 // invalid channel number
144                 Arguments.of(CHANNEL_PREFIX + "11", CHANNEL_ALARM_OPENHAB, "",
145                         WlanThermoUnknownChannelException.class));
146     }
147
148     static Stream<Arguments> setState() {
149         return Stream.of(
150                 // All channels
151                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_NAME, new StringType("Kanal Eins"), true),
152                 Arguments.of(CHANNEL_PREFIX + "2", CHANNEL_NAME, new StringType("Kanal 2"), true),
153                 Arguments.of(CHANNEL_PREFIX + "3", CHANNEL_NAME, new StringType("Kanal 3"), true),
154                 Arguments.of(CHANNEL_PREFIX + "4", CHANNEL_NAME, new StringType("Kanal 4"), true),
155                 Arguments.of(CHANNEL_PREFIX + "5", CHANNEL_NAME, new StringType("Kanal 5"), true),
156                 Arguments.of(CHANNEL_PREFIX + "6", CHANNEL_NAME, new StringType("Kanal 6"), true),
157                 Arguments.of(CHANNEL_PREFIX + "7", CHANNEL_NAME, new StringType("Kanal 7"), true),
158                 Arguments.of(CHANNEL_PREFIX + "8", CHANNEL_NAME, new StringType("Kanal 8"), true),
159                 Arguments.of(CHANNEL_PREFIX + "9", CHANNEL_NAME, new StringType("Kanal 9"), true),
160                 Arguments.of(CHANNEL_PREFIX + "10", CHANNEL_NAME, new StringType("Kanal 10"), true),
161                 // invalid channel number
162                 Arguments.of(CHANNEL_PREFIX + "11", CHANNEL_NAME, new StringType("Kanal 11"), false),
163
164                 // all channel values
165                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_NAME, new StringType("Kanal Eins"), true),
166                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_TYP, new StringType("1000K/Maverick"), false),
167                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_TEMP, new QuantityType<>(23.7, SIUnits.CELSIUS), false),
168                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_MIN, new QuantityType<>(17, SIUnits.CELSIUS), true),
169                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_MAX, new QuantityType<>(104, SIUnits.CELSIUS), true),
170                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_DEVICE, OnOffType.OFF, true),
171                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_PUSH, OnOffType.ON, true),
172                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_OPENHAB_HIGH, OnOffType.OFF, false),
173                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_ALARM_OPENHAB_LOW, OnOffType.OFF, false),
174                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_COLOR,
175                         HSBType.fromRGB(Color.decode("#270000").getRed(), Color.decode("#270000").getGreen(),
176                                 Color.decode("#270000").getBlue()),
177                         true),
178                 Arguments.of(CHANNEL_PREFIX + "1", CHANNEL_COLOR_NAME,
179                         new StringType(WlanThermoEsp32Util.toColorName("#270000")), true),
180
181                 // all pitmaster
182                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_CHANNEL_ID, new DecimalType(1), true),
183                 Arguments.of(CHANNEL_PITMASTER_2, CHANNEL_PITMASTER_CHANNEL_ID, new DecimalType(1), false),
184
185                 // all pitmaster values
186                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_CHANNEL_ID, new DecimalType(1), true),
187                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_PIDPROFILE, new DecimalType(0), true),
188                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_DUTY_CYCLE, new DecimalType(0), false),
189                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_SETPOINT, new QuantityType<>(100, SIUnits.CELSIUS),
190                         true),
191                 Arguments.of(CHANNEL_PITMASTER_1, CHANNEL_PITMASTER_STATE, new StringType("off"), true));
192     }
193
194     @ParameterizedTest
195     @MethodSource("getTrigger")
196     void getTrigger(String groupId, String id, String expectedTrigger,
197             @Nullable Class<WlanThermoException> exceptionClass) {
198         Executable test = () -> Assertions.assertEquals(expectedTrigger, WlanThermoEsp32CommandHandler
199                 .getTrigger(new ChannelUID(THING_UID, groupId, id), WlanThermoUtil.requireNonNull(data)));
200         if (exceptionClass != null) {
201             Assertions.assertThrows(exceptionClass, test);
202         } else {
203             Assertions.assertDoesNotThrow(test);
204         }
205     }
206
207     @ParameterizedTest
208     @MethodSource("getState")
209     void getState(String groupId, String id, State expectedState, @Nullable Class<WlanThermoException> exceptionClass) {
210         Executable test = () -> Assertions.assertEquals(expectedState,
211                 WlanThermoEsp32CommandHandler.getState(new ChannelUID(THING_UID, groupId, id),
212                         WlanThermoUtil.requireNonNull(data), WlanThermoUtil.requireNonNull(settings)));
213         if (exceptionClass != null) {
214             Assertions.assertThrows(exceptionClass, test);
215         } else {
216             Assertions.assertDoesNotThrow(test);
217         }
218     }
219
220     @ParameterizedTest
221     @MethodSource("setState")
222     void setState(String groupId, String id, Command command, boolean expectedResult) {
223         Assertions.assertDoesNotThrow(() -> Assertions.assertEquals(expectedResult,
224                 WlanThermoEsp32CommandHandler.setState(new ChannelUID(THING_UID, groupId, id), command,
225                         WlanThermoUtil.requireNonNull(data), WlanThermoUtil.requireNonNull(settings))));
226     }
227 }