]> git.basschouten.com Git - openhab-addons.git/blob
38fb80f0990e4b54ffa8e33d23cf5182f49eebf5
[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.windcentrale.internal.dto;
14
15 import static org.hamcrest.CoreMatchers.notNullValue;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.core.Is.is;
18
19 import java.io.IOException;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23
24 /**
25  * Tests (de)serialization of AWS Cognito requests/responses to/from JSON.
26  *
27  * @author Wouter Born - Initial contribution
28  */
29 @NonNullByDefault
30 public class CognitoGsonTest {
31
32     private static final DataUtil DATA_UTIL = new DataUtil(CognitoGson.GSON);
33
34     @Test
35     public void serializeInitiateAuthRequestSrp() throws IOException {
36         String json = DATA_UTIL.toJson(InitiateAuthRequest.userSrpAuth("clientId123", "username456", "srpA789"));
37         assertThat(json, is(DATA_UTIL.fromFile("initiate-auth-request-srp.json")));
38     }
39
40     @Test
41     public void deserializeChallengeResponseSrp() throws IOException {
42         ChallengeResponse response = DATA_UTIL.fromJson("challenge-response-srp.json", ChallengeResponse.class);
43         assertThat(response, is(notNullValue()));
44
45         assertThat(response.challengeName, is("PASSWORD_VERIFIER"));
46         assertThat(response.getSalt(), is("salt123"));
47         assertThat(response.getSecretBlock(), is("secretBlock456"));
48         assertThat(response.getSrpB(), is("srpB789"));
49         assertThat(response.getUsername(), is("username@acme.com"));
50         assertThat(response.getUserIdForSrp(), is("userid@acme.com"));
51     }
52
53     @Test
54     public void serializeInitiateAuthRequestRefresh() throws IOException {
55         String json = DATA_UTIL.toJson(InitiateAuthRequest.refreshTokenAuth("clientId123", "refreshToken123"));
56         assertThat(json, is(DATA_UTIL.fromFile("initiate-auth-request-refresh.json")));
57     }
58
59     @Test
60     public void deserializeInitiateAuthResponseRefresh() throws IOException {
61         AuthenticationResultResponse response = DATA_UTIL.fromJson("authentication-result-response-refresh.json",
62                 AuthenticationResultResponse.class);
63         assertThat(response, is(notNullValue()));
64
65         assertThat(response.getAccessToken(), is("accessToken123"));
66         assertThat(response.getExpiresIn(), is(3600));
67         assertThat(response.getIdToken(), is("idToken456"));
68         assertThat(response.getRefreshToken(), is(""));
69         assertThat(response.getTokenType(), is("Bearer"));
70     }
71
72     @Test
73     public void serializeRespondToAuthChallengeRequest() throws IOException {
74         String json = DATA_UTIL.toJson(new RespondToAuthChallengeRequest("clientId123", "username@acme.com",
75                 "passwordClaimSecretBlock456", "passwordClaimSignature789", "Thu Apr 6 07:16:19 UTC 2023"));
76         assertThat(json, is(DATA_UTIL.fromFile("respond-to-auth-challenge-request.json")));
77     }
78
79     @Test
80     public void deserializeRespondToAuthChallengeResponse() throws IOException {
81         AuthenticationResultResponse response = DATA_UTIL.fromJson("authentication-result-response-challenge.json",
82                 AuthenticationResultResponse.class);
83         assertThat(response, is(notNullValue()));
84
85         assertThat(response.getAccessToken(), is("accessToken123"));
86         assertThat(response.getExpiresIn(), is(3600));
87         assertThat(response.getIdToken(), is("idToken456"));
88         assertThat(response.getRefreshToken(), is("refreshToken789"));
89         assertThat(response.getTokenType(), is("Bearer"));
90     }
91
92     @Test
93     public void deserializeErrorResponseInvalidParameter() throws IOException {
94         CognitoError response = DATA_UTIL.fromJson("cognito-error-response-invalid-parameter.json", CognitoError.class);
95         assertThat(response, is(notNullValue()));
96
97         assertThat(response.type, is("InvalidParameterException"));
98         assertThat(response.message, is("Missing required parameter REFRESH_TOKEN"));
99     }
100
101     @Test
102     public void deserializeErrorResponseNotAuthorized() throws IOException {
103         CognitoError response = DATA_UTIL.fromJson("cognito-error-response-not-authorized.json", CognitoError.class);
104         assertThat(response, is(notNullValue()));
105
106         assertThat(response.type, is("NotAuthorizedException"));
107         assertThat(response.message, is("Incorrect username or password."));
108     }
109 }