Search in sources :

Example 1 with Relationship

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

the class RecordTerminalState method patch.

private boolean patch(Resource resource, RequestScope requestScope) {
    boolean isUpdated = false;
    // Update attributes first
    Map<String, Object> attributes = resource.getAttributes();
    if (attributes != null) {
        for (Map.Entry<String, Object> entry : attributes.entrySet()) {
            String fieldName = entry.getKey();
            Object newVal = entry.getValue();
            isUpdated |= record.updateAttribute(fieldName, newVal);
        }
    }
    // Relations next
    Map<String, Relationship> relationships = resource.getRelationships();
    if (relationships != null) {
        for (Map.Entry<String, Relationship> entry : relationships.entrySet()) {
            String fieldName = entry.getKey();
            Relationship relationship = entry.getValue();
            Set<PersistentResource> resources = (relationship == null) ? null : relationship.toPersistentResources(requestScope);
            isUpdated |= record.updateRelation(fieldName, resources);
        }
    }
    return isUpdated;
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) Relationship(com.yahoo.elide.jsonapi.models.Relationship) ToString(lombok.ToString) Map(java.util.Map)

Example 2 with Relationship

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

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

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

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

Relationship (com.yahoo.elide.jsonapi.models.Relationship)23 Resource (com.yahoo.elide.jsonapi.models.Resource)20 Test (org.junit.jupiter.api.Test)17 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)15 ArrayList (java.util.ArrayList)15 ResourceIdentifier (com.yahoo.elide.jsonapi.models.ResourceIdentifier)14 TestUser (com.yahoo.elide.core.security.TestUser)6 User (com.yahoo.elide.core.security.User)6 PersistentResource (com.yahoo.elide.core.PersistentResource)5 NoShareEntity (example.NoShareEntity)5 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)5 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)4 Data (com.yahoo.elide.jsonapi.models.Data)4 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)4 Map (java.util.Map)4 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)3 RelationshipType (com.yahoo.elide.core.dictionary.RelationshipType)3 InternalServerErrorException (com.yahoo.elide.core.exceptions.InternalServerErrorException)3 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)3