Search in sources :

Example 1 with MutationType

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

the class OrchestratorTest method itemsPlacedInStorageArePublishedToNetwork.

/**
 * When an item is placed into storage, a cascade of
 * things happen which should ultimately result in a mutation call
 * to the API category, with an {@link MutationType} corresponding to the type of
 * modification that was made to the storage.
 * @throws AmplifyException On failure to load model schema into registry
 */
// Casting ? in HubEvent<?> to PendingMutation<? extends Model>
@SuppressWarnings("unchecked")
@Test
public void itemsPlacedInStorageArePublishedToNetwork() throws AmplifyException {
    // Arrange: orchestrator is running
    orchestrator.start().test();
    orchestratorInitObserver.await(10, TimeUnit.SECONDS);
    HubAccumulator accumulator = HubAccumulator.create(HubChannel.DATASTORE, isProcessed(susan), 1).start();
    // Act: Put BlogOwner into storage, and wait for it to complete.
    SynchronousStorageAdapter.delegatingTo(localStorageAdapter).save(susan);
    // Assert that the event is published out to the API
    assertEquals(Collections.singletonList(susan), Observable.fromIterable(accumulator.await(10, TimeUnit.SECONDS)).map(HubEvent::getData).map(data -> (OutboxMutationEvent<BlogOwner>) data).map(OutboxMutationEvent::getElement).map(OutboxMutationEvent.OutboxMutationEventElement::getModel).toList().blockingGet());
    assertTrue(orchestrator.stop().blockingAwait(5, TimeUnit.SECONDS));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AmplifyException(com.amplifyframework.AmplifyException) MutationType(com.amplifyframework.api.graphql.MutationType) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) ModelProvider(com.amplifyframework.core.model.ModelProvider) DataStoreChannelEventName(com.amplifyframework.datastore.DataStoreChannelEventName) RunWith(org.junit.runner.RunWith) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) Observable(io.reactivex.rxjava3.core.Observable) TestHubEventFilters.isProcessed(com.amplifyframework.datastore.syncengine.TestHubEventFilters.isProcessed) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) HubEvent(com.amplifyframework.hub.HubEvent) Before(org.junit.Before) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) Amplify(com.amplifyframework.core.Amplify) HubChannel(com.amplifyframework.hub.HubChannel) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) AppSyncClient(com.amplifyframework.datastore.appsync.AppSyncClient) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) Mockito.times(org.mockito.Mockito.times) Logger(com.amplifyframework.logging.Logger) Mockito.verify(org.mockito.Mockito.verify) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) TimeUnit(java.util.concurrent.TimeUnit) GraphQLBehavior(com.amplifyframework.api.graphql.GraphQLBehavior) InMemoryStorageAdapter(com.amplifyframework.datastore.storage.InMemoryStorageAdapter) Temporal(com.amplifyframework.core.model.temporal.Temporal) ShadowLog(org.robolectric.shadows.ShadowLog) ApiMocking(com.amplifyframework.testutils.mocks.ApiMocking) Collections(java.util.Collections) SimpleModelProvider(com.amplifyframework.datastore.model.SimpleModelProvider) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) HubEvent(com.amplifyframework.hub.HubEvent) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) Test(org.junit.Test)

Example 2 with MutationType

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

the class OutboxMutationFailedEvent method create.

/**
 * Constructs an outbox mutation error event.
 * @param pendingMutation pending mutation that failed to publish
 * @param errors the list of graphQL errors that caused the failure
 * @param <M> Class type of the model.
 * @return Outbox mutation error event.
 */
@NonNull
public static <M extends Model> OutboxMutationFailedEvent<M> create(@NonNull PendingMutation<M> pendingMutation, @NonNull List<GraphQLResponse.Error> errors) {
    Objects.requireNonNull(pendingMutation);
    Objects.requireNonNull(errors);
    MutationErrorType errorType = MutationErrorType.fromGraphQlErrors(errors);
    String operation = pendingMutation.getMutationType().name();
    MutationType opType = MutationType.valueOf(operation);
    String name = pendingMutation.getModelSchema().getName();
    M model = pendingMutation.getMutatedItem();
    return new OutboxMutationFailedEvent<>(errorType, opType, name, model);
}
Also used : MutationType(com.amplifyframework.api.graphql.MutationType) NonNull(androidx.annotation.NonNull)

Aggregations

MutationType (com.amplifyframework.api.graphql.MutationType)2 NonNull (androidx.annotation.NonNull)1 AmplifyException (com.amplifyframework.AmplifyException)1 GraphQLBehavior (com.amplifyframework.api.graphql.GraphQLBehavior)1 Amplify (com.amplifyframework.core.Amplify)1 ModelProvider (com.amplifyframework.core.model.ModelProvider)1 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)1 Temporal (com.amplifyframework.core.model.temporal.Temporal)1 DataStoreChannelEventName (com.amplifyframework.datastore.DataStoreChannelEventName)1 DataStoreConfiguration (com.amplifyframework.datastore.DataStoreConfiguration)1 AppSyncClient (com.amplifyframework.datastore.appsync.AppSyncClient)1 ModelMetadata (com.amplifyframework.datastore.appsync.ModelMetadata)1 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)1 SimpleModelProvider (com.amplifyframework.datastore.model.SimpleModelProvider)1 InMemoryStorageAdapter (com.amplifyframework.datastore.storage.InMemoryStorageAdapter)1 SynchronousStorageAdapter (com.amplifyframework.datastore.storage.SynchronousStorageAdapter)1 TestHubEventFilters.isProcessed (com.amplifyframework.datastore.syncengine.TestHubEventFilters.isProcessed)1 HubChannel (com.amplifyframework.hub.HubChannel)1 HubEvent (com.amplifyframework.hub.HubEvent)1 Logger (com.amplifyframework.logging.Logger)1