use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.
the class DataStoreSupportsFilteringTest method testPrefixOperator.
@Test
public void testPrefixOperator() throws Exception {
DataStoreTransaction testTransaction = searchStore.beginReadTransaction();
FilterPredicate filter = (FilterPredicate) filterParser.parseFilterExpression("name==drum*", ClassType.of(Item.class), false);
EntityProjection projection = EntityProjection.builder().type(Item.class).filterExpression(filter).build();
DataStoreIterable<Object> loaded = testTransaction.loadObjects(projection, mockScope);
assertTrue(loaded.needsInMemoryFilter());
assertTrue(loaded.needsInMemoryPagination());
assertTrue(loaded.needsInMemorySort());
verify(wrappedTransaction, times(0)).loadObjects(any(), any());
}
use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.
the class DataStoreSupportsFilteringTest method testNgramTooSmall.
@Test
public void testNgramTooSmall() throws Exception {
DataStoreTransaction testTransaction = searchStore.beginReadTransaction();
FilterPredicate filter = (FilterPredicate) filterParser.parseFilterExpression("description==*ru*", ClassType.of(Item.class), false);
EntityProjection projection = EntityProjection.builder().type(Item.class).filterExpression(filter).build();
assertThrows(InvalidValueException.class, () -> testTransaction.loadObjects(projection, mockScope));
}
use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.
the class PersistentResource method loadRecords.
/**
* Load a collection from the datastore.
*
* @param projection the projection to load
* @param requestScope the request scope
* @param ids a list of object identifiers to optionally load. Can be empty.
* @return a filtered collection of resources loaded from the datastore.
*/
public static Observable<PersistentResource> loadRecords(EntityProjection projection, List<String> ids, RequestScope requestScope) {
Type<?> loadClass = projection.getType();
Pagination pagination = projection.getPagination();
Sorting sorting = projection.getSorting();
FilterExpression filterExpression = projection.getFilterExpression();
EntityDictionary dictionary = requestScope.getDictionary();
DataStoreTransaction tx = requestScope.getTransaction();
if (shouldSkipCollection(loadClass, ReadPermission.class, requestScope, projection.getRequestedFields())) {
if (ids.isEmpty()) {
return Observable.empty();
}
throw new InvalidObjectIdentifierException(ids.toString(), dictionary.getJsonAliasFor(loadClass));
}
Set<String> requestedFields = projection.getRequestedFields();
if (pagination != null && !pagination.isDefaultInstance() && !CanPaginateVisitor.canPaginate(loadClass, dictionary, requestScope, requestedFields)) {
throw new BadRequestException(String.format("Cannot paginate %s", dictionary.getJsonAliasFor(loadClass)));
}
Set<PersistentResource> newResources = new LinkedHashSet<>();
if (!ids.isEmpty()) {
String typeAlias = dictionary.getJsonAliasFor(loadClass);
newResources = requestScope.getNewPersistentResources().stream().filter(resource -> typeAlias.equals(resource.getTypeName()) && ids.contains(resource.getUUID().orElse(""))).collect(Collectors.toSet());
FilterExpression idExpression = buildIdFilterExpression(ids, loadClass, dictionary, requestScope);
// Combine filters if necessary
filterExpression = Optional.ofNullable(filterExpression).map(fe -> (FilterExpression) new AndFilterExpression(idExpression, fe)).orElse(idExpression);
}
Optional<FilterExpression> permissionFilter = getPermissionFilterExpression(loadClass, requestScope, requestedFields);
if (permissionFilter.isPresent()) {
if (filterExpression != null) {
filterExpression = new AndFilterExpression(filterExpression, permissionFilter.get());
} else {
filterExpression = permissionFilter.get();
}
}
EntityProjection modifiedProjection = projection.copyOf().filterExpression(filterExpression).sorting(sorting).pagination(pagination).build();
Observable<PersistentResource> existingResources = filter(ReadPermission.class, Optional.ofNullable(modifiedProjection.getFilterExpression()), projection.getRequestedFields(), Observable.fromIterable(new PersistentResourceSet(tx.loadObjects(modifiedProjection, requestScope), requestScope)));
// TODO: Sort again in memory now that two sets are glommed together?
Observable<PersistentResource> allResources = Observable.fromIterable(newResources).mergeWith(existingResources);
Set<String> foundIds = new HashSet<>();
allResources = allResources.doOnNext((resource) -> {
String id = (String) resource.getUUID().orElseGet(resource::getId);
if (ids.contains(id)) {
foundIds.add(id);
}
});
allResources = allResources.doOnComplete(() -> {
Set<String> missedIds = Sets.difference(new HashSet<>(ids), foundIds);
if (!missedIds.isEmpty()) {
throw new InvalidObjectIdentifierException(missedIds.toString(), dictionary.getJsonAliasFor(loadClass));
}
});
return allResources;
}
use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.
the class CSVExportFormatterTest method testProjectionWithNullAttributesHeader.
@Test
public void testProjectionWithNullAttributesHeader() {
CSVExportFormatter formatter = new CSVExportFormatter(elide, false);
TableExport queryObj = new TableExport();
// Prepare EntityProjection
Set<Attribute> attributes = null;
EntityProjection projection = EntityProjection.builder().type(TableExport.class).attributes(attributes).build();
String output = formatter.preFormat(projection, queryObj);
assertEquals("", output);
}
use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.
the class CSVExportFormatterTest method testResourceToCSV.
@Test
public void testResourceToCSV() {
CSVExportFormatter formatter = new CSVExportFormatter(elide, false);
TableExport queryObj = new TableExport();
String query = "{ tableExport { edges { node { query queryType createdOn} } } }";
String id = "edc4a871-dff2-4054-804e-d80075cf827d";
queryObj.setId(id);
queryObj.setQuery(query);
queryObj.setQueryType(QueryType.GRAPHQL_V1_0);
queryObj.setResultType(ResultType.CSV);
String row = "\"{ tableExport { edges { node { query queryType createdOn} } } }\", \"GRAPHQL_V1_0\"" + ", \"" + FORMATTER.format(queryObj.getCreatedOn());
// Prepare EntityProjection
Set<Attribute> attributes = new LinkedHashSet<>();
attributes.add(Attribute.builder().type(TableExport.class).name("query").alias("query").build());
attributes.add(Attribute.builder().type(TableExport.class).name("queryType").build());
attributes.add(Attribute.builder().type(TableExport.class).name("createdOn").build());
EntityProjection projection = EntityProjection.builder().type(TableExport.class).attributes(attributes).build();
Map<String, Object> resourceAttributes = new LinkedHashMap<>();
resourceAttributes.put("query", query);
resourceAttributes.put("queryType", queryObj.getQueryType());
resourceAttributes.put("createdOn", queryObj.getCreatedOn());
Resource resource = new Resource("tableExport", "0", resourceAttributes, null, null, null);
PersistentResource persistentResource = mock(PersistentResource.class);
when(persistentResource.getObject()).thenReturn(queryObj);
when(persistentResource.getRequestScope()).thenReturn(scope);
when(persistentResource.toResource(any(), any())).thenReturn(resource);
when(scope.getEntityProjection()).thenReturn(projection);
String output = formatter.format(persistentResource, 1);
assertTrue(output.contains(row));
}
Aggregations