Search in sources :

Example 51 with Resource

use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.

the class IncludedProcessorTest method testExecuteSingleRelationOnCollection.

@Test
public void testExecuteSingleRelationOnCollection() throws Exception {
    JsonApiDocument jsonApiDocument = new JsonApiDocument();
    Set<PersistentResource> parents = new HashSet<>();
    parents.add(parentRecord1);
    parents.add(parentRecord2);
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.put(INCLUDE, Collections.singletonList("children"));
    testScope.setQueryParams(queryParams);
    includedProcessor.execute(jsonApiDocument, parents, queryParams);
    List<Resource> expectedIncluded = Arrays.asList(childRecord1.toResource(), childRecord2.toResource());
    List<Resource> actualIncluded = jsonApiDocument.getIncluded();
    assertEquals(expectedIncluded, actualIncluded, "Included Processor added requested resource from all records");
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) PersistentResource(com.yahoo.elide.core.PersistentResource) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 52 with Resource

use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.

the class IncludedProcessorTest method testExecuteMultipleRelations.

@Test
public void testExecuteMultipleRelations() throws Exception {
    JsonApiDocument jsonApiDocument = new JsonApiDocument();
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.put(INCLUDE, Arrays.asList("children", "spouses"));
    testScope.setQueryParams(queryParams);
    includedProcessor.execute(jsonApiDocument, parentRecord1, queryParams);
    List<Resource> expectedIncluded = Arrays.asList(childRecord1.toResource(), parentRecord2.toResource());
    List<Resource> actualIncluded = jsonApiDocument.getIncluded();
    assertEquals(expectedIncluded, actualIncluded, "Included Processor added single requested resource from 'include' query param");
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Test(org.junit.jupiter.api.Test)

Example 53 with Resource

use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.

the class PersistentResource method getRelationshipsWithRelationshipFunction.

/**
 * Get relationship mappings.
 *
 * @param relationshipFunction a function to load the value of a relationship. Takes a string of the relationship
 *                             name and returns the relationship's value.
 * @return Relationship mapping
 */
protected Map<String, Relationship> getRelationshipsWithRelationshipFunction(final Function<String, Observable<PersistentResource>> relationshipFunction) {
    final Map<String, Relationship> relationshipMap = new LinkedHashMap<>();
    final Set<String> relationshipFields = filterFields(dictionary.getRelationships(obj));
    for (String field : relationshipFields) {
        TreeMap<String, Resource> orderedById = new TreeMap<>(lengthFirstComparator);
        for (PersistentResource relationship : relationshipFunction.apply(field).toList().blockingGet()) {
            orderedById.put(relationship.getId(), new ResourceIdentifier(relationship.getTypeName(), relationship.getId()).castToResource());
        }
        Observable<Resource> resources = Observable.fromIterable(orderedById.values());
        Data<Resource> data;
        RelationshipType relationshipType = getRelationshipType(field);
        if (relationshipType.isToOne()) {
            data = new Data<>(firstOrNullIfEmpty(resources));
        } else {
            data = new Data<>(resources);
        }
        Map<String, String> links = null;
        if (requestScope.getElideSettings().isEnableJsonLinks()) {
            links = requestScope.getElideSettings().getJsonApiLinks().getRelationshipLinks(this, field);
        }
        relationshipMap.put(field, new Relationship(links, data));
    }
    return relationshipMap;
}
Also used : Resource(com.yahoo.elide.jsonapi.models.Resource) RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType) TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap) ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) Relationship(com.yahoo.elide.jsonapi.models.Relationship)

Example 54 with Resource

use of com.yahoo.elide.jsonapi.models.Resource in project faf-java-api by FAForever.

the class LeaderboardControllerTest method getLadder1v1.

@Test
public void getLadder1v1() throws Exception {
    when(leaderboardService.getLadder1v1Leaderboard(1, 100)).thenReturn(new PageImpl<>(Arrays.asList(new Ladder1v1LeaderboardEntry().setId(14).setPlayerName("JUnit 14").setMean(1500f).setDeviation(51f).setNumGames((short) 514).setRank(1).setWonGames((short) 270), new Ladder1v1LeaderboardEntry().setId(5).setPlayerName("JUnit 5").setMean(1400f).setDeviation(67f).setNumGames((short) 65).setRank(2).setWonGames((short) 32))));
    CompletableFuture<JsonApiDocument> result = instance.getLadder1v1(1, 100);
    assertThat(result.get(), is(notNullValue()));
    Collection<Resource> resources = result.get().getData().get();
    assertThat(resources, hasSize(2));
    Iterator<Resource> iterator = resources.iterator();
    Resource firstEntry = iterator.next();
    assertThat(firstEntry.getId(), is("14"));
    assertThat(firstEntry.getAttributes().get("name"), is("JUnit 14"));
    assertThat(firstEntry.getAttributes().get("mean"), is(1500f));
    assertThat(firstEntry.getAttributes().get("deviation"), is(51f));
    assertThat(firstEntry.getAttributes().get("numGames"), is((short) 514));
    assertThat(firstEntry.getAttributes().get("wonGames"), is((short) 270));
    assertThat(firstEntry.getAttributes().get("rank"), is(1));
    assertThat(firstEntry.getAttributes().get("rating"), is(1347));
    Resource secondEntry = iterator.next();
    assertThat(secondEntry.getId(), is("5"));
    assertThat(secondEntry.getAttributes().get("name"), is("JUnit 5"));
    assertThat(secondEntry.getAttributes().get("mean"), is(1400f));
    assertThat(secondEntry.getAttributes().get("deviation"), is(67f));
    assertThat(secondEntry.getAttributes().get("numGames"), is((short) 65));
    assertThat(secondEntry.getAttributes().get("wonGames"), is((short) 32));
    assertThat(secondEntry.getAttributes().get("rank"), is(2));
    assertThat(secondEntry.getAttributes().get("rating"), is(1199));
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) Test(org.junit.Test)

Example 55 with Resource

use of com.yahoo.elide.jsonapi.models.Resource in project faf-java-api by FAForever.

the class FeaturedModsController method getFiles.

@Async
@RequestMapping(path = "/{modId}/files/{version}")
@ApiOperation("Lists the required files for a specific featured mod version")
public CompletableFuture<JsonApiDocument> getFiles(@PathVariable("modId") int modId, @PathVariable("version") String version, @RequestParam(value = "page[number]", required = false) Integer page) {
    Integer innerPage = Optional.ofNullable(page).orElse(0);
    if (innerPage > 1) {
        return CompletableFuture.completedFuture(new JsonApiDocument(new Data<>(Collections.emptyList())));
    }
    ImmutableMap<Integer, FeaturedMod> mods = Maps.uniqueIndex(featuredModService.getFeaturedMods(), FeaturedMod::getId);
    FeaturedMod featuredMod = mods.get(modId);
    Integer innerVersion = "latest".equals(version) ? null : Integer.valueOf(version);
    List<Resource> values = featuredModService.getFiles(featuredMod.getTechnicalName(), innerVersion).stream().map(modFileMapper()).collect(Collectors.toList());
    return CompletableFuture.completedFuture(new JsonApiDocument(new Data<>(values)));
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) Data(com.yahoo.elide.jsonapi.models.Data) FeaturedMod(com.faforever.api.data.domain.FeaturedMod) Async(org.springframework.scheduling.annotation.Async) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Resource (com.yahoo.elide.jsonapi.models.Resource)55 Test (org.junit.jupiter.api.Test)35 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)31 PersistentResource (com.yahoo.elide.core.PersistentResource)28 Relationship (com.yahoo.elide.jsonapi.models.Relationship)21 ArrayList (java.util.ArrayList)17 ResourceIdentifier (com.yahoo.elide.jsonapi.models.ResourceIdentifier)16 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)13 Data (com.yahoo.elide.jsonapi.models.Data)13 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)9 LinkedHashSet (java.util.LinkedHashSet)9 Parent (example.Parent)8 HashSet (java.util.HashSet)8 EntityProjection (com.yahoo.elide.core.request.EntityProjection)7 Child (example.Child)7 LinkedHashMap (java.util.LinkedHashMap)7 RequestScope (com.yahoo.elide.core.RequestScope)6 TestUser (com.yahoo.elide.core.security.TestUser)6 User (com.yahoo.elide.core.security.User)6 TestRequestScope (com.yahoo.elide.core.TestRequestScope)5