Search in sources :

Example 56 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class PersistentResourceTest method testLoadRecordSuccess.

@Test()
public void testLoadRecordSuccess() {
    Child child1 = newChild(1);
    EntityProjection collection = EntityProjection.builder().type(Child.class).build();
    when(tx.loadObject(eq(collection), eq(1L), any())).thenReturn(child1);
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    goodScope.setEntityProjection(collection);
    PersistentResource<Child> loaded = PersistentResource.loadRecord(EntityProjection.builder().type(Child.class).build(), "1", goodScope);
    assertEquals(child1, loaded.getObject(), "The load function should return the requested child object");
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) Child(example.Child) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 57 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection 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 58 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class PersistentResourceTest method testLoadRecordInvalidId.

@Test
public void testLoadRecordInvalidId() {
    EntityProjection collection = EntityProjection.builder().type(Child.class).build();
    when(tx.loadObject(eq(collection), eq("1"), any())).thenReturn(null);
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    goodScope.setEntityProjection(collection);
    assertThrows(InvalidObjectIdentifierException.class, () -> PersistentResource.loadRecord(EntityProjection.builder().type(Child.class).build(), "1", goodScope));
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) Child(example.Child) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 59 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class PersistentResourceTest method testTransferPermissionErrorOnUpdateSingularRelationship.

@Test
public void testTransferPermissionErrorOnUpdateSingularRelationship() {
    example.User userModel = new example.User();
    userModel.setId(1);
    NoShareEntity noShare = new NoShareEntity();
    noShare.setId(1);
    List<Resource> idList = new ArrayList<>();
    idList.add(new ResourceIdentifier("noshare", "1").castToResource());
    Relationship ids = new Relationship(null, new Data<>(idList));
    EntityProjection collection = EntityProjection.builder().type(NoShareEntity.class).build();
    when(tx.loadObject(eq(collection), eq(1L), any())).thenReturn(noShare);
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    PersistentResource<example.User> userResource = new PersistentResource<>(userModel, goodScope.getUUIDFor(userModel), goodScope);
    assertThrows(ForbiddenAccessException.class, () -> userResource.updateRelation("noShare", ids.toPersistentResources(goodScope)));
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) TestUser(com.yahoo.elide.core.security.TestUser) User(com.yahoo.elide.core.security.User) 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 60 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class InMemoryStoreTransactionTest method testFilterPredicateInMemoryOnComplexAttribute.

@Test
public void testFilterPredicateInMemoryOnComplexAttribute() {
    FilterExpression expression = new InPredicate(new Path(Author.class, dictionary, "homeAddress.street1"), "Foo");
    EntityProjection projection = EntityProjection.builder().type(Author.class).filterExpression(expression).build();
    DataStoreIterable filterInMemory = new DataStoreIterableBuilder(Arrays.asList(author1, author2)).filterInMemory(true).build();
    when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(filterInMemory);
    Collection<Object> loaded = ImmutableList.copyOf(inMemoryStoreTransaction.loadObjects(projection, scope));
    assertEquals(1, loaded.size());
    assertTrue(loaded.contains(author1));
}
Also used : Path(com.yahoo.elide.core.Path) EntityProjection(com.yahoo.elide.core.request.EntityProjection) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Author(example.Author) 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)

Aggregations

EntityProjection (com.yahoo.elide.core.request.EntityProjection)108 Test (org.junit.jupiter.api.Test)90 Book (example.Book)41 RequestScope (com.yahoo.elide.core.RequestScope)27 Author (example.Author)27 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)22 Publisher (example.Publisher)22 Relationship (com.yahoo.elide.core.request.Relationship)19 Path (com.yahoo.elide.core.Path)18 TestRequestScope (com.yahoo.elide.core.TestRequestScope)18 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)18 HashMap (java.util.HashMap)18 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)18 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)16 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)15 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)15 Collection (java.util.Collection)15 LinkedHashSet (java.util.LinkedHashSet)14 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)13 Editor (example.Editor)13