]> git.basschouten.com Git - openhab-addons.git/blob
0770483b5240def6fa826a78032c04f8e7ba7df2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.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;
38
39 /**
40  * The {@link ChargeStatisticsTest} is responsible for handling commands, which are
41  * sent to one of the channels.
42  *
43  * @author Bernd Weymann - Initial contribution
44  */
45 @NonNullByDefault
46 @SuppressWarnings("null")
47 public class ChargeStatisticsTest {
48     private final Logger logger = LoggerFactory.getLogger(VehicleHandler.class);
49
50     private static final int EXPECTED_UPDATE_COUNT = 3;
51
52     @Nullable
53     ArgumentCaptor<ChannelUID> channelCaptor;
54     @Nullable
55     ArgumentCaptor<State> stateCaptor;
56     @Nullable
57     ThingHandlerCallback tc;
58     @Nullable
59     VehicleHandler cch;
60     @Nullable
61     List<ChannelUID> allChannels;
62     @Nullable
63     List<State> allStates;
64     String driveTrain = Constants.EMPTY;
65     boolean imperial;
66
67     /**
68      * Prepare environment for Vehicle Status Updates
69      */
70     public void setup(String type, boolean imperial) {
71         driveTrain = type;
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);
82         cch.setCallback(tc);
83         channelCaptor = ArgumentCaptor.forClass(ChannelUID.class);
84         stateCaptor = ArgumentCaptor.forClass(State.class);
85     }
86
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();
94
95         assertNotNull(driveTrain);
96         ChargeStatisticWrapper checker = new ChargeStatisticWrapper(statusContent);
97         trace();
98         return checker.checkResults(allChannels, allStates);
99     }
100
101     private void trace() {
102         for (int i = 0; i < allChannels.size(); i++) {
103             logger.info("Channel {} {}", allChannels.get(i), allStates.get(i));
104         }
105     }
106
107     @Test
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()));
113     }
114
115     @Test
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()));
121     }
122
123     @Test
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()));
129     }
130
131     @Test
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()));
138     }
139 }