Search in sources :

Example 21 with Resource

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

the class LeaderboardController method getSingleGlobal.

@Async
@RequestMapping(path = "/global/{playerId}", method = RequestMethod.GET)
@ApiOperation("Lists the global leaderboard for the specified player")
public CompletableFuture<JsonApiDocument> getSingleGlobal(@PathVariable("playerId") String playerId) {
    GlobalLeaderboardEntry entry = leaderboardService.getGlobalEntry(Integer.valueOf(playerId));
    if (entry == null) {
        throw new ResourceNotFoundException("No global leaderboard entry found for player: " + playerId);
    }
    Resource resource = new Resource(GLOBAL_LEADERBOARD_ENTRY, playerId, ImmutableMap.<String, Object>builder().put("name", entry.getPlayerName()).put("mean", entry.getMean()).put("deviation", entry.getDeviation()).put("numGames", entry.getNumGames()).put("rank", entry.getRank()).put("rating", (int) (entry.getMean() - 3 * entry.getDeviation())).build(), null, null, null);
    return CompletableFuture.completedFuture(new JsonApiDocument(new Data<>(resource)));
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) Data(com.yahoo.elide.jsonapi.models.Data) ResourceNotFoundException(com.faforever.api.web.ResourceNotFoundException) Async(org.springframework.scheduling.annotation.Async) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 22 with Resource

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

the class LeaderboardController method getSingleLadder1v1.

@Async
@RequestMapping(path = "/ladder1v1/{playerId}", method = RequestMethod.GET)
@ApiOperation("Lists the ladder1v1 leaderboard for the specified player")
public CompletableFuture<JsonApiDocument> getSingleLadder1v1(@PathVariable("playerId") String playerId) {
    Ladder1v1LeaderboardEntry entry = leaderboardService.getLadder1v1Entry(Integer.valueOf(playerId));
    if (entry == null) {
        throw new ResourceNotFoundException("No ladder1v1 entry found for player: " + playerId);
    }
    Resource resource = new Resource(LADDER_1V1_LEADERBOARD_ENTRY, playerId, ImmutableMap.<String, Object>builder().put("name", entry.getPlayerName()).put("mean", entry.getMean()).put("deviation", entry.getDeviation()).put("numGames", entry.getNumGames()).put("wonGames", entry.getWonGames()).put("rank", entry.getRank()).put("rating", (int) (entry.getMean() - 3 * entry.getDeviation())).build(), null, null, null);
    return CompletableFuture.completedFuture(new JsonApiDocument(new Data<>(resource)));
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) Data(com.yahoo.elide.jsonapi.models.Data) ResourceNotFoundException(com.faforever.api.web.ResourceNotFoundException) Async(org.springframework.scheduling.annotation.Async) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 23 with Resource

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

the class PersistentResourceTest method testSuccessfulManyToManyRelationshipUpdate.

@Test
public /*
     * The following are ids for a hypothetical relationship.
     * GIVEN:
     * all (all the ids in the DB) = 1,2,3,4,5
     * mine (everything the current user has access to) = 1,2,3
     * requested (what the user wants to change to) = 3,6
     * THEN:
     * deleted (what gets removed from the DB) = 1,2
     * final (what get stored in the relationship) = 3,4,5,6
     * BECAUSE:
     * notMine = all - mine
     * updated = (requested UNION mine) - (requested INTERSECT mine)
     * deleted = (mine - requested)
     * final = (notMine) UNION requested
     */
void testSuccessfulManyToManyRelationshipUpdate() throws Exception {
    Parent parent = new Parent();
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    Child child1 = newChild(1);
    Child child2 = newChild(2);
    Child child3 = newChild(3);
    // Not accessible to goodUser
    Child child4 = newChild(-4);
    // Not accessible to goodUser
    Child child5 = newChild(-5);
    Child child6 = newChild(6);
    // All = (1,2,3,4,5)
    // Mine = (1,2,3)
    Set<Child> allChildren = new HashSet<>();
    allChildren.add(child1);
    allChildren.add(child2);
    allChildren.add(child3);
    allChildren.add(child4);
    allChildren.add(child5);
    parent.setChildren(allChildren);
    parent.setSpouses(Sets.newHashSet());
    when(tx.getToManyRelation(any(), eq(parent), any(), any())).thenReturn(new DataStoreIterableBuilder(allChildren).build());
    PersistentResource<Parent> parentResource = new PersistentResource<>(parent, "1", goodScope);
    // Requested = (3,6)
    List<Resource> idList = new ArrayList<>();
    idList.add(new ResourceIdentifier("child", "3").castToResource());
    idList.add(new ResourceIdentifier("child", "6").castToResource());
    Relationship ids = new Relationship(null, new Data<>(idList));
    when(tx.loadObject(any(), eq(2L), any())).thenReturn(child2);
    when(tx.loadObject(any(), eq(3L), any())).thenReturn(child3);
    when(tx.loadObject(any(), eq(-4L), any())).thenReturn(child4);
    when(tx.loadObject(any(), eq(-5L), any())).thenReturn(child5);
    when(tx.loadObject(any(), eq(6L), any())).thenReturn(child6);
    // Final set after operation = (3,4,5,6)
    Set<Child> expected = new HashSet<>();
    expected.add(child3);
    expected.add(child4);
    expected.add(child5);
    expected.add(child6);
    boolean updated = parentResource.updateRelation("children", ids.toPersistentResources(goodScope));
    goodScope.saveOrCreateObjects();
    verify(tx, times(1)).save(parent, goodScope);
    verify(tx, times(1)).save(child1, goodScope);
    verify(tx, times(1)).save(child2, goodScope);
    verify(tx, times(1)).save(child6, goodScope);
    verify(tx, never()).save(child4, goodScope);
    verify(tx, never()).save(child5, goodScope);
    verify(tx, never()).save(child3, goodScope);
    assertTrue(updated, "Many-2-many relationship should be updated.");
    assertTrue(parent.getChildren().containsAll(expected), "All expected members were updated");
    assertTrue(expected.containsAll(parent.getChildren()), "All expected members were updated");
/*
         * No tests for reference integrity since the parent is the owner and
         * this is a many to many relationship.
         */
}
Also used : DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Parent(example.Parent) Resource(com.yahoo.elide.jsonapi.models.Resource) ArrayList(java.util.ArrayList) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Child(example.Child) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 24 with Resource

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

the class PersistentResourceTest method testTransferPermissionSuccessOnUpdateManyRelationshipPackageLevel.

@Test
public void testTransferPermissionSuccessOnUpdateManyRelationshipPackageLevel() {
    ContainerWithPackageShare containerWithPackageShare = new ContainerWithPackageShare();
    ShareableWithPackageShare shareableWithPackageShare = new ShareableWithPackageShare();
    shareableWithPackageShare.setContainerWithPackageShare(containerWithPackageShare);
    List<Resource> shareableList = new ArrayList<>();
    shareableList.add(new ResourceIdentifier("shareableWithPackageShare", "1").castToResource());
    Relationship shareables = new Relationship(null, new Data<>(shareableList));
    when(tx.loadObject(any(), eq(1L), any())).thenReturn(shareableWithPackageShare);
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    PersistentResource<ContainerWithPackageShare> containerResource = new PersistentResource<>(containerWithPackageShare, goodScope.getUUIDFor(containerWithPackageShare), goodScope);
    containerResource.updateRelation("shareableWithPackageShares", shareables.toPersistentResources(goodScope));
    assertEquals(1, containerWithPackageShare.getShareableWithPackageShares().size());
    assertTrue(containerWithPackageShare.getShareableWithPackageShares().contains(shareableWithPackageShare));
}
Also used : ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) ShareableWithPackageShare(example.nontransferable.ShareableWithPackageShare) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Resource(com.yahoo.elide.jsonapi.models.Resource) ArrayList(java.util.ArrayList) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) ContainerWithPackageShare(example.nontransferable.ContainerWithPackageShare) Test(org.junit.jupiter.api.Test)

Example 25 with Resource

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

the class PersistentResourceTest method testSuccessfulManyToManyRelationshipNullUpdate.

@Test
public /*
     * The following are ids for a hypothetical relationship.
     * GIVEN:
     * all (all the ids in the DB) = null
     * mine (everything the current user has access to) = null
     * requested (what the user wants to change to) = 1,2,3
     * THEN:
     * deleted (what gets removed from the DB) = nothing
     * final (what get stored in the relationship) = 1,2,3
     * BECAUSE:
     * notMine = all - mine
     * updated = (requested UNION mine) - (requested INTERSECT mine)
     * deleted = (mine - requested)
     * final = (notMine) UNION requested
     */
void testSuccessfulManyToManyRelationshipNullUpdate() throws Exception {
    Parent parent = new Parent();
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    Child child1 = newChild(1);
    Child child2 = newChild(2);
    Child child3 = newChild(3);
    // All = null
    // Mine = null
    Set<Child> allChildren = new HashSet<>();
    allChildren.add(child1);
    allChildren.add(child2);
    allChildren.add(child3);
    parent.setChildren(null);
    parent.setSpouses(Sets.newHashSet());
    when(tx.getToManyRelation(any(), eq(parent), any(), any())).thenReturn(null);
    PersistentResource<Parent> parentResource = new PersistentResource<>(parent, "1", goodScope);
    // Requested = (1,2,3)
    List<Resource> idList = new ArrayList<>();
    idList.add(new ResourceIdentifier("child", "3").castToResource());
    idList.add(new ResourceIdentifier("child", "2").castToResource());
    idList.add(new ResourceIdentifier("child", "1").castToResource());
    Relationship ids = new Relationship(null, new Data<>(idList));
    when(tx.loadObject(any(), eq(1L), any())).thenReturn(child1);
    when(tx.loadObject(any(), eq(2L), any())).thenReturn(child2);
    when(tx.loadObject(any(), eq(3L), any())).thenReturn(child3);
    // Final set after operation = (1,2,3)
    Set<Child> expected = new HashSet<>();
    expected.add(child1);
    expected.add(child2);
    expected.add(child3);
    boolean updated = parentResource.updateRelation("children", ids.toPersistentResources(goodScope));
    goodScope.saveOrCreateObjects();
    verify(tx, times(1)).save(parent, goodScope);
    verify(tx, times(1)).save(child1, goodScope);
    verify(tx, times(1)).save(child2, goodScope);
    verify(tx, times(1)).save(child3, goodScope);
    assertTrue(updated, "Many-2-many relationship should be updated.");
    assertTrue(parent.getChildren().containsAll(expected), "All expected members were updated");
    assertTrue(expected.containsAll(parent.getChildren()), "All expected members were updated");
/*
         * No tests for reference integrity since the parent is the owner and
         * this is a many to many relationship.
         */
}
Also used : Parent(example.Parent) Resource(com.yahoo.elide.jsonapi.models.Resource) ArrayList(java.util.ArrayList) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Child(example.Child) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

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