Search in sources :

Example 11 with Relationship

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

the class PersistentResourceTest method testSuccessfulManyToManyRelationshipNoopUpdate.

@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) = 1,2,3
     * THEN:
     * deleted (what gets removed from the DB) = nothing
     * final (what get stored in the relationship) = 1,2,3,4,5
     * BECAUSE:
     * notMine = all - mine
     * updated = (requested UNION mine) - (requested INTERSECT mine)
     * deleted = (mine - requested)
     * final = (notMine) UNION requested
     */
void testSuccessfulManyToManyRelationshipNoopUpdate() 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);
    // 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 = (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);
    when(tx.loadObject(any(), eq(-4L), any())).thenReturn(child4);
    when(tx.loadObject(any(), eq(-5L), any())).thenReturn(child5);
    // Final set after operation = (1,2,3,4,5)
    Set<Child> expected = new HashSet<>();
    expected.add(child1);
    expected.add(child2);
    expected.add(child3);
    expected.add(child4);
    expected.add(child5);
    boolean updated = parentResource.updateRelation("children", ids.toPersistentResources(goodScope));
    goodScope.saveOrCreateObjects();
    verify(tx, never()).save(parent, goodScope);
    verify(tx, never()).save(child1, goodScope);
    verify(tx, never()).save(child2, goodScope);
    verify(tx, never()).save(child4, goodScope);
    verify(tx, never()).save(child5, goodScope);
    verify(tx, never()).save(child3, goodScope);
    assertFalse(updated, "Many-2-many relationship should not 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 12 with Relationship

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

the class PersistentResourceTest method testGetRelationships.

@Test
public void testGetRelationships() {
    FunWithPermissions fun = new FunWithPermissions();
    fun.setRelation1(Sets.newHashSet());
    fun.setRelation2(Sets.newHashSet());
    fun.setRelation3(null);
    RequestScope scope = new TestRequestScope(tx, goodUser, dictionary);
    PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, "3", scope);
    Map<String, Relationship> relationships = funResource.getRelationships();
    assertEquals(5, relationships.size(), "All relationships should be returned.");
    assertTrue(relationships.containsKey("relation1"), "relation1 should be present");
    assertTrue(relationships.containsKey("relation2"), "relation2 should be present");
    assertTrue(relationships.containsKey("relation3"), "relation3 should be present");
    assertTrue(relationships.containsKey("relation4"), "relation4 should be present");
    assertTrue(relationships.containsKey("relation5"), "relation5 should be present");
    scope = new TestRequestScope(tx, badUser, dictionary);
    PersistentResource<FunWithPermissions> funResourceWithBadScope = new PersistentResource<>(fun, "3", scope);
    relationships = funResourceWithBadScope.getRelationships();
    assertEquals(0, relationships.size(), "All relationships should be filtered out");
}
Also used : Relationship(com.yahoo.elide.jsonapi.models.Relationship) FunWithPermissions(example.FunWithPermissions) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 13 with Relationship

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

the class PersistentResourceTest method testTransferPermissionErrorOnUpdateSingularRelationship.

@Test
public void testTransferPermissionErrorOnUpdateSingularRelationship() {
    example.User userModel = new example.User();
    userModel.setId(1);
    NoShareEntity noShare = new NoShareEntity();
    noShare.setId(1);
    List<Resource> idList = new ArrayList<>();
    idList.add(new ResourceIdentifier("noshare", "1").castToResource());
    Relationship ids = new Relationship(null, new Data<>(idList));
    EntityProjection collection = EntityProjection.builder().type(NoShareEntity.class).build();
    when(tx.loadObject(eq(collection), eq(1L), any())).thenReturn(noShare);
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    PersistentResource<example.User> userResource = new PersistentResource<>(userModel, goodScope.getUUIDFor(userModel), goodScope);
    assertThrows(ForbiddenAccessException.class, () -> userResource.updateRelation("noShare", ids.toPersistentResources(goodScope)));
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) 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 14 with Relationship

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

the class PersistentResourceTest method testRelationshipMissingData.

/**
 * Verify that Relationship toMany cannot contain null resources, but toOne can.
 *
 * @throws Exception
 */
@Test
public void testRelationshipMissingData() throws Exception {
    User goodUser = new TestUser("1");
    @SuppressWarnings("resource") DataStoreTransaction tx = mock(DataStoreTransaction.class);
    RequestScope goodScope = new RequestScope(null, null, NO_VERSION, null, tx, goodUser, null, null, UUID.randomUUID(), elideSettings);
    // null resource in toMany relationship is not valid
    List<Resource> idList = new ArrayList<>();
    idList.add(new ResourceIdentifier("child", "3").castToResource());
    idList.add(new ResourceIdentifier("child", "6").castToResource());
    idList.add(null);
    assertThrows(NullPointerException.class, () -> new Relationship(Collections.emptyMap(), new Data<>(idList)));
    // However null toOne relationship is valid
    Relationship toOneRelationship = new Relationship(Collections.emptyMap(), new Data<>((Resource) null));
    assertTrue(toOneRelationship.getData().get().isEmpty());
    assertNull(toOneRelationship.toPersistentResources(goodScope));
    // no Data
    Relationship nullRelationship = new Relationship(Collections.emptyMap(), null);
    assertNull(nullRelationship.getData());
    assertNull(nullRelationship.toPersistentResources(goodScope));
}
Also used : ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) TestUser(com.yahoo.elide.core.security.TestUser) User(com.yahoo.elide.core.security.User) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Resource(com.yahoo.elide.jsonapi.models.Resource) ArrayList(java.util.ArrayList) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Data(com.yahoo.elide.jsonapi.models.Data) TestUser(com.yahoo.elide.core.security.TestUser) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 15 with Relationship

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

the class PersistentResourceTest method testSuccessfulOneToOneRelationshipAddNull.

/**
 * Avoid NPE when PATCH or POST defines relationship with null id
 * <pre>
 * <code>
 * "relationships": {
 *   "left": {
 *     "data": {
 *       "type": "right",
 *       "id": null
 *     }
 *   }
 * }
 * </code>
 * </pre>
 */
@Test
public void testSuccessfulOneToOneRelationshipAddNull() throws Exception {
    Left left = new Left();
    left.setId(2);
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    PersistentResource<Left> leftResource = new PersistentResource<>(left, "2", goodScope);
    Relationship ids = new Relationship(null, new Data<>(new Resource("right", null, null, null, null, null)));
    InvalidObjectIdentifierException thrown = assertThrows(InvalidObjectIdentifierException.class, () -> leftResource.updateRelation("one2one", ids.toPersistentResources(goodScope)));
    assertEquals("Unknown identifier null for right", thrown.getMessage());
}
Also used : Left(example.Left) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Resource(com.yahoo.elide.jsonapi.models.Resource) InvalidObjectIdentifierException(com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) 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