2 * Copyright (c) 2010-2023 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.windcentrale.internal.dto;
15 import static org.hamcrest.CoreMatchers.notNullValue;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.core.Is.is;
19 import java.io.IOException;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
25 * Tests (de)serialization of AWS Cognito requests/responses to/from JSON.
27 * @author Wouter Born - Initial contribution
30 public class CognitoGsonTest {
32 private static final DataUtil DATA_UTIL = new DataUtil(CognitoGson.GSON);
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")));
41 public void deserializeChallengeResponseSrp() throws IOException {
42 ChallengeResponse response = DATA_UTIL.fromJson("challenge-response-srp.json", ChallengeResponse.class);
43 assertThat(response, is(notNullValue()));
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"));
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")));
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()));
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"));
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")));
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()));
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"));
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()));
97 assertThat(response.type, is("InvalidParameterException"));
98 assertThat(response.message, is("Missing required parameter REFRESH_TOKEN"));
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()));
106 assertThat(response.type, is("NotAuthorizedException"));
107 assertThat(response.message, is("Incorrect username or password."));