2 * Copyright (c) 2010-2021 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.bmwconnecteddrive.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.bmwconnecteddrive.internal.ConnectedDriveConstants.VehicleType;
27 import org.openhab.binding.bmwconnecteddrive.internal.dto.LifetimeWrapper;
28 import org.openhab.binding.bmwconnecteddrive.internal.util.FileReader;
29 import org.openhab.binding.bmwconnecteddrive.internal.utils.Constants;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.ThingHandlerCallback;
34 import org.openhab.core.types.State;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link AllTripTests} is responsible for handling commands, which are
40 * sent to one of the channels.
42 * @author Bernd Weymann - Initial contribution
45 @SuppressWarnings("null")
46 public class AllTripTests {
47 private final Logger logger = LoggerFactory.getLogger(VehicleHandler.class);
49 private static final int HYBRID_CALL_TIMES = 6;
52 ArgumentCaptor<ChannelUID> channelCaptor;
54 ArgumentCaptor<State> stateCaptor;
56 ThingHandlerCallback tc;
60 List<ChannelUID> allChannels;
62 List<State> allStates;
63 String driveTrain = Constants.EMPTY;
67 * Prepare environment for Vehicle Status Updates
69 public void setup(String type, boolean imperial) {
71 this.imperial = imperial;
72 Thing thing = mock(Thing.class);
73 when(thing.getUID()).thenReturn(new ThingUID("testbinding", "test"));
74 BMWConnectedDriveOptionProvider op = mock(BMWConnectedDriveOptionProvider.class);
75 cch = new VehicleHandler(thing, op, type, imperial);
76 tc = mock(ThingHandlerCallback.class);
78 channelCaptor = ArgumentCaptor.forClass(ChannelUID.class);
79 stateCaptor = ArgumentCaptor.forClass(State.class);
82 private boolean testTrip(String statusContent, int callbacksExpected, Optional<Map<String, State>> concreteChecks) {
83 assertNotNull(statusContent);
84 cch.allTripsCallback.onResponse(statusContent);
85 verify(tc, times(callbacksExpected)).stateUpdated(channelCaptor.capture(), stateCaptor.capture());
86 allChannels = channelCaptor.getAllValues();
87 allStates = stateCaptor.getAllValues();
89 assertNotNull(driveTrain);
90 LifetimeWrapper checker = new LifetimeWrapper(driveTrain, imperial, statusContent);
92 if (concreteChecks.isPresent()) {
93 return checker.append(concreteChecks.get()).checkResults(allChannels, allStates);
95 return checker.checkResults(allChannels, allStates);
99 private void trace() {
100 for (int i = 0; i < allChannels.size(); i++) {
101 logger.info("Channel {} {}", allChannels.get(i), allStates.get(i));
106 public void testi3Rex() {
107 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
108 setup(VehicleType.ELECTRIC_REX.toString(), false);
109 String content = FileReader.readFileInString("src/test/resources/responses/I01_REX/all-trips.json");
110 assertTrue(testTrip(content, HYBRID_CALL_TIMES, Optional.empty()));
114 public void test530E() {
115 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
116 setup(VehicleType.PLUGIN_HYBRID.toString(), false);
117 String content = FileReader.readFileInString("src/test/resources/responses/530E/all-trips.json");
118 assertTrue(testTrip(content, HYBRID_CALL_TIMES, Optional.empty()));
122 public void testi3RexImperial() {
123 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
124 setup(VehicleType.ELECTRIC_REX.toString(), true);
125 String content = FileReader.readFileInString("src/test/resources/responses/I01_REX/all-trips.json");
126 assertTrue(testTrip(content, HYBRID_CALL_TIMES, Optional.empty()));
130 public void test530EImperial() {
131 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
132 setup(VehicleType.PLUGIN_HYBRID.toString(), true);
133 String content = FileReader.readFileInString("src/test/resources/responses/530E/all-trips.json");
134 assertTrue(testTrip(content, HYBRID_CALL_TIMES, Optional.empty()));