Search in sources :

Example 6 with Relationship

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

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

Example 8 with Relationship

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

the class PersistentResourceTest method testTransferPermissionErrorOnUpdateRelationshipPackageLevel.

@Test
public void testTransferPermissionErrorOnUpdateRelationshipPackageLevel() {
    ContainerWithPackageShare containerWithPackageShare = new ContainerWithPackageShare();
    Untransferable untransferable = new Untransferable();
    untransferable.setContainerWithPackageShare(containerWithPackageShare);
    List<Resource> unShareableList = new ArrayList<>();
    unShareableList.add(new ResourceIdentifier("untransferable", "1").castToResource());
    Relationship unShareales = new Relationship(null, new Data<>(unShareableList));
    when(tx.loadObject(any(), eq(1L), any())).thenReturn(untransferable);
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    PersistentResource<ContainerWithPackageShare> containerResource = new PersistentResource<>(containerWithPackageShare, goodScope.getUUIDFor(containerWithPackageShare), goodScope);
    assertThrows(ForbiddenAccessException.class, () -> containerResource.updateRelation("untransferables", unShareales.toPersistentResources(goodScope)));
}
Also used : ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Resource(com.yahoo.elide.jsonapi.models.Resource) ArrayList(java.util.ArrayList) Untransferable(example.nontransferable.Untransferable) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) ContainerWithPackageShare(example.nontransferable.ContainerWithPackageShare) Test(org.junit.jupiter.api.Test)

Example 9 with Relationship

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

the class PersistentResourceTest method testUpdatePermissionCheckedOnInverseRelationship.

@Test
public void testUpdatePermissionCheckedOnInverseRelationship() {
    Left left = new Left();
    left.setId(1);
    Right right = new Right();
    Set<Right> rights = Sets.newHashSet(right);
    left.setNoInverseUpdate(rights);
    right.setNoUpdate(Sets.newHashSet(left));
    List<Resource> empty = new ArrayList<>();
    Relationship ids = new Relationship(null, new Data<>(empty));
    when(tx.getToManyRelation(any(), eq(left), any(), any())).thenReturn(new DataStoreIterableBuilder(rights).build());
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    PersistentResource<Left> leftResource = new PersistentResource<>(left, goodScope.getUUIDFor(left), goodScope);
    assertThrows(ForbiddenAccessException.class, () -> leftResource.updateRelation("noInverseUpdate", ids.toPersistentResources(goodScope)));
    // Modifications have a deferred check component:
    leftResource.getRequestScope().getPermissionExecutor().executeCommitChecks();
}
Also used : Left(example.Left) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Right(example.Right) Resource(com.yahoo.elide.jsonapi.models.Resource) ArrayList(java.util.ArrayList) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 10 with Relationship

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

the class PersistentResourceTest method testTransferPermissionSuccessOnClearSingularRelationship.

@Test
public void testTransferPermissionSuccessOnClearSingularRelationship() {
    example.User userModel = new example.User();
    userModel.setId(1);
    NoShareEntity noShare = new NoShareEntity();
    /* The noshare already exists so no exception should be thrown */
    userModel.setNoShare(noShare);
    List<Resource> empty = new ArrayList<>();
    Relationship ids = new Relationship(null, new Data<>(empty));
    when(tx.getToOneRelation(any(), eq(userModel), any(), any())).thenReturn(noShare);
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    PersistentResource<example.User> userResource = new PersistentResource<>(userModel, goodScope.getUUIDFor(userModel), goodScope);
    boolean returnVal = userResource.updateRelation("noShare", ids.toPersistentResources(goodScope));
    assertTrue(returnVal);
    assertNull(userModel.getNoShare());
}
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) Relationship(com.yahoo.elide.jsonapi.models.Relationship) NoShareEntity(example.NoShareEntity) 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