Search in sources :

Example 6 with DataStoreIterableBuilder

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());
}
Also used : Path(com.yahoo.elide.core.Path) EntityProjection(com.yahoo.elide.core.request.EntityProjection) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Book(example.Book) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Test(org.junit.jupiter.api.Test)

Example 7 with DataStoreIterableBuilder

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));
}
Also used : TestUser(com.yahoo.elide.core.security.TestUser) User(com.yahoo.elide.core.security.User) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) 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) NoShareEntity(example.NoShareEntity) Test(org.junit.jupiter.api.Test)

Example 8 with DataStoreIterableBuilder

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.
         */
}
Also used : DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Parent(example.Parent) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Child(example.Child) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 9 with DataStoreIterableBuilder

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.");
}
Also used : DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) FunWithPermissions(example.FunWithPermissions) Child(example.Child) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 10 with DataStoreIterableBuilder

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());
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) 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)

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