]> git.basschouten.com Git - openhab-addons.git/blob
3a326ac36b1aceb471ffdf012bcf0b8bfbbb3b96
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.vesync.internal.handler.requests;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.Test;
22 import org.openhab.binding.vesync.internal.exceptions.AuthenticationException;
23 import org.openhab.binding.vesync.internal.VeSyncConstants;
24 import org.openhab.binding.vesync.internal.dto.requests.VesyncAuthenticatedRequest;
25 import org.openhab.binding.vesync.internal.dto.requests.VesyncLoginCredentials;
26 import org.openhab.binding.vesync.internal.dto.responses.VesyncLoginResponse;
27
28 /**
29  * The {@link VesyncLoginCredentials} class implements unit test case for {@link VesyncLoginResponse}
30  *
31  * @author David Goodyear - Initial contribution
32  */
33 @NonNullByDefault
34 public class VesyncAuthenticatedRequestTest {
35
36     public static final VesyncLoginResponse.VesyncUserSession testUser = VeSyncConstants.GSON.fromJson(
37             org.openhab.binding.vesync.internal.handler.responses.VesyncLoginResponseTest.testGoodLoginResponseBody,
38             VesyncLoginResponse.class).result;
39
40     @Test
41     public void checkBaseFieldsExist() {
42         String content = VeSyncConstants.GSON.toJson(new VesyncLoginCredentials("username", "passmd5"));
43
44         assertEquals(true, content.contains("\"timeZone\": \"America/New_York\""));
45         assertEquals(true, content.contains("\"acceptLanguage\": \"en\""));
46
47         assertEquals(true, content.contains("\"appVersion\": \"2.5.1\""));
48         assertEquals(true, content.contains("\"phoneBrand\": \"SM N9005\""));
49         assertEquals(true, content.contains("\"phoneOS\": \"Android\""));
50
51         Pattern p = Pattern.compile("\"traceId\": \"\\d+\"");
52         Matcher m = p.matcher(content);
53         assertEquals(true, m.find());
54     }
55
56     @Test
57     public void checkAuthenicationData() {
58
59         // Simulate as the code flow should run - parse data and then use it
60         VesyncLoginResponse response = VeSyncConstants.GSON
61                 .fromJson(org.openhab.binding.vesync.internal.handler.responses.VesyncLoginResponseTest.testGoodLoginResponseBody, VesyncLoginResponse.class);
62
63         String content = "";
64
65         try {
66             content = VeSyncConstants.GSON.toJson(new VesyncAuthenticatedRequest(response.result));
67         } catch (AuthenticationException ae) {
68
69         }
70
71         assertEquals(true, content.contains("\"token\": \"AccessTokenString=\""));
72         assertEquals(true, content.contains("\"accountID\": \"5328043\""));
73     }
74 }