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());
}
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);
}
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));
}
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()));
}
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());
}
});
}
Aggregations