use of com.yahoo.elide.core.request.Attribute in project elide by yahoo.
the class ParameterizedModelTest method testInvoke.
@Test
public void testInvoke() {
ParameterizedModel testModel = spy(ParameterizedModel.class);
Attribute testAttribute = Attribute.builder().type(STRING_TYPE).name("foo").build();
String testValue = "bar";
testModel.addAttributeValue(testAttribute, testValue);
assertEquals(testValue, testModel.invoke(testAttribute));
}
use of com.yahoo.elide.core.request.Attribute in project elide by yahoo.
the class JSONExportFormatter method getAttributes.
private static Map<String, Object> getAttributes(PersistentResource resource) {
final Map<String, Object> attributes = new LinkedHashMap<>();
final Set<Attribute> attrFields = resource.getRequestScope().getEntityProjection().getAttributes();
for (Attribute field : attrFields) {
String alias = field.getAlias();
String fieldName = StringUtils.isNotEmpty(alias) ? alias : field.getName();
attributes.put(fieldName, resource.getAttribute(field));
}
return attributes;
}
use of com.yahoo.elide.core.request.Attribute in project elide by yahoo.
the class SortingImpl method getValidSortingRules.
/**
* Given the sorting rules validate sorting rules against the entities bound to the entityClass.
* @param entityClass The root class for sorting (eg. /book?sort=-title this would be package.Book)
* @param attributes The attributes that are being requested for the sorted model
* @param dictionary The elide entity dictionary
* @param <T> The entityClass
* @return The valid sorting rules - validated through the entity dictionary, or empty dictionary
* @throws InvalidValueException when sorting values are not valid for the jpa entity
*/
private <T> Map<Path, SortOrder> getValidSortingRules(final Type<T> entityClass, final Set<Attribute> attributes, final EntityDictionary dictionary) throws InvalidValueException {
Map<Path, SortOrder> returnMap = new LinkedHashMap<>();
for (Map.Entry<String, SortOrder> entry : replaceIdRule(dictionary.getIdFieldName(entityClass)).entrySet()) {
String dotSeparatedPath = entry.getKey();
SortOrder order = entry.getValue();
Path path;
if (dotSeparatedPath.contains(".")) {
// Creating a path validates that the dot separated path is valid.
path = new Path(entityClass, dictionary, dotSeparatedPath);
} else {
Attribute attribute = attributes.stream().filter(attr -> attr.getName().equals(dotSeparatedPath) || attr.getAlias().equals(dotSeparatedPath)).findFirst().orElse(null);
if (attribute == null) {
path = new Path(entityClass, dictionary, dotSeparatedPath);
} else {
path = new Path(entityClass, dictionary, attribute.getName(), attribute.getAlias(), attribute.getArguments());
}
}
if (!isValidSortRulePath(path, dictionary)) {
throw new InvalidValueException("Cannot sort across a to-many relationship: " + path.getFieldPath());
}
returnMap.put(path, order);
}
return returnMap;
}
use of com.yahoo.elide.core.request.Attribute in project elide by yahoo.
the class PersistentResourceTest method testSetAttributeHookInUpdateAttribute.
@Test
public void testSetAttributeHookInUpdateAttribute() {
Parent parent = newParent(1);
ArgumentCaptor<Attribute> attributeArgument = ArgumentCaptor.forClass(Attribute.class);
RequestScope goodScope = buildRequestScope(tx, goodUser);
PersistentResource<Parent> parentResource = new PersistentResource<>(parent, "1", goodScope);
parentResource.updateAttribute("firstName", "foobar");
verify(tx, times(1)).setAttribute(eq(parent), attributeArgument.capture(), eq(goodScope));
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 QueryEngineTest method testMultipleTimeGrainsSortedByDayAlias.
@Test
public void testMultipleTimeGrainsSortedByDayAlias() throws Exception {
Map<String, Argument> dayArguments = new HashMap<>();
dayArguments.put("grain", Argument.builder().name("grain").value(TimeGrain.DAY).build());
Map<String, Argument> monthArguments = new HashMap<>();
monthArguments.put("grain", Argument.builder().name("grain").value(TimeGrain.MONTH).build());
Map<String, Sorting.SortOrder> sortMap = new TreeMap<>();
sortMap.put("byDay", Sorting.SortOrder.asc);
Set<Attribute> sortAttributes = new HashSet<>(Arrays.asList(Attribute.builder().type(TIME_TYPE).name("recordedDate").alias("byDay").arguments(dayArguments.values()).build()));
Query query = Query.builder().source(playerStatsTable).metricProjection(playerStatsTable.getMetricProjection("highScore")).timeDimensionProjection(playerStatsTable.getTimeDimensionProjection("recordedDate", "byDay", dayArguments)).timeDimensionProjection(playerStatsTable.getTimeDimensionProjection("recordedDate", "byMonth", monthArguments)).sorting(new SortingImpl(sortMap, ClassType.of(PlayerStats.class), sortAttributes, dictionary)).build();
List<PlayerStats> results = toList(engine.executeQuery(query, transaction).getData());
assertEquals(3, results.size());
assertEquals(2412, results.get(0).getHighScore());
assertEquals(new Day(Date.valueOf("2019-07-11")), results.get(0).fetch("byDay", null));
assertEquals(new Month(Date.valueOf("2019-07-01")), results.get(0).fetch("byMonth", null));
assertEquals(1234, results.get(1).getHighScore());
assertEquals(new Day(Date.valueOf("2019-07-12")), results.get(1).fetch("byDay", null));
assertEquals(new Month(Date.valueOf("2019-07-01")), results.get(1).fetch("byMonth", null));
assertEquals(1000, results.get(2).getHighScore());
assertEquals(new Day(Date.valueOf("2019-07-13")), results.get(2).fetch("byDay", null));
assertEquals(new Month(Date.valueOf("2019-07-01")), results.get(2).fetch("byMonth", null));
}
Aggregations