Search in sources :

Example 1 with ResourceIdentifier

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

the class JsonApiTest method readSingleIncluded.

@Test
public void readSingleIncluded() throws Exception {
    String doc = "{\"data\":{\"type\":\"parent\",\"id\":\"123\",\"attributes\":{\"firstName\":\"bob\"},\"relationships\":{\"children\":{\"links\":{\"self\":\"/parent/123/relationships/child\",\"related\":\"/parent/123/child\"},\"data\":{\"type\":\"child\",\"id\":\"2\"}}}},\"included\":[{\"type\":\"child\",\"id\":\"2\",\"relationships\":{\"parents\":{\"links\":{\"self\":\"/parent/123/relationships/child\",\"related\":\"/parent/123/child\"},\"data\":{\"type\":\"parent\",\"id\":\"123\"}}}}]}";
    JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
    Data<Resource> dataObj = jsonApiDocument.getData();
    Resource data = dataObj.getSingleValue();
    Map<String, Object> attributes = data.getAttributes();
    List<Resource> included = jsonApiDocument.getIncluded();
    Resource includedChild = IterableUtils.first(included);
    ResourceIdentifier parent = includedChild.getRelationships().get("parents").getResourceIdentifierData().getSingleValue();
    assertEquals("parent", data.getType());
    assertEquals("123", data.getId());
    assertEquals("bob", attributes.get("firstName"));
    assertEquals("child", includedChild.getType());
    assertEquals("2", includedChild.getId());
    assertEquals("123", parent.getId());
    checkEquality(jsonApiDocument);
}
Also used : ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) 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 2 with ResourceIdentifier

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

the class PersistentResourceTest method testTransferPermissionSuccessOnUpdateManyRelationship.

@Test
public void testTransferPermissionSuccessOnUpdateManyRelationship() {
    example.User userModel = new example.User();
    userModel.setId(1);
    NoShareEntity noShare1 = new NoShareEntity();
    noShare1.setId(1);
    NoShareEntity noShare2 = new NoShareEntity();
    noShare2.setId(2);
    HashSet<NoShareEntity> noshares = Sets.newHashSet(noShare1, noShare2);
    /* The no shares already exist so no exception should be thrown */
    userModel.setNoShares(noshares);
    List<Resource> idList = new ArrayList<>();
    idList.add(new ResourceIdentifier("noshare", "1").castToResource());
    Relationship ids = new Relationship(null, new Data<>(idList));
    when(tx.loadObject(any(), eq(1L), any())).thenReturn(noShare1);
    when(tx.getToManyRelation(any(), eq(userModel), any(), any())).thenReturn(new DataStoreIterableBuilder(noshares).build());
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    PersistentResource<example.User> userResource = new PersistentResource<>(userModel, goodScope.getUUIDFor(userModel), goodScope);
    boolean returnVal = userResource.updateRelation("noShares", ids.toPersistentResources(goodScope));
    assertTrue(returnVal);
    assertEquals(1, userModel.getNoShares().size());
    assertTrue(userModel.getNoShares().contains(noShare1));
}
Also used : TestUser(com.yahoo.elide.core.security.TestUser) User(com.yahoo.elide.core.security.User) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) 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) NoShareEntity(example.NoShareEntity) Test(org.junit.jupiter.api.Test)

Example 3 with ResourceIdentifier

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

the class PersistentResourceTest method testTransferPermissionErrorOnUpdateManyRelationship.

@Test
public void testTransferPermissionErrorOnUpdateManyRelationship() {
    example.User userModel = new example.User();
    userModel.setId(1);
    NoShareEntity noShare1 = new NoShareEntity();
    noShare1.setId(1);
    NoShareEntity noShare2 = new NoShareEntity();
    noShare2.setId(2);
    List<Resource> idList = new ArrayList<>();
    idList.add(new ResourceIdentifier("noshare", "1").castToResource());
    idList.add(new ResourceIdentifier("noshare", "2").castToResource());
    Relationship ids = new Relationship(null, new Data<>(idList));
    when(tx.loadObject(any(), eq(1L), any())).thenReturn(noShare1);
    when(tx.loadObject(any(), eq(2L), any())).thenReturn(noShare2);
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    PersistentResource<example.User> userResource = new PersistentResource<>(userModel, goodScope.getUUIDFor(userModel), goodScope);
    assertThrows(ForbiddenAccessException.class, () -> userResource.updateRelation("noShares", ids.toPersistentResources(goodScope)));
}
Also used : TestUser(com.yahoo.elide.core.security.TestUser) User(com.yahoo.elide.core.security.User) 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) NoShareEntity(example.NoShareEntity) Test(org.junit.jupiter.api.Test)

Example 4 with ResourceIdentifier

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

the class PersistentResourceTest method testSuccessfulOneToOneRelationshipAdd.

@Test
public void testSuccessfulOneToOneRelationshipAdd() throws Exception {
    Left left = new Left();
    Right right = new Right();
    left.setId(2);
    right.setId(3);
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    PersistentResource<Left> leftResource = new PersistentResource<>(left, "2", goodScope);
    Relationship ids = new Relationship(null, new Data<>(new ResourceIdentifier("right", "3").castToResource()));
    when(tx.loadObject(any(), eq(3L), any())).thenReturn(right);
    boolean updated = leftResource.updateRelation("one2one", ids.toPersistentResources(goodScope));
    goodScope.saveOrCreateObjects();
    verify(tx, times(1)).save(left, goodScope);
    verify(tx, times(1)).save(right, goodScope);
    verify(tx, times(1)).getToOneRelation(tx, left, getRelationship(ClassType.of(Right.class), "one2one"), goodScope);
    assertTrue(updated, "The one-2-one relationship should be added.");
    assertEquals(3, left.getOne2one().getId(), "The correct object was set in the one-2-one relationship");
}
Also used : ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) Left(example.Left) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Right(example.Right) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 5 with ResourceIdentifier

use of com.yahoo.elide.jsonapi.models.ResourceIdentifier 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)

Aggregations

ResourceIdentifier (com.yahoo.elide.jsonapi.models.ResourceIdentifier)14 Resource (com.yahoo.elide.jsonapi.models.Resource)13 Test (org.junit.jupiter.api.Test)13 Relationship (com.yahoo.elide.jsonapi.models.Relationship)12 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)11 ArrayList (java.util.ArrayList)10 TestUser (com.yahoo.elide.core.security.TestUser)5 User (com.yahoo.elide.core.security.User)5 NoShareEntity (example.NoShareEntity)4 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)3 Child (example.Child)3 Parent (example.Parent)3 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 PersistentResource (com.yahoo.elide.core.PersistentResource)2 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)2 ContainerWithPackageShare (example.nontransferable.ContainerWithPackageShare)2 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)1 RelationshipType (com.yahoo.elide.core.dictionary.RelationshipType)1 EntityProjection (com.yahoo.elide.core.request.EntityProjection)1