use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class PersistentResourceTest method testClearToManyRelationSuccess.
@Test()
public void testClearToManyRelationSuccess() {
Child child = newChild(1);
Parent parent1 = newParent(1, child);
Parent parent2 = newParent(2, child);
Parent parent3 = newParent(3, child);
Set<Parent> parents = Sets.newHashSet(parent1, parent2, parent3);
child.setParents(parents);
when(tx.getToManyRelation(any(), eq(child), any(), any())).thenReturn(new DataStoreIterableBuilder(parents).build());
RequestScope goodScope = buildRequestScope(tx, goodUser);
goodScope.setEntityProjection(EntityProjection.builder().type(Child.class).relationship("parents", EntityProjection.builder().type(Parent.class).build()).build());
PersistentResource<Child> childResource = new PersistentResource<>(child, "1", goodScope);
childResource.clearRelation("parents");
assertEquals(0, child.getParents().size(), "The many-2-many relationship should be cleared");
assertEquals(0, parent1.getChildren().size(), "The many-2-many inverse relationship should be cleared");
assertEquals(0, parent3.getChildren().size(), "The many-2-many inverse relationship should be cleared");
assertEquals(0, parent3.getChildren().size(), "The many-2-many inverse relationship should be cleared");
goodScope.saveOrCreateObjects();
verify(tx, times(1)).save(child, goodScope);
verify(tx, times(1)).save(parent1, goodScope);
verify(tx, times(1)).save(parent2, goodScope);
verify(tx, times(1)).save(parent3, goodScope);
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class PersistentResourceTest method testGetRelationSuccess.
@Test
public void testGetRelationSuccess() {
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(children);
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(3, results.size(), "All of relation elements should be returned.");
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class PersistentResourceTest method testSuccessfulManyToManyRelationshipUpdate.
@Test
public /*
* The following are ids for a hypothetical relationship.
* GIVEN:
* all (all the ids in the DB) = 1,2,3,4,5
* mine (everything the current user has access to) = 1,2,3
* requested (what the user wants to change to) = 3,6
* THEN:
* deleted (what gets removed from the DB) = 1,2
* final (what get stored in the relationship) = 3,4,5,6
* BECAUSE:
* notMine = all - mine
* updated = (requested UNION mine) - (requested INTERSECT mine)
* deleted = (mine - requested)
* final = (notMine) UNION requested
*/
void testSuccessfulManyToManyRelationshipUpdate() throws Exception {
Parent parent = new Parent();
RequestScope goodScope = buildRequestScope(tx, goodUser);
Child child1 = newChild(1);
Child child2 = newChild(2);
Child child3 = newChild(3);
// Not accessible to goodUser
Child child4 = newChild(-4);
// Not accessible to goodUser
Child child5 = newChild(-5);
Child child6 = newChild(6);
// 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);
// Requested = (3,6)
List<Resource> idList = new ArrayList<>();
idList.add(new ResourceIdentifier("child", "3").castToResource());
idList.add(new ResourceIdentifier("child", "6").castToResource());
Relationship ids = new Relationship(null, new Data<>(idList));
when(tx.loadObject(any(), eq(2L), any())).thenReturn(child2);
when(tx.loadObject(any(), eq(3L), any())).thenReturn(child3);
when(tx.loadObject(any(), eq(-4L), any())).thenReturn(child4);
when(tx.loadObject(any(), eq(-5L), any())).thenReturn(child5);
when(tx.loadObject(any(), eq(6L), any())).thenReturn(child6);
// Final set after operation = (3,4,5,6)
Set<Child> expected = new HashSet<>();
expected.add(child3);
expected.add(child4);
expected.add(child5);
expected.add(child6);
boolean updated = parentResource.updateRelation("children", ids.toPersistentResources(goodScope));
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(child6, goodScope);
verify(tx, never()).save(child4, goodScope);
verify(tx, never()).save(child5, goodScope);
verify(tx, never()).save(child3, goodScope);
assertTrue(updated, "Many-2-many relationship should be updated.");
assertTrue(parent.getChildren().containsAll(expected), "All expected members were updated");
assertTrue(expected.containsAll(parent.getChildren()), "All expected members were updated");
/*
* 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 testOwningRelationshipInverseUpdates.
@Test
public void testOwningRelationshipInverseUpdates() {
Parent parent = newParent(1);
Child child = newChild(2);
when(tx.getToManyRelation(any(), eq(parent), any(), any())).thenReturn(new DataStoreIterableBuilder(parent.getChildren()).build());
RequestScope goodScope = buildRequestScope(tx, goodUser);
PersistentResource<Parent> parentResource = new PersistentResource<>(parent, goodScope.getUUIDFor(parent), goodScope);
PersistentResource<Child> childResource = new PersistentResource<>(child, parentResource, "children", goodScope.getUUIDFor(child), goodScope);
parentResource.addRelation("children", childResource);
goodScope.saveOrCreateObjects();
goodScope.getDirtyResources().clear();
verify(tx, times(1)).save(parent, goodScope);
verify(tx, times(1)).save(child, goodScope);
assertEquals(1, parent.getChildren().size(), "The owning relationship should be updated");
assertTrue(parent.getChildren().contains(child), "The owning relationship should be updated");
assertEquals(1, child.getParents().size(), "The non-owning relationship should also be updated");
assertTrue(child.getParents().contains(parent), "The non-owning relationship should also be updated");
reset(tx);
when(tx.getToManyRelation(any(), eq(parent), any(), any())).thenReturn(new DataStoreIterableBuilder(parent.getChildren()).build());
parentResource.clearRelation("children");
goodScope.saveOrCreateObjects();
verify(tx, times(1)).save(parent, goodScope);
verify(tx, times(1)).save(child, goodScope);
assertEquals(0, parent.getChildren().size(), "The owning relationship should be updated");
assertEquals(0, child.getParents().size(), "The non-owning relationship should also be updated");
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class PersistentResourceTest method testUpdateToManyRelationHookInUpdateRelationBidirection.
@Test
public void testUpdateToManyRelationHookInUpdateRelationBidirection() {
Parent parent = new Parent();
Child child1 = newChild(1);
Child child2 = newChild(2);
Child child3 = newChild(3);
Set<Child> children = Sets.newHashSet(child1, child2);
parent.setChildren(children);
child1.setParents(Sets.newHashSet(parent));
child2.setParents(Sets.newHashSet(parent));
when(tx.getToManyRelation(any(), eq(parent), any(), any())).thenReturn(new DataStoreIterableBuilder(children).build());
RequestScope goodScope = buildRequestScope(tx, goodUser);
PersistentResource<Parent> parentResource = new PersistentResource<>(parent, "3", goodScope);
PersistentResource<Child> childResource1 = new PersistentResource<>(child1, "1", goodScope);
PersistentResource<Child> childResource3 = new PersistentResource<>(child3, "1", goodScope);
parentResource.updateRelation("children", Sets.newHashSet(childResource1, childResource3));
verify(tx, times(1)).updateToManyRelation(eq(tx), eq(parent), any(), any(), any(), eq(goodScope));
verify(tx, never()).updateToManyRelation(eq(tx), eq(child1), any(), any(), any(), eq(goodScope));
verify(tx, times(1)).updateToManyRelation(eq(tx), eq(child2), any(), any(), any(), eq(goodScope));
verify(tx, times(1)).updateToManyRelation(eq(tx), eq(child3), any(), any(), any(), eq(goodScope));
}
Aggregations