]> git.basschouten.com Git - openhab-addons.git/blob
3b34f58610c057b5d73baf4e2c355ce1d14d32b4
[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.assertEquals;
16 import static org.mockito.Mockito.*;
17
18 import java.util.Map;
19
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;
29
30 /**
31  * The {@link AuthTest} is responsible for handling commands, which are
32  * sent to one of the channels.
33  *
34  * @author Bernd Weymann - Initial contribution
35  */
36 @NonNullByDefault
37 public class AuthTest {
38     private final Logger logger = LoggerFactory.getLogger(VehicleHandler.class);
39
40     @Test
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");
46     }
47
48     @Test
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");
60     }
61
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());
74     }
75 }