Search in sources :

Example 31 with JsonApiDocument

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

the class JsonApiPatch method handleReplaceOp.

/**
 * Replace data via patch extension.
 */
private Supplier<Pair<Integer, JsonNode>> handleReplaceOp(String path, JsonNode patchVal, PatchRequestScope requestScope, PatchAction action) {
    try {
        JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchVal);
        if (!path.contains("relationships")) {
            // Reserved
            Data<Resource> data = value.getData();
            Collection<Resource> resources = data.get();
            // Defer relationship updating until the end
            getSingleResource(resources).setRelationships(null);
            // Reparse since we mangle it first
            action.doc = requestScope.getMapper().readJsonApiPatchExtValue(patchVal);
            action.path = path;
            action.isPostProcessing = true;
        }
        // Defer relationship updating until the end
        PatchVisitor visitor = new PatchVisitor(new PatchRequestScope(path, value, requestScope));
        return visitor.visit(JsonApiParser.parse(path));
    } catch (IOException e) {
        throw new InvalidEntityBodyException("Could not parse patch extension value: " + patchVal);
    }
}
Also used : InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PatchVisitor(com.yahoo.elide.jsonapi.parser.PatchVisitor) IOException(java.io.IOException)

Example 32 with JsonApiDocument

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

the class JsonApiPatch method handleAddOp.

/**
 * Add a document via patch extension.
 */
private Supplier<Pair<Integer, JsonNode>> handleAddOp(String path, JsonNode patchValue, PatchRequestScope requestScope, PatchAction action) {
    try {
        JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchValue);
        Data<Resource> data = value.getData();
        if (data == null || data.get() == null) {
            throw new InvalidEntityBodyException("Expected an entity body but received none.");
        }
        Collection<Resource> resources = data.get();
        if (!path.contains("relationships")) {
            // Reserved key for relationships
            String id = getSingleResource(resources).getId();
            if (StringUtils.isEmpty(id)) {
                throw new InvalidEntityBodyException("Patch extension requires all objects to have an assigned " + "ID (temporary or permanent) when assigning relationships.");
            }
            String fullPath = path + "/" + id;
            // Defer relationship updating until the end
            getSingleResource(resources).setRelationships(null);
            // Reparse since we mangle it first
            action.doc = requestScope.getMapper().readJsonApiPatchExtValue(patchValue);
            action.path = fullPath;
            action.isPostProcessing = true;
        }
        PostVisitor visitor = new PostVisitor(new PatchRequestScope(path, value, requestScope));
        return visitor.visit(JsonApiParser.parse(path));
    } catch (HttpStatusException e) {
        action.cause = e;
        throw e;
    } catch (IOException e) {
        throw new InvalidEntityBodyException("Could not parse patch extension value: " + patchValue);
    }
}
Also used : InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) IOException(java.io.IOException) PostVisitor(com.yahoo.elide.jsonapi.parser.PostVisitor)

Example 33 with JsonApiDocument

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

the class AbstractHibernateTestService method assertEqualDocuments.

protected void assertEqualDocuments(String actual, String expected) {
    try {
        JsonApiDocument expectedDoc = jsonApiMapper.readJsonApiDocument(expected);
        JsonApiDocument actualDoc = jsonApiMapper.readJsonApiDocument(actual);
        assertEquals(expectedDoc, actualDoc, "\n" + actual + "\n" + expected + "\n");
    } catch (IOException e) {
        fail("\n" + actual + "\n" + expected + "\n", e);
    }
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) IOException(java.io.IOException)

Example 34 with JsonApiDocument

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

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

the class JsonApiTest method checkEquality.

private void checkEquality(JsonApiDocument doc1) {
    JsonApiDocument doc2;
    try {
        String json = mapper.writeJsonApiDocument(doc1);
        doc2 = mapper.readJsonApiDocument(json);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    assertEquals(doc1, doc2);
    assertEquals(doc1.hashCode(), doc2.hashCode());
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Aggregations

JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)51 Resource (com.yahoo.elide.jsonapi.models.Resource)31 Test (org.junit.jupiter.api.Test)26 PersistentResource (com.yahoo.elide.core.PersistentResource)25 RequestScope (com.yahoo.elide.core.RequestScope)15 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)9 Data (com.yahoo.elide.jsonapi.models.Data)8 IOException (java.io.IOException)7 TestRequestScope (com.yahoo.elide.core.TestRequestScope)6 Parent (example.Parent)6 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)5 EntityProjectionMaker (com.yahoo.elide.jsonapi.EntityProjectionMaker)4 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)4 Relationship (com.yahoo.elide.jsonapi.models.Relationship)4 BaseVisitor (com.yahoo.elide.jsonapi.parser.BaseVisitor)4 Child (example.Child)4 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)3 JsonApiMapper (com.yahoo.elide.jsonapi.JsonApiMapper)3 DocumentProcessor (com.yahoo.elide.jsonapi.document.processors.DocumentProcessor)3 IncludedProcessor (com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)3