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.ChargeStatisticWrapper;
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 ChargeStatisticsTest} 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 ChargeStatisticsTest {
48 private final Logger logger = LoggerFactory.getLogger(VehicleHandler.class);
50 private static final int EXPECTED_UPDATE_COUNT = 3;
53 ArgumentCaptor<ChannelUID> channelCaptor;
55 ArgumentCaptor<State> stateCaptor;
57 ThingHandlerCallback tc;
61 List<ChannelUID> allChannels;
63 List<State> allStates;
64 String driveTrain = Constants.EMPTY;
68 * Prepare environment for Vehicle Status Updates
70 public void setup(String type, boolean imperial) {
72 this.imperial = imperial;
73 Thing thing = mock(Thing.class);
74 when(thing.getUID()).thenReturn(new ThingUID("testbinding", "test"));
75 MyBMWCommandOptionProvider cop = mock(MyBMWCommandOptionProvider.class);
76 cch = new VehicleHandler(thing, cop, type);
77 VehicleConfiguration vc = new VehicleConfiguration();
78 vc.vin = Constants.ANONYMOUS;
79 Optional<VehicleConfiguration> ovc = Optional.of(vc);
80 cch.configuration = ovc;
81 tc = mock(ThingHandlerCallback.class);
83 channelCaptor = ArgumentCaptor.forClass(ChannelUID.class);
84 stateCaptor = ArgumentCaptor.forClass(State.class);
87 private boolean testVehicle(String statusContent, int callbacksExpected,
88 Optional<Map<String, State>> concreteChecks) {
89 assertNotNull(statusContent);
90 cch.chargeStatisticsCallback.onResponse(statusContent);
91 verify(tc, times(callbacksExpected)).stateUpdated(channelCaptor.capture(), stateCaptor.capture());
92 allChannels = channelCaptor.getAllValues();
93 allStates = stateCaptor.getAllValues();
95 assertNotNull(driveTrain);
96 ChargeStatisticWrapper checker = new ChargeStatisticWrapper(statusContent);
98 return checker.checkResults(allChannels, allStates);
101 private void trace() {
102 for (int i = 0; i < allChannels.size(); i++) {
103 logger.info("Channel {} {}", allChannels.get(i), allStates.get(i));
108 public void testI01REX() {
109 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
110 setup(VehicleType.ELECTRIC_REX.toString(), false);
111 String content = FileReader.readFileInString("src/test/resources/responses/I01_REX/charge-statistics-de.json");
112 assertTrue(testVehicle(content, EXPECTED_UPDATE_COUNT, Optional.empty()));
116 public void testG21() {
117 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
118 setup(VehicleType.PLUGIN_HYBRID.toString(), false);
119 String content = FileReader.readFileInString("src/test/resources/responses/G21/charging-statistics_0.json");
120 assertTrue(testVehicle(content, EXPECTED_UPDATE_COUNT, Optional.empty()));
124 public void testG30() {
125 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
126 setup(VehicleType.PLUGIN_HYBRID.toString(), false);
127 String content = FileReader.readFileInString("src/test/resources/responses/G30/charging-statistics_0.json");
128 assertTrue(testVehicle(content, EXPECTED_UPDATE_COUNT, Optional.empty()));
132 public void testI01NOREX() {
133 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
134 setup(VehicleType.ELECTRIC.toString(), false);
135 String content = FileReader
136 .readFileInString("src/test/resources/responses/I01_NOREX/charging-statistics_0.json");
137 assertTrue(testVehicle(content, EXPECTED_UPDATE_COUNT, Optional.empty()));