use of com.amplifyframework.testutils.HubAccumulator in project amplify-android by aws-amplify.
the class HybridAssociationSyncInstrumentationTest method associatedModelsAreSyncedUpToCloud.
/**
* When we save {@link SerializedModel}s, we should find them in the cloud,
* shortly there-after. Saving associated serialized models will work.
* @throws AmplifyException For a variety of reasons, including failure to build schema,
* or bad interaction with API or DataStore
*/
@Ignore("It passes. Not automating due to operational concerns as noted in class-level @Ignore.")
@Test
public void associatedModelsAreSyncedUpToCloud() throws AmplifyException {
// First up, we're going to save a "leaf" model, a BlogOwner.
String ownerModelName = BlogOwner.class.getSimpleName();
ModelSchema ownerSchema = schemaProvider.modelSchemas().get(ownerModelName);
assertNotNull(ownerSchema);
BlogOwner owner = BlogOwner.builder().name("Guillermo Esteban").build();
Map<String, Object> ownerData = new HashMap<>();
ownerData.put("id", owner.getId());
ownerData.put("name", owner.getName());
SerializedModel serializedOwner = SerializedModel.builder().serializedData(ownerData).modelSchema(ownerSchema).build();
// Setup an accumulator so we know when there has been a publication.
HubAccumulator ownerAccumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(ownerModelName, owner.getId()), 1).start();
hybridBehaviors.save(serializedOwner);
ownerAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
// Validate that the Blog Owner was saved locally, and in the cloud.
List<SerializedModel> allCurrentSerializedOwners = hybridBehaviors.list(ownerSchema.getName());
assertTrue(allCurrentSerializedOwners.contains(serializedOwner));
List<BlogOwner> allCurrentBlogOwners = normalBehaviors.list(BlogOwner.class);
assertTrue(allCurrentBlogOwners.contains(owner));
assertEquals(owner, api.get(BlogOwner.class, owner.getId()));
// Now, we're going to save a type with a connection.
// Build a blog, and its serialized form. Blog has association to a BlogOwner.
Blog blog = Blog.builder().name("A wonderful blog").owner(owner).build();
Map<String, Object> blogData = new HashMap<>();
blogData.put("id", blog.getId());
blogData.put("name", blog.getName());
blogData.put("owner", SerializedModel.builder().serializedData(Collections.singletonMap("id", owner.getId())).modelSchema(null).build());
String blogSchemaName = Blog.class.getSimpleName();
ModelSchema blogSchema = schemaProvider.modelSchemas().get(blogSchemaName);
assertNotNull(blogSchema);
SerializedModel serializedBlog = SerializedModel.builder().serializedData(blogData).modelSchema(blogSchema).build();
// Save the blog
HubAccumulator blogAccumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(blogSchemaName, blog.getId()), 1).start();
hybridBehaviors.save(serializedBlog);
blogAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
// Validate that we find the blog locally, and on the remote system.
List<SerializedModel> allCurrentSerializedBlogs = hybridBehaviors.list(blogSchema.getName());
assertTrue(allCurrentSerializedBlogs.contains(serializedBlog));
List<Blog> allCurrentBlogs = normalBehaviors.list(Blog.class);
assertTrue(allCurrentBlogs.contains(blog));
Blog foundBlog = api.get(Blog.class, blog.getId());
assertEquals(blog, foundBlog);
assertEquals(owner.getId(), foundBlog.getOwner().getId());
}
use of com.amplifyframework.testutils.HubAccumulator in project amplify-android by aws-amplify.
the class HybridTemporalSyncInstrumentationTest method temporalTypesAreSyncedDownFromCloud.
/**
* It is possible to receive a model with temporal types over a subscription.
* After receiving such a model, we can query it locally and inspect its fields.
* The temporal values should be the same as what was saved remotely.
* @throws DataStoreException on failure to interact with AppSync
*/
@Ignore("It passes. Not automating due to operational concerns as noted in class-level @Ignore.")
@Test
public void temporalTypesAreSyncedDownFromCloud() throws DataStoreException {
// Save a meeting, remotely. Wait for it to show up locally.
Meeting meeting = createMeeting();
HubAccumulator receiptAccumulator = HubAccumulator.create(HubChannel.DATASTORE, receiptOf(meeting.getId()), 1).start();
appSync.create(meeting, modelSchema);
receiptAccumulator.awaitFirst(TIMEOUT_SECONDS, TimeUnit.SECONDS);
// Look through the models that now exist locally.
// One of them should be the thing we just saved to the backend.
// Any others will have come in through the base sync in @Before.
// When we find the clone, validate its fields.
List<SerializedModel> clonedMeetings = hybridBehaviors.list(modelSchema.getName());
SerializedModel clone = findById(clonedMeetings, meeting.getId());
assertEquals(toMap(meeting), clone.getSerializedData());
}
use of com.amplifyframework.testutils.HubAccumulator in project amplify-android by aws-amplify.
the class MultiAuthSyncEngineInstrumentationTest method verifyScenario.
private void verifyScenario(Class<? extends Model> modelType, boolean signInToCognito, boolean signInWithOidc, AuthorizationType expectedAuthType) throws AmplifyException, IOException {
configure(modelType, signInToCognito, signInWithOidc, expectedAuthType);
String modelId = UUID.randomUUID().toString();
Model testRecord = createRecord(modelType, modelId);
HubAccumulator expectedEventAccumulator = null;
if (expectedAuthType != null) {
expectedEventAccumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(modelType.getSimpleName(), testRecord.getId()), 1).start();
} else {
expectedEventAccumulator = HubAccumulator.create(HubChannel.DATASTORE, networkStatusFailure(), 1).start();
}
dataStore.start();
dataStore.save(testRecord);
expectedEventAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
assertFalse(requestInterceptor.hasUnexpectedRequests());
// Sign the user out if sign-in was required.
if (signInToCognito) {
auth.signOut(AuthSignOutOptions.builder().build());
}
}
use of com.amplifyframework.testutils.HubAccumulator in project amplify-android by aws-amplify.
the class PersistentMutationOutboxTest method enqueuePersistsMutationAndNotifiesObserver.
/**
* Enqueueing a mutation should have the result of persisting
* the mutation to storage, and notifying any observers that
* a new mutation has been enqueued.
* @throws DataStoreException On failure to query results, for assertions
* @throws InterruptedException If interrupted while awaiting terminal result in test observer
*/
@Test
public void enqueuePersistsMutationAndNotifiesObserver() throws DataStoreException, InterruptedException {
// Observe the queue
TestObserver<OutboxEvent> queueObserver = mutationOutbox.events().test();
BlogOwner jameson = BlogOwner.builder().name("Jameson Williams").build();
PendingMutation<BlogOwner> createJameson = PendingMutation.creation(jameson, schema);
HubAccumulator savedMutationsAccumulator = HubAccumulator.create(HubChannel.DATASTORE, isEnqueued(jameson), 1).start();
// Enqueue an save for a Jameson BlogOwner object,
// and make sure that it calls back onComplete().
TestObserver<Void> saveObserver = mutationOutbox.enqueue(createJameson).test();
saveObserver.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
saveObserver.assertNoErrors().assertComplete();
saveObserver.dispose();
// Wait for a Hub event telling us that the model got successfully enqueued.
savedMutationsAccumulator.await();
// Expected to observe the mutation on the subject
queueObserver.awaitCount(1);
queueObserver.assertValue(OutboxEvent.CONTENT_AVAILABLE);
queueObserver.dispose();
// Assert that the storage contains the mutation
assertEquals(Collections.singletonList(converter.toRecord(createJameson)), storage.query(PersistentRecord.class));
assertTrue(mutationOutbox.hasPendingMutation(jameson.getId()));
assertEquals(createJameson, mutationOutbox.peek());
}
use of com.amplifyframework.testutils.HubAccumulator in project amplify-android by aws-amplify.
the class PersistentMutationOutboxTest method outboxStatusIsPublishedToHubOnEnqueue.
/**
* Enqueueing a mutation should publish current outbox status.
*/
@Test
public void outboxStatusIsPublishedToHubOnEnqueue() {
BlogOwner raphael = BlogOwner.builder().name("Raphael Kim").build();
PendingMutation<BlogOwner> createRaphael = PendingMutation.creation(raphael, schema);
// Start listening for publication events.
// outbox should not be empty
HubAccumulator statusAccumulator = HubAccumulator.create(HubChannel.DATASTORE, isOutboxEmpty(false), 1).start();
// Enqueue an save for a Raphael BlogOwner object,
// and make sure that outbox status is published to hub.
mutationOutbox.enqueue(createRaphael).test();
statusAccumulator.await();
}
Aggregations