use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterObserveQueryTest method querySavedDataWithStringPredicates.
/**
* Test querying the saved item in the SQLite database with
* predicate conditions.
*
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
* @throws InterruptedException On unexpected failure manipulating items in/out of DataStore
*/
@Test
public void querySavedDataWithStringPredicates() throws DataStoreException, InterruptedException {
final List<Post> savedModels = new ArrayList<>();
BlogOwner blogOwner = BlogOwner.builder().name("Test Dummy").build();
adapter.save(blogOwner);
Blog blog = Blog.builder().name("Blogging for Dummies").owner(blogOwner).build();
adapter.save(blog);
final int numModels = 10;
for (int counter = 0; counter < numModels; counter++) {
final Post post = Post.builder().title(counter + "-title").status(PostStatus.INACTIVE).rating(counter).blog(blog).build();
adapter.save(post);
savedModels.add(post);
}
CountDownLatch latch = new CountDownLatch(1);
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
Consumer<DataStoreQuerySnapshot<Post>> onQuerySnapshot = value -> {
assertTrue(value.getItems().contains(savedModels.get(4)));
assertTrue(value.getItems().contains(savedModels.get(7)));
assertFalse(value.getItems().contains(savedModels.get(9)));
latch.countDown();
};
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
adapter.observeQuery(Post.class, new ObserveQueryOptions(Post.TITLE.beginsWith("4").or(Post.TITLE.beginsWith("7")).or(Post.TITLE.beginsWith("9")).and(not(Post.TITLE.gt(8))), null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
assertTrue(latch.await(15, TimeUnit.SECONDS));
}
use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterObserveQueryTest method queryWithOrderByRelatedModel.
/**
* Test query with order by. Validate that a list of Blog can be sorted by the names of BlogOwners.
*
* @throws DataStoreException On failure to arrange items into store, or from the query action itself.
* @throws InterruptedException InterruptedException.
*/
@Test
public void queryWithOrderByRelatedModel() throws DataStoreException, InterruptedException {
// Expect: Create BlogOwners and their respective blogs
List<String> names = Arrays.asList("Joe", "Bob", "Dan", "Jane");
List<Blog> blogs = new ArrayList<>();
CountDownLatch latch = new CountDownLatch(1);
for (String name : names) {
BlogOwner owner = BlogOwner.builder().name(name).build();
adapter.save(owner);
Blog blog = Blog.builder().name("").owner(owner).build();
adapter.save(blog);
blogs.add(blog);
}
List<Blog> sorted = new ArrayList<>(blogs);
Collections.sort(sorted, Comparator.comparing(blog -> blog.getOwner().getName()));
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
Consumer<DataStoreQuerySnapshot<Blog>> onQuerySnapshot = value -> {
assertEquals(sorted, value.getItems());
latch.countDown();
};
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
List<QuerySortBy> sortBy = new ArrayList<>();
sortBy.add(BlogOwner.NAME.ascending());
// Act: Query Blogs sorted by owner's name
adapter.observeQuery(Blog.class, new ObserveQueryOptions(null, sortBy), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
// assert
assertTrue(latch.await(30, TimeUnit.SECONDS));
}
use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterObserveQueryTest method querySavedDataWithMultipleItems.
/**
* Test querying the saved item in the SQLite database with observeQuery.
*
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
* @throws InterruptedException On unexpected failure manipulating items in/out of DataStore
*/
@Test
public void querySavedDataWithMultipleItems() throws DataStoreException, InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
final List<BlogOwner> savedModels = new ArrayList<>();
final int numModels = 10;
for (int counter = 0; counter < numModels; counter++) {
final BlogOwner blogOwner = BlogOwner.builder().name("namePrefix:" + counter).build();
adapter.save(blogOwner);
savedModels.add(blogOwner);
}
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
Consumer<DataStoreQuerySnapshot<BlogOwner>> onQuerySnapshot = value -> {
for (BlogOwner blogOwner : savedModels) {
assertTrue(value.getItems().contains(blogOwner));
}
latch.countDown();
};
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
adapter.observeQuery(BlogOwner.class, new ObserveQueryOptions(null, null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.
the class SubscriptionProcessorTest method arrangeDataEmittingSubscription.
@SuppressWarnings("SameParameterValue")
private static <T extends Model> void arrangeDataEmittingSubscription(AppSync appSync, ModelSchema modelSchema, SubscriptionType subscriptionType, GraphQLResponse<ModelWithMetadata<T>> response) throws DataStoreException {
Answer<Cancelable> answer = invocation -> {
final int startConsumerIndex = 1;
Consumer<String> onStart = invocation.getArgument(startConsumerIndex);
onStart.accept(RandomString.string());
final int dataConsumerIndex = 2;
Consumer<GraphQLResponse<ModelWithMetadata<T>>> onData = invocation.getArgument(dataConsumerIndex);
onData.accept(response);
return new NoOpCancelable();
};
arrangeSubscription(appSync, answer, modelSchema, subscriptionType);
}
use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.
the class AWSS3StorageDownloadTest method testDownloadFileIsCancelable.
/**
* Tests that file download operation can be canceled while the
* transfer hasn't completed yet.
*
* @throws Exception if download is not canceled successfully
* before timeout
*/
@Ignore("Contains tests that hang, or hang the suite overall.")
@SuppressWarnings("unchecked")
public void testDownloadFileIsCancelable() throws Exception {
final CountDownLatch canceled = new CountDownLatch(1);
final AtomicReference<Cancelable> opContainer = new AtomicReference<>();
final AtomicReference<Throwable> errorContainer = new AtomicReference<>();
// Listen to Hub events for cancel
SubscriptionToken cancelToken = Amplify.Hub.subscribe(HubChannel.STORAGE, hubEvent -> {
if (StorageChannelEventName.DOWNLOAD_STATE.toString().equals(hubEvent.getName())) {
HubEvent<String> stateEvent = (HubEvent<String>) hubEvent;
TransferState state = TransferState.getState(stateEvent.getData());
if (TransferState.CANCELED.equals(state)) {
canceled.countDown();
}
}
});
subscriptions.add(cancelToken);
// Begin downloading a large file
StorageDownloadFileOperation<?> op = storageCategory.downloadFile(LARGE_FILE_NAME, downloadFile, options, progress -> {
if (progress.getCurrentBytes() > 0 && canceled.getCount() > 0) {
opContainer.get().cancel();
}
}, result -> errorContainer.set(new RuntimeException("Download completed without canceling.")), errorContainer::set);
opContainer.set(op);
// Assert that the required conditions have been met
assertTrue(canceled.await(EXTENDED_TIMEOUT_MS, TimeUnit.MILLISECONDS));
assertNull(errorContainer.get());
}
Aggregations