Search in sources :

Example 11 with ResourceIdentifier

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

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

the class PersistentResourceTest method testTransferPermissionSuccessOnUpdateSingularRelationship.

@Test
public void testTransferPermissionSuccessOnUpdateSingularRelationship() {
    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> idList = new ArrayList<>();
    idList.add(new ResourceIdentifier("noshare", "1").castToResource());
    Relationship ids = new Relationship(null, new Data<>(idList));
    when(tx.getToOneRelation(any(), eq(userModel), any(), any())).thenReturn(noShare);
    when(tx.loadObject(any(), eq(1L), 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));
    assertFalse(returnVal);
    assertEquals(noShare, 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) ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) Relationship(com.yahoo.elide.jsonapi.models.Relationship) NoShareEntity(example.NoShareEntity) Test(org.junit.jupiter.api.Test)

Example 13 with ResourceIdentifier

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

the class JsonApiTest method readListIncluded.

@Test
public void readListIncluded() throws IOException {
    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> list = jsonApiDocument.getData();
    Resource data = list.get().iterator().next();
    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 14 with ResourceIdentifier

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

the class PersistentResource method getRelationshipsWithRelationshipFunction.

/**
 * Get relationship mappings.
 *
 * @param relationshipFunction a function to load the value of a relationship. Takes a string of the relationship
 *                             name and returns the relationship's value.
 * @return Relationship mapping
 */
protected Map<String, Relationship> getRelationshipsWithRelationshipFunction(final Function<String, Observable<PersistentResource>> relationshipFunction) {
    final Map<String, Relationship> relationshipMap = new LinkedHashMap<>();
    final Set<String> relationshipFields = filterFields(dictionary.getRelationships(obj));
    for (String field : relationshipFields) {
        TreeMap<String, Resource> orderedById = new TreeMap<>(lengthFirstComparator);
        for (PersistentResource relationship : relationshipFunction.apply(field).toList().blockingGet()) {
            orderedById.put(relationship.getId(), new ResourceIdentifier(relationship.getTypeName(), relationship.getId()).castToResource());
        }
        Observable<Resource> resources = Observable.fromIterable(orderedById.values());
        Data<Resource> data;
        RelationshipType relationshipType = getRelationshipType(field);
        if (relationshipType.isToOne()) {
            data = new Data<>(firstOrNullIfEmpty(resources));
        } else {
            data = new Data<>(resources);
        }
        Map<String, String> links = null;
        if (requestScope.getElideSettings().isEnableJsonLinks()) {
            links = requestScope.getElideSettings().getJsonApiLinks().getRelationshipLinks(this, field);
        }
        relationshipMap.put(field, new Relationship(links, data));
    }
    return relationshipMap;
}
Also used : Resource(com.yahoo.elide.jsonapi.models.Resource) RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType) TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap) ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) Relationship(com.yahoo.elide.jsonapi.models.Relationship)

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