Search in sources :

Example 6 with TableExport

use of com.yahoo.elide.async.models.TableExport 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 7 with TableExport

use of com.yahoo.elide.async.models.TableExport 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 8 with TableExport

use of com.yahoo.elide.async.models.TableExport in project elide by yahoo.

the class FileResultStorageEngine method storeResults.

@Override
public TableExportResult storeResults(TableExport tableExport, Observable<String> result) {
    log.debug("store TableExportResults for Download");
    String extension = this.isExtensionEnabled() ? tableExport.getResultType().getFileExtensionType().getExtension() : FileExtensionType.NONE.getExtension();
    TableExportResult exportResult = new TableExportResult();
    try (BufferedWriter writer = getWriter(tableExport.getId(), extension)) {
        result.map(record -> record.concat(System.lineSeparator())).subscribe(recordCharArray -> {
            writer.write(recordCharArray);
            writer.flush();
        }, throwable -> {
            StringBuilder message = new StringBuilder();
            message.append(throwable.getClass().getCanonicalName()).append(" : ");
            message.append(throwable.getMessage());
            exportResult.setMessage(message.toString());
            throw new IllegalStateException(STORE_ERROR, throwable);
        }, writer::flush);
    } catch (IOException e) {
        throw new IllegalStateException(STORE_ERROR, e);
    }
    return exportResult;
}
Also used : Setter(lombok.Setter) FileExtensionType(com.yahoo.elide.async.models.FileExtensionType) Getter(lombok.Getter) Iterator(java.util.Iterator) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) IOException(java.io.IOException) Singleton(javax.inject.Singleton) File(java.io.File) TableExportResult(com.yahoo.elide.async.models.TableExportResult) Slf4j(lombok.extern.slf4j.Slf4j) Paths(java.nio.file.Paths) Observable(io.reactivex.Observable) BufferedReader(java.io.BufferedReader) TableExport(com.yahoo.elide.async.models.TableExport) IOException(java.io.IOException) TableExportResult(com.yahoo.elide.async.models.TableExportResult) BufferedWriter(java.io.BufferedWriter)

Example 9 with TableExport

use of com.yahoo.elide.async.models.TableExport in project elide by yahoo.

the class RedisResultStorageEngine method storeResults.

@Override
public TableExportResult storeResults(TableExport tableExport, Observable<String> result) {
    log.debug("store TableExportResults for Download");
    String extension = this.isExtensionEnabled() ? tableExport.getResultType().getFileExtensionType().getExtension() : FileExtensionType.NONE.getExtension();
    TableExportResult exportResult = new TableExportResult();
    String key = tableExport.getId() + extension;
    result.map(record -> record).subscribe(recordCharArray -> {
        jedis.rpush(key, recordCharArray);
    }, throwable -> {
        StringBuilder message = new StringBuilder();
        message.append(throwable.getClass().getCanonicalName()).append(" : ");
        message.append(throwable.getMessage());
        exportResult.setMessage(message.toString());
        throw new IllegalStateException(STORE_ERROR, throwable);
    });
    jedis.expire(key, expirationSeconds);
    return exportResult;
}
Also used : TableExportResult(com.yahoo.elide.async.models.TableExportResult) Setter(lombok.Setter) Slf4j(lombok.extern.slf4j.Slf4j) UnifiedJedis(redis.clients.jedis.UnifiedJedis) FileExtensionType(com.yahoo.elide.async.models.FileExtensionType) Getter(lombok.Getter) Iterator(java.util.Iterator) Observable(io.reactivex.Observable) Singleton(javax.inject.Singleton) TableExport(com.yahoo.elide.async.models.TableExport) TableExportResult(com.yahoo.elide.async.models.TableExportResult)

Example 10 with TableExport

use of com.yahoo.elide.async.models.TableExport in project elide by yahoo.

the class GraphQLTableExportOperationTest method testProcessMultipleQuery.

@Test
public void testProcessMultipleQuery() {
    TableExport queryObj = new TableExport();
    String query = "{\"query\":\"{ tableExport { edges { node { principalName } } } } { asyncQuery { edges { node { principalName } } } }\",\"variables\":null}";
    String id = "edc4a871-dff2-4094-804e-d80075cf827d";
    queryObj.setId(id);
    queryObj.setQuery(query);
    queryObj.setQueryType(QueryType.GRAPHQL_V1_0);
    queryObj.setResultType(ResultType.CSV);
    GraphQLTableExportOperation graphQLOperation = new GraphQLTableExportOperation(new JSONExportFormatter(elide), asyncExecutorService, queryObj, requestScope, engine);
    TableExportResult queryResultObj = (TableExportResult) graphQLOperation.call();
    assertEquals(200, queryResultObj.getHttpStatus());
    assertEquals("Export is only supported for single Query with one root projection.", queryResultObj.getMessage());
    assertNull(queryResultObj.getRecordCount());
    assertNull(queryResultObj.getUrl());
}
Also used : TableExport(com.yahoo.elide.async.models.TableExport) JSONExportFormatter(com.yahoo.elide.async.export.formatter.JSONExportFormatter) TableExportResult(com.yahoo.elide.async.models.TableExportResult) Test(org.junit.jupiter.api.Test)

Aggregations

TableExport (com.yahoo.elide.async.models.TableExport)28 Test (org.junit.jupiter.api.Test)20 TableExportResult (com.yahoo.elide.async.models.TableExportResult)13 EntityProjection (com.yahoo.elide.core.request.EntityProjection)11 JSONExportFormatter (com.yahoo.elide.async.export.formatter.JSONExportFormatter)10 Attribute (com.yahoo.elide.core.request.Attribute)9 LinkedHashSet (java.util.LinkedHashSet)8 PersistentResource (com.yahoo.elide.core.PersistentResource)4 FileExtensionType (com.yahoo.elide.async.models.FileExtensionType)3 RequestScope (com.yahoo.elide.core.RequestScope)3 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)3 Resource (com.yahoo.elide.jsonapi.models.Resource)3 Observable (io.reactivex.Observable)3 LinkedHashMap (java.util.LinkedHashMap)3 TableExportFormatter (com.yahoo.elide.async.export.formatter.TableExportFormatter)2 AsyncAPIResult (com.yahoo.elide.async.models.AsyncAPIResult)2 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 Singleton (javax.inject.Singleton)2 Getter (lombok.Getter)2