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());
}
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);
}
}
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);
}
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;
}
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));
}
Aggregations