Search in sources :

Example 21 with Relationship

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

the class JsonApiTest method readSingle.

@Test
public void readSingle() throws IOException {
    String doc = "{\"data\":{\"type\":\"parent\",\"id\":\"123\",\"attributes\":{\"firstName\":\"bob\"},\"relationships\":{\"children\":{\"data\":{\"type\":\"child\",\"id\":\"2\"}}}}}";
    JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
    Data<Resource> dataObj = jsonApiDocument.getData();
    Resource data = dataObj.getSingleValue();
    Map<String, Object> attributes = data.getAttributes();
    Map<String, Relationship> relations = data.getRelationships();
    assertEquals("parent", data.getType());
    assertEquals("123", data.getId());
    assertEquals("bob", attributes.get("firstName"));
    assertEquals("child", relations.get("children").getData().getSingleValue().getType());
    assertEquals("2", relations.get("children").getData().getSingleValue().getId());
    checkEquality(jsonApiDocument);
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Test(org.junit.jupiter.api.Test)

Example 22 with Relationship

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

the class JsonApiTest method readSingleWithMeta.

@Test
public void readSingleWithMeta() throws IOException {
    String doc = "{\"data\":{\"type\":\"parent\",\"id\":\"123\",\"attributes\":{\"firstName\":\"bob\"},\"relationships\":{\"children\":{\"data\":{\"type\":\"child\",\"id\":\"2\"}}}},\"meta\":{\"additional\":\"info\"}}";
    JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
    Meta meta = jsonApiDocument.getMeta();
    Data<Resource> dataObj = jsonApiDocument.getData();
    Resource data = dataObj.getSingleValue();
    Map<String, Object> attributes = data.getAttributes();
    Map<String, Relationship> relations = data.getRelationships();
    assertEquals(meta.getMetaMap().get("additional"), "info");
    assertEquals(data.getType(), "parent");
    assertEquals(data.getId(), "123");
    assertEquals(attributes.get("firstName"), "bob");
    assertEquals(relations.get("children").getData().getSingleValue().getType(), "child");
    assertEquals(relations.get("children").getData().getSingleValue().getId(), "2");
    checkEquality(jsonApiDocument);
}
Also used : Meta(com.yahoo.elide.jsonapi.models.Meta) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Test(org.junit.jupiter.api.Test)

Example 23 with Relationship

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

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