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