Search in sources :

Example 6 with AWSConfiguration

use of com.amazonaws.mobile.config.AWSConfiguration in project aws-mobile-appsync-sdk-android by awslabs.

the class MultiClientInstrumentationTest method testUseSameClientDatabasePrefixForDifferentAuthModes.

@Test
public void testUseSameClientDatabasePrefixForDifferentAuthModes() throws JSONException {
    // Construct client-1 with API_KEY AuthMode and MultiAuthAndroidIntegTestApp_API_KEY prefix
    AWSConfiguration awsConfiguration = new AWSConfiguration(getTargetContext());
    JSONObject appSyncConfig = awsConfiguration.optJsonObject("AppSync");
    AWSAppSyncClient.builder().context(getTargetContext()).apiKey(new BasicAPIKeyAuthProvider(appSyncConfig.getString("ApiKey"))).serverUrl(appSyncConfig.getString("ApiUrl")).region(Regions.fromName(appSyncConfig.getString("Region"))).useClientDatabasePrefix(true).clientDatabasePrefix(appSyncConfig.getString("ClientDatabasePrefix")).build();
    // Construct client-2 with AWS_IAM AuthMode and same prefix ("MultiAuthAndroidIntegTestApp_API_KEY")
    RuntimeException exception = assertThrows(RuntimeException.class, () -> AWSAppSyncClient.builder().context(getTargetContext()).cognitoUserPoolsAuthProvider(() -> {
        try {
            return AWSMobileClient.getInstance().getTokens().getIdToken().toString();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }).useClientDatabasePrefix(true).clientDatabasePrefix(appSyncConfig.getString("ClientDatabasePrefix")).build());
    String expected = "ClientDatabasePrefix validation failed. The ClientDatabasePrefix ";
    assertTrue(exception.getLocalizedMessage().startsWith(expected));
}
Also used : JSONObject(org.json.JSONObject) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) BasicAPIKeyAuthProvider(com.amazonaws.mobileconnectors.appsync.sigv4.BasicAPIKeyAuthProvider) AWSAppSyncClientException(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClientException) JSONException(org.json.JSONException) ClearCacheException(com.amazonaws.mobileconnectors.appsync.ClearCacheException) Test(org.junit.Test)

Example 7 with AWSConfiguration

use of com.amazonaws.mobile.config.AWSConfiguration in project aws-mobile-appsync-sdk-android by awslabs.

the class MultiClientInstrumentationTest method testConfigHasClientDatabasePrefixAndUseClientDatabasePrefixTrue.

@Test
public void testConfigHasClientDatabasePrefixAndUseClientDatabasePrefixTrue() {
    AWSConfiguration awsConfiguration = new AWSConfiguration(getTargetContext());
    JSONObject appSyncConfig = awsConfiguration.optJsonObject("AppSync");
    AWSAppSyncClients.validateAppSyncClient(AWSAppSyncClient.builder().context(getTargetContext()).awsConfiguration(awsConfiguration).useClientDatabasePrefix(true).build(), JsonExtract.stringValue(appSyncConfig, "ClientDatabasePrefix"), JsonExtract.stringValue(appSyncConfig, "AuthMode"));
}
Also used : JSONObject(org.json.JSONObject) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) Test(org.junit.Test)

Example 8 with AWSConfiguration

use of com.amazonaws.mobile.config.AWSConfiguration in project aws-mobile-appsync-sdk-android by awslabs.

the class MultiClientInstrumentationTest method testNoContextThrowsException.

@Test
public void testNoContextThrowsException() {
    AWSConfiguration awsConfiguration = new AWSConfiguration(getTargetContext());
    JSONObject appSyncConfig = awsConfiguration.optJsonObject("AppSync");
    RuntimeException exception = assertThrows(RuntimeException.class, () -> AWSAppSyncClient.builder().apiKey(new BasicAPIKeyAuthProvider(appSyncConfig.getString("ApiKey"))).serverUrl(appSyncConfig.getString("ApiUrl")).region(Regions.fromName(appSyncConfig.getString("Region"))).useClientDatabasePrefix(true).clientDatabasePrefix(appSyncConfig.getString("ClientDatabasePrefix")).build());
    String expected = "A valid Android Context is required.";
    assertTrue(exception.getLocalizedMessage().startsWith(expected));
}
Also used : JSONObject(org.json.JSONObject) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) BasicAPIKeyAuthProvider(com.amazonaws.mobileconnectors.appsync.sigv4.BasicAPIKeyAuthProvider) Test(org.junit.Test)

Example 9 with AWSConfiguration

use of com.amazonaws.mobile.config.AWSConfiguration in project aws-mobile-appsync-sdk-android by awslabs.

the class AWSAppSyncClients method withUserPoolsFromAWSConfiguration.

@NonNull
public static AWSAppSyncClient withUserPoolsFromAWSConfiguration() {
    AWSConfiguration awsConfiguration = new AWSConfiguration(getTargetContext());
    awsConfiguration.setConfiguration("MultiAuthAndroidIntegTestApp_AMAZON_COGNITO_USER_POOLS");
    return AWSAppSyncClient.builder().context(getTargetContext()).awsConfiguration(awsConfiguration).cognitoUserPoolsAuthProvider(() -> {
        try {
            String idToken = TestAWSMobileClient.instance(getTargetContext()).getTokens().getIdToken().getTokenString();
            Log.d(TAG, "idToken = " + idToken);
            return idToken;
        } catch (Exception failure) {
            failure.printStackTrace();
            return null;
        }
    }).useClientDatabasePrefix(true).build();
}
Also used : AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) JSONException(org.json.JSONException) NonNull(androidx.annotation.NonNull)

Example 10 with AWSConfiguration

use of com.amazonaws.mobile.config.AWSConfiguration in project aws-mobile-appsync-sdk-android by awslabs.

the class AWSAppSyncClients method withAPIKEYFromAWSConfiguration.

@NonNull
public static AWSAppSyncClient withAPIKEYFromAWSConfiguration(boolean subscriptionsAutoReconnect, long credentialsDelay) {
    AWSConfiguration awsConfiguration = new AWSConfiguration(getTargetContext());
    JSONObject ccpConfig = awsConfiguration.optJsonObject("CredentialsProvider");
    String cognitoIdentityPoolID = JsonExtract.stringValue(ccpConfig, "CognitoIdentity.Default.PoolId");
    String cognitoRegion = JsonExtract.stringValue(ccpConfig, "CognitoIdentity.Default.Region");
    DelayedCognitoCredentialsProvider credentialsProvider = new DelayedCognitoCredentialsProvider(cognitoIdentityPoolID, Regions.fromName(cognitoRegion), credentialsDelay);
    AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider, Region.getRegion("us-west-2"));
    S3ObjectManager s3ObjectManager = new S3ObjectManagerImplementation((s3Client));
    return AWSAppSyncClient.builder().context(getTargetContext()).awsConfiguration(awsConfiguration).conflictResolver(new TestConflictResolver()).s3ObjectManager(s3ObjectManager).subscriptionsAutoReconnect(subscriptionsAutoReconnect).mutationQueueExecutionTimeout(TimeUnit.SECONDS.toMillis(30)).persistentMutationsCallback(LoggingPersistentMutationsCallback.instance()).useClientDatabasePrefix(true).build();
}
Also used : S3ObjectManager(com.apollographql.apollo.api.S3ObjectManager) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) JSONObject(org.json.JSONObject) DelayedCognitoCredentialsProvider(com.amazonaws.mobileconnectors.appsync.identity.DelayedCognitoCredentialsProvider) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) S3ObjectManagerImplementation(com.amazonaws.mobileconnectors.appsync.S3ObjectManagerImplementation) NonNull(androidx.annotation.NonNull)

Aggregations

AWSConfiguration (com.amazonaws.mobile.config.AWSConfiguration)48 JSONObject (org.json.JSONObject)28 Test (org.junit.Test)26 JSONException (org.json.JSONException)16 Context (android.content.Context)13 NonNull (androidx.annotation.NonNull)11 BasicAPIKeyAuthProvider (com.amazonaws.mobileconnectors.appsync.sigv4.BasicAPIKeyAuthProvider)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 BeforeClass (org.junit.BeforeClass)7 AWSAppSyncClient (com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient)6 UserStateDetails (com.amazonaws.mobile.client.UserStateDetails)4 AWSCredentials (com.amazonaws.auth.AWSCredentials)3 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)3 SignInResult (com.amazonaws.mobile.client.results.SignInResult)3 CognitoUserPool (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool)3 UserNotConfirmedException (com.amazonaws.services.cognitoidentityprovider.model.UserNotConfirmedException)3 Nullable (androidx.annotation.Nullable)2 ApplicationProvider.getApplicationContext (androidx.test.core.app.ApplicationProvider.getApplicationContext)2 AWSMobileClient (com.amazonaws.mobile.client.AWSMobileClient)2 Callback (com.amazonaws.mobile.client.Callback)2