use of example.Parent 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 example.Parent in project elide by yahoo.
the class JsonApiTest method writeListIncluded.
@Test
public void writeListIncluded() throws JsonProcessingException {
Parent parent = new Parent();
Child child = new Child();
parent.setId(123L);
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);
PersistentResource<Parent> pRec = new PersistentResource<>(parent, userScope.getUUIDFor(parent), userScope);
JsonApiDocument jsonApiDocument = new JsonApiDocument();
jsonApiDocument.setData(new Data<>(Collections.singletonList(pRec.toResource())));
jsonApiDocument.addIncluded(new PersistentResource<>(child, pRec, "children", userScope.getUUIDFor(child), userScope).toResource());
// duplicate will be ignored
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 example.Parent in project elide by yahoo.
the class IncludedProcessorTest method newParent.
private static Parent newParent(int id) {
Parent parent = new Parent();
parent.setId(id);
parent.setChildren(new HashSet<>());
parent.setSpouses(new HashSet<>());
return parent;
}
use of example.Parent in project elide by yahoo.
the class PersistentResourceTest method testUpdateAttributeInvalidAttribute.
@Test
public void testUpdateAttributeInvalidAttribute() {
Parent parent = newParent(1);
RequestScope goodScope = buildRequestScope(tx, goodUser);
PersistentResource<Parent> parentResource = new PersistentResource<>(parent, "1", goodScope);
assertThrows(InvalidAttributeException.class, () -> parentResource.updateAttribute("invalid", "foobar"));
}
use of example.Parent in project elide by yahoo.
the class PersistentResourceTest method testDeleteBidirectionalRelation.
@Test
public void testDeleteBidirectionalRelation() {
Left left = new Left();
Right right = new Right();
left.setOne2one(right);
right.setOne2one(left);
RequestScope scope = new TestRequestScope(tx, goodUser, dictionary);
PersistentResource<Left> leftResource = new PersistentResource<>(left, "3", scope);
leftResource.deleteInverseRelation("one2one", right);
assertNull(right.getOne2one(), "The one-2-one inverse relationship should have been unset");
assertEquals(right, left.getOne2one(), "The owning relationship should NOT have been unset");
Child child = new Child();
Parent parent = new Parent();
child.setParents(Sets.newHashSet(parent));
parent.setChildren(Sets.newHashSet(child));
parent.setSpouses(Sets.newHashSet());
scope = new TestRequestScope(tx, goodUser, dictionary);
PersistentResource<Child> childResource = new PersistentResource<>(child, "4", scope);
childResource.deleteInverseRelation("parents", parent);
assertEquals(parent.getChildren().size(), 0, "The many-2-many inverse collection should have been cleared.");
assertTrue(child.getParents().contains(parent), "The owning relationship should NOT have been touched");
}
Aggregations