Search in sources :

Example 11 with Attribute

use of com.yahoo.elide.core.request.Attribute 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 12 with Attribute

use of com.yahoo.elide.core.request.Attribute 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 13 with Attribute

use of com.yahoo.elide.core.request.Attribute in project elide by yahoo.

the class MatchesTemplateVisitorTest method predicateWithAliasMatchesTest.

@Test
public void predicateWithAliasMatchesTest() throws Exception {
    FilterExpression clientExpression = dialect.parseFilterExpression("highScore==123", playerStatsType, true);
    Attribute attribute = Attribute.builder().type(ClassType.of(Long.class)).name("highScore").alias("myScore").build();
    FilterExpression templateExpression = dialect.parseFilterExpression("myScore=={{foo}}", playerStatsType, false, true, Set.of(attribute));
    Map<String, Argument> extractedArgs = new HashMap<>();
    Argument expected = Argument.builder().name("foo").value(123L).build();
    assertTrue(MatchesTemplateVisitor.isValid(templateExpression, clientExpression, extractedArgs));
    assertEquals(1, extractedArgs.size());
    assertEquals(extractedArgs.get("foo"), expected);
}
Also used : Argument(com.yahoo.elide.core.request.Argument) Attribute(com.yahoo.elide.core.request.Attribute) HashMap(java.util.HashMap) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Test(org.junit.jupiter.api.Test)

Example 14 with Attribute

use of com.yahoo.elide.core.request.Attribute in project elide by yahoo.

the class PersistentResourceTest method testPatchRequestScope.

@Test
public void testPatchRequestScope() {
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    PatchRequestScope parentScope = new PatchRequestScope(null, "/book", NO_VERSION, tx, new TestUser("1"), UUID.randomUUID(), null, Collections.emptyMap(), elideSettings);
    PatchRequestScope scope = new PatchRequestScope(parentScope.getPath(), parentScope.getJsonApiDocument(), parentScope);
    // verify wrap works
    assertEquals(parentScope.getUpdateStatusCode(), scope.getUpdateStatusCode());
    assertEquals(parentScope.getObjectEntityCache(), scope.getObjectEntityCache());
    Parent parent = newParent(7);
    PersistentResource<Parent> parentResource = new PersistentResource<>(parent, "1", scope);
    parentResource.updateAttribute("firstName", "foobar");
    ArgumentCaptor<Attribute> attributeArgument = ArgumentCaptor.forClass(Attribute.class);
    verify(tx, times(1)).setAttribute(eq(parent), attributeArgument.capture(), eq(scope));
    assertEquals(attributeArgument.getValue().getName(), "firstName");
    assertEquals(attributeArgument.getValue().getArguments().iterator().next().getValue(), "foobar");
}
Also used : PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Parent(example.Parent) Attribute(com.yahoo.elide.core.request.Attribute) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) TestUser(com.yahoo.elide.core.security.TestUser) Test(org.junit.jupiter.api.Test)

Example 15 with Attribute

use of com.yahoo.elide.core.request.Attribute in project elide by yahoo.

the class EntityHydrator method coerceObjectToEntity.

/**
 * Coerces results from a {@link Query} into an Object.
 *
 * @param result a fieldName-value map
 * @param counter Monotonically increasing number to generate IDs.
 * @return A hydrated entity object.
 */
protected Object coerceObjectToEntity(Map<String, Object> result, MutableInt counter) {
    Table table = getBaseTable(query);
    Type<?> entityClass = entityDictionary.getEntityClass(table.getName(), table.getVersion());
    // Construct the object.
    Object entityInstance;
    try {
        entityInstance = entityClass.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
    result.forEach((fieldName, value) -> {
        ColumnProjection columnProjection = query.getColumnProjection(fieldName);
        Column column = table.getColumn(Column.class, columnProjection.getName());
        Type<?> fieldType = getType(entityClass, columnProjection);
        Attribute attribute = projectionToAttribute(columnProjection, fieldType);
        ValueType valueType = column.getValueType();
        if (entityInstance instanceof ParameterizedModel) {
            // This is an ENUM_TEXT or ENUM_ORDINAL type.
            if (// Java enums can be coerced directly via CoerceUtil - so skip them.
            !fieldType.isEnum() && valueType == ValueType.TEXT && column.getValues() != null && !column.getValues().isEmpty()) {
                value = convertToEnumValue(value, column.getValues());
            }
            ((ParameterizedModel) entityInstance).addAttributeValue(attribute, CoerceUtil.coerce(value, fieldType));
        } else {
            getEntityDictionary().setValue(entityInstance, fieldName, value);
        }
    });
    // Set the ID (it must be coerced from an integer)
    getEntityDictionary().setValue(entityInstance, getEntityDictionary().getIdFieldName(entityClass), counter.getAndIncrement());
    return entityInstance;
}
Also used : Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) Attribute(com.yahoo.elide.core.request.Attribute) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) ParameterizedModel(com.yahoo.elide.core.type.ParameterizedModel) SQLColumnProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLColumnProjection) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection)

Aggregations

Attribute (com.yahoo.elide.core.request.Attribute)24 Test (org.junit.jupiter.api.Test)19 EntityProjection (com.yahoo.elide.core.request.EntityProjection)11 TableExport (com.yahoo.elide.async.models.TableExport)9 LinkedHashSet (java.util.LinkedHashSet)8 LinkedHashMap (java.util.LinkedHashMap)5 Argument (com.yahoo.elide.core.request.Argument)4 PersistentResource (com.yahoo.elide.core.PersistentResource)3 Resource (com.yahoo.elide.jsonapi.models.Resource)3 PersistentResourceFetcherTest (com.yahoo.elide.graphql.PersistentResourceFetcherTest)2 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)2 Parent (example.Parent)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Path (com.yahoo.elide.core.Path)1 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)1 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)1 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)1 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)1 InvalidParameterizedAttributeException (com.yahoo.elide.core.exceptions.InvalidParameterizedAttributeException)1