Skip to content

Commit abce668

Browse files
author
Karthikeyan
authored
Add AWSConfiguration(JSONObject) constructor (#1002)
* Add AWSConfiguration(JSONObject) constructor * Add changelog entry for AWSConfiguration(JSONObject)
1 parent b78beb2 commit abce668

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change Log - AWS SDK for Android
22

3+
## [Release 2.13.6](https://github.com/aws/aws-sdk-android/releases/tag/release_v2.13.6)
4+
5+
### New Features
6+
7+
* **AWS Core Runtime**
8+
* Add `AWSConfiguration(JSONObject)` constructor to construct a `AWSConfiguration` object from the configuration passed via a `JSONObject`.
9+
310
## [Release 2.13.5](https://github.com/aws/aws-sdk-android/releases/tag/release_v2.13.5)
411

512
### Bug Fixes

aws-android-sdk-core/src/main/java/com/amazonaws/mobile/config/AWSConfiguration.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* express or implied. See the License for the specific language governing
1313
* permissions and limitations under the License.
1414
*/
15+
1516
package com.amazonaws.mobile.config;
1617

1718
import android.content.Context;
@@ -35,6 +36,31 @@ public class AWSConfiguration {
3536
private JSONObject mJSONObject;
3637
private String configName; // "Default" or something else like "Backup"
3738

39+
/**
40+
* Construct an AWSConfiguration object based on the JSONObject passed in.
41+
*
42+
* @param jsonObject contains the configuration information
43+
*/
44+
public AWSConfiguration(JSONObject jsonObject) {
45+
this(jsonObject, DEFAULT);
46+
}
47+
48+
/**
49+
* Construct an AWSConfiguration object based on the JSONObject passed in.
50+
*
51+
* @param jsonObject contains the configuration information
52+
* @param configName name of the configuration,
53+
* "Default" or something else like "Backup"
54+
*/
55+
public AWSConfiguration(JSONObject jsonObject, String configName) {
56+
if (jsonObject == null) {
57+
throw new IllegalArgumentException("JSONObject cannot be null.");
58+
}
59+
60+
this.configName = configName;
61+
this.mJSONObject = jsonObject;
62+
}
63+
3864
/**
3965
* Constructs an AWSConfiguration object
4066
*
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)