]> git.basschouten.com Git - openhab-addons.git/blob
e4e60d4197d08698a08b6e5a71d5f14608748db9
[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.bmwconnecteddrive.internal.handler;
14
15 import static org.junit.jupiter.api.Assertions.assertNotNull;
16 import static org.mockito.Mockito.*;
17
18 import java.util.List;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.junit.jupiter.api.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.openhab.binding.bmwconnecteddrive.internal.ConnectedDriveConstants.VehicleType;
25 import org.openhab.binding.bmwconnecteddrive.internal.util.FileReader;
26 import org.openhab.binding.bmwconnecteddrive.internal.utils.Constants;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.thing.binding.ThingHandlerCallback;
31 import org.openhab.core.types.State;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * The {@link ChargeProfileTest} is responsible for handling commands, which are
37  * sent to one of the channels.
38  *
39  * @author Bernd Weymann - Initial contribution
40  */
41 @NonNullByDefault
42 @SuppressWarnings("null")
43 public class ChargeProfileTest {
44     private final Logger logger = LoggerFactory.getLogger(VehicleHandler.class);
45
46     private static final int PROFILE_CALLBACK_NUMBER = 37;
47
48     @Nullable
49     ArgumentCaptor<ChannelUID> channelCaptor;
50     @Nullable
51     ArgumentCaptor<State> stateCaptor;
52     @Nullable
53     ThingHandlerCallback tc;
54     @Nullable
55     VehicleHandler cch;
56     @Nullable
57     List<ChannelUID> allChannels;
58     @Nullable
59     List<State> allStates;
60     String driveTrain = Constants.EMPTY;
61     boolean imperial;
62
63     /**
64      * Prepare environment for Vehicle Status Updates
65      */
66     public void setup(String type, boolean imperial) {
67         driveTrain = type;
68         this.imperial = imperial;
69         Thing thing = mock(Thing.class);
70         when(thing.getUID()).thenReturn(new ThingUID("testbinding", "test"));
71         BMWConnectedDriveOptionProvider op = mock(BMWConnectedDriveOptionProvider.class);
72         cch = new VehicleHandler(thing, op, type, imperial);
73         tc = mock(ThingHandlerCallback.class);
74         cch.setCallback(tc);
75         channelCaptor = ArgumentCaptor.forClass(ChannelUID.class);
76         stateCaptor = ArgumentCaptor.forClass(State.class);
77     }
78
79     private boolean testProfile(String statusContent, int callbacksExpected) {
80         assertNotNull(statusContent);
81
82         cch.chargeProfileCallback.onResponse(statusContent);
83         verify(tc, times(callbacksExpected)).stateUpdated(channelCaptor.capture(), stateCaptor.capture());
84         allChannels = channelCaptor.getAllValues();
85         allStates = stateCaptor.getAllValues();
86
87         assertNotNull(driveTrain);
88         trace();
89         return true;
90     }
91
92     private void trace() {
93         for (int i = 0; i < allChannels.size(); i++) {
94             logger.info("Channel {} {}", allChannels.get(i), allStates.get(i));
95         }
96     }
97
98     /**
99      * Channel testbinding::test:charge#profile-climate ON
100      * Channel testbinding::test:charge#profile-mode IMMEDIATE_CHARGING
101      * Channel testbinding::test:charge#window-start 11:00
102      * Channel testbinding::test:charge#window-end 17:00
103      * Channel testbinding::test:charge#timer1-departure 05:00
104      * Channel testbinding::test:charge#timer1-enabled OFF
105      * Channel testbinding::test:charge#timer1-days MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY
106      * Channel testbinding::test:charge#timer2-departure 12:00
107      * Channel testbinding::test:charge#timer2-enabled ON
108      * Channel testbinding::test:charge#timer2-days SATURDAY
109      * Channel testbinding::test:charge#timer3-departure 00:00
110      * Channel testbinding::test:charge#timer3-enabled OFF
111      * Channel testbinding::test:charge#timer3-days
112      */
113     @Test
114     public void testChargingProfile() {
115         logger.info("{}", Thread.currentThread().getStackTrace()[1].getMethodName());
116         setup(VehicleType.ELECTRIC_REX.toString(), false);
117         String content = FileReader.readFileInString("src/test/resources/webapi/charging-profile.json");
118         testProfile(content, PROFILE_CALLBACK_NUMBER);
119     }
120 }