Search in sources :

Example 21 with Cancelable

use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.

the class AWSS3StorageUploadTest method testUploadFileIsCancelable.

/**
 * Tests that file upload operation can be canceled while the
 * transfer hasn't completed yet.
 *
 * @throws Exception if upload is not canceled successfully
 *         before timeout
 */
@SuppressWarnings("unchecked")
@Ignore("Contains test which either hang themselves, or hang the suite overall.")
public void testUploadFileIsCancelable() throws Exception {
    final CountDownLatch canceled = new CountDownLatch(1);
    final AtomicReference<Cancelable> opContainer = new AtomicReference<>();
    final AtomicReference<Throwable> errorContainer = new AtomicReference<>();
    // Create a file large enough that transfer won't finish before being canceled
    File uploadFile = new RandomTempFile(LARGE_FILE_SIZE);
    // Listen to Hub events for cancel
    SubscriptionToken cancelToken = Amplify.Hub.subscribe(HubChannel.STORAGE, hubEvent -> {
        if (StorageChannelEventName.UPLOAD_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 uploading a large file
    StorageUploadFileOperation<?> op = storageCategory.uploadFile(uploadFile.getName(), uploadFile, options, progress -> {
        if (progress.getCurrentBytes() > 0) {
            opContainer.get().cancel();
        }
    }, result -> errorContainer.set(new RuntimeException("Upload 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());
}
Also used : RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) HubEvent(com.amplifyframework.hub.HubEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TransferState(com.amazonaws.mobileconnectors.s3.transferutility.TransferState) SubscriptionToken(com.amplifyframework.hub.SubscriptionToken) Cancelable(com.amplifyframework.core.async.Cancelable) File(java.io.File) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Ignore(org.junit.Ignore)

Example 22 with Cancelable

use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterObserveQueryTest method querySavedDataWithSingleItem.

/**
 * 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 querySavedDataWithSingleItem() throws DataStoreException, InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    final BlogOwner blogOwner = BlogOwner.builder().name("Alan Turing").build();
    adapter.save(blogOwner);
    Consumer<Cancelable> observationStarted = NoOpConsumer.create();
    Consumer<DataStoreQuerySnapshot<BlogOwner>> onQuerySnapshot = value -> {
        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));
}
Also used : Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) Blog(com.amplifyframework.testmodels.commentsblog.Blog) ArrayList(java.util.ArrayList) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) HashSet(java.util.HashSet) Consumer(com.amplifyframework.core.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) NoOpConsumer(com.amplifyframework.core.NoOpConsumer) PostStatus(com.amplifyframework.testmodels.commentsblog.PostStatus) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Before(org.junit.Before) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Comment(com.amplifyframework.testmodels.commentsblog.Comment) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Action(com.amplifyframework.core.Action) StrictMode(com.amplifyframework.datastore.StrictMode) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) QuerySortBy(com.amplifyframework.core.model.query.QuerySortBy) Comparator(java.util.Comparator) NoOpAction(com.amplifyframework.core.NoOpAction) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Post(com.amplifyframework.testmodels.commentsblog.Post) Collections(java.util.Collections) QueryPredicate.not(com.amplifyframework.core.model.query.predicate.QueryPredicate.not) Assert.assertEquals(org.junit.Assert.assertEquals) DataStoreException(com.amplifyframework.datastore.DataStoreException) Action(com.amplifyframework.core.Action) NoOpAction(com.amplifyframework.core.NoOpAction) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) CountDownLatch(java.util.concurrent.CountDownLatch) Cancelable(com.amplifyframework.core.async.Cancelable) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Test(org.junit.Test)

Example 23 with Cancelable

use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterObserveQueryTest method observeQueryWithOrderBy.

/**
 * Test observe query with order by.
 * Validate that a list of BlogOwners can be sorted first by name in descending order,
 * then by wea in ascending order.
 *
 * @throws DataStoreException On failure to arrange items into store, or from the
 * query action itself
 * @throws InterruptedException interruptedException.
 */
@Test
public void observeQueryWithOrderBy() throws DataStoreException, InterruptedException {
    // Expect
    List<String> names = Arrays.asList("Joe", "Joe", "Joe", "Bob", "Bob", "Bob", "Dan", "Dan", "Dan");
    List<String> weas = Arrays.asList("pon", "lth", "ver", "kly", "ken", "sel", "ner", "rer", "ned");
    List<BlogOwner> owners = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);
    for (int i = 0; i < names.size(); i++) {
        BlogOwner owner = BlogOwner.builder().name(names.get(i)).wea(weas.get(i)).build();
        adapter.save(owner);
        owners.add(owner);
    }
    List<BlogOwner> sorted = new ArrayList<>(owners);
    Collections.sort(sorted, Comparator.comparing(BlogOwner::getName).reversed().thenComparing(BlogOwner::getWea));
    Consumer<Cancelable> observationStarted = NoOpConsumer.create();
    Consumer<DataStoreQuerySnapshot<BlogOwner>> 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.descending());
    sortBy.add(BlogOwner.WEA.ascending());
    // Act
    adapter.observeQuery(BlogOwner.class, new ObserveQueryOptions(null, sortBy), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
    assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) Blog(com.amplifyframework.testmodels.commentsblog.Blog) ArrayList(java.util.ArrayList) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) HashSet(java.util.HashSet) Consumer(com.amplifyframework.core.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) NoOpConsumer(com.amplifyframework.core.NoOpConsumer) PostStatus(com.amplifyframework.testmodels.commentsblog.PostStatus) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Before(org.junit.Before) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Comment(com.amplifyframework.testmodels.commentsblog.Comment) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Action(com.amplifyframework.core.Action) StrictMode(com.amplifyframework.datastore.StrictMode) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) QuerySortBy(com.amplifyframework.core.model.query.QuerySortBy) Comparator(java.util.Comparator) NoOpAction(com.amplifyframework.core.NoOpAction) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Post(com.amplifyframework.testmodels.commentsblog.Post) Collections(java.util.Collections) QueryPredicate.not(com.amplifyframework.core.model.query.predicate.QueryPredicate.not) Assert.assertEquals(org.junit.Assert.assertEquals) DataStoreException(com.amplifyframework.datastore.DataStoreException) Action(com.amplifyframework.core.Action) NoOpAction(com.amplifyframework.core.NoOpAction) QuerySortBy(com.amplifyframework.core.model.query.QuerySortBy) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Cancelable(com.amplifyframework.core.async.Cancelable) Test(org.junit.Test)

Example 24 with Cancelable

use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterObserveQueryTest method queryWithMaliciousPredicates.

/**
 * Test query with SQL injection.
 *
 * @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 queryWithMaliciousPredicates() throws DataStoreException, InterruptedException {
    final BlogOwner jane = BlogOwner.builder().name("Jane Doe").build();
    adapter.save(jane);
    QueryPredicate predicate = BlogOwner.NAME.eq("Jane; DROP TABLE Person; --");
    CountDownLatch latch = new CountDownLatch(2);
    Consumer<Cancelable> observationStarted = NoOpConsumer.create();
    Consumer<DataStoreQuerySnapshot<BlogOwner>> onMaliciousQuerySnapshot = resultOfMaliciousQuery -> {
        assertTrue(resultOfMaliciousQuery.getItems().isEmpty());
        latch.countDown();
    };
    Consumer<DataStoreException> onObservationError = value -> {
    };
    Action onObservationComplete = NoOpAction.create();
    adapter.observeQuery(BlogOwner.class, new ObserveQueryOptions(predicate, null), observationStarted, onMaliciousQuerySnapshot, onObservationError, onObservationComplete);
    Consumer<DataStoreQuerySnapshot<BlogOwner>> onAfterMaliciousQuery = resultAfterMaliciousQuery -> {
        assertTrue(resultAfterMaliciousQuery.getItems().contains(jane));
        latch.countDown();
    };
    adapter.observeQuery(BlogOwner.class, new ObserveQueryOptions(null, null), observationStarted, onAfterMaliciousQuery, onObservationError, onObservationComplete);
    assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) Blog(com.amplifyframework.testmodels.commentsblog.Blog) ArrayList(java.util.ArrayList) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) HashSet(java.util.HashSet) Consumer(com.amplifyframework.core.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) NoOpConsumer(com.amplifyframework.core.NoOpConsumer) PostStatus(com.amplifyframework.testmodels.commentsblog.PostStatus) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Before(org.junit.Before) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Comment(com.amplifyframework.testmodels.commentsblog.Comment) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Action(com.amplifyframework.core.Action) StrictMode(com.amplifyframework.datastore.StrictMode) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) QuerySortBy(com.amplifyframework.core.model.query.QuerySortBy) Comparator(java.util.Comparator) NoOpAction(com.amplifyframework.core.NoOpAction) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Post(com.amplifyframework.testmodels.commentsblog.Post) Collections(java.util.Collections) QueryPredicate.not(com.amplifyframework.core.model.query.predicate.QueryPredicate.not) Assert.assertEquals(org.junit.Assert.assertEquals) DataStoreException(com.amplifyframework.datastore.DataStoreException) Action(com.amplifyframework.core.Action) NoOpAction(com.amplifyframework.core.NoOpAction) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) CountDownLatch(java.util.concurrent.CountDownLatch) Cancelable(com.amplifyframework.core.async.Cancelable) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Test(org.junit.Test)

Example 25 with Cancelable

use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterObserveQueryTest method querySavedDataWithMultipleItemsThenItemSaves.

/**
 * 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 querySavedDataWithMultipleItemsThenItemSaves() throws DataStoreException, InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    CountDownLatch changeLatch = new CountDownLatch(1);
    Consumer<Cancelable> observationStarted = NoOpConsumer.create();
    Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
    Action onObservationComplete = NoOpAction.create();
    final List<BlogOwner> savedModels = new ArrayList<>();
    final int numModels = 10;
    AtomicInteger count = new AtomicInteger(0);
    for (int counter = 0; counter < numModels; counter++) {
        final BlogOwner blogOwner = BlogOwner.builder().name("namePrefix:" + counter).build();
        adapter.save(blogOwner);
        savedModels.add(blogOwner);
    }
    Consumer<DataStoreQuerySnapshot<BlogOwner>> onQuerySnapshot = value -> {
        if (count.get() == 0) {
            for (BlogOwner blogOwner : savedModels) {
                assertTrue(value.getItems().contains(blogOwner));
            }
            latch.countDown();
        } else {
            assertEquals(12, value.getItems().size());
            changeLatch.countDown();
        }
        count.incrementAndGet();
    };
    adapter.observeQuery(BlogOwner.class, new ObserveQueryOptions(null, null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
    assertTrue(latch.await(30, TimeUnit.SECONDS));
    for (int counter = 11; counter < 13; counter++) {
        final BlogOwner blogOwner = BlogOwner.builder().name("namePrefix:" + counter).build();
        savedModels.add(blogOwner);
        adapter.save(blogOwner);
    }
    assertTrue(changeLatch.await(30, TimeUnit.SECONDS));
}
Also used : Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) Blog(com.amplifyframework.testmodels.commentsblog.Blog) ArrayList(java.util.ArrayList) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) HashSet(java.util.HashSet) Consumer(com.amplifyframework.core.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) NoOpConsumer(com.amplifyframework.core.NoOpConsumer) PostStatus(com.amplifyframework.testmodels.commentsblog.PostStatus) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Before(org.junit.Before) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Comment(com.amplifyframework.testmodels.commentsblog.Comment) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Action(com.amplifyframework.core.Action) StrictMode(com.amplifyframework.datastore.StrictMode) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) QuerySortBy(com.amplifyframework.core.model.query.QuerySortBy) Comparator(java.util.Comparator) NoOpAction(com.amplifyframework.core.NoOpAction) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Post(com.amplifyframework.testmodels.commentsblog.Post) Collections(java.util.Collections) QueryPredicate.not(com.amplifyframework.core.model.query.predicate.QueryPredicate.not) Assert.assertEquals(org.junit.Assert.assertEquals) DataStoreException(com.amplifyframework.datastore.DataStoreException) Action(com.amplifyframework.core.Action) NoOpAction(com.amplifyframework.core.NoOpAction) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Cancelable(com.amplifyframework.core.async.Cancelable)

Aggregations

Cancelable (com.amplifyframework.core.async.Cancelable)40 Test (org.junit.Test)30 DataStoreException (com.amplifyframework.datastore.DataStoreException)29 Action (com.amplifyframework.core.Action)28 Consumer (com.amplifyframework.core.Consumer)26 CountDownLatch (java.util.concurrent.CountDownLatch)24 ArrayList (java.util.ArrayList)23 List (java.util.List)23 TimeUnit (java.util.concurrent.TimeUnit)23 ObserveQueryOptions (com.amplifyframework.core.model.query.ObserveQueryOptions)22 DataStoreQuerySnapshot (com.amplifyframework.datastore.DataStoreQuerySnapshot)22 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)22 Collections (java.util.Collections)22 Model (com.amplifyframework.core.model.Model)20 Arrays (java.util.Arrays)20 NoOpAction (com.amplifyframework.core.NoOpAction)19 NoOpConsumer (com.amplifyframework.core.NoOpConsumer)19 QuerySortBy (com.amplifyframework.core.model.query.QuerySortBy)19 Post (com.amplifyframework.testmodels.commentsblog.Post)19 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)19