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.assertEquals;
16 import static org.mockito.Mockito.*;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.bmwconnecteddrive.internal.ConnectedDriveConfiguration;
24 import org.openhab.binding.bmwconnecteddrive.internal.utils.BimmerConstants;
25 import org.openhab.binding.bmwconnecteddrive.internal.utils.HTTPConstants;
26 import org.openhab.core.io.net.http.HttpClientFactory;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link AuthTest} is responsible for handling commands, which are
32 * sent to one of the channels.
34 * @author Bernd Weymann - Initial contribution
37 public class AuthTest {
38 private final Logger logger = LoggerFactory.getLogger(VehicleHandler.class);
41 public void testAuthServerMap() {
42 Map<String, String> authServers = BimmerConstants.AUTH_SERVER_MAP;
43 assertEquals(3, authServers.size(), "Number of Servers");
44 Map<String, String> api = BimmerConstants.SERVER_MAP;
45 assertEquals(3, api.size(), "Number of Servers");
49 public void testTokenDecoding() {
50 String headerValue = "https://www.bmw-connecteddrive.com/app/static/external-dispatch.html#access_token=SfXKgkEXeeFJkVqdD4XMmfUU224MRuyh&token_type=Bearer&expires_in=7199";
51 HttpClientFactory hcf = mock(HttpClientFactory.class);
52 when(hcf.getCommonHttpClient()).thenReturn(mock(HttpClient.class));
53 when(hcf.createHttpClient(HTTPConstants.AUTH_HTTP_CLIENT_NAME)).thenReturn(mock(HttpClient.class));
54 ConnectedDriveConfiguration config = new ConnectedDriveConfiguration();
55 config.region = BimmerConstants.REGION_ROW;
56 ConnectedDriveProxy dcp = new ConnectedDriveProxy(hcf, config);
57 dcp.tokenFromUrl(headerValue);
58 Token t = dcp.getToken();
59 assertEquals("Bearer SfXKgkEXeeFJkVqdD4XMmfUU224MRuyh", t.getBearerToken(), "Token");
62 public void testRealTokenUpdate() {
63 ConnectedDriveConfiguration config = new ConnectedDriveConfiguration();
64 config.region = BimmerConstants.REGION_ROW;
65 config.userName = "bla";
66 config.password = "blub";
67 HttpClientFactory hcf = mock(HttpClientFactory.class);
68 when(hcf.getCommonHttpClient()).thenReturn(mock(HttpClient.class));
69 when(hcf.createHttpClient(HTTPConstants.AUTH_HTTP_CLIENT_NAME)).thenReturn(mock(HttpClient.class));
70 ConnectedDriveProxy dcp = new ConnectedDriveProxy(hcf, config);
71 Token t = dcp.getToken();
72 logger.info("Token {}", t.getBearerToken());
73 logger.info("Expires {}", t.isExpired());