Search in sources :

Example 26 with JsonApiDocument

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

the class ResourceIT method testRootCollection.

@Test
public void testRootCollection() throws Exception {
    String actual = given().when().get("/parent").then().statusCode(HttpStatus.SC_OK).extract().body().asString();
    JsonApiDocument doc = jsonApiMapper.readJsonApiDocument(actual);
    assertEquals(4, doc.getData().get().size());
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Test(org.junit.jupiter.api.Test) IntegrationTest(com.yahoo.elide.initialization.IntegrationTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 27 with JsonApiDocument

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

the class IntegrationTest method assertEqualDocuments.

protected void assertEqualDocuments(final String actual, final 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 28 with JsonApiDocument

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

the class BaseState method getResponseBody.

protected static JsonNode getResponseBody(PersistentResource resource, RequestScope requestScope) {
    MultivaluedMap<String, String> queryParams = requestScope.getQueryParams();
    JsonApiDocument jsonApiDocument = new JsonApiDocument();
    // TODO Make this a document processor
    Data<Resource> data = resource == null ? null : new Data<>(resource.toResource());
    jsonApiDocument.setData(data);
    // TODO Iterate over set of document processors
    DocumentProcessor includedProcessor = new IncludedProcessor();
    includedProcessor.execute(jsonApiDocument, resource, queryParams);
    return requestScope.getMapper().toJsonObject(jsonApiDocument);
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) DocumentProcessor(com.yahoo.elide.jsonapi.document.processors.DocumentProcessor) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) IncludedProcessor(com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)

Example 29 with JsonApiDocument

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

the class CollectionTerminalState method createObject.

private PersistentResource createObject(RequestScope requestScope) throws ForbiddenAccessException, InvalidObjectIdentifierException {
    JsonApiDocument doc = requestScope.getJsonApiDocument();
    JsonApiMapper mapper = requestScope.getMapper();
    if (doc.getData() == null) {
        throw new InvalidEntityBodyException("Invalid JSON-API document: " + doc);
    }
    Data<Resource> data = doc.getData();
    Collection<Resource> resources = data.get();
    Resource resource = (resources.size() == 1) ? IterableUtils.first(resources) : null;
    if (resource == null) {
        try {
            throw new InvalidEntityBodyException(mapper.writeJsonApiDocument(doc));
        } catch (JsonProcessingException e) {
            throw new InternalServerErrorException(e);
        }
    }
    String id = resource.getId();
    Type<?> newObjectClass = requestScope.getDictionary().getEntityClass(resource.getType(), requestScope.getApiVersion());
    if (newObjectClass == null) {
        throw new UnknownEntityException("Entity " + resource.getType() + " not found");
    }
    if (!entityClass.isAssignableFrom(newObjectClass)) {
        throw new InvalidValueException("Cannot assign value of type: " + resource.getType() + " to type: " + entityClass);
    }
    PersistentResource pResource = PersistentResource.createObject(parent.orElse(null), relationName.orElse(null), newObjectClass, requestScope, Optional.ofNullable(id));
    Map<String, Object> attributes = resource.getAttributes();
    if (attributes != null) {
        for (Map.Entry<String, Object> entry : attributes.entrySet()) {
            String fieldName = entry.getKey();
            Object val = entry.getValue();
            pResource.updateAttribute(fieldName, val);
        }
    }
    Map<String, Relationship> relationships = resource.getRelationships();
    if (relationships != null) {
        for (Map.Entry<String, Relationship> entry : relationships.entrySet()) {
            String fieldName = entry.getKey();
            Relationship relationship = entry.getValue();
            Set<PersistentResource> resourceSet = (relationship == null) ? null : relationship.toPersistentResources(requestScope);
            pResource.updateRelation(fieldName, resourceSet);
        }
    }
    return pResource;
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) UnknownEntityException(com.yahoo.elide.core.exceptions.UnknownEntityException) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) ToString(lombok.ToString) InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) Relationship(com.yahoo.elide.jsonapi.models.Relationship) InternalServerErrorException(com.yahoo.elide.core.exceptions.InternalServerErrorException) JsonApiMapper(com.yahoo.elide.jsonapi.JsonApiMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 30 with JsonApiDocument

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

the class RelationshipTerminalState method handleGet.

@Override
public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) {
    JsonApiDocument doc = new JsonApiDocument();
    RequestScope requestScope = state.getRequestScope();
    JsonApiMapper mapper = requestScope.getMapper();
    MultivaluedMap<String, String> queryParams = requestScope.getQueryParams();
    Map<String, Relationship> relationships = record.toResource(parentProjection).getRelationships();
    if (relationships != null && relationships.containsKey(relationshipName)) {
        Relationship relationship = relationships.get(relationshipName);
        // Handle valid relationship
        // Set data
        Data<Resource> data = relationship.getData();
        doc.setData(data);
        // Run include processor
        DocumentProcessor includedProcessor = new IncludedProcessor();
        includedProcessor.execute(doc, record, queryParams);
        return () -> Pair.of(HttpStatus.SC_OK, mapper.toJsonObject(doc));
    }
    // Handle no data for relationship
    if (relationshipType.isToMany()) {
        doc.setData(new Data<>(new ArrayList<>()));
    } else if (relationshipType.isToOne()) {
        doc.setData(new Data<>((Resource) null));
    } else {
        throw new IllegalStateException("Failed to GET a relationship; relationship is neither toMany nor toOne");
    }
    return () -> Pair.of(HttpStatus.SC_OK, mapper.toJsonObject(doc));
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) DocumentProcessor(com.yahoo.elide.jsonapi.document.processors.DocumentProcessor) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) ArrayList(java.util.ArrayList) Data(com.yahoo.elide.jsonapi.models.Data) RequestScope(com.yahoo.elide.core.RequestScope) Relationship(com.yahoo.elide.jsonapi.models.Relationship) JsonApiMapper(com.yahoo.elide.jsonapi.JsonApiMapper) IncludedProcessor(com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)

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