Search in sources :

Example 1 with Right

use of example.Right in project elide by yahoo.

the class PersistentResourceTest method testDeleteBidirectionalRelation.

@Test
public void testDeleteBidirectionalRelation() {
    Left left = new Left();
    Right right = new Right();
    left.setOne2one(right);
    right.setOne2one(left);
    RequestScope scope = new TestRequestScope(tx, goodUser, dictionary);
    PersistentResource<Left> leftResource = new PersistentResource<>(left, "3", scope);
    leftResource.deleteInverseRelation("one2one", right);
    assertNull(right.getOne2one(), "The one-2-one inverse relationship should have been unset");
    assertEquals(right, left.getOne2one(), "The owning relationship should NOT have been unset");
    Child child = new Child();
    Parent parent = new Parent();
    child.setParents(Sets.newHashSet(parent));
    parent.setChildren(Sets.newHashSet(child));
    parent.setSpouses(Sets.newHashSet());
    scope = new TestRequestScope(tx, goodUser, dictionary);
    PersistentResource<Child> childResource = new PersistentResource<>(child, "4", scope);
    childResource.deleteInverseRelation("parents", parent);
    assertEquals(parent.getChildren().size(), 0, "The many-2-many inverse collection should have been cleared.");
    assertTrue(child.getParents().contains(parent), "The owning relationship should NOT have been touched");
}
Also used : Left(example.Left) Parent(example.Parent) Right(example.Right) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Child(example.Child) Test(org.junit.jupiter.api.Test)

Example 2 with Right

use of example.Right in project elide by yahoo.

the class PersistentResourceTest method testClearRelationInvalidToOneUpdatePermission.

@Test
public void testClearRelationInvalidToOneUpdatePermission() {
    Left left = new Left();
    left.setId(1);
    Right right = new Right();
    right.setId(1);
    left.setNoUpdateOne2One(right);
    right.setNoUpdateOne2One(left);
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    goodScope.setEntityProjection(EntityProjection.builder().type(Left.class).relationship("noUpdateOne2One", EntityProjection.builder().type(Right.class).build()).build());
    PersistentResource<Left> leftResource = new PersistentResource<>(left, "1", goodScope);
    assertThrows(ForbiddenAccessException.class, () -> leftResource.clearRelation("noUpdateOne2One"));
    // Modifications have a deferred check component:
    leftResource.getRequestScope().getPermissionExecutor().executeCommitChecks();
}
Also used : Left(example.Left) Right(example.Right) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 3 with Right

use of example.Right in project elide by yahoo.

the class PersistentResourceTest method testDeletePermissionCheckedOnInverseRelationship.

@Test
public void testDeletePermissionCheckedOnInverseRelationship() {
    Left left = new Left();
    left.setId(1);
    Right right = new Right();
    right.setId(2);
    Set<Right> rights = Sets.newHashSet(right);
    left.setFieldLevelDelete(Sets.newHashSet(right));
    right.setAllowDeleteAtFieldLevel(Sets.newHashSet(left));
    // Bad User triggers the delete permission failure
    when(tx.getToManyRelation(any(), eq(left), any(), any())).thenReturn(new DataStoreIterableBuilder(rights).build());
    RequestScope badScope = buildRequestScope(tx, badUser);
    PersistentResource<Left> leftResource = new PersistentResource<>(left, badScope.getUUIDFor(left), badScope);
    assertTrue(leftResource.clearRelation("fieldLevelDelete"));
    assertEquals(0, leftResource.getObject().getFieldLevelDelete().size());
}
Also used : Left(example.Left) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Right(example.Right) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 4 with Right

use of example.Right 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 Right

use of example.Right 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)

Aggregations

Left (example.Left)9 Right (example.Right)9 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)8 Test (org.junit.jupiter.api.Test)8 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)3 Relationship (com.yahoo.elide.jsonapi.models.Relationship)2 Child (example.Child)2 Parent (example.Parent)2 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)1 Resource (com.yahoo.elide.jsonapi.models.Resource)1 ResourceIdentifier (com.yahoo.elide.jsonapi.models.ResourceIdentifier)1 Embedded (example.Embedded)1 ArrayList (java.util.ArrayList)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1