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