use of com.yahoo.elide.jsonapi.models.Data in project elide by yahoo.
the class JsonApiTest method writeEmptyList.
@Test
public void writeEmptyList() throws JsonProcessingException {
String expected = "{\"data\":[]}";
Data<Resource> empty = new Data<>(new ArrayList<>());
JsonApiDocument jsonApiDocument = new JsonApiDocument();
jsonApiDocument.setData(empty);
Data<Resource> data = jsonApiDocument.getData();
String doc = mapper.writeJsonApiDocument(jsonApiDocument);
assertEquals(data, jsonApiDocument.getData());
assertEquals(expected, doc);
checkEquality(jsonApiDocument);
}
use of com.yahoo.elide.jsonapi.models.Data in project elide by yahoo.
the class JsonApiTest method compareNullAndEmpty.
@Test
public void compareNullAndEmpty() {
Data<Resource> empty = new Data<>((Resource) null);
JsonApiDocument jsonApiEmpty = new JsonApiDocument();
jsonApiEmpty.setData(empty);
JsonApiDocument jsonApiNull = new JsonApiDocument();
jsonApiNull.setData(null);
assertEquals(jsonApiEmpty, jsonApiNull);
assertEquals(jsonApiEmpty.hashCode(), jsonApiNull.hashCode());
}
use of com.yahoo.elide.jsonapi.models.Data in project elide by yahoo.
the class JsonApiTest method writeEmptyObject.
@Test
public void writeEmptyObject() throws JsonProcessingException {
String expected = "{\"data\":null}";
Data<Resource> empty = new Data<>((Resource) null);
JsonApiDocument jsonApiDocument = new JsonApiDocument();
jsonApiDocument.setData(empty);
Data<Resource> data = jsonApiDocument.getData();
String doc = mapper.writeJsonApiDocument(jsonApiDocument);
assertEquals(data, jsonApiDocument.getData());
assertEquals(expected, doc);
checkEquality(jsonApiDocument);
}
use of com.yahoo.elide.jsonapi.models.Data in project elide by yahoo.
the class CollectionTerminalState method getData.
private Data getData(Set<PersistentResource> collection, EntityDictionary dictionary) {
Preconditions.checkNotNull(collection);
List<Resource> resources = collection.stream().map(PersistentResource::toResource).collect(Collectors.toList());
if (parent.isPresent()) {
Type<?> parentClass = parent.get().getResourceType();
String relationshipName = relationName.orElseThrow(IllegalStateException::new);
RelationshipType type = dictionary.getRelationshipType(parentClass, relationshipName);
return new Data<>(resources, type);
}
return new Data<>(resources);
}
use of com.yahoo.elide.jsonapi.models.Data in project elide by yahoo.
the class CollectionTerminalState method handlePost.
@Override
public Supplier<Pair<Integer, JsonNode>> handlePost(StateContext state) {
RequestScope requestScope = state.getRequestScope();
JsonApiMapper mapper = requestScope.getMapper();
newObject = createObject(requestScope);
parent.ifPresent(persistentResource -> persistentResource.addRelation(relationName.get(), newObject));
return () -> {
JsonApiDocument returnDoc = new JsonApiDocument();
returnDoc.setData(new Data<>(newObject.toResource()));
JsonNode responseBody = mapper.getObjectMapper().convertValue(returnDoc, JsonNode.class);
return Pair.of(HttpStatus.SC_CREATED, responseBody);
};
}
Aggregations