Search in sources :

Example 26 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.

the class PersistentResourceTest method testLoadRecords.

@Test()
public void testLoadRecords() {
    Child child1 = newChild(1);
    Child child2 = newChild(-2);
    Child child3 = newChild(-3);
    Child child4 = newChild(4);
    Child child5 = newChild(5);
    EntityProjection collection = EntityProjection.builder().type(Child.class).build();
    when(tx.loadObjects(eq(collection), any(RequestScope.class))).thenReturn(new DataStoreIterableBuilder(Lists.newArrayList(child1, child2, child3, child4, child5)).build());
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    goodScope.setEntityProjection(collection);
    Set<PersistentResource> loaded = PersistentResource.loadRecords(EntityProjection.builder().type(Child.class).build(), new ArrayList<>(), goodScope).toList(LinkedHashSet::new).blockingGet();
    Set<Child> expected = Sets.newHashSet(child1, child4, child5);
    Set<Object> actual = loaded.stream().map(PersistentResource::getObject).collect(Collectors.toSet());
    assertEquals(3, actual.size(), "The returned list should be filtered to only include elements that have read permission");
    assertEquals(expected, actual, "The returned list should only include elements with a positive ID");
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Child(example.Child) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 27 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.

the class PersistentResourceTest method testUpdatePermissionCheckedOnInverseRelationship.

@Test
public void testUpdatePermissionCheckedOnInverseRelationship() {
    Left left = new Left();
    left.setId(1);
    Right right = new Right();
    Set<Right> rights = Sets.newHashSet(right);
    left.setNoInverseUpdate(rights);
    right.setNoUpdate(Sets.newHashSet(left));
    List<Resource> empty = new ArrayList<>();
    Relationship ids = new Relationship(null, new Data<>(empty));
    when(tx.getToManyRelation(any(), eq(left), any(), any())).thenReturn(new DataStoreIterableBuilder(rights).build());
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    PersistentResource<Left> leftResource = new PersistentResource<>(left, goodScope.getUUIDFor(left), goodScope);
    assertThrows(ForbiddenAccessException.class, () -> leftResource.updateRelation("noInverseUpdate", ids.toPersistentResources(goodScope)));
    // Modifications have a deferred check component:
    leftResource.getRequestScope().getPermissionExecutor().executeCommitChecks();
}
Also used : Left(example.Left) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Right(example.Right) Resource(com.yahoo.elide.jsonapi.models.Resource) ArrayList(java.util.ArrayList) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 28 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.

the class PersistentResourceTest method testUpdateToManyRelationHookInClearRelationBidirection.

@Test
public void testUpdateToManyRelationHookInClearRelationBidirection() {
    Parent parent = new Parent();
    Child child1 = newChild(1);
    Child child2 = newChild(2);
    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);
    parentResource.clearRelation("children");
    verify(tx, times(1)).updateToManyRelation(eq(tx), eq(parent), any(), any(), any(), eq(goodScope));
    verify(tx, times(1)).updateToManyRelation(eq(tx), eq(child1), any(), any(), any(), eq(goodScope));
    verify(tx, times(1)).updateToManyRelation(eq(tx), eq(child2), any(), any(), any(), eq(goodScope));
}
Also used : DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Parent(example.Parent) Child(example.Child) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 29 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.

the class PersistentResourceTest method testSuccessfulManyToManyRelationshipNoopUpdate.

@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) = 1,2,3
     * THEN:
     * deleted (what gets removed from the DB) = nothing
     * final (what get stored in the relationship) = 1,2,3,4,5
     * BECAUSE:
     * notMine = all - mine
     * updated = (requested UNION mine) - (requested INTERSECT mine)
     * deleted = (mine - requested)
     * final = (notMine) UNION requested
     */
void testSuccessfulManyToManyRelationshipNoopUpdate() 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);
    // 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 = (1,2,3)
    List<Resource> idList = new ArrayList<>();
    idList.add(new ResourceIdentifier("child", "3").castToResource());
    idList.add(new ResourceIdentifier("child", "2").castToResource());
    idList.add(new ResourceIdentifier("child", "1").castToResource());
    Relationship ids = new Relationship(null, new Data<>(idList));
    when(tx.loadObject(any(), eq(1L), any())).thenReturn(child1);
    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);
    // Final set after operation = (1,2,3,4,5)
    Set<Child> expected = new HashSet<>();
    expected.add(child1);
    expected.add(child2);
    expected.add(child3);
    expected.add(child4);
    expected.add(child5);
    boolean updated = parentResource.updateRelation("children", ids.toPersistentResources(goodScope));
    goodScope.saveOrCreateObjects();
    verify(tx, never()).save(parent, goodScope);
    verify(tx, never()).save(child1, goodScope);
    verify(tx, never()).save(child2, goodScope);
    verify(tx, never()).save(child4, goodScope);
    verify(tx, never()).save(child5, goodScope);
    verify(tx, never()).save(child3, goodScope);
    assertFalse(updated, "Many-2-many relationship should not 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.
         */
}
Also used : DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Parent(example.Parent) Resource(com.yahoo.elide.jsonapi.models.Resource) ArrayList(java.util.ArrayList) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Child(example.Child) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 30 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.

the class PersistentResourceTest method testClearRelationInvalidToManyUpdatePermission.

@Test
public void testClearRelationInvalidToManyUpdatePermission() {
    Left left = new Left();
    left.setId(1);
    Right right1 = new Right();
    right1.setId(1);
    Right right2 = new Right();
    right2.setId(2);
    Set<Right> noInverseUpdate = Sets.newHashSet(right1, right2);
    left.setNoInverseUpdate(noInverseUpdate);
    right1.setNoUpdate(Sets.newHashSet(left));
    right2.setNoUpdate(Sets.newHashSet(left));
    when(tx.getToManyRelation(any(), eq(left), any(), any())).thenReturn(new DataStoreIterableBuilder(noInverseUpdate).build());
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    goodScope.setEntityProjection(EntityProjection.builder().type(Left.class).relationship("noInverseUpdate", EntityProjection.builder().type(Right.class).build()).build());
    PersistentResource<Left> leftResource = new PersistentResource<>(left, "1", goodScope);
    assertThrows(ForbiddenAccessException.class, () -> leftResource.clearRelation("noInverseUpdate"));
    // Modifications have a deferred check component:
    leftResource.getRequestScope().getPermissionExecutor().executeCommitChecks();
}
Also used : Left(example.Left) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Right(example.Right) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Aggregations

DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)46 Test (org.junit.jupiter.api.Test)41 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)20 Child (example.Child)16 Book (example.Book)15 EntityProjection (com.yahoo.elide.core.request.EntityProjection)13 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)12 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)11 Parent (example.Parent)11 Path (com.yahoo.elide.core.Path)8 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)7 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)7 Sorting (com.yahoo.elide.core.request.Sorting)7 GraphQLTest (com.yahoo.elide.graphql.GraphQLTest)7 Author (example.Author)7 ArrayList (java.util.ArrayList)7 RequestScope (com.yahoo.elide.core.RequestScope)6 PaginationImpl (com.yahoo.elide.core.pagination.PaginationImpl)6 Relationship (com.yahoo.elide.core.request.Relationship)6 HashMap (java.util.HashMap)6