use of com.yahoo.elide.core.request.Relationship in project elide by yahoo.
the class VerifyFieldAccessFilterExpressionVisitor method getValueChecked.
private Observable<PersistentResource> getValueChecked(PersistentResource<?> resource, String fieldName, RequestScope requestScope) {
EntityDictionary dictionary = resource.getDictionary();
// checkFieldAwareReadPermissions
requestScope.getPermissionExecutor().checkSpecificFieldPermissions(resource, null, ReadPermission.class, fieldName);
Object entity = resource.getObject();
if (entity == null || resource.getDictionary().getRelationshipType(resource.getResourceType(), fieldName) == RelationshipType.NONE) {
return Observable.empty();
}
Relationship relationship = Relationship.builder().name(fieldName).alias(fieldName).projection(EntityProjection.builder().type(dictionary.getParameterizedType(resource.getResourceType(), fieldName)).build()).build();
// use no filter to allow the read directly from loaded resource
return resource.getRelationChecked(relationship);
}
use of com.yahoo.elide.core.request.Relationship in project elide by yahoo.
the class LifeCycleTest method testRemoveFromCollectionTrigger.
@Test
public void testRemoveFromCollectionTrigger() {
PropertyTestModel mockModel = mock(PropertyTestModel.class);
DataStoreTransaction tx = mock(DataStoreTransaction.class);
RequestScope scope = buildRequestScope(dictionary, tx);
when(tx.createNewObject(ClassType.of(PropertyTestModel.class), scope)).thenReturn(mockModel);
PropertyTestModel childModel1 = mock(PropertyTestModel.class);
PropertyTestModel childModel2 = mock(PropertyTestModel.class);
PropertyTestModel childModel3 = mock(PropertyTestModel.class);
when(childModel1.getId()).thenReturn("2");
when(childModel2.getId()).thenReturn("3");
when(childModel3.getId()).thenReturn("4");
// First we test removing from a newly created object.
PersistentResource resource = PersistentResource.createObject(ClassType.of(PropertyTestModel.class), scope, Optional.of("1"));
PersistentResource childResource1 = new PersistentResource(childModel1, "2", scope);
PersistentResource childResource2 = new PersistentResource(childModel2, "3", scope);
PersistentResource childResource3 = new PersistentResource(childModel3, "3", scope);
resource.updateRelation("models", new HashSet<>(Arrays.asList(childResource1, childResource2)));
scope.runQueuedPreSecurityTriggers();
scope.runQueuedPreCommitTriggers();
scope.runQueuedPostCommitTriggers();
verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
ArgumentCaptor<ChangeSpec> changes = ArgumentCaptor.forClass(ChangeSpec.class);
verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(POSTCOMMIT), changes.capture());
changes.getValue().getModified().equals(List.of(childModel1, childModel2));
changes.getValue().getOriginal().equals(List.of());
// Build another resource, scope & reset the mock to do a pure update (no create):
scope = buildRequestScope(dictionary, tx);
resource = new PersistentResource(mockModel, scope.getUUIDFor(mockModel), scope);
reset(mockModel);
Relationship relationship = Relationship.builder().projection(EntityProjection.builder().type(PropertyTestModel.class).build()).name("models").build();
when(tx.getToManyRelation(tx, mockModel, relationship, scope)).thenReturn(new DataStoreIterableBuilder<Object>(Arrays.asList(childModel1, childModel2)).build());
when(mockModel.getModels()).thenReturn(new HashSet<>(Arrays.asList(childModel1, childModel2)));
resource.updateRelation("models", new HashSet<>(Arrays.asList(childResource1, childResource3)));
scope.runQueuedPreSecurityTriggers();
scope.runQueuedPreCommitTriggers();
scope.runQueuedPostCommitTriggers();
verify(mockModel, never()).relationCallback(eq(CREATE), any(), any());
changes = ArgumentCaptor.forClass(ChangeSpec.class);
verify(mockModel, times(1)).relationCallback(eq(UPDATE), eq(POSTCOMMIT), changes.capture());
changes.getValue().getModified().equals(List.of(childModel1, childModel3));
changes.getValue().getOriginal().equals(List.of(childModel1, childModel2));
}
use of com.yahoo.elide.core.request.Relationship in project elide by yahoo.
the class RecordState method handle.
@Override
public void handle(StateContext state, SubCollectionRelationshipContext ctx) {
String id = ctx.entity().id().getText();
String subCollection = ctx.entity().term().getText();
String relationName = ctx.relationship().term().getText();
PersistentResource childRecord;
Relationship childRelationship = projection.getRelationship(subCollection).orElseThrow(IllegalStateException::new);
childRecord = resource.getRelation(childRelationship, id);
state.setState(new RelationshipTerminalState(childRecord, relationName, childRelationship.getProjection()));
}
use of com.yahoo.elide.core.request.Relationship in project elide by yahoo.
the class SubCollectionFetchQueryBuilderTest method testSubCollectionFetchWithJoinFilter.
@Test
public void testSubCollectionFetchWithJoinFilter() {
Author author = new Author();
author.setId(1L);
Book book = new Book();
book.setId(2);
List<Path.PathElement> publisherNamePath = Arrays.asList(new Path.PathElement(Book.class, Publisher.class, PUBLISHER), new Path.PathElement(Publisher.class, String.class, NAME));
FilterPredicate publisherNamePredicate = new InPredicate(new Path(publisherNamePath), PUB1);
EntityProjection entityProjection = EntityProjection.builder().type(Book.class).filterExpression(publisherNamePredicate).build();
Relationship relationshipProjection = Relationship.builder().name(BOOKS).projection(entityProjection).build();
RelationshipImpl relationship = new RelationshipImpl(ClassType.of(Author.class), author, relationshipProjection);
SubCollectionFetchQueryBuilder builder = new SubCollectionFetchQueryBuilder(relationship, dictionary, new TestSessionWrapper());
TestQueryWrapper query = (TestQueryWrapper) builder.build();
String expected = "SELECT example_Book FROM example.Author example_Author__fetch " + "JOIN example_Author__fetch.books example_Book " + "LEFT JOIN example_Book.publisher example_Book_publisher " + "WHERE example_Book_publisher.name IN (:books_publisher_name_XXX) AND example_Author__fetch=:example_Author__fetch ";
String actual = query.getQueryText();
actual = actual.replaceFirst(":publisher_name_\\w+_\\w+", ":books_publisher_name_XXX");
assertEquals(expected, actual);
}
use of com.yahoo.elide.core.request.Relationship in project elide by yahoo.
the class SubCollectionFetchQueryBuilderTest method testSubCollectionFetchWithRelationshipSorting.
@Test
public void testSubCollectionFetchWithRelationshipSorting() {
Author author = new Author();
author.setId(1L);
Book book = new Book();
book.setId(2);
Map<String, Sorting.SortOrder> sorting = new HashMap<>();
sorting.put(PUBLISHER + PERIOD + NAME, Sorting.SortOrder.asc);
EntityProjection entityProjection = EntityProjection.builder().type(Book.class).sorting(new SortingImpl(sorting, Book.class, dictionary)).build();
Relationship relationshipProjection = Relationship.builder().name(BOOKS).projection(entityProjection).build();
RelationshipImpl relationship = new RelationshipImpl(ClassType.of(Author.class), author, relationshipProjection);
SubCollectionFetchQueryBuilder builder = new SubCollectionFetchQueryBuilder(relationship, dictionary, new TestSessionWrapper());
TestQueryWrapper query = (TestQueryWrapper) builder.build();
String expected = "SELECT example_Book FROM example.Author example_Author__fetch " + "JOIN example_Author__fetch.books example_Book " + "LEFT JOIN example_Book.publisher example_Book_publisher " + "WHERE example_Author__fetch=:example_Author__fetch order by example_Book_publisher.name asc";
String actual = query.getQueryText();
actual = actual.trim().replaceAll(" +", " ");
assertEquals(expected, actual);
}
Aggregations