Skip to content

Commit edaa8f7

Browse files
Test that errorHandler and Hub events are called when sync responses have data and errors
1 parent 3a9886a commit edaa8f7

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

aws-datastore/src/test/java/com/amplifyframework/datastore/syncengine/SyncProcessorTest.java

+48
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.amplifyframework.AmplifyException;
2121
import com.amplifyframework.api.graphql.GraphQLRequest;
22+
import com.amplifyframework.api.graphql.GraphQLResponse;
2223
import com.amplifyframework.api.graphql.PaginatedResult;
2324
import com.amplifyframework.core.model.Model;
2425
import com.amplifyframework.core.model.ModelProvider;
@@ -54,6 +55,7 @@
5455
import com.amplifyframework.util.ForEach;
5556
import com.amplifyframework.util.Time;
5657

58+
import com.google.common.collect.Lists;
5759
import org.junit.Before;
5860
import org.junit.Test;
5961
import org.junit.runner.RunWith;
@@ -66,6 +68,7 @@
6668
import java.util.Iterator;
6769
import java.util.LinkedHashSet;
6870
import java.util.List;
71+
import java.util.Objects;
6972
import java.util.Random;
7073
import java.util.Set;
7174
import java.util.Timer;
@@ -86,6 +89,7 @@
8689
import static org.junit.Assert.assertFalse;
8790
import static org.junit.Assert.assertNull;
8891
import static org.junit.Assert.assertTrue;
92+
import static org.mockito.ArgumentMatchers.argThat;
8993
import static org.mockito.Mockito.any;
9094
import static org.mockito.Mockito.mock;
9195
import static org.mockito.Mockito.spy;
@@ -609,6 +613,50 @@ public void userProvidedErrorCallbackInvokedOnFailure() throws DataStoreExceptio
609613
verify(this.errorHandler, times(1)).accept(any());
610614
}
611615

616+
/**
617+
* Verify that the user-provided onError callback is invoked for every error if sync contains data and errors.
618+
* Verify that Hub events are emitted once per page with non-applicable data
619+
* @throws DataStoreException On failure to build GraphQLRequest for sync query.
620+
*/
621+
@Test
622+
public void userProvidedErrorCallbackInvokedOnNonApplicableData() throws DataStoreException {
623+
// Arrange: mock data + errors when invoking hydrate on the mock object. Each response is a page in a paginated
624+
// syncQuery
625+
AppSyncMocking.sync(appSync)
626+
.mockSuccessResponse(BlogOwner.class, null, "someToken",
627+
Lists.newArrayList(BLOGGER_JAMESON),
628+
Lists.newArrayList(
629+
new GraphQLResponse.Error("My Error 1", null, null, null),
630+
new GraphQLResponse.Error("My Error 2", null, null, null),
631+
new GraphQLResponse.Error("My Error 3", null, null, null)
632+
)
633+
)
634+
.mockSuccessResponse(BlogOwner.class, "someToken", null,
635+
Lists.newArrayList(BLOGGER_ISLA),
636+
Lists.newArrayList(
637+
new GraphQLResponse.Error("My Error 4", null, null, null),
638+
new GraphQLResponse.Error("My Error 5", null, null, null)
639+
)
640+
);
641+
642+
// Collects nonApplicableDataReceived events.
643+
HubAccumulator nonApplicableDataReceivedAccumulator =
644+
createAccumulator(forEvent(DataStoreChannelEventName.NON_APPLICABLE_DATA_RECEIVED), 2);
645+
646+
// Act: Start the accumulators.
647+
nonApplicableDataReceivedAccumulator.start();
648+
// Act: call hydrate.
649+
syncProcessor.hydrate().test().awaitDone(OP_TIMEOUT_MS, TimeUnit.MILLISECONDS).assertComplete();
650+
651+
// Assert: The error handler is called once for each GraphQL error
652+
verify(this.errorHandler, times(5))
653+
.accept(argThat(dataStoreException -> Objects.requireNonNull(dataStoreException.getMessage())
654+
.startsWith("Error received when syncing data:")));
655+
// Assert: A single nonApplicableDataReceived event is emitted to the Hub for each syncQuery page
656+
assertEquals(2, nonApplicableDataReceivedAccumulator
657+
.await((int) OP_TIMEOUT_MS, TimeUnit.MILLISECONDS).size());
658+
}
659+
612660

613661
/**
614662
* Verify that retry is called on appsync failure when syncRetry is set to true.

0 commit comments

Comments
 (0)