2 * Copyright (c) 2010-2023 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.i18n.LocationProvider;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.ThingHandlerCallback;
36 import org.openhab.core.types.State;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link ChargeStatisticsTest} is responsible for handling commands, which are
42 * sent to one of the channels.
44 * @author Bernd Weymann - Initial contribution
47 @SuppressWarnings("null")
48 public class ChargeStatisticsTest {
49 private final Logger logger = LoggerFactory.getLogger(VehicleHandler.class);
51 private static final int EXPECTED_UPDATE_COUNT = 3;
54 ArgumentCaptor<ChannelUID> channelCaptor;
56 ArgumentCaptor<State> stateCaptor;
58 ThingHandlerCallback tc;
62 List<ChannelUID> allChannels;
64 List<State> allStates;
65 String driveTrain = Constants.EMPTY;
69 * Prepare environment for Vehicle Status Updates
71 public void setup(String type, boolean imperial) {
73 this.imperial = imperial;
74 Thing thing = mock(Thing.class);
75 when(thing.getUID()).thenReturn(new ThingUID("testbinding", "test"));
76 MyBMWCommandOptionProvider cop = mock(MyBMWCommandOptionProvider.class);
77 LocationProvider locationProvider = mock(LocationProvider.class);
78 cch = new VehicleHandler(thing, cop, locationProvider, type);
79 VehicleConfiguration vc = new VehicleConfiguration();
80 vc.vin = Constants.ANONYMOUS;
81 Optional<VehicleConfiguration> ovc = Optional.of(vc);
82 cch.configuration = ovc;
83 tc = mock(ThingHandlerCallback.class);
85 channelCaptor = ArgumentCaptor.forClass(ChannelUID.class);
86 stateCaptor = ArgumentCaptor.forClass(State.class);
89 private boolean testVehicle(String statusContent, int callbacksExpected,
90 Optional<Map<String, State>> concreteChecks) {
91 assertNotNull(statusContent);
92 cch.chargeStatisticsCallback.onResponse(statusContent);
93 verify(tc, times(callbacksExpected)).stateUpdated(channelCaptor.capture(), stateCaptor.capture());
94 allChannels = channelCaptor.getAllValues();
95 allStates = stateCaptor.getAllValues();
97 assertNotNull(driveTrain);
98 ChargeStatisticWrapper checker = new ChargeStatisticWrapper(statusContent);
100 return checker.checkResults(allChannels, allStates);
103 private void trace() {
104 for (int i = 0; i < allChannels.size(); i++) {
105 logger.info("Channel {} {}", allChannels.get(i), allStates.get(i));
110 public void testI01REX() {
111 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
112 setup(VehicleType.ELECTRIC_REX.toString(), false);
113 String content = FileReader.readFileInString("src/test/resources/responses/I01_REX/charge-statistics-de.json");
114 assertTrue(testVehicle(content, EXPECTED_UPDATE_COUNT, Optional.empty()));
118 public void testG21() {
119 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
120 setup(VehicleType.PLUGIN_HYBRID.toString(), false);
121 String content = FileReader.readFileInString("src/test/resources/responses/G21/charging-statistics_0.json");
122 assertTrue(testVehicle(content, EXPECTED_UPDATE_COUNT, Optional.empty()));
126 public void testG30() {
127 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
128 setup(VehicleType.PLUGIN_HYBRID.toString(), false);
129 String content = FileReader.readFileInString("src/test/resources/responses/G30/charging-statistics_0.json");
130 assertTrue(testVehicle(content, EXPECTED_UPDATE_COUNT, Optional.empty()));
134 public void testI01NOREX() {
135 logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
136 setup(VehicleType.ELECTRIC.toString(), false);
137 String content = FileReader
138 .readFileInString("src/test/resources/responses/I01_NOREX/charging-statistics_0.json");
139 assertTrue(testVehicle(content, EXPECTED_UPDATE_COUNT, Optional.empty()));