Skip to content
7 changes: 0 additions & 7 deletions appsync/aws-sdk-appsync-amplify/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ group = properties["POM_GROUP"].toString()

android {
namespace = "com.amazonaws.sdk.appsync.amplify"
defaultConfig {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

testOptions {
execution = "ANDROIDX_TEST_ORCHESTRATOR"
}
}

dependencies {
Expand Down
12 changes: 12 additions & 0 deletions appsync/aws-sdk-appsync-events/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@ dependencies {
testImplementation(libs.test.kotest.assertions)
testImplementation(libs.test.kotest.assertions.json)
testImplementation(libs.test.mockwebserver)
testImplementation(libs.test.turbine)

androidTestApi(project(":aws-sdk-appsync-amplify"))
androidTestImplementation(project(":aws-auth-cognito"))
androidTestImplementation(project(":core-kotlin"))
androidTestImplementation(libs.test.androidx.runner)
androidTestImplementation(libs.test.androidx.junit)
androidTestImplementation(libs.test.kotlin.coroutines)
androidTestImplementation(libs.test.kotest.assertions)
androidTestImplementation(libs.test.turbine)

androidTestUtil(libs.test.androidx.orchestrator)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amazonaws.sdk.appsync.events.test">

<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2025 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.amazonaws.sdk.appsync.events

import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry
import com.amazonaws.sdk.appsync.amplify.authorizers.AmplifyIamAuthorizer
import com.amazonaws.sdk.appsync.events.data.PublishResult
import com.amazonaws.sdk.appsync.events.test.R
import com.amazonaws.sdk.appsync.events.utils.getEventsConfig
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin
import com.amplifyframework.core.Amplify
import com.amplifyframework.core.configuration.AmplifyOutputs
import io.kotest.matchers.shouldBe
import java.util.UUID
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.JsonPrimitive
import org.junit.BeforeClass
import org.junit.Test

internal class EventsRestClientAmplifyIamTests {
private val eventsConfig = getEventsConfig(InstrumentationRegistry.getInstrumentation().targetContext)
private val iamAuthorizer = AmplifyIamAuthorizer(eventsConfig.awsRegion)
private val defaultChannel = "default/${UUID.randomUUID()}"
private val events = Events(eventsConfig.url)

companion object {
@BeforeClass
@JvmStatic
fun setup() {
Amplify.addPlugin(AWSCognitoAuthPlugin())
Amplify.configure(
AmplifyOutputs.fromResource(R.raw.amplify_outputs),
ApplicationProvider.getApplicationContext()
)
}
}

@Test
fun testPublishWithIam(): Unit = runTest {
// Publish the REST message
val restClient = events.createRestClient(publishAuthorizer = iamAuthorizer)
val result = restClient.publish(
channelName = defaultChannel,
event = JsonPrimitive(true)
)

// Assert expected REST response
(result is PublishResult.Response) shouldBe true
(result as PublishResult.Response).apply {
failedEvents.size shouldBe 0
successfulEvents.size shouldBe 1
successfulEvents[0].apply {
index shouldBe 0
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright 2025 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.amazonaws.sdk.appsync.events

import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry
import com.amazonaws.sdk.appsync.amplify.authorizers.AmplifyUserPoolAuthorizer
import com.amazonaws.sdk.appsync.events.data.EventsException
import com.amazonaws.sdk.appsync.events.data.PublishResult
import com.amazonaws.sdk.appsync.events.test.R
import com.amazonaws.sdk.appsync.events.utils.Credentials
import com.amazonaws.sdk.appsync.events.utils.getEventsConfig
import com.amplifyframework.auth.AuthException
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin
import com.amplifyframework.core.configuration.AmplifyOutputs
import com.amplifyframework.kotlin.core.Amplify
import io.kotest.matchers.shouldBe
import java.util.UUID
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.JsonPrimitive
import org.junit.After
import org.junit.BeforeClass
import org.junit.Test

internal class EventsRestClientAmplifyUserPoolTests {
private val eventsConfig = getEventsConfig(InstrumentationRegistry.getInstrumentation().targetContext)
private val userPoolAuthorizer = AmplifyUserPoolAuthorizer()
private val defaultChannel = "default/${UUID.randomUUID()}"
private val events = Events(eventsConfig.url)

companion object {
@BeforeClass
@JvmStatic
fun setup() {
Amplify.addPlugin(AWSCognitoAuthPlugin())
Amplify.configure(
AmplifyOutputs.fromResource(R.raw.amplify_outputs),
ApplicationProvider.getApplicationContext()
)
}
}

@After
fun tearDown() {
runBlocking { Amplify.Auth.signOut() }
}

@Test
fun testFailedPublishWithUnauthenticatedUserPool(): Unit = runTest {
// Publish the REST message
val restClient = events.createRestClient(publishAuthorizer = userPoolAuthorizer)
val result = restClient.publish(
channelName = defaultChannel,
event = JsonPrimitive(true)
)

// Assert expected REST response
(result is PublishResult.Failure) shouldBe true
(result as PublishResult.Failure).apply {
error shouldBe EventsException(
"An unknown error occurred",
cause = AuthException("Token is null", "Token received but is null. Check if you are signed in")
)
}
}

@Test
fun testPublishWithAuthenticatedUserPool(): Unit = runTest {
val credentials = Credentials.load(InstrumentationRegistry.getInstrumentation().targetContext)
Amplify.Auth.signIn(credentials.first, credentials.second)

// Publish the REST message
val restClient = events.createRestClient(publishAuthorizer = userPoolAuthorizer)
val result = restClient.publish(
channelName = defaultChannel,
event = JsonPrimitive(true)
)

// Assert expected REST response
(result is PublishResult.Response) shouldBe true
(result as PublishResult.Response).apply {
failedEvents.size shouldBe 0
successfulEvents.size shouldBe 1
successfulEvents[0].apply {
index shouldBe 0
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2025 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.amazonaws.sdk.appsync.events

import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry
import com.amazonaws.sdk.appsync.core.authorizers.ApiKeyAuthorizer
import com.amazonaws.sdk.appsync.events.data.PublishResult
import com.amazonaws.sdk.appsync.events.test.R
import com.amazonaws.sdk.appsync.events.utils.getEventsConfig
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin
import com.amplifyframework.core.Amplify
import com.amplifyframework.core.configuration.AmplifyOutputs
import io.kotest.matchers.shouldBe
import java.util.UUID
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.JsonPrimitive
import org.junit.BeforeClass
import org.junit.Test

internal class EventsRestClientApiKeyTests {
private val eventsConfig = getEventsConfig(InstrumentationRegistry.getInstrumentation().targetContext)
private val apiKeyAuthorizer = ApiKeyAuthorizer(eventsConfig.apiKey)
private val defaultChannel = "default/${UUID.randomUUID()}"
private val events = Events(eventsConfig.url)

companion object {
@BeforeClass
@JvmStatic
fun setup() {
Amplify.addPlugin(AWSCognitoAuthPlugin())
Amplify.configure(
AmplifyOutputs.fromResource(R.raw.amplify_outputs),
ApplicationProvider.getApplicationContext()
)
}
}

@Test
fun testPublishWithApiKey(): Unit = runTest {
// Publish the REST message
val restClient = events.createRestClient(publishAuthorizer = apiKeyAuthorizer)
val result = restClient.publish(
channelName = defaultChannel,
event = JsonPrimitive(true)
)

// Assert expected REST response
(result is PublishResult.Response) shouldBe true
(result as PublishResult.Response).apply {
failedEvents.size shouldBe 0
successfulEvents.size shouldBe 1
successfulEvents[0].apply {
index shouldBe 0
}
}
}
}
Loading