use of com.amplifyframework.core.model.query.QuerySortBy 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));
}
use of com.amplifyframework.core.model.query.QuerySortBy in project amplify-android by aws-amplify.
the class ObserveQueryExecutorTest method observeQueryReturnsSortedListOfTotalItemsWithInt.
/**
* ObserveQuery returns sorted list of total items with int.
* @throws InterruptedException interrupted exception.
* @throws AmplifyException data store exception.
*/
@Test
public void observeQueryReturnsSortedListOfTotalItemsWithInt() throws InterruptedException, AmplifyException {
CountDownLatch latch = new CountDownLatch(1);
CountDownLatch changeLatch = new CountDownLatch(1);
AtomicInteger count = new AtomicInteger();
List<Post> posts = new ArrayList<>();
for (int counter = 0; counter < 5; counter++) {
final Post post = Post.builder().title(counter + "-title").status(PostStatus.INACTIVE).rating(counter).build();
posts.add(post);
}
int maxRecords = 50;
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
SyncStatus mockSyncStatus = mock(SyncStatus.class);
when(mockSyncStatus.get(any(), any())).thenReturn(false);
Subject<StorageItemChange<? extends Model>> subject = PublishSubject.<StorageItemChange<? extends Model>>create().toSerialized();
Consumer<DataStoreQuerySnapshot<Post>> onQuerySnapshot = value -> {
if (count.get() == 0) {
Assert.assertTrue(value.getItems().contains(posts.get(0)));
latch.countDown();
} else if (count.get() == 1) {
List<Post> sorted = new ArrayList<>(posts);
Collections.sort(sorted, Comparator.comparing(Post::getRating));
assertEquals(sorted, value.getItems());
Assert.assertEquals(11, value.getItems().size());
changeLatch.countDown();
}
count.getAndIncrement();
};
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
SqlQueryProcessor mockSqlQueryProcessor = mock(SqlQueryProcessor.class);
when(mockSqlQueryProcessor.queryOfflineData(eq(Post.class), any(), any())).thenReturn(posts);
when(mockSqlQueryProcessor.modelExists(any(), any())).thenReturn(true);
ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 5);
ObserveQueryExecutor<Post> observeQueryExecutor = new ObserveQueryExecutor<>(subject, mockSqlQueryProcessor, threadPool, mockSyncStatus, new ModelSorter<>(), maxRecords, 2);
List<QuerySortBy> sortBy = new ArrayList<>();
sortBy.add(Post.RATING.ascending());
observeQueryExecutor.observeQuery(Post.class, new ObserveQueryOptions(null, sortBy), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
Assert.assertTrue(latch.await(2, TimeUnit.SECONDS));
for (int i = 5; i < 11; i++) {
Post itemChange = Post.builder().title(i + "-title").status(PostStatus.INACTIVE).rating(i).build();
posts.add(itemChange);
subject.onNext(StorageItemChange.<Post>builder().changeId(UUID.randomUUID().toString()).initiator(StorageItemChange.Initiator.SYNC_ENGINE).item(itemChange).patchItem(SerializedModel.create(itemChange, ModelSchema.fromModelClass(Post.class))).modelSchema(ModelSchema.fromModelClass(BlogOwner.class)).predicate(QueryPredicates.all()).type(StorageItemChange.Type.CREATE).build());
}
Assert.assertTrue(changeLatch.await(5, TimeUnit.SECONDS));
}
use of com.amplifyframework.core.model.query.QuerySortBy in project amplify-android by aws-amplify.
the class ModelSorter method getComparator.
private Comparator<T> getComparator(List<QuerySortBy> sortByList, Class<T> itemClass, Consumer<DataStoreException> onObservationError) {
QuerySortBy sortBy = sortByList.get(0);
Comparator<T> comparator = new ModelComparator<T>(sortBy, itemClass, onObservationError);
QuerySortOrder sortOrder = sortBy.getSortOrder();
if (sortOrder == QuerySortOrder.DESCENDING) {
comparator = comparator.reversed();
}
for (int i = 1; i < sortByList.size(); i++) {
QuerySortBy nextSortBy = sortByList.get(i);
Comparator<T> nextComparator = comparator.thenComparing(new ModelComparator<T>(nextSortBy, itemClass, onObservationError));
if (nextSortBy.getSortOrder() == QuerySortOrder.DESCENDING) {
nextComparator.reversed();
}
}
return comparator;
}
Aggregations