|
19 | 19 |
|
20 | 20 | import com.amplifyframework.AmplifyException;
|
21 | 21 | import com.amplifyframework.api.graphql.GraphQLRequest;
|
| 22 | +import com.amplifyframework.api.graphql.GraphQLResponse; |
22 | 23 | import com.amplifyframework.api.graphql.PaginatedResult;
|
23 | 24 | import com.amplifyframework.core.model.Model;
|
24 | 25 | import com.amplifyframework.core.model.ModelProvider;
|
|
54 | 55 | import com.amplifyframework.util.ForEach;
|
55 | 56 | import com.amplifyframework.util.Time;
|
56 | 57 |
|
| 58 | +import com.google.common.collect.Lists; |
57 | 59 | import org.junit.Before;
|
58 | 60 | import org.junit.Test;
|
59 | 61 | import org.junit.runner.RunWith;
|
|
66 | 68 | import java.util.Iterator;
|
67 | 69 | import java.util.LinkedHashSet;
|
68 | 70 | import java.util.List;
|
| 71 | +import java.util.Objects; |
69 | 72 | import java.util.Random;
|
70 | 73 | import java.util.Set;
|
71 | 74 | import java.util.Timer;
|
|
86 | 89 | import static org.junit.Assert.assertFalse;
|
87 | 90 | import static org.junit.Assert.assertNull;
|
88 | 91 | import static org.junit.Assert.assertTrue;
|
| 92 | +import static org.mockito.ArgumentMatchers.argThat; |
89 | 93 | import static org.mockito.Mockito.any;
|
90 | 94 | import static org.mockito.Mockito.mock;
|
91 | 95 | import static org.mockito.Mockito.spy;
|
@@ -609,6 +613,50 @@ public void userProvidedErrorCallbackInvokedOnFailure() throws DataStoreExceptio
|
609 | 613 | verify(this.errorHandler, times(1)).accept(any());
|
610 | 614 | }
|
611 | 615 |
|
| 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 | + |
612 | 660 |
|
613 | 661 | /**
|
614 | 662 | * Verify that retry is called on appsync failure when syncRetry is set to true.
|
|
0 commit comments