Search in sources :

Example 1 with Attribute

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));
}
Also used : Attribute(com.yahoo.elide.core.request.Attribute) Test(org.junit.jupiter.api.Test)

Example 2 with Attribute

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;
}
Also used : Attribute(com.yahoo.elide.core.request.Attribute) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with Attribute

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;
}
Also used : Path(com.yahoo.elide.core.Path) InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) Attribute(com.yahoo.elide.core.request.Attribute) ToString(lombok.ToString) LinkedHashMap(java.util.LinkedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with Attribute

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");
}
Also used : Parent(example.Parent) Attribute(com.yahoo.elide.core.request.Attribute) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 5 with Attribute

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));
}
Also used : Argument(com.yahoo.elide.core.request.Argument) Query(com.yahoo.elide.datastores.aggregation.query.Query) HashMap(java.util.HashMap) Attribute(com.yahoo.elide.core.request.Attribute) PlayerStats(example.PlayerStats) TreeMap(java.util.TreeMap) Month(com.yahoo.elide.datastores.aggregation.timegrains.Month) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) Day(com.yahoo.elide.datastores.aggregation.timegrains.Day) HashSet(java.util.HashSet) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

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