|
| 1 | +/* |
| 2 | + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 11 | + * OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | + * License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.amazonaws.mobile.config; |
| 17 | + |
| 18 | + |
| 19 | +import com.amazonaws.auth.CognitoCredentialsProvider; |
| 20 | + |
| 21 | +import org.json.JSONException; |
| 22 | +import org.json.JSONObject; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import static org.junit.Assert.assertEquals; |
| 26 | +import static org.junit.Assert.assertNotNull; |
| 27 | +import static org.junit.Assert.fail; |
| 28 | + |
| 29 | +public class AWSConfigurationTest { |
| 30 | + |
| 31 | + private static final String jsonString = "{\n" + |
| 32 | + " \"UserAgent\": \"aws-amplify-cli/0.1.0\",\n" + |
| 33 | + " \"Version\": \"1.0\",\n" + |
| 34 | + " \"IdentityManager\": {\n" + |
| 35 | + " \"Default\": {}\n" + |
| 36 | + " },\n" + |
| 37 | + " \"AppSync\": {\n" + |
| 38 | + " \"Default\": {\n" + |
| 39 | + " \"ApiUrl\": \"redacted\",\n" + |
| 40 | + " \"Region\": \"us-east-1\",\n" + |
| 41 | + " \"AuthMode\": \"API_KEY\",\n" + |
| 42 | + " \"ApiKey\": \"da2-xyz\",\n" + |
| 43 | + " \"ClientDatabasePrefix\": \"redacted\"\n" + |
| 44 | + " }\n" + |
| 45 | + " },\n" + |
| 46 | + " \"CredentialsProvider\": {\n" + |
| 47 | + " \"CognitoIdentity\": {\n" + |
| 48 | + " \"Default\": {\n" + |
| 49 | + " \"PoolId\": \"redacted\",\n" + |
| 50 | + " \"Region\": \"us-east-1\"\n" + |
| 51 | + " }\n" + |
| 52 | + " }\n" + |
| 53 | + " }\n" + |
| 54 | + "}"; |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testAWSConfigurationWithJSONObject() { |
| 58 | + try { |
| 59 | + JSONObject jsonObject = new JSONObject(jsonString); |
| 60 | + |
| 61 | + AWSConfiguration awsConfiguration = new AWSConfiguration(jsonObject); |
| 62 | + assertNotNull(awsConfiguration); |
| 63 | + |
| 64 | + assertNotNull(awsConfiguration.optJsonObject("AppSync")); |
| 65 | + assertEquals("API_KEY", awsConfiguration.optJsonObject("AppSync").getString("AuthMode")); |
| 66 | + } catch (JSONException e) { |
| 67 | + fail("Error in constructing AWSConfiguration." + e.getLocalizedMessage()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testCognitoCachingCredentialsProviderWithAWSConfiguration() { |
| 73 | + try { |
| 74 | + JSONObject jsonObject = new JSONObject(jsonString); |
| 75 | + |
| 76 | + AWSConfiguration awsConfiguration = new AWSConfiguration(jsonObject); |
| 77 | + assertNotNull(awsConfiguration); |
| 78 | + CognitoCredentialsProvider cognitoCredentialsProvider = new CognitoCredentialsProvider(awsConfiguration); |
| 79 | + assertNotNull(cognitoCredentialsProvider); |
| 80 | + } catch (JSONException e) { |
| 81 | + fail("Error in constructing AWSConfiguration." + e.getLocalizedMessage()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testAWSConfigurationWithNullJSONObject() { |
| 87 | + try { |
| 88 | + JSONObject jsonObject = null; |
| 89 | + new AWSConfiguration(jsonObject); |
| 90 | + fail("No exception thrown when a null JSONObject is passed in."); |
| 91 | + } catch (IllegalArgumentException e) { |
| 92 | + assertEquals("JSONObject cannot be null.", e.getLocalizedMessage()); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments