2 * Copyright (c) 2010-2022 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.mybmw.internal.handler;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
18 import java.util.List;
20 import java.util.Optional;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.junit.jupiter.api.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.openhab.binding.mybmw.internal.MyBMWConstants.VehicleType;
27 import org.openhab.binding.mybmw.internal.VehicleConfiguration;
28 import org.openhab.binding.mybmw.internal.dto.StatusWrapper;
29 import org.openhab.binding.mybmw.internal.util.FileReader;
30 import org.openhab.binding.mybmw.internal.utils.Constants;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.ThingHandlerCallback;
35 import org.openhab.core.types.State;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link VehicleTests} is responsible for handling commands, which are
41 * sent to one of the channels.
43 * @author Bernd Weymann - Initial contribution
46 @SuppressWarnings("null")
47 public class VehicleTests {
48 private final Logger logger = LoggerFactory.getLogger(VehicleHandler.class);
50 private static final int STATUS_ELECTRIC = 12;
51 private static final int STATUS_CONV = 9;
52 private static final int RANGE_HYBRID = 9;
53 private static final int RANGE_CONV = 4;
54 private static final int RANGE_ELECTRIC = 4;
55 private static final int DOORS = 11;
56 private static final int CHECK_EMPTY = 3;
57 private static final int CHECK_AVAILABLE = 3;
58 private static final int SERVICE_AVAILABLE = 3;
59 private static final int SERVICE_EMPTY = 3;
60 private static final int LOCATION = 3;
61 private static final int CHARGE_PROFILE = 44;
62 private static final int TIRES = 8;
65 ArgumentCaptor<ChannelUID> channelCaptor;
67 ArgumentCaptor<State> stateCaptor;
69 ThingHandlerCallback tc;
73 List<ChannelUID> allChannels;
75 List<State> allStates;
76 String driveTrain = Constants.EMPTY;
79 * Prepare environment for Vehicle Status Updates
81 public void setup(String type, String vin) {
83 Thing thing = mock(Thing.class);
84 when(thing.getUID()).thenReturn(new ThingUID("testbinding", "test"));
85 MyBMWCommandOptionProvider cop = mock(MyBMWCommandOptionProvider.class);
86 cch = new VehicleHandler(thing, cop, type);
87 VehicleConfiguration vc = new VehicleConfiguration();
89 Optional<VehicleConfiguration> ovc = Optional.of(vc);
90 cch.configuration = ovc;
91 tc = mock(ThingHandlerCallback.class);
93 channelCaptor = ArgumentCaptor.forClass(ChannelUID.class);
94 stateCaptor = ArgumentCaptor.forClass(State.class);
97 private boolean testVehicle(String statusContent, int callbacksExpected,
98 Optional<Map<String, State>> concreteChecks) {
99 assertNotNull(statusContent);
100 cch.vehicleStatusCallback.onResponse(statusContent);
101 verify(tc, times(callbacksExpected)).stateUpdated(channelCaptor.capture(), stateCaptor.capture());
102 allChannels = channelCaptor.getAllValues();
103 allStates = stateCaptor.getAllValues();
105 assertNotNull(driveTrain);
106 StatusWrapper checker = new StatusWrapper(driveTrain, statusContent);
108 if (concreteChecks.isPresent()) {
109 return checker.append(concreteChecks.get()).checkResults(allChannels, allStates);
111 return checker.checkResults(allChannels, allStates);
115 private void trace() {
116 for (int i = 0; i < allChannels.size(); i++) {
117 // change to info for debugging channel updates
118 logger.debug("Channel {} {}", allChannels.get(i), allStates.get(i));
123 * Test various Vehicles from users which delivered their fingerprint.
124 * The tests are checking the chain from "JSON to Channel update".
125 * Checks are done in an automated way cross checking the data from JSON and data delivered via Channel.
126 * Also important the updates are counted in order to check if code changes are affecting Channel Updates.
128 * With the given output the updated Channels are visible.
132 * Channel testbinding::test:status#lock Locked
133 * Channel testbinding::test:status#service-date 2023-11-01T00:00:00.000+0100
134 * Channel testbinding::test:status#check-control No Issues
135 * Channel testbinding::test:status#last-update 2021-12-21T16:46:02.000+0100
136 * Channel testbinding::test:status#doors Closed
137 * Channel testbinding::test:status#windows Closed
138 * Channel testbinding::test:status#plug-connection Not connected
139 * Channel testbinding::test:status#charge Not Charging
140 * Channel testbinding::test:status#charge-type Not Available
141 * Channel testbinding::test:range#electric 76 km
142 * Channel testbinding::test:range#radius-electric 60.800000000000004 km
143 * Channel testbinding::test:range#fuel 31 km
144 * Channel testbinding::test:range#radius-fuel 24.8 km
145 * Channel testbinding::test:range#hybrid 31 km
146 * Channel testbinding::test:range#radius-hybrid 24.8 km
147 * Channel testbinding::test:range#mileage 31537 km
148 * Channel testbinding::test:range#soc 74 %
149 * Channel testbinding::test:range#remaining-fuel 4 l
150 * Channel testbinding::test:doors#driver-front Closed
151 * Channel testbinding::test:doors#driver-rear Closed
152 * Channel testbinding::test:doors#passenger-front Closed
153 * Channel testbinding::test:doors#passenger-rear Closed
154 * Channel testbinding::test:doors#trunk Closed
155 * Channel testbinding::test:doors#hood Closed
156 * Channel testbinding::test:doors#win-driver-front Closed
157 * Channel testbinding::test:doors#win-driver-rear Undef
158 * Channel testbinding::test:doors#win-passenger-front Closed
159 * Channel testbinding::test:doors#win-passenger-rear Undef
160 * Channel testbinding::test:doors#sunroof Closed
161 * Channel testbinding::test:location#gps 1.2345,6.789
162 * Channel testbinding::test:location#heading 222 °
163 * Channel testbinding::test:service#name Brake Fluid
164 * Channel testbinding::test:service#date 2023-11-01T00:00:00.000+0100
165 * Channel testbinding::test:profile#prefs Chargingwindow
166 * Channel testbinding::test:profile#mode Immediatecharging
167 * Channel testbinding::test:profile#control Weeklyplanner
168 * Channel testbinding::test:profile#target 100
169 * Channel testbinding::test:profile#limit OFF
170 * Channel testbinding::test:profile#climate OFF
171 * Channel testbinding::test:profile#window-start 1970-01-01T11:00:00.000+0100
172 * Channel testbinding::test:profile#window-end 1970-01-01T14:30:00.000+0100
173 * Channel testbinding::test:profile#timer1-departure 1970-01-01T16:00:00.000+0100
174 * Channel testbinding::test:profile#timer1-enabled OFF
175 * Channel testbinding::test:profile#timer1-day-mon ON
176 * Channel testbinding::test:profile#timer1-day-tue ON
177 * Channel testbinding::test:profile#timer1-day-wed ON
178 * Channel testbinding::test:profile#timer1-day-thu ON
179 * Channel testbinding::test:profile#timer1-day-fri ON
180 * Channel testbinding::test:profile#timer1-day-sat ON
181 * Channel testbinding::test:profile#timer1-day-sun ON
182 * Channel testbinding::test:profile#timer2-departure 1970-01-01T12:02:00.000+0100
183 * Channel testbinding::test:profile#timer2-enabled ON
184 * Channel testbinding::test:profile#timer2-day-mon OFF
185 * Channel testbinding::test:profile#timer2-day-tue OFF
186 * Channel testbinding::test:profile#timer2-day-wed OFF
187 * Channel testbinding::test:profile#timer2-day-thu OFF
188 * Channel testbinding::test:profile#timer2-day-fri OFF
189 * Channel testbinding::test:profile#timer2-day-sat OFF
190 * Channel testbinding::test:profile#timer2-day-sun ON
191 * Channel testbinding::test:profile#timer3-departure 1970-01-01T13:03:00.000+0100
192 * Channel testbinding::test:profile#timer3-enabled OFF
193 * Channel testbinding::test:profile#timer3-day-mon OFF
194 * Channel testbinding::test:profile#timer3-day-tue OFF
195 * Channel testbinding::test:profile#timer3-day-wed OFF
196 * Channel testbinding::test:profile#timer3-day-thu OFF
197 * Channel testbinding::test:profile#timer3-day-fri OFF
198 * Channel testbinding::test:profile#timer3-day-sat ON
199 * Channel testbinding::test:profile#timer3-day-sun OFF
200 * Channel testbinding::test:profile#timer4-departure 1970-01-01T12:02:00.000+0100
201 * Channel testbinding::test:profile#timer4-enabled OFF
202 * Channel testbinding::test:profile#timer4-day-mon OFF
203 * Channel testbinding::test:profile#timer4-day-tue OFF
204 * Channel testbinding::test:profile#timer4-day-wed OFF
205 * Channel testbinding::test:profile#timer4-day-thu OFF
206 * Channel testbinding::test:profile#timer4-day-fri OFF
207 * Channel testbinding::test:profile#timer4-day-sat OFF
208 * Channel testbinding::test:profile#timer4-day-sun ON
211 public void testI01Rex() {
212 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
213 setup(VehicleType.ELECTRIC_REX.toString(), Constants.ANONYMOUS);
214 String content = FileReader.readFileInString("src/test/resources/responses/I01_REX/vehicles.json");
215 assertTrue(testVehicle(content, STATUS_ELECTRIC + RANGE_HYBRID + DOORS + LOCATION + SERVICE_AVAILABLE
216 + CHECK_EMPTY + CHARGE_PROFILE + TIRES, Optional.empty()));
220 public void testF11() {
221 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
222 setup(VehicleType.CONVENTIONAL.toString(), "some_vin_F11");
223 String content = FileReader.readFileInString("src/test/resources/responses/F11/vehicles_v2_bmw_0.json");
224 assertTrue(testVehicle(content,
225 STATUS_CONV + DOORS + RANGE_CONV + SERVICE_AVAILABLE + CHECK_EMPTY + LOCATION + TIRES,
230 public void testF31() {
231 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
232 setup(VehicleType.CONVENTIONAL.toString(), "some_vin_F31");
233 String content = FileReader.readFileInString("src/test/resources/responses/F31/vehicles_v2_bmw_0.json");
234 assertTrue(testVehicle(content,
235 STATUS_CONV + DOORS + RANGE_CONV + SERVICE_AVAILABLE + CHECK_EMPTY + LOCATION + TIRES,
240 public void testF44() {
241 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
242 setup(VehicleType.CONVENTIONAL.toString(), "some_vin_F44");
243 String content = FileReader.readFileInString("src/test/resources/responses/F44/vehicles_v2_bmw_0.json");
244 assertTrue(testVehicle(content,
245 STATUS_CONV + DOORS + RANGE_CONV + LOCATION + SERVICE_EMPTY + CHECK_EMPTY + TIRES, Optional.empty()));
249 public void testF45() {
250 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
251 setup(VehicleType.PLUGIN_HYBRID.toString(), "some_vin_F45");
252 String content = FileReader.readFileInString("src/test/resources/responses/F45/vehicles_v2_bmw_0.json");
253 assertTrue(testVehicle(content, STATUS_ELECTRIC + DOORS + RANGE_HYBRID + SERVICE_AVAILABLE + CHECK_EMPTY
254 + LOCATION + CHARGE_PROFILE + TIRES, Optional.empty()));
258 public void testF48() {
259 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
260 setup(VehicleType.CONVENTIONAL.toString(), "some_vin_F48");
261 String content = FileReader.readFileInString("src/test/resources/responses/F48/vehicles_v2_bmw_0.json");
262 assertTrue(testVehicle(content,
263 STATUS_CONV + DOORS + RANGE_CONV + SERVICE_AVAILABLE + CHECK_AVAILABLE + LOCATION + TIRES,
268 public void testG01() {
269 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
270 setup(VehicleType.PLUGIN_HYBRID.toString(), "some_vin_G01");
271 String content = FileReader.readFileInString("src/test/resources/responses/G01/vehicles_v2_bmw_0.json");
272 assertTrue(testVehicle(content, STATUS_ELECTRIC + DOORS + RANGE_HYBRID + SERVICE_AVAILABLE + CHECK_EMPTY
273 + LOCATION + CHARGE_PROFILE + TIRES, Optional.empty()));
277 public void testG05() {
278 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
279 setup(VehicleType.PLUGIN_HYBRID.toString(), "some_vin_G05");
280 String content = FileReader.readFileInString("src/test/resources/responses/G05/vehicles_v2_bmw_0.json");
281 assertTrue(testVehicle(content, STATUS_ELECTRIC + DOORS + RANGE_HYBRID + SERVICE_AVAILABLE + CHECK_EMPTY
282 + LOCATION + CHARGE_PROFILE + TIRES, Optional.empty()));
286 public void testG08() {
287 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
288 setup(VehicleType.ELECTRIC.toString(), "some_vin_G08");
289 String content = FileReader.readFileInString("src/test/resources/responses/G08/vehicles_v2_bmw_0.json");
290 assertTrue(testVehicle(content, STATUS_ELECTRIC + DOORS + RANGE_ELECTRIC + SERVICE_AVAILABLE + CHECK_EMPTY
291 + LOCATION + CHARGE_PROFILE + TIRES, Optional.empty()));
295 public void testG21() {
296 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
297 setup(VehicleType.PLUGIN_HYBRID.toString(), "some_vin_G21");
298 String content = FileReader.readFileInString("src/test/resources/responses/G21/vehicles_v2_bmw_0.json");
299 assertTrue(testVehicle(content, STATUS_ELECTRIC + DOORS + RANGE_HYBRID + SERVICE_AVAILABLE + CHECK_EMPTY
300 + LOCATION + CHARGE_PROFILE + TIRES, Optional.empty()));
304 public void testG30() {
305 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
306 setup(VehicleType.PLUGIN_HYBRID.toString(), "some_vin_G30");
307 String content = FileReader.readFileInString("src/test/resources/responses/G30/vehicles_v2_bmw_0.json");
308 assertTrue(testVehicle(content, STATUS_ELECTRIC + DOORS + RANGE_HYBRID + SERVICE_AVAILABLE + CHECK_EMPTY
309 + LOCATION + CHARGE_PROFILE + TIRES, Optional.empty()));
313 public void testI01NoRex() {
314 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
315 setup(VehicleType.ELECTRIC.toString(), "some_vin_I01_NOREX");
316 String content = FileReader.readFileInString("src/test/resources/responses/I01_NOREX/vehicles_v2_bmw_0.json");
317 assertTrue(testVehicle(content, STATUS_ELECTRIC + DOORS + RANGE_ELECTRIC + SERVICE_AVAILABLE + CHECK_EMPTY
318 + LOCATION + CHARGE_PROFILE + TIRES, Optional.empty()));
322 public void test530e() {
323 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
324 setup(VehicleType.PLUGIN_HYBRID.toString(), "anonymous");
325 String content = FileReader.readFileInString("src/test/resources/responses/530e/vehicles.json");
326 assertTrue(testVehicle(content, STATUS_ELECTRIC + DOORS + RANGE_HYBRID + SERVICE_AVAILABLE + CHECK_EMPTY
327 + LOCATION + CHARGE_PROFILE + TIRES, Optional.empty()));
331 public void test340i() {
332 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
333 setup(VehicleType.MILD_HYBRID.toString(), "anonymous");
334 String content = FileReader.readFileInString("src/test/resources/responses/G21/340i.json");
335 assertTrue(testVehicle(content, 38, Optional.empty()));
336 // assertTrue(testVehicle(content,
337 // STATUS_CONV + DOORS + RANGE_CONV + SERVICE_AVAILABLE + CHECK_EMPTY + LOCATION + TIRES,
338 // Optional.empty()));