2 * Copyright (c) 2010-2021 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.bmwconnecteddrive.internal.handler;
15 import static org.junit.jupiter.api.Assertions.assertNotNull;
16 import static org.mockito.Mockito.*;
18 import java.util.List;
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;
36 * The {@link ChargeProfileTest} is responsible for handling commands, which are
37 * sent to one of the channels.
39 * @author Bernd Weymann - Initial contribution
42 @SuppressWarnings("null")
43 public class ChargeProfileTest {
44 private final Logger logger = LoggerFactory.getLogger(VehicleHandler.class);
46 private static final int PROFILE_CALLBACK_NUMBER = 37;
49 ArgumentCaptor<ChannelUID> channelCaptor;
51 ArgumentCaptor<State> stateCaptor;
53 ThingHandlerCallback tc;
57 List<ChannelUID> allChannels;
59 List<State> allStates;
60 String driveTrain = Constants.EMPTY;
64 * Prepare environment for Vehicle Status Updates
66 public void setup(String type, boolean imperial) {
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);
75 channelCaptor = ArgumentCaptor.forClass(ChannelUID.class);
76 stateCaptor = ArgumentCaptor.forClass(State.class);
79 private boolean testProfile(String statusContent, int callbacksExpected) {
80 assertNotNull(statusContent);
82 cch.chargeProfileCallback.onResponse(statusContent);
83 verify(tc, times(callbacksExpected)).stateUpdated(channelCaptor.capture(), stateCaptor.capture());
84 allChannels = channelCaptor.getAllValues();
85 allStates = stateCaptor.getAllValues();
87 assertNotNull(driveTrain);
92 private void trace() {
93 for (int i = 0; i < allChannels.size(); i++) {
94 logger.info("Channel {} {}", allChannels.get(i), allStates.get(i));
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
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);