]> git.basschouten.com Git - openhab-addons.git/blob
d9fee9778d5cd6737a35f774fd58d0718e1577d3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.bmwconnecteddrive.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.bmwconnecteddrive.internal.ConnectedDriveConstants.VehicleType;
27 import org.openhab.binding.bmwconnecteddrive.internal.dto.TripWrapper;
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;
37
38 /**
39  * The {@link LastTripTests} is responsible for handling commands, which are
40  * sent to one of the channels.
41  *
42  * @author Bernd Weymann - Initial contribution
43  */
44 @NonNullByDefault
45 @SuppressWarnings("null")
46 public class LastTripTests {
47     private final Logger logger = LoggerFactory.getLogger(VehicleHandler.class);
48
49     private static final int HYBRID_CALL_TIMES = 6;
50
51     @Nullable
52     ArgumentCaptor<ChannelUID> channelCaptor;
53     @Nullable
54     ArgumentCaptor<State> stateCaptor;
55     @Nullable
56     ThingHandlerCallback tc;
57     @Nullable
58     VehicleHandler cch;
59     @Nullable
60     List<ChannelUID> allChannels;
61     @Nullable
62     List<State> allStates;
63     String driveTrain = Constants.EMPTY;
64     boolean imperial;
65
66     /**
67      * Prepare environment for Vehicle Status Updates
68      */
69     public void setup(String type, boolean imperial) {
70         driveTrain = type;
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);
77         cch.setCallback(tc);
78         channelCaptor = ArgumentCaptor.forClass(ChannelUID.class);
79         stateCaptor = ArgumentCaptor.forClass(State.class);
80     }
81
82     private boolean testTrip(String statusContent, int callbacksExpected, Optional<Map<String, State>> concreteChecks) {
83         assertNotNull(statusContent);
84         cch.lastTripCallback.onResponse(statusContent);
85         verify(tc, times(callbacksExpected)).stateUpdated(channelCaptor.capture(), stateCaptor.capture());
86         allChannels = channelCaptor.getAllValues();
87         allStates = stateCaptor.getAllValues();
88
89         assertNotNull(driveTrain);
90         TripWrapper checker = new TripWrapper(driveTrain, imperial, statusContent);
91         trace();
92         if (concreteChecks.isPresent()) {
93             return checker.append(concreteChecks.get()).checkResults(allChannels, allStates);
94         } else {
95             return checker.checkResults(allChannels, allStates);
96         }
97     }
98
99     private void trace() {
100         for (int i = 0; i < allChannels.size(); i++) {
101             logger.info("Channel {} {}", allChannels.get(i), allStates.get(i));
102         }
103     }
104
105     @Test
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/last-trip.json");
110         assertTrue(testTrip(content, HYBRID_CALL_TIMES, Optional.empty()));
111     }
112
113     @Test
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/last-trip.json");
118         assertTrue(testTrip(content, HYBRID_CALL_TIMES, Optional.empty()));
119     }
120
121     @Test
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/last-trip.json");
126         assertTrue(testTrip(content, HYBRID_CALL_TIMES, Optional.empty()));
127     }
128
129     @Test
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/last-trip.json");
134         assertTrue(testTrip(content, HYBRID_CALL_TIMES, Optional.empty()));
135     }
136 }