use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class JPQLTransaction method getToManyRelation.
@Override
public <T, R> DataStoreIterable<R> getToManyRelation(DataStoreTransaction relationTx, T entity, Relationship relation, RequestScope scope) {
FilterExpression filterExpression = relation.getProjection().getFilterExpression();
Sorting sorting = relation.getProjection().getSorting();
Pagination pagination = relation.getProjection().getPagination();
EntityDictionary dictionary = scope.getDictionary();
Iterable val = (Iterable) com.yahoo.elide.core.PersistentResource.getValue(entity, relation.getName(), scope);
// If the query is safe for N+1 and the value is an ORM managed, persistent collection, run a JPQL query...
if (doInDatabase(entity) && val instanceof Collection && isPersistentCollection().test((Collection<?>) val)) {
/*
* If there is no filtering or sorting required in the data store, and the pagination is default,
* return the proxy and let Hibernate manage the SQL generation.
*/
if (filterExpression == null && sorting == null && (pagination == null || (pagination.isDefaultInstance()))) {
return new DataStoreIterableBuilder<R>(addSingleElement(val)).allInMemory().build();
}
RelationshipImpl relationship = new RelationshipImpl(dictionary.lookupEntityClass(EntityDictionary.getType(entity)), entity, relation);
if (pagination != null && pagination.returnPageTotals()) {
pagination.setPageTotals(getTotalRecords(relationship, scope.getDictionary()));
}
final Query query = new SubCollectionFetchQueryBuilder(relationship, dictionary, sessionWrapper).build();
if (query != null) {
return new DataStoreIterableBuilder(addSingleElement(query.list())).build();
}
}
return new DataStoreIterableBuilder<R>(addSingleElement(val)).allInMemory().build();
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class LifeCycleTest method testElidePatchRelationshipAddMultiple.
@Test
public void testElidePatchRelationshipAddMultiple() {
DataStore store = mock(DataStore.class);
DataStoreTransaction tx = mock(DataStoreTransaction.class);
FieldTestModel parent = mock(FieldTestModel.class);
FieldTestModel child1 = mock(FieldTestModel.class);
FieldTestModel child2 = mock(FieldTestModel.class);
FieldTestModel child3 = mock(FieldTestModel.class);
Elide elide = getElide(store, dictionary, MOCK_AUDIT_LOGGER);
String body = "{\"data\": {\"type\":\"testModel\",\"id\":\"1\",\"relationships\": { \"models\": { \"data\": [ { \"type\": \"testModel\", \"id\": \"2\" }, {\"type\": \"testModel\", \"id\": \"3\" } ] } } } }";
dictionary.setValue(parent, "id", "1");
dictionary.setValue(child1, "id", "2");
dictionary.setValue(child2, "id", "3");
dictionary.setValue(child3, "id", "4");
when(store.beginTransaction()).thenReturn(tx);
when(tx.loadObject(isA(EntityProjection.class), eq("1"), isA(RequestScope.class))).thenReturn(parent);
when(tx.loadObject(isA(EntityProjection.class), eq("2"), isA(RequestScope.class))).thenReturn(child1);
when(tx.loadObject(isA(EntityProjection.class), eq("3"), isA(RequestScope.class))).thenReturn(child2);
when(tx.loadObject(isA(EntityProjection.class), eq("4"), isA(RequestScope.class))).thenReturn(child3);
DataStoreIterable iterable = new DataStoreIterableBuilder(List.of(child3)).build();
when(tx.getToManyRelation(any(), any(), isA(Relationship.class), isA(RequestScope.class))).thenReturn(iterable);
String contentType = JSONAPI_CONTENT_TYPE;
ElideResponse response = elide.patch(baseUrl, contentType, contentType, "/testModel/1", body, null, NO_VERSION);
assertEquals(HttpStatus.SC_NO_CONTENT, response.getResponseCode());
verify(parent, times(1)).relationCallback(eq(UPDATE), eq(POSTCOMMIT), notNull());
verify(parent, times(4)).classCallback(eq(UPDATE), any());
verify(parent, never()).classAllFieldsCallback(any(), any());
verify(parent, never()).attributeCallback(any(), any(), any());
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class SubscriptionDataFetcherTest method testRootNonSchemaQuery.
@Test
void testRootNonSchemaQuery() {
Book book1 = new Book();
book1.setTitle("Book 1");
book1.setId(1);
Book book2 = new Book();
book2.setTitle("Book 2");
book2.setId(2);
when(dataStoreTransaction.loadObjects(any(), any())).thenReturn(new DataStoreIterableBuilder(List.of(book1, book2)).build());
List<String> responses = List.of("{\"book\":null}");
List<String> errors = List.of("QUERY not supported for subscription models");
String graphQLRequest = "query {book(topic: ADDED) {id title}}";
assertSubscriptionEquals(graphQLRequest, responses, errors);
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class SubscriptionDataFetcherTest method testComplexAttribute.
@Test
void testComplexAttribute() {
Author author1 = new Author();
author1.setId(1L);
author1.setHomeAddress(new Address());
Author author2 = new Author();
author2.setId(2L);
Address address = new Address();
address.setStreet1("123");
address.setStreet2("XYZ");
author2.setHomeAddress(address);
when(dataStoreTransaction.loadObjects(any(), any())).thenReturn(new DataStoreIterableBuilder(List.of(author1, author2)).build());
List<String> responses = List.of("{\"author\":{\"id\":\"1\",\"homeAddress\":{\"street1\":null,\"street2\":null}}}", "{\"author\":{\"id\":\"2\",\"homeAddress\":{\"street1\":\"123\",\"street2\":\"XYZ\"}}}");
String graphQLRequest = "subscription {author(topic: UPDATED) {id homeAddress { street1 street2 }}}";
assertSubscriptionEquals(graphQLRequest, responses);
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class SubscriptionDataFetcherTest method testErrorInSubscriptionStream.
@Test
void testErrorInSubscriptionStream() {
Book book1 = new Book();
book1.setTitle("Book 1");
book1.setId(1);
Book book2 = new Book();
book2.setTitle("Book 2");
book2.setId(2);
reset(dataStoreTransaction);
when(dataStoreTransaction.getAttribute(any(), any(), any())).thenThrow(new BadRequestException("Bad Request"));
when(dataStoreTransaction.loadObjects(any(), any())).thenReturn(new DataStoreIterableBuilder(List.of(book1, book2)).build());
List<String> responses = List.of("{\"book\":{\"id\":\"1\",\"title\":null}}", "{\"book\":{\"id\":\"2\",\"title\":null}}");
List<String> errors = List.of("Bad Request", "Bad Request");
String graphQLRequest = "subscription {book(topic: ADDED) {id title}}";
assertSubscriptionEquals(graphQLRequest, responses, errors);
}
Aggregations