use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class InMemoryStoreTransactionTest method testFilteringRequiresInMemoryPagination.
@Test
public void testFilteringRequiresInMemoryPagination() {
FilterExpression expression = new InPredicate(new Path(Book.class, dictionary, "genre"), "Literary Fiction");
PaginationImpl pagination = new PaginationImpl(ClassType.of(Book.class), 0, 2, 10, 10, true, false);
EntityProjection projection = EntityProjection.builder().type(Book.class).filterExpression(expression).pagination(pagination).build();
DataStoreIterable filterInMemory = new DataStoreIterableBuilder(books).filterInMemory(true).build();
when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(filterInMemory);
Collection<Object> loaded = Lists.newArrayList(inMemoryStoreTransaction.loadObjects(projection, scope));
verify(wrappedTransaction, times(1)).loadObjects(any(EntityProjection.class), eq(scope));
assertEquals(2, loaded.size());
assertTrue(loaded.contains(book1));
assertTrue(loaded.contains(book3));
assertEquals(2, pagination.getPageTotals());
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class PersistentResourceTest method testTransferPermissionSuccessOnUpdateManyRelationship.
@Test
public void testTransferPermissionSuccessOnUpdateManyRelationship() {
example.User userModel = new example.User();
userModel.setId(1);
NoShareEntity noShare1 = new NoShareEntity();
noShare1.setId(1);
NoShareEntity noShare2 = new NoShareEntity();
noShare2.setId(2);
HashSet<NoShareEntity> noshares = Sets.newHashSet(noShare1, noShare2);
/* The no shares already exist so no exception should be thrown */
userModel.setNoShares(noshares);
List<Resource> idList = new ArrayList<>();
idList.add(new ResourceIdentifier("noshare", "1").castToResource());
Relationship ids = new Relationship(null, new Data<>(idList));
when(tx.loadObject(any(), eq(1L), any())).thenReturn(noShare1);
when(tx.getToManyRelation(any(), eq(userModel), any(), any())).thenReturn(new DataStoreIterableBuilder(noshares).build());
RequestScope goodScope = buildRequestScope(tx, goodUser);
PersistentResource<example.User> userResource = new PersistentResource<>(userModel, goodScope.getUUIDFor(userModel), goodScope);
boolean returnVal = userResource.updateRelation("noShares", ids.toPersistentResources(goodScope));
assertTrue(returnVal);
assertEquals(1, userModel.getNoShares().size());
assertTrue(userModel.getNoShares().contains(noShare1));
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class PersistentResourceTest method testClearRelationFilteredByReadAccess.
@Test()
public void testClearRelationFilteredByReadAccess() {
Parent parent = new Parent();
RequestScope goodScope = buildRequestScope(tx, goodUser);
goodScope.setEntityProjection(EntityProjection.builder().type(Parent.class).relationship("children", EntityProjection.builder().type(Child.class).build()).build());
Child child1 = newChild(1);
Child child2 = newChild(2);
Child child3 = newChild(3);
Child child4 = newChild(-4);
// Not accessible to goodUser
child4.setId(-4);
Child child5 = newChild(-5);
// Not accessible to goodUser
child5.setId(-5);
// All = (1,2,3,4,5)
// Mine = (1,2,3)
Set<Child> allChildren = new HashSet<>();
allChildren.add(child1);
allChildren.add(child2);
allChildren.add(child3);
allChildren.add(child4);
allChildren.add(child5);
parent.setChildren(allChildren);
parent.setSpouses(Sets.newHashSet());
when(tx.getToManyRelation(any(), eq(parent), any(), any())).thenReturn(new DataStoreIterableBuilder(allChildren).build());
PersistentResource<Parent> parentResource = new PersistentResource<>(parent, "1", goodScope);
// Final set after operation = (4,5)
Set<Child> expected = new HashSet<>();
expected.add(child4);
expected.add(child5);
boolean updated = parentResource.clearRelation("children");
goodScope.saveOrCreateObjects();
verify(tx, times(1)).save(parent, goodScope);
verify(tx, times(1)).save(child1, goodScope);
verify(tx, times(1)).save(child2, goodScope);
verify(tx, times(1)).save(child3, goodScope);
verify(tx, never()).save(child4, goodScope);
verify(tx, never()).save(child5, goodScope);
assertTrue(updated, "The relationship should have been partially cleared.");
assertTrue(parent.getChildren().containsAll(expected), "The unfiltered remaining members are left");
assertTrue(expected.containsAll(parent.getChildren()), "The unfiltered remaining members are left");
/*
* No tests for reference integrity since the parent is the owner and
* this is a many to many relationship.
*/
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class PersistentResourceTest method testGetRelationFilteredSuccess.
@Test
public void testGetRelationFilteredSuccess() {
FunWithPermissions fun = new FunWithPermissions();
Child child1 = newChild(1);
Child child2 = newChild(-2);
Child child3 = newChild(3);
Set<Child> children = Sets.newHashSet(child1, child2, child3);
fun.setRelation2(Sets.newHashSet(child1, child2, child3));
RequestScope scope = new TestRequestScope(tx, goodUser, dictionary);
PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, "3", scope);
when(scope.getTransaction().getToManyRelation(any(), eq(fun), any(), any())).thenReturn(new DataStoreIterableBuilder(children).build());
Set<PersistentResource> results = getRelation(funResource, "relation2");
assertEquals(2, results.size(), "Only filtered relation elements should be returned.");
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class PersistentResourceTest method testGetRelationWithPredicateSuccess.
@Test
public void testGetRelationWithPredicateSuccess() {
Parent parent = newParent(1);
Child child1 = newChild(1, "paul john");
Child child2 = newChild(2, "john buzzard");
Child child3 = newChild(3, "chris smith");
parent.setChildren(Sets.newHashSet(child1, child2, child3));
when(tx.getToManyRelation(eq(tx), any(), any(), any())).thenReturn(new DataStoreIterableBuilder(Sets.newHashSet(child1)).build());
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
queryParams.add("filter[child.name]", "paul john");
RequestScope goodScope = buildRequestScope("/child", tx, goodUser, queryParams);
PersistentResource<Parent> parentResource = new PersistentResource<>(parent, "1", goodScope);
Set<PersistentResource> results = getRelation(parentResource, "children");
assertEquals(1, results.size());
assertEquals("paul john", ((Child) IterableUtils.first(results).getObject()).getName());
}
Aggregations