InMemoryStoreTransaction implements DataStoreTransaction { @Override public Iterable<Object> loadObjects(Class<?> entityClass, Optional<FilterExpression> filterExpression, Optional<Sorting> sorting, Optional<Pagination> pagination, RequestScope scope) { DataFetcher fetcher = new DataFetcher() { @Override public Iterable<Object> fetch(Optional<FilterExpression> filterExpression, Optional<Sorting> sorting, Optional<Pagination> pagination, RequestScope scope) { return tx.loadObjects(entityClass, filterExpression, sorting, pagination, scope); } }; return (Iterable<Object>) fetchData(fetcher, entityClass, filterExpression, sorting, pagination, false, scope); } InMemoryStoreTransaction(DataStoreTransaction tx); @Override void save(Object entity, RequestScope scope); @Override void delete(Object entity, RequestScope scope); @Override User accessUser(Object opaqueUser); @Override void preCommit(); @Override T createNewObject(Class<T> entityClass); @Override Object getRelation(DataStoreTransaction relationTx,
                              Object entity,
                              String relationName,
                              Optional<FilterExpression> filterExpression,
                              Optional<Sorting> sorting,
                              Optional<Pagination> pagination,
                              RequestScope scope); @Override void updateToManyRelation(DataStoreTransaction relationTx,
                                     Object entity,
                                     String relationName,
                                     Set<Object> newRelationships,
                                     Set<Object> deletedRelationships,
                                     RequestScope scope); @Override void updateToOneRelation(DataStoreTransaction relationTx,
                                    Object entity,
                                    String relationName,
                                    Object relationshipValue,
                                    RequestScope scope); @Override Object getAttribute(Object entity, String attributeName, RequestScope scope); @Override void setAttribute(Object entity, String attributeName, Object attributeValue, RequestScope scope); @Override void flush(RequestScope scope); @Override void commit(RequestScope scope); @Override void createObject(Object entity, RequestScope scope); @Override Object loadObject(Class<?> entityClass,
                      Serializable id,
                      Optional<FilterExpression> filterExpression,
                      RequestScope scope); @Override Iterable<Object> loadObjects(Class<?> entityClass,
                                        Optional<FilterExpression> filterExpression,
                                        Optional<Sorting> sorting,
                                        Optional<Pagination> pagination,
                                        RequestScope scope); @Override void close();  }
@Test public void testFullFilterPredicatePushDown() { FilterExpression expression = new InPredicate(new Path(Book.class, dictionary, "genre"), "Literary Fiction"); when(wrappedTransaction.supportsFiltering(eq(Book.class), any())).thenReturn(DataStoreTransaction.FeatureSupport.FULL); when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.of(expression)), eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books); Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects( Book.class, Optional.of(expression), Optional.empty(), Optional.empty(), scope); verify(wrappedTransaction, times(1)).loadObjects( eq(Book.class), eq(Optional.of(expression)), eq(Optional.empty()), eq(Optional.empty()), eq(scope)); assertEquals(3, loaded.size()); assertTrue(loaded.contains(book1)); assertTrue(loaded.contains(book2)); assertTrue(loaded.contains(book3)); }
@Test public void testTransactionRequiresInMemoryFilterDuringLoad() { FilterExpression expression = new InPredicate(new Path(Book.class, dictionary, "genre"), "Literary Fiction"); when(wrappedTransaction.supportsFiltering(eq(Book.class), any())).thenReturn(DataStoreTransaction.FeatureSupport.FULL); when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.of(expression)), eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books); Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects( Book.class, Optional.of(expression), Optional.empty(), Optional.empty(), scope); verify(wrappedTransaction, times(1)).loadObjects( eq(Book.class), eq(Optional.of(expression)), eq(Optional.empty()), eq(Optional.empty()), eq(scope)); assertEquals(3, loaded.size()); assertTrue(loaded.contains(book1)); assertTrue(loaded.contains(book2)); assertTrue(loaded.contains(book3)); }
@Test public void testDataStoreRequiresTotalInMemoryFilter() { FilterExpression expression = new InPredicate(new Path(Book.class, dictionary, "genre"), "Literary Fiction"); when(wrappedTransaction.supportsFiltering(eq(Book.class), any())).thenReturn(DataStoreTransaction.FeatureSupport.NONE); when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()), eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books); Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects( Book.class, Optional.of(expression), Optional.empty(), Optional.empty(), scope); verify(wrappedTransaction, times(1)).loadObjects( eq(Book.class), eq(Optional.empty()), eq(Optional.empty()), eq(Optional.empty()), eq(scope)); assertEquals(2, loaded.size()); assertTrue(loaded.contains(book1)); assertTrue(loaded.contains(book3)); }
@Test public void testDataStoreRequiresPartialInMemoryFilter() { FilterExpression expression1 = new InPredicate(new Path(Book.class, dictionary, "genre"), "Literary Fiction"); FilterExpression expression2 = new InPredicate(new Path(Book.class, dictionary, "editor.firstName"), "Jane"); FilterExpression expression = new AndFilterExpression(expression1, expression2); when(wrappedTransaction.supportsFiltering(eq(Book.class), any())).thenReturn(DataStoreTransaction.FeatureSupport.PARTIAL); when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.of(expression1)), eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books); Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects( Book.class, Optional.of(expression), Optional.empty(), Optional.empty(), scope); verify(wrappedTransaction, times(1)).loadObjects( eq(Book.class), eq(Optional.of(expression1)), eq(Optional.empty()), eq(Optional.empty()), eq(scope)); assertEquals(1, loaded.size()); assertTrue(loaded.contains(book3)); }
@Test public void testSortingPushDown() { Map<String, Sorting.SortOrder> sortOrder = new HashMap<>(); sortOrder.put("title", Sorting.SortOrder.asc); Sorting sorting = new Sorting(sortOrder); when(wrappedTransaction.supportsFiltering(eq(Book.class), any())).thenReturn(DataStoreTransaction.FeatureSupport.FULL); when(wrappedTransaction.supportsSorting(eq(Book.class), any())).thenReturn(true); when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()), eq(Optional.of(sorting)), eq(Optional.empty()), eq(scope))).thenReturn((Set) books); Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects( Book.class, Optional.empty(), Optional.of(sorting), Optional.empty(), scope); verify(wrappedTransaction, times(1)).loadObjects( eq(Book.class), eq(Optional.empty()), eq(Optional.of(sorting)), eq(Optional.empty()), eq(scope)); assertEquals(3, loaded.size()); }
@Test public void testDataStoreRequiresInMemorySorting() { Map<String, Sorting.SortOrder> sortOrder = new HashMap<>(); sortOrder.put("title", Sorting.SortOrder.asc); Sorting sorting = new Sorting(sortOrder); when(wrappedTransaction.supportsFiltering(eq(Book.class), any())).thenReturn(DataStoreTransaction.FeatureSupport.FULL); when(wrappedTransaction.supportsSorting(eq(Book.class), any())).thenReturn(false); when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()), eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books); Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects( Book.class, Optional.empty(), Optional.of(sorting), Optional.empty(), scope); verify(wrappedTransaction, times(1)).loadObjects( eq(Book.class), eq(Optional.empty()), eq(Optional.empty()), eq(Optional.empty()), eq(scope)); assertEquals(3, loaded.size()); List<String> bookTitles = loaded.stream().map((o) -> ((Book) o).getTitle()).collect(Collectors.toList()); assertEquals(bookTitles, Lists.newArrayList("Book 1", "Book 2", "Book 3")); }
@Test public void testFilteringRequiresInMemorySorting() { FilterExpression expression = new InPredicate(new Path(Book.class, dictionary, "genre"), "Literary Fiction"); Map<String, Sorting.SortOrder> sortOrder = new HashMap<>(); sortOrder.put("title", Sorting.SortOrder.asc); Sorting sorting = new Sorting(sortOrder); when(wrappedTransaction.supportsFiltering(eq(Book.class), any())).thenReturn(DataStoreTransaction.FeatureSupport.NONE); when(wrappedTransaction.supportsSorting(eq(Book.class), any())).thenReturn(true); when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()), eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books); Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects( Book.class, Optional.of(expression), Optional.of(sorting), Optional.empty(), scope); verify(wrappedTransaction, times(1)).loadObjects( eq(Book.class), eq(Optional.empty()), eq(Optional.empty()), eq(Optional.empty()), eq(scope)); assertEquals(2, loaded.size()); List<String> bookTitles = loaded.stream().map((o) -> ((Book) o).getTitle()).collect(Collectors.toList()); assertEquals(Lists.newArrayList("Book 1", "Book 3"), bookTitles); }
@Test public void testPaginationPushDown() { Pagination pagination = Pagination.getDefaultPagination(elideSettings); when(wrappedTransaction.supportsFiltering(eq(Book.class), any())).thenReturn(DataStoreTransaction.FeatureSupport.FULL); when(wrappedTransaction.supportsPagination(eq(Book.class))).thenReturn(true); when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()), eq(Optional.empty()), eq(Optional.of(pagination)), eq(scope))).thenReturn((Set) books); Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects( Book.class, Optional.empty(), Optional.empty(), Optional.of(pagination), scope); verify(wrappedTransaction, times(1)).loadObjects( eq(Book.class), eq(Optional.empty()), eq(Optional.empty()), eq(Optional.of(pagination)), eq(scope)); assertEquals(3, loaded.size()); }
@Test public void testDataStoreRequiresInMemoryPagination() { Pagination pagination = Pagination.getDefaultPagination(elideSettings); when(wrappedTransaction.supportsFiltering(eq(Book.class), any())).thenReturn(DataStoreTransaction.FeatureSupport.FULL); when(wrappedTransaction.supportsPagination(eq(Book.class))).thenReturn(false); when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()), eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books); Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects( Book.class, Optional.empty(), Optional.empty(), Optional.of(pagination), scope); verify(wrappedTransaction, times(1)).loadObjects( eq(Book.class), eq(Optional.empty()), eq(Optional.empty()), eq(Optional.empty()), eq(scope)); assertEquals(3, loaded.size()); assertTrue(loaded.contains(book1)); assertTrue(loaded.contains(book2)); assertTrue(loaded.contains(book3)); }
@Test public void testFilteringRequiresInMemoryPagination() { FilterExpression expression = new InPredicate(new Path(Book.class, dictionary, "genre"), "Literary Fiction"); Pagination pagination = Pagination.getDefaultPagination(elideSettings); when(wrappedTransaction.supportsFiltering(eq(Book.class), any())).thenReturn(DataStoreTransaction.FeatureSupport.NONE); when(wrappedTransaction.supportsPagination(eq(Book.class))).thenReturn(true); when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()), eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books); Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects( Book.class, Optional.of(expression), Optional.empty(), Optional.of(pagination), scope); verify(wrappedTransaction, times(1)).loadObjects( eq(Book.class), eq(Optional.empty()), eq(Optional.empty()), eq(Optional.empty()), eq(scope)); assertEquals(2, loaded.size()); assertTrue(loaded.contains(book1)); assertTrue(loaded.contains(book3)); }
@Test public void testSortingRequiresInMemoryPagination() { Pagination pagination = Pagination.getDefaultPagination(elideSettings); Map<String, Sorting.SortOrder> sortOrder = new HashMap<>(); sortOrder.put("title", Sorting.SortOrder.asc); Sorting sorting = new Sorting(sortOrder); when(wrappedTransaction.supportsFiltering(eq(Book.class), any())).thenReturn(DataStoreTransaction.FeatureSupport.FULL); when(wrappedTransaction.supportsSorting(eq(Book.class), any())).thenReturn(false); when(wrappedTransaction.supportsPagination(eq(Book.class))).thenReturn(true); when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()), eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books); Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects( Book.class, Optional.empty(), Optional.of(sorting), Optional.of(pagination), scope); verify(wrappedTransaction, times(1)).loadObjects( eq(Book.class), eq(Optional.empty()), eq(Optional.empty()), eq(Optional.empty()), eq(scope)); assertEquals(3, loaded.size()); assertTrue(loaded.contains(book1)); assertTrue(loaded.contains(book2)); assertTrue(loaded.contains(book3)); }