use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class JsonApiTest method writeSingle.
@Test
public void writeSingle() throws JsonProcessingException {
Parent parent = new Parent();
Child child = new Child();
parent.setId(123L);
child.setId(2);
parent.setChildren(Collections.singleton(child));
parent.setFirstName("bob");
child.setParents(Collections.singleton(parent));
child.setFriends(new HashSet<>());
RequestScope userScope = new TestRequestScope(BASE_URL, tx, user, dictionary);
JsonApiDocument jsonApiDocument = new JsonApiDocument();
jsonApiDocument.setData(new Data<>(new PersistentResource<>(parent, userScope.getUUIDFor(parent), userScope).toResource()));
String expected = "{\"data\":{" + "\"type\":\"parent\"," + "\"id\":\"123\"," + "\"attributes\":{\"firstName\":\"bob\"}," + "\"relationships\":{" + "\"children\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/relationships/children\",\"related\":\"http://localhost:8080/json/parent/123/children\"}," + "\"data\":[{\"type\":\"child\",\"id\":\"2\"}]}," + "\"spouses\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/relationships/spouses\",\"related\":\"http://localhost:8080/json/parent/123/spouses\"}," + "\"data\":[]}}," + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123\"}}}";
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.JsonApiDocument 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.JsonApiDocument 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.JsonApiDocument 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.JsonApiDocument in project elide by yahoo.
the class JsonApiTest method readList.
@Test
public void readList() 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\"}}}}]}";
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();
assertEquals("parent", data.getType());
assertEquals("123", data.getId());
assertEquals("bob", attributes.get("firstName"));
assertEquals("2", data.getRelationships().get("children").getData().getSingleValue().getId());
assertNull(included);
checkEquality(jsonApiDocument);
}
Aggregations