use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class JsonApiTest method writeSingleIncluded.
@Test
public void writeSingleIncluded() throws JsonProcessingException {
Parent parent = new Parent();
Child child = new Child();
parent.setId(123L);
child.setId(2);
parent.setFirstName("bob");
parent.setChildren(Collections.singleton(child));
child.setParents(Collections.singleton(parent));
child.setFriends(new HashSet<>());
RequestScope userScope = new TestRequestScope(BASE_URL, tx, user, dictionary);
PersistentResource<Parent> pRec = new PersistentResource<>(parent, userScope.getUUIDFor(parent), userScope);
JsonApiDocument jsonApiDocument = new JsonApiDocument();
jsonApiDocument.setData(new Data<>(pRec.toResource()));
jsonApiDocument.addIncluded(new PersistentResource<>(child, pRec, "children", userScope.getUUIDFor(child), 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\"}}," + "\"included\":[{" + "\"type\":\"child\"," + "\"id\":\"2\"," + "\"attributes\":{\"name\":null}," + "\"relationships\":{" + "\"friends\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/children/2/relationships/friends\",\"related\":\"http://localhost:8080/json/parent/123/children/2/friends\"}," + "\"data\":[]}," + "\"parents\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/children/2/relationships/parents\",\"related\":\"http://localhost:8080/json/parent/123/children/2/parents\"}," + "\"data\":[{\"type\":\"parent\",\"id\":\"123\"}]}}," + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/children/2\"}}]}";
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 writeSingleNoAttributesNoRel.
@Test
public void writeSingleNoAttributesNoRel() throws JsonProcessingException {
Parent parent = new Parent();
parent.setId(123L);
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\":null}," + "\"relationships\":{" + "\"children\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/relationships/children\",\"related\":\"http://localhost:8080/json/parent/123/children\"}," + "\"data\":[]}," + "\"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 compareOrder.
@Test
public void compareOrder() {
Parent parent1 = new Parent();
parent1.setId(123L);
Parent parent2 = new Parent();
parent2.setId(456L);
RequestScope userScope = new TestRequestScope(BASE_URL, tx, user, dictionary);
PersistentResource<Parent> pRec1 = new PersistentResource<>(parent1, userScope.getUUIDFor(parent1), userScope);
PersistentResource<Parent> pRec2 = new PersistentResource<>(parent2, userScope.getUUIDFor(parent2), userScope);
JsonApiDocument jsonApiDocument1 = new JsonApiDocument();
jsonApiDocument1.setData(new Data<>(Lists.newArrayList(pRec1.toResource(), pRec2.toResource())));
JsonApiDocument jsonApiDocument2 = new JsonApiDocument();
jsonApiDocument2.setData(new Data<>(Lists.newArrayList(pRec2.toResource(), pRec1.toResource())));
assertEquals(jsonApiDocument1, jsonApiDocument2);
assertEquals(jsonApiDocument1.hashCode(), jsonApiDocument2.hashCode());
jsonApiDocument1.getData().sort((a, b) -> Integer.compare(a.hashCode(), b.hashCode()));
jsonApiDocument2.getData().sort((a, b) -> Integer.compare(b.hashCode(), a.hashCode()));
assertEquals(jsonApiDocument1, jsonApiDocument2);
assertEquals(jsonApiDocument1.hashCode(), jsonApiDocument2.hashCode());
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class IncludedProcessorTest method testIncludeForbiddenRelationship.
@Test
public void testIncludeForbiddenRelationship() {
JsonApiDocument jsonApiDocument = new JsonApiDocument();
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
queryParams.put(INCLUDE, Collections.singletonList("relation1"));
testScope.setQueryParams(queryParams);
includedProcessor.execute(jsonApiDocument, funWithPermissionsRecord, queryParams);
assertNull(jsonApiDocument.getIncluded(), "Included Processor included forbidden relationship");
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class IncludedProcessorTest method testNoQueryParams.
@Test
public void testNoQueryParams() throws Exception {
JsonApiDocument jsonApiDocument = new JsonApiDocument();
includedProcessor.execute(jsonApiDocument, parentRecord1, null);
assertNull(jsonApiDocument.getIncluded(), "Included Processor adds no resources when not given query params");
}
Aggregations