From a32d1bc8fb10f1d81f18222d41da43f9b5dc462a Mon Sep 17 00:00:00 2001 From: Matt Creaser Date: Fri, 12 Apr 2024 11:13:26 -0300 Subject: [PATCH] Convert configuration reader test to Kotlin and add tests for parsing amplify outputs --- .../aws/AWSApiPluginConfigurationReader.java | 5 +- .../AWSApiPluginConfigurationReaderTest.java | 81 ------------ .../AWSApiPluginConfigurationReaderTest.kt | 119 ++++++++++++++++++ .../AmplifyOutputsDataBuilder.kt | 14 +++ 4 files changed, 136 insertions(+), 83 deletions(-) delete mode 100644 aws-api/src/test/java/com/amplifyframework/api/aws/AWSApiPluginConfigurationReaderTest.java create mode 100644 aws-api/src/test/java/com/amplifyframework/api/aws/AWSApiPluginConfigurationReaderTest.kt diff --git a/aws-api/src/main/java/com/amplifyframework/api/aws/AWSApiPluginConfigurationReader.java b/aws-api/src/main/java/com/amplifyframework/api/aws/AWSApiPluginConfigurationReader.java index c5190e3b90..6ab15ae4d9 100644 --- a/aws-api/src/main/java/com/amplifyframework/api/aws/AWSApiPluginConfigurationReader.java +++ b/aws-api/src/main/java/com/amplifyframework/api/aws/AWSApiPluginConfigurationReader.java @@ -35,7 +35,7 @@ * associated key. */ final class AWSApiPluginConfigurationReader { - private static final String GEN2_API_NAME = "default"; + static final String GEN2_API_NAME = "default"; private AWSApiPluginConfigurationReader() { /* no instances */ } @@ -100,8 +100,9 @@ private static AuthorizationType getAuthorizationType(AmplifyOutputsData.AwsApps return AuthorizationType.AWS_LAMBDA; case OPENID_CONNECT: return AuthorizationType.OPENID_CONNECT; + default: + return AuthorizationType.NONE; } - return AuthorizationType.NONE; } private static AWSApiPluginConfiguration parseConfigurationJson(JSONObject configurationJson) diff --git a/aws-api/src/test/java/com/amplifyframework/api/aws/AWSApiPluginConfigurationReaderTest.java b/aws-api/src/test/java/com/amplifyframework/api/aws/AWSApiPluginConfigurationReaderTest.java deleted file mode 100644 index cd8aaf605e..0000000000 --- a/aws-api/src/test/java/com/amplifyframework/api/aws/AWSApiPluginConfigurationReaderTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - -package com.amplifyframework.api.aws; - -import com.amplifyframework.api.ApiException; -import com.amplifyframework.testutils.Resources; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** - * Tests the {@link AWSApiPluginConfigurationReader} JSON parser utility. - */ -@RunWith(RobolectricTestRunner.class) -public final class AWSApiPluginConfigurationReaderTest { - - /** - * An attempt to read a null JSON object should result in a - * configuration exception. - * @throws ApiException From API configuration - */ - @Test(expected = ApiException.class) - public void readFromNullJsonObjectThrowsConfigurationException() throws ApiException { - AWSApiPluginConfigurationReader.readFrom(null); - } - - /** - * An attempt to read from an empty JSON object should result in a configuration - * exception. - * @throws JSONException On failure to arrange test inputs - * @throws ApiException From API configuration - */ - @Test(expected = ApiException.class) - public void readFromApiWithNoSpecThrowsConfigurationException() throws JSONException, ApiException { - final JSONObject emptyApiSpec = new JSONObject().put("api1", new JSONObject()); - AWSApiPluginConfigurationReader.readFrom(emptyApiSpec); - } - - /** - * Validates that the configuration reader is able to parse a valid config - * for a single API spec, returning a modeled object that resembles the input. - * @throws ApiException From API configuration - */ - @Test - public void readFromWellFormedJsonObjectProducesValidConfig() throws ApiException { - - // Arrange an input JSONObject - final JSONObject json = Resources.readAsJson("single-api.config"); - - // Act: try to parse it to a modeled configuration object - final AWSApiPluginConfiguration config = AWSApiPluginConfigurationReader.readFrom(json); - - // Assert: the modeled version "matches" the raw json - assertNotNull(config); - assertEquals(1, config.getApis().size()); - assertTrue(config.getApis().containsKey("api1")); - assertEquals(EndpointType.GRAPHQL, config.getApi("api1").getEndpointType()); - assertEquals("https://www.foo.bar/baz", config.getApi("api1").getEndpoint()); - assertEquals("us-east-1", config.getApi("api1").getRegion()); - } -} diff --git a/aws-api/src/test/java/com/amplifyframework/api/aws/AWSApiPluginConfigurationReaderTest.kt b/aws-api/src/test/java/com/amplifyframework/api/aws/AWSApiPluginConfigurationReaderTest.kt new file mode 100644 index 0000000000..9d0ba3542a --- /dev/null +++ b/aws-api/src/test/java/com/amplifyframework/api/aws/AWSApiPluginConfigurationReaderTest.kt @@ -0,0 +1,119 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package com.amplifyframework.api.aws + +import com.amplifyframework.api.ApiException +import com.amplifyframework.api.aws.AWSApiPluginConfigurationReader.GEN2_API_NAME +import com.amplifyframework.core.configuration.AmplifyOutputsData +import com.amplifyframework.testutils.Resources +import com.amplifyframework.testutils.configuration.amplifyOutputsData +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.matchers.maps.shouldContainKey +import io.kotest.matchers.nulls.shouldBeNull +import io.kotest.matchers.shouldBe +import org.json.JSONObject +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +/** + * Tests the [AWSApiPluginConfigurationReader] JSON parser utility. + */ +@RunWith(RobolectricTestRunner::class) +class AWSApiPluginConfigurationReaderTest { + + fun `read from null json object throws configuration exception`() { + shouldThrow { + AWSApiPluginConfigurationReader.readFrom(null) + } + } + fun `read from api with no spec throws configuration exception`() { + val emptyApiSpec = JSONObject().put("api1", JSONObject()) + + shouldThrow { + AWSApiPluginConfigurationReader.readFrom(emptyApiSpec) + } + } + + @Test + fun `read from well formed json object produces valid config`() { + // Arrange an input JSONObject + val json = Resources.readAsJson("single-api.config") + + // Act: try to parse it to a modeled configuration object + val config = AWSApiPluginConfigurationReader.readFrom(json) + + // Assert: the modeled version "matches" the raw json + assertNotNull(config) + assertEquals(1, config.apis.size.toLong()) + assertTrue(config.apis.containsKey("api1")) + assertEquals(EndpointType.GRAPHQL, config.getApi("api1")!!.endpointType) + assertEquals("https://www.foo.bar/baz", config.getApi("api1")!!.endpoint) + assertEquals("us-east-1", config.getApi("api1")!!.region) + } + + @Test + fun `reads from AmplifyOutputsData`() { + val outputs = amplifyOutputsData { + data { + awsRegion = "test-region" + apiKey = "api-key" + url = "https://aws.com" + defaultAuthorizationType = AmplifyOutputsData.AwsAppsyncAuthorizationType.OPENID_CONNECT + } + } + + val config = AWSApiPluginConfigurationReader.from(outputs) + + config.apis shouldContainKey GEN2_API_NAME + config.apis[GEN2_API_NAME]!!.run { + region shouldBe "test-region" + endpointType shouldBe EndpointType.GRAPHQL + endpoint shouldBe "https://aws.com" + authorizationType shouldBe AuthorizationType.OPENID_CONNECT + apiKey shouldBe "api-key" + } + } + + @Test + fun `apiKey can be null`() { + val outputs = amplifyOutputsData { + data { + apiKey = null + } + } + + val config = AWSApiPluginConfigurationReader.from(outputs) + + config.apis shouldContainKey GEN2_API_NAME + config.apis[GEN2_API_NAME]!!.run { + apiKey.shouldBeNull() + } + } + + @Test + fun `throws if no data config in AmplifyOutputsData`() { + val outputs = amplifyOutputsData { + // do not add data + } + + shouldThrow { + AWSApiPluginConfigurationReader.from(outputs) + } + } +} diff --git a/testutils/src/main/java/com/amplifyframework/testutils/configuration/AmplifyOutputsDataBuilder.kt b/testutils/src/main/java/com/amplifyframework/testutils/configuration/AmplifyOutputsDataBuilder.kt index eca6135160..8caf7e47b5 100644 --- a/testutils/src/main/java/com/amplifyframework/testutils/configuration/AmplifyOutputsDataBuilder.kt +++ b/testutils/src/main/java/com/amplifyframework/testutils/configuration/AmplifyOutputsDataBuilder.kt @@ -40,6 +40,10 @@ class AmplifyOutputsDataBuilder : AmplifyOutputsData { auth = AuthBuilder().apply(func) } + fun data(func: DataBuilder.() -> Unit) { + data = DataBuilder().apply(func) + } + fun geo(func: GeoBuilder.() -> Unit) { geo = GeoBuilder().apply(func) } @@ -102,6 +106,16 @@ class OauthBuilder : AmplifyOutputsData.Auth.Oauth { AmplifyOutputsData.Auth.Oauth.ResponseType.Code } +class DataBuilder : AmplifyOutputsData.Data { + override var awsRegion: String = "us-east-1" + override var url: String = "https://test.com" + override var apiKey: String? = null + override var defaultAuthorizationType: AmplifyOutputsData.AwsAppsyncAuthorizationType = + AmplifyOutputsData.AwsAppsyncAuthorizationType.AMAZON_COGNITO_USER_POOLS + override val authorizationTypes: MutableList = + mutableListOf(AmplifyOutputsData.AwsAppsyncAuthorizationType.AMAZON_COGNITO_USER_POOLS) +} + class GeoBuilder : AmplifyOutputsData.Geo { override var awsRegion: String = "us-east-1" override var maps: AmplifyOutputsData.Geo.Maps? = null