Skip to content

Commit

Permalink
Convert configuration reader test to Kotlin and add tests for parsing…
Browse files Browse the repository at this point in the history
… amplify outputs
  • Loading branch information
mattcreaser committed Apr 16, 2024
1 parent 9ad42d1 commit a32d1bc
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 */ }

Expand Down Expand Up @@ -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)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<ApiException> {
AWSApiPluginConfigurationReader.readFrom(null)
}
}
fun `read from api with no spec throws configuration exception`() {
val emptyApiSpec = JSONObject().put("api1", JSONObject())

shouldThrow<ApiException> {
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<ApiException> {
AWSApiPluginConfigurationReader.from(outputs)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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<AmplifyOutputsData.AwsAppsyncAuthorizationType> =
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
Expand Down

0 comments on commit a32d1bc

Please sign in to comment.