forked from openhab/openhab-addons
/
MyBMWProxyBackendIT.java
231 lines (192 loc) · 8.48 KB
/
MyBMWProxyBackendIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.mybmw.internal.handler.backend;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openhab.binding.mybmw.internal.MyBMWBridgeConfiguration;
import org.openhab.binding.mybmw.internal.dto.charge.ChargingSessionsContainer;
import org.openhab.binding.mybmw.internal.dto.charge.ChargingStatisticsContainer;
import org.openhab.binding.mybmw.internal.dto.remote.ExecutionStatusContainer;
import org.openhab.binding.mybmw.internal.dto.vehicle.Vehicle;
import org.openhab.binding.mybmw.internal.dto.vehicle.VehicleBase;
import org.openhab.binding.mybmw.internal.dto.vehicle.VehicleStateContainer;
import org.openhab.binding.mybmw.internal.handler.enums.ExecutionState;
import org.openhab.binding.mybmw.internal.handler.enums.RemoteService;
import org.openhab.binding.mybmw.internal.utils.BimmerConstants;
import org.openhab.binding.mybmw.internal.utils.ImageProperties;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import ch.qos.logback.classic.Level;
/**
* this integration test runs only if the connected account is set via environment variables
* CONNECTED_USER
* CONNECTED_PASSWORD
*
* if you want to execute the tests, please set the env variables and remove the disabled annotation
*
* @author Martin Grassl - initial contribution
*/
@NonNullByDefault
public class MyBMWProxyBackendIT {
private final Logger logger = LoggerFactory.getLogger(MyBMWProxyBackendIT.class);
public MyBMWHttpProxy initializeProxy() {
String connectedUser = System.getenv("CONNECTED_USER");
String connectedPassword = System.getenv("CONNECTED_PASSWORD");
assertNotNull(connectedUser);
assertNotNull(connectedPassword);
MyBMWBridgeConfiguration configuration = new MyBMWBridgeConfiguration();
configuration.language = "en";
configuration.region = BimmerConstants.REGION_ROW;
configuration.userName = connectedUser;
configuration.password = connectedPassword;
return new MyBMWHttpProxy(new MyHttpClientFactory(), configuration);
}
@BeforeEach
public void setupLogger() {
Logger root = LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
((ch.qos.logback.classic.Logger) root).setLevel(Level.DEBUG);
logger.trace("tracing enabled");
logger.debug("debugging enabled");
logger.info("info enabled");
}
@Test
public void testSequence() {
MyBMWHttpProxy myBMWProxy = initializeProxy();
// get list of vehicles
List<@NonNull VehicleBase> vehicles = null;
try {
vehicles = myBMWProxy.requestVehiclesBase();
} catch (NetworkException e) {
fail(e.getReason(), e);
}
assertNotNull(vehicles);
assertEquals(2, vehicles.size());
for (VehicleBase vehicleBase : vehicles) {
assertNotNull(vehicleBase.getVin());
assertNotNull(vehicleBase.getAttributes().getBrand());
// get image
try {
byte[] bmwImage = myBMWProxy.requestImage(vehicleBase.getVin(), vehicleBase.getAttributes().getBrand(),
new ImageProperties());
assertNotNull(bmwImage);
} catch (NetworkException e) {
fail(e.getReason(), e);
}
// get state
VehicleStateContainer vehicleState = null;
try {
vehicleState = myBMWProxy.requestVehicleState(vehicleBase.getVin(),
vehicleBase.getAttributes().getBrand());
} catch (NetworkException e) {
fail(e.getReason(), e);
}
assertNotNull(vehicleState);
// get charge statistics -> only successful for electric vehicles
ChargingStatisticsContainer chargeStatisticsContainer = null;
try {
chargeStatisticsContainer = myBMWProxy.requestChargeStatistics(vehicleBase.getVin(),
vehicleBase.getAttributes().getBrand());
assertNotNull(chargeStatisticsContainer);
} catch (NetworkException e) {
logger.trace("error: {}", e.toString());
}
ChargingSessionsContainer chargeSessionsContainer = null;
try {
chargeSessionsContainer = myBMWProxy.requestChargeSessions(vehicleBase.getVin(),
vehicleBase.getAttributes().getBrand());
assertNotNull(chargeSessionsContainer);
} catch (NetworkException e) {
logger.trace("error: {}", e.toString());
}
ExecutionStatusContainer remoteExecutionResponse = null;
try {
remoteExecutionResponse = myBMWProxy.executeRemoteServiceCall(vehicleBase.getVin(),
vehicleBase.getAttributes().getBrand(), RemoteService.LIGHT_FLASH);
} catch (NetworkException e) {
fail(e.getReason(), e);
}
assertNotNull(remoteExecutionResponse);
logger.warn("{}", remoteExecutionResponse.toString());
ExecutionStatusContainer remoteExecutionStatusResponse = null;
try {
remoteExecutionStatusResponse = myBMWProxy.executeRemoteServiceStatusCall(
vehicleBase.getAttributes().getBrand(), remoteExecutionResponse.getEventId());
assertNotNull(remoteExecutionStatusResponse);
logger.warn("{}", remoteExecutionStatusResponse.toString());
int counter = 0;
while (!ExecutionState.EXECUTED.toString().equals(remoteExecutionStatusResponse.getEventStatus())
&& counter++ < 10) {
remoteExecutionStatusResponse = myBMWProxy.executeRemoteServiceStatusCall(
vehicleBase.getAttributes().getBrand(), remoteExecutionResponse.getEventId());
logger.warn("{}", remoteExecutionStatusResponse.toString());
Thread.sleep(5000);
}
} catch (NetworkException e) {
fail(e.getReason(), e);
} catch (InterruptedException e) {
fail(e.getMessage(), e);
}
}
}
@Test
@Disabled
public void testGetVehicles() {
MyBMWHttpProxy myBMWProxy = initializeProxy();
try {
List<@NonNull Vehicle> vehicles = myBMWProxy.requestVehicles();
logger.warn(ResponseContentAnonymizer.anonymizeResponseContent(new Gson().toJson(vehicles)));
assertNotNull(vehicles);
assertEquals(2, vehicles.size());
} catch (NetworkException e) {
fail(e.getReason(), e);
}
}
}
/**
* @author Martin Grassl - initial contribution
*/
@NonNullByDefault
class MyHttpClientFactory implements HttpClientFactory {
private final Logger logger = LoggerFactory.getLogger(MyHttpClientFactory.class);
@Override
public HttpClient createHttpClient(String consumerName) {
// Instantiate and configure the SslContextFactory
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
// Instantiate HttpClient with the SslContextFactory
HttpClient httpClient = new HttpClient(sslContextFactory);
// Configure HttpClient, for example:
httpClient.setFollowRedirects(false);
// Start HttpClient
try {
httpClient.start();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return httpClient;
}
@Override
public HttpClient getCommonHttpClient() {
return createHttpClient("test");
}
}