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));
}
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));
}
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);
}
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");
}
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;
}
Aggregations