use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class JsonApiTest method readSingle.
@Test
public void readSingle() throws IOException {
String doc = "{\"data\":{\"type\":\"parent\",\"id\":\"123\",\"attributes\":{\"firstName\":\"bob\"},\"relationships\":{\"children\":{\"data\":{\"type\":\"child\",\"id\":\"2\"}}}}}";
JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
Data<Resource> dataObj = jsonApiDocument.getData();
Resource data = dataObj.getSingleValue();
Map<String, Object> attributes = data.getAttributes();
Map<String, Relationship> relations = data.getRelationships();
assertEquals("parent", data.getType());
assertEquals("123", data.getId());
assertEquals("bob", attributes.get("firstName"));
assertEquals("child", relations.get("children").getData().getSingleValue().getType());
assertEquals("2", relations.get("children").getData().getSingleValue().getId());
checkEquality(jsonApiDocument);
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class JsonApiTest method writeList.
@Test
public void writeList() throws JsonProcessingException {
Parent parent = new Parent();
Child child = new Child();
parent.setId(123L);
parent.setSpouses(Sets.newHashSet());
child.setId(2);
parent.setChildren(Collections.singleton(child));
child.setParents(Collections.singleton(parent));
parent.setFirstName("bob");
child.setFriends(new HashSet<>());
RequestScope userScope = new TestRequestScope(BASE_URL, tx, user, dictionary);
JsonApiDocument jsonApiDocument = new JsonApiDocument();
jsonApiDocument.setData(new Data<>(Collections.singletonList(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 readSingleWithMeta.
@Test
public void readSingleWithMeta() throws IOException {
String doc = "{\"data\":{\"type\":\"parent\",\"id\":\"123\",\"attributes\":{\"firstName\":\"bob\"},\"relationships\":{\"children\":{\"data\":{\"type\":\"child\",\"id\":\"2\"}}}},\"meta\":{\"additional\":\"info\"}}";
JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
Meta meta = jsonApiDocument.getMeta();
Data<Resource> dataObj = jsonApiDocument.getData();
Resource data = dataObj.getSingleValue();
Map<String, Object> attributes = data.getAttributes();
Map<String, Relationship> relations = data.getRelationships();
assertEquals(meta.getMetaMap().get("additional"), "info");
assertEquals(data.getType(), "parent");
assertEquals(data.getId(), "123");
assertEquals(attributes.get("firstName"), "bob");
assertEquals(relations.get("children").getData().getSingleValue().getType(), "child");
assertEquals(relations.get("children").getData().getSingleValue().getId(), "2");
checkEquality(jsonApiDocument);
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class JsonApiTest method readListWithMeta.
@Test
public void readListWithMeta() 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\"}}}}],\"meta\":{\"additional\":\"info\"}}";
JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
Meta meta = jsonApiDocument.getMeta();
Data<Resource> list = jsonApiDocument.getData();
Resource data = list.get().iterator().next();
Map<String, Object> attributes = data.getAttributes();
List<Resource> included = jsonApiDocument.getIncluded();
assertEquals(meta.getMetaMap().get("additional"), "info");
assertEquals(data.getType(), "parent");
assertEquals(data.getId(), "123");
assertEquals(attributes.get("firstName"), "bob");
assertEquals(data.getRelationships().get("children").getData().getSingleValue().getId(), "2");
assertNull(included);
checkEquality(jsonApiDocument);
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class JsonApiTest method writeNullObject.
@Test
public void writeNullObject() throws JsonProcessingException {
String expected = "{\"data\":null}";
JsonApiDocument jsonApiDocument = new JsonApiDocument();
jsonApiDocument.setData(null);
assertNull(jsonApiDocument.getData());
String doc = mapper.writeJsonApiDocument(jsonApiDocument);
assertNull(jsonApiDocument.getData());
assertEquals(expected, doc);
checkEquality(jsonApiDocument);
}
Aggregations