Search in sources :

Example 46 with JsonApiDocument

use of com.yahoo.elide.jsonapi.models.JsonApiDocument 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 47 with JsonApiDocument

use of com.yahoo.elide.jsonapi.models.JsonApiDocument 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 48 with JsonApiDocument

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

the class IncludedProcessorTest method testNoQueryIncludeParams.

@Test
public void testNoQueryIncludeParams() throws Exception {
    JsonApiDocument jsonApiDocument = new JsonApiDocument();
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.put("unused", Collections.emptyList());
    testScope.setQueryParams(queryParams);
    includedProcessor.execute(jsonApiDocument, parentRecord1, queryParams);
    assertNull(jsonApiDocument.getIncluded(), "Included Processor adds no resources when not given query params");
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Test(org.junit.jupiter.api.Test)

Example 49 with JsonApiDocument

use of com.yahoo.elide.jsonapi.models.JsonApiDocument 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 50 with JsonApiDocument

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

the class PlayerEventsControllerTest method update.

@Test
public void update() throws Exception {
    AchievementUpdateRequest[] updateRequests = new AchievementUpdateRequest[] { new AchievementUpdateRequest(1, "111", Operation.INCREMENT, 7), new AchievementUpdateRequest(1, "222", Operation.INCREMENT, 19), new AchievementUpdateRequest(1, "333", Operation.SET_STEPS_AT_LEAST, 9), new AchievementUpdateRequest(1, "444", Operation.SET_STEPS_AT_LEAST, 13), new AchievementUpdateRequest(1, "555", Operation.UNLOCK, 11), new AchievementUpdateRequest(1, "666", Operation.UNLOCK, 17) };
    when(achievementService.increment(anyInt(), any(), anyInt())).thenAnswer(invocation -> new UpdatedAchievementResponse(invocation.getArgument(1), true, AchievementState.UNLOCKED, invocation.getArgument(2)));
    when(achievementService.setStepsAtLeast(anyInt(), any(), anyInt())).thenAnswer(invocation -> new UpdatedAchievementResponse(invocation.getArgument(1), true, AchievementState.UNLOCKED, invocation.getArgument(2)));
    when(achievementService.unlock(anyInt(), any())).thenAnswer(invocation -> new UpdatedAchievementResponse(invocation.getArgument(1), true, AchievementState.UNLOCKED));
    JsonApiDocument result = instance.update(updateRequests);
    verify(achievementService).increment(1, "111", 7);
    verify(achievementService).increment(1, "222", 19);
    verify(achievementService).setStepsAtLeast(1, "333", 9);
    verify(achievementService).setStepsAtLeast(1, "444", 13);
    verify(achievementService).unlock(1, "555");
    verify(achievementService).unlock(1, "666");
    assertThat(result.getData().get(), hasSize(6));
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Test(org.junit.Test)

Aggregations

JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)51 Resource (com.yahoo.elide.jsonapi.models.Resource)31 Test (org.junit.jupiter.api.Test)26 PersistentResource (com.yahoo.elide.core.PersistentResource)25 RequestScope (com.yahoo.elide.core.RequestScope)15 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)9 Data (com.yahoo.elide.jsonapi.models.Data)8 IOException (java.io.IOException)7 TestRequestScope (com.yahoo.elide.core.TestRequestScope)6 Parent (example.Parent)6 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)5 EntityProjectionMaker (com.yahoo.elide.jsonapi.EntityProjectionMaker)4 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)4 Relationship (com.yahoo.elide.jsonapi.models.Relationship)4 BaseVisitor (com.yahoo.elide.jsonapi.parser.BaseVisitor)4 Child (example.Child)4 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)3 JsonApiMapper (com.yahoo.elide.jsonapi.JsonApiMapper)3 DocumentProcessor (com.yahoo.elide.jsonapi.document.processors.DocumentProcessor)3 IncludedProcessor (com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)3