Search in sources :

Example 1 with SubscriptionType

use of com.amplifyframework.api.graphql.SubscriptionType in project amplify-android by aws-amplify.

the class SubscriptionProcessorTest method arrangeStartedSubscriptions.

private static void arrangeStartedSubscriptions(AppSync appSync, List<ModelSchema> modelSchemas, SubscriptionType[] subscriptionTypes) {
    Answer<Cancelable> answer = invocation -> {
        final int startConsumerIndex = 1;
        Consumer<String> onStart = invocation.getArgument(startConsumerIndex);
        onStart.accept(RandomString.string());
        return new NoOpCancelable();
    };
    arrangeSubscriptions(appSync, answer, modelSchemas, subscriptionTypes);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ModelProvider(com.amplifyframework.core.model.ModelProvider) Pair(android.util.Pair) RunWith(org.junit.runner.RunWith) AppSync(com.amplifyframework.datastore.appsync.AppSync) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Answer(org.mockito.stubbing.Answer) Consumer(com.amplifyframework.core.Consumer) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) Observable(io.reactivex.rxjava3.core.Observable) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) Before(org.junit.Before) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Model(com.amplifyframework.core.model.Model) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) Action(com.amplifyframework.core.Action) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) RandomString(com.amplifyframework.testutils.random.RandomString) Temporal(com.amplifyframework.core.model.temporal.Temporal) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) Consumer(com.amplifyframework.core.Consumer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable)

Example 2 with SubscriptionType

use of com.amplifyframework.api.graphql.SubscriptionType in project amplify-android by aws-amplify.

the class SubscriptionProcessorTest method appSyncInvokedWhenSubscriptionsStarted.

/**
 * When {@link SubscriptionProcessor#startSubscriptions()} is invoked,
 * the {@link AppSync} client receives subscription requests.
 */
@Test
public void appSyncInvokedWhenSubscriptionsStarted() {
    // For every Class-SubscriptionType pairing, use a CountDownLatch
    // to tell whether or not we've "seen" a subscription event for it.
    Map<Pair<ModelSchema, SubscriptionType>, CountDownLatch> seen = new HashMap<>();
    // Build a stream of such pairs.
    Observable.fromIterable(modelSchemas).flatMap(modelSchema -> Observable.fromArray(SubscriptionType.values()).map(value -> Pair.create(modelSchema, value))).blockingForEach(pair -> {
        // For each one, store a latch. Add a mocking behavior to count down
        // the latch when the subscription API is hit, for that class and subscription type.
        CountDownLatch latch = new CountDownLatch(1);
        seen.put(Pair.create(pair.first, pair.second), latch);
        Answer<Cancelable> answer = invocation -> {
            latch.countDown();
            return new NoOpCancelable();
        };
        arrangeSubscription(appSync, answer, pair.first, pair.second);
    });
    // Act: start some subscriptions.
    try {
        subscriptionProcessor.startSubscriptions();
    } catch (DataStoreException exception) {
    // startSubscriptions throws this exception if it doesn't receive the start_ack messages after a time out.
    // This test doesn't mock those start_ack messages, so this expection is expected.  That's okay though -
    // we just want to verify that the subscriptions were requested.
    }
    // Make sure that all of the subscriptions have been
    Observable.fromIterable(seen.entrySet()).blockingForEach(entry -> {
        CountDownLatch latch = entry.getValue();
        assertTrue(latch.await(OPERATION_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ModelProvider(com.amplifyframework.core.model.ModelProvider) Pair(android.util.Pair) RunWith(org.junit.runner.RunWith) AppSync(com.amplifyframework.datastore.appsync.AppSync) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Answer(org.mockito.stubbing.Answer) Consumer(com.amplifyframework.core.Consumer) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) Observable(io.reactivex.rxjava3.core.Observable) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) Before(org.junit.Before) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Model(com.amplifyframework.core.model.Model) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) Action(com.amplifyframework.core.Action) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) RandomString(com.amplifyframework.testutils.random.RandomString) Temporal(com.amplifyframework.core.model.temporal.Temporal) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) DataStoreException(com.amplifyframework.datastore.DataStoreException) HashMap(java.util.HashMap) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) CountDownLatch(java.util.concurrent.CountDownLatch) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) Pair(android.util.Pair) Test(org.junit.Test)

Example 3 with SubscriptionType

use of com.amplifyframework.api.graphql.SubscriptionType in project amplify-android by aws-amplify.

the class AuthRuleRequestDecoratorTest method ownerArgumentAddedForRestrictedReadWithOidc.

/**
 * Verify that owner argument is required for all subscriptions if ModelOperation.READ is specified
 * while using OpenID Connect auth mode.
 * @throws AmplifyException if a ModelSchema can't be derived from the Model class.
 */
@Test
public void ownerArgumentAddedForRestrictedReadWithOidc() throws AmplifyException {
    final AuthorizationType mode = AuthorizationType.OPENID_CONNECT;
    final String expectedOwner = FakeOidcAuthProvider.SUB;
    // OwnerOidc class has restriction on every operation including READ
    for (SubscriptionType subscriptionType : SubscriptionType.values()) {
        GraphQLRequest<OwnerOidc> originalRequest = createRequest(OwnerOidc.class, subscriptionType);
        GraphQLRequest<OwnerOidc> modifiedRequest = decorator.decorate(originalRequest, mode);
        assertEquals(expectedOwner, getOwnerField(modifiedRequest));
    }
}
Also used : SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) AuthorizationType(com.amplifyframework.api.aws.AuthorizationType) Test(org.junit.Test)

Example 4 with SubscriptionType

use of com.amplifyframework.api.graphql.SubscriptionType in project amplify-android by aws-amplify.

the class AuthRuleRequestDecoratorTest method ownerArgumentAddedIfOwnerIsNotInGroupWithUserPools.

/**
 * Verify owner argument is added if model contains both owner-based and group-based
 * authorization and the user is not in any read-restricted group.
 * @throws AmplifyException if a ModelSchema can't be derived from the Model class.
 */
@Test
public void ownerArgumentAddedIfOwnerIsNotInGroupWithUserPools() throws AmplifyException {
    final AuthorizationType mode = AuthorizationType.AMAZON_COGNITO_USER_POOLS;
    final String expectedOwner = FakeCognitoAuthProvider.USERNAME;
    // but user is not in the read-restricted group.
    for (SubscriptionType subscriptionType : SubscriptionType.values()) {
        GraphQLRequest<OwnerNotInGroup> originalRequest = createRequest(OwnerNotInGroup.class, subscriptionType);
        GraphQLRequest<OwnerNotInGroup> modifiedRequest = decorator.decorate(originalRequest, mode);
        assertEquals(expectedOwner, getOwnerField(modifiedRequest));
    }
}
Also used : SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) AuthorizationType(com.amplifyframework.api.aws.AuthorizationType) Test(org.junit.Test)

Example 5 with SubscriptionType

use of com.amplifyframework.api.graphql.SubscriptionType in project amplify-android by aws-amplify.

the class AuthRuleRequestDecoratorTest method ownerArgumentNotAddedIfOwnerIsInGroupWithUserPools.

/**
 * Verify owner argument is NOT added if model contains both owner-based and group-based
 * authorization and the user is in any of the read-restricted groups.
 * @throws AmplifyException if a ModelSchema can't be derived from the Model class.
 */
@Test
public void ownerArgumentNotAddedIfOwnerIsInGroupWithUserPools() throws AmplifyException {
    final AuthorizationType mode = AuthorizationType.AMAZON_COGNITO_USER_POOLS;
    // and user is in the read-restricted group.
    for (SubscriptionType subscriptionType : SubscriptionType.values()) {
        GraphQLRequest<OwnerInGroup> originalRequest = createRequest(OwnerInGroup.class, subscriptionType);
        GraphQLRequest<OwnerInGroup> modifiedRequest = decorator.decorate(originalRequest, mode);
        assertNull(getOwnerField(modifiedRequest));
    }
}
Also used : SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) AuthorizationType(com.amplifyframework.api.aws.AuthorizationType) Test(org.junit.Test)

Aggregations

SubscriptionType (com.amplifyframework.api.graphql.SubscriptionType)15 Test (org.junit.Test)12 AuthorizationType (com.amplifyframework.api.aws.AuthorizationType)9 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)6 Action (com.amplifyframework.core.Action)6 Consumer (com.amplifyframework.core.Consumer)6 Cancelable (com.amplifyframework.core.async.Cancelable)6 Model (com.amplifyframework.core.model.Model)6 ModelSchema (com.amplifyframework.core.model.ModelSchema)6 DataStoreException (com.amplifyframework.datastore.DataStoreException)6 ModelProvider (com.amplifyframework.core.model.ModelProvider)5 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)5 AppSync (com.amplifyframework.datastore.appsync.AppSync)5 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)5 Completable (io.reactivex.rxjava3.core.Completable)5 Observable (io.reactivex.rxjava3.core.Observable)5 List (java.util.List)5 TimeUnit (java.util.concurrent.TimeUnit)5 NoOpCancelable (com.amplifyframework.core.async.NoOpCancelable)4 NonNull (androidx.annotation.NonNull)3