]> git.basschouten.com Git - openhab-addons.git/blob
a1d4a8816110dc48cb0c8d046dcffca485ac4082
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.mybmw.internal.handler;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
17
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Optional;
21
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;
39
40 /**
41  * The {@link ChargeStatisticsTest} is responsible for handling commands, which are
42  * sent to one of the channels.
43  *
44  * @author Bernd Weymann - Initial contribution
45  */
46 @NonNullByDefault
47 @SuppressWarnings("null")
48 public class ChargeStatisticsTest {
49     private final Logger logger = LoggerFactory.getLogger(VehicleHandler.class);
50
51     private static final int EXPECTED_UPDATE_COUNT = 3;
52
53     @Nullable
54     ArgumentCaptor<ChannelUID> channelCaptor;
55     @Nullable
56     ArgumentCaptor<State> stateCaptor;
57     @Nullable
58     ThingHandlerCallback tc;
59     @Nullable
60     VehicleHandler cch;
61     @Nullable
62     List<ChannelUID> allChannels;
63     @Nullable
64     List<State> allStates;
65     String driveTrain = Constants.EMPTY;
66     boolean imperial;
67
68     /**
69      * Prepare environment for Vehicle Status Updates
70      */
71     public void setup(String type, boolean imperial) {
72         driveTrain = type;
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);
84         cch.setCallback(tc);
85         channelCaptor = ArgumentCaptor.forClass(ChannelUID.class);
86         stateCaptor = ArgumentCaptor.forClass(State.class);
87     }
88
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();
96
97         assertNotNull(driveTrain);
98         ChargeStatisticWrapper checker = new ChargeStatisticWrapper(statusContent);
99         trace();
100         return checker.checkResults(allChannels, allStates);
101     }
102
103     private void trace() {
104         for (int i = 0; i < allChannels.size(); i++) {
105             logger.info("Channel {} {}", allChannels.get(i), allStates.get(i));
106         }
107     }
108
109     @Test
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()));
115     }
116
117     @Test
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()));
123     }
124
125     @Test
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()));
131     }
132
133     @Test
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()));
140     }
141 }