Search in sources :

Example 51 with PersistentResource

use of com.yahoo.elide.core.PersistentResource 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));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TableExport(com.yahoo.elide.async.models.TableExport) EntityProjection(com.yahoo.elide.core.request.EntityProjection) PersistentResource(com.yahoo.elide.core.PersistentResource) Attribute(com.yahoo.elide.core.request.Attribute) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 52 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class JSONExportFormatterTest method testFormat.

@Test
public void testFormat() {
    JSONExportFormatter formatter = new JSONExportFormatter(elide);
    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 start = "{\"query\":\"{ tableExport { edges { node { query queryType createdOn} } } }\"," + "\"queryType\":\"GRAPHQL_V1_0\",\"createdOn\":\"" + 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(start));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TableExport(com.yahoo.elide.async.models.TableExport) EntityProjection(com.yahoo.elide.core.request.EntityProjection) PersistentResource(com.yahoo.elide.core.PersistentResource) Attribute(com.yahoo.elide.core.request.Attribute) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 53 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class JSONExportFormatterTest method testNullResourceToJSON.

@Test
public void testNullResourceToJSON() {
    JSONExportFormatter formatter = new JSONExportFormatter(elide);
    PersistentResource persistentResource = null;
    String output = formatter.format(persistentResource, 1);
    assertNull(output);
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) Test(org.junit.jupiter.api.Test)

Example 54 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class JSONExportFormatterTest method testResourceToJSON.

@Test
public void testResourceToJSON() {
    JSONExportFormatter formatter = new JSONExportFormatter(elide);
    TableExport queryObj = new TableExport();
    String id = "edc4a871-dff2-4054-804e-d80075cf827d";
    queryObj.setId(id);
    String start = "{\"query\":\"{ tableExport { edges { node { query queryType} } } }\"," + "\"queryType\":\"GRAPHQL_V1_0\"}";
    // 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());
    EntityProjection projection = EntityProjection.builder().type(TableExport.class).attributes(attributes).build();
    Map<String, Object> resourceAttributes = new LinkedHashMap<>();
    resourceAttributes.put("query", "{ tableExport { edges { node { query queryType} } } }");
    resourceAttributes.put("queryType", QueryType.GRAPHQL_V1_0);
    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.resourceToJSON(elide.getMapper().getObjectMapper(), persistentResource);
    assertTrue(output.contains(start));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TableExport(com.yahoo.elide.async.models.TableExport) EntityProjection(com.yahoo.elide.core.request.EntityProjection) PersistentResource(com.yahoo.elide.core.PersistentResource) Attribute(com.yahoo.elide.core.request.Attribute) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 55 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class SubscriptionDataFetcher method get.

@Override
public Object get(DataFetchingEnvironment environment) throws Exception {
    OperationDefinition.Operation op = environment.getOperationDefinition().getOperation();
    if (op != OperationDefinition.Operation.SUBSCRIPTION) {
        throw new InvalidEntityBodyException(String.format("%s not supported for subscription models.", op));
    }
    /* build environment object, extracts required fields */
    Environment context = new Environment(environment, nonEntityDictionary);
    /* safe enable debugging */
    if (log.isDebugEnabled()) {
        logContext(log, RelationshipOp.FETCH, context);
    }
    if (context.isRoot()) {
        String entityName = context.field.getName();
        String aliasName = context.field.getAlias();
        EntityProjection projection = context.requestScope.getProjectionInfo().getProjection(aliasName, entityName);
        Flowable<PersistentResource> recordPublisher = PersistentResource.loadRecords(projection, new ArrayList<>(), context.requestScope).toFlowable(BackpressureStrategy.BUFFER).onBackpressureBuffer(bufferSize, true, false);
        return recordPublisher.map(SubscriptionNodeContainer::new);
    }
    // as the PersistentResourceFetcher.
    return context.container.processFetch(context);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) PersistentResource(com.yahoo.elide.core.PersistentResource) InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Environment(com.yahoo.elide.graphql.Environment) SubscriptionNodeContainer(com.yahoo.elide.graphql.subscriptions.containers.SubscriptionNodeContainer) OperationDefinition(graphql.language.OperationDefinition)

Aggregations

PersistentResource (com.yahoo.elide.core.PersistentResource)100 Test (org.junit.jupiter.api.Test)71 RequestScope (com.yahoo.elide.core.RequestScope)60 ReadPermission (com.yahoo.elide.annotation.ReadPermission)18 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)18 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)17 Include (com.yahoo.elide.annotation.Include)16 Entity (javax.persistence.Entity)16 Resource (com.yahoo.elide.jsonapi.models.Resource)13 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)10 NotFilterExpression (com.yahoo.elide.core.filter.expression.NotFilterExpression)10 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)10 PermissionExecutor (com.yahoo.elide.core.security.PermissionExecutor)10 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)10 Book (example.Book)10 LinkedHashSet (java.util.LinkedHashSet)9 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)8 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)8 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)8 RSQLFilterDialect (com.yahoo.elide.core.filter.dialect.RSQLFilterDialect)7