Search in sources :

Example 11 with Relationship

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

the class JpaDataStoreTransactionTest method testGetRelationDelegation.

@ParameterizedTest
@MethodSource("getTestArguments")
public void testGetRelationDelegation(boolean delegateToInMemory, int numberOfAuthors, FilterExpression filter, boolean usesInMemory) throws Exception {
    AbstractJpaTransaction tx = new AbstractJpaTransaction(entityManager, (unused) -> {
    }, DEFAULT_LOGGER, delegateToInMemory, false) {

        @Override
        public boolean isOpen() {
            return false;
        }

        @Override
        public void begin() {
        }

        @Override
        protected Predicate<Collection<?>> isPersistentCollection() {
            return (unused) -> true;
        }
    };
    EntityProjection projection = EntityProjection.builder().type(Author.class).build();
    List<Author> authors = new ArrayList<>();
    Author author1 = mock(Author.class);
    authors.add(author1);
    for (int idx = 1; idx < numberOfAuthors; idx++) {
        authors.add(mock(Author.class));
    }
    when(query.getResultList()).thenReturn(authors);
    DataStoreIterable<Author> loadedAuthors = tx.loadObjects(projection, scope);
    assertFalse(loadedAuthors.needsInMemoryPagination());
    assertFalse(loadedAuthors.needsInMemorySort());
    assertFalse(loadedAuthors.needsInMemoryFilter());
    Relationship relationship = Relationship.builder().name("books").projection(EntityProjection.builder().type(Book.class).filterExpression(filter).build()).build();
    PersistentSet returnCollection = mock(PersistentSet.class);
    when(author1.getBooks()).thenReturn(returnCollection);
    DataStoreIterable<Book> loadedBooks = tx.getToManyRelation(tx, author1, relationship, scope);
    assertEquals(usesInMemory, loadedBooks.needsInMemoryFilter());
    assertEquals(usesInMemory, loadedBooks.needsInMemorySort());
    assertEquals(usesInMemory, loadedBooks.needsInMemoryPagination());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Author(example.Author) ClassType(com.yahoo.elide.core.type.ClassType) PersistentSet(org.hibernate.collection.internal.PersistentSet) ArrayList(java.util.ArrayList) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) TestInstance(org.junit.jupiter.api.TestInstance) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AbstractJpaTransaction(com.yahoo.elide.datastores.jpa.transaction.AbstractJpaTransaction) BeforeAll(org.junit.jupiter.api.BeforeAll) NO_VERSION(com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Relationship(com.yahoo.elide.core.request.Relationship) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) RequestScope(com.yahoo.elide.core.RequestScope) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) DEFAULT_LOGGER(com.yahoo.elide.datastores.jpa.JpaDataStore.DEFAULT_LOGGER) Predicate(java.util.function.Predicate) Collection(java.util.Collection) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Mockito.when(org.mockito.Mockito.when) EntityManager(javax.persistence.EntityManager) Arguments(org.junit.jupiter.params.provider.Arguments) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Query(javax.persistence.Query) Stream(java.util.stream.Stream) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) EntityProjection(com.yahoo.elide.core.request.EntityProjection) ArrayList(java.util.ArrayList) PersistentSet(org.hibernate.collection.internal.PersistentSet) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) Collection(java.util.Collection) Author(example.Author) AbstractJpaTransaction(com.yahoo.elide.datastores.jpa.transaction.AbstractJpaTransaction) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 12 with Relationship

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

the class InMemoryStoreTransactionTest method testGetToOneRelationship.

@Test
public void testGetToOneRelationship() {
    Relationship relationship = Relationship.builder().projection(EntityProjection.builder().type(Book.class).build()).name("publisher").alias("publisher").build();
    when(wrappedTransaction.getToOneRelation(eq(inMemoryStoreTransaction), eq(book1), any(), eq(scope))).thenReturn(publisher1);
    Publisher loaded = inMemoryStoreTransaction.getToOneRelation(inMemoryStoreTransaction, book1, relationship, scope);
    verify(wrappedTransaction, times(1)).getToOneRelation(eq(inMemoryStoreTransaction), eq(book1), eq(relationship), eq(scope));
    assertEquals(publisher1, loaded);
}
Also used : Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) Publisher(example.Publisher) Test(org.junit.jupiter.api.Test)

Example 13 with Relationship

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

the class InMemoryStoreTransactionTest method testTransactionRequiresInMemoryFilterDuringGetRelation.

@Test
public void testTransactionRequiresInMemoryFilterDuringGetRelation() {
    FilterExpression expression = new InPredicate(new Path(Book.class, dictionary, "genre"), "Literary Fiction");
    Relationship relationship = Relationship.builder().projection(EntityProjection.builder().type(Book.class).filterExpression(expression).build()).name("books").alias("books").build();
    ArgumentCaptor<Relationship> relationshipArgument = ArgumentCaptor.forClass(Relationship.class);
    when(scope.getNewPersistentResources()).thenReturn(Sets.newHashSet(mock(PersistentResource.class)));
    when(wrappedTransaction.getToManyRelation(eq(inMemoryStoreTransaction), eq(author1), any(), eq(scope))).thenReturn(new DataStoreIterableBuilder<>(books).build());
    Collection<Object> loaded = ImmutableList.copyOf((Iterable) inMemoryStoreTransaction.getToManyRelation(inMemoryStoreTransaction, author1, relationship, scope));
    verify(wrappedTransaction, times(1)).getToManyRelation(eq(inMemoryStoreTransaction), eq(author1), relationshipArgument.capture(), eq(scope));
    assertNull(relationshipArgument.getValue().getProjection().getFilterExpression());
    assertNull(relationshipArgument.getValue().getProjection().getSorting());
    assertNull(relationshipArgument.getValue().getProjection().getPagination());
    assertEquals(2, loaded.size());
    assertTrue(loaded.contains(book1));
    assertTrue(loaded.contains(book3));
}
Also used : Path(com.yahoo.elide.core.Path) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Test(org.junit.jupiter.api.Test)

Example 14 with Relationship

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

the class RecordState method handle.

@Override
public void handle(StateContext state, SubCollectionSubCollectionContext ctx) {
    String id = ctx.entity().id().getText();
    String subCollection = ctx.entity().term().getText();
    Relationship relationship = projection.getRelationship(subCollection).orElseThrow(IllegalStateException::new);
    state.setState(new RecordState(resource.getRelation(relationship, id), relationship.getProjection()));
}
Also used : Relationship(com.yahoo.elide.core.request.Relationship)

Example 15 with Relationship

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

the class IncludedProcessor method addResourcesForPath.

/**
 * Adds all the relation resources for a given relation path to the included block of the
 * JsonApiDocument.
 */
private void addResourcesForPath(JsonApiDocument jsonApiDocument, PersistentResource<?> rec, List<String> relationPath, EntityProjection projection) {
    // Pop off a relation of relation path
    String relation = relationPath.remove(0);
    Set<PersistentResource> collection;
    Relationship relationship = projection.getRelationship(relation).orElseThrow(IllegalStateException::new);
    try {
        collection = rec.getRelationCheckedFiltered(relationship).toList(LinkedHashSet::new).blockingGet();
    } catch (ForbiddenAccessException e) {
        return;
    }
    collection.forEach(resource -> {
        jsonApiDocument.addIncluded(resource.toResource());
        // If more relations left in the path, process a level deeper
        if (!relationPath.isEmpty()) {
            // Use a copy of the relationPath to preserve the path for remaining branches of the relationship tree
            addResourcesForPath(jsonApiDocument, resource, new ArrayList<>(relationPath), relationship.getProjection());
        }
    });
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PersistentResource(com.yahoo.elide.core.PersistentResource) Relationship(com.yahoo.elide.core.request.Relationship) ForbiddenAccessException(com.yahoo.elide.core.exceptions.ForbiddenAccessException)

Aggregations

Relationship (com.yahoo.elide.core.request.Relationship)22 Book (example.Book)15 Test (org.junit.jupiter.api.Test)15 EntityProjection (com.yahoo.elide.core.request.EntityProjection)14 RelationshipImpl (com.yahoo.elide.datastores.jpql.query.RelationshipImpl)12 Author (example.Author)12 SubCollectionFetchQueryBuilder (com.yahoo.elide.datastores.jpql.query.SubCollectionFetchQueryBuilder)8 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)5 Publisher (example.Publisher)5 HashMap (java.util.HashMap)5 Path (com.yahoo.elide.core.Path)4 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)4 SubCollectionPageTotalsQueryBuilder (com.yahoo.elide.datastores.jpql.query.SubCollectionPageTotalsQueryBuilder)4 PersistentResource (com.yahoo.elide.core.PersistentResource)3 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)3 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)3 RequestScope (com.yahoo.elide.core.RequestScope)2 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)2 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)2 PaginationImpl (com.yahoo.elide.core.pagination.PaginationImpl)2