use of example.Child 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.Child 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.Child in project elide by yahoo.
the class PersistenceResourceTestSetup method newChild.
protected static Child newChild(int id) {
Child child = new Child();
child.setId(id);
child.setParents(new HashSet<>());
child.setFriends(new HashSet<>());
return child;
}
use of example.Child in project elide by yahoo.
the class PersistentResourceNoopUpdateTest method testNOOPToManyAddRelation.
@Test
public void testNOOPToManyAddRelation() {
FunWithPermissions fun = new FunWithPermissions();
Child child = newChild(1);
Set<Child> children = new HashSet<>();
children.add(child);
fun.setRelation1(children);
DataStoreTransaction tx = mock(DataStoreTransaction.class);
RequestScope goodScope = new RequestScope(null, null, NO_VERSION, null, tx, goodUser, null, null, UUID.randomUUID(), elideSettings);
PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, "3", goodScope);
PersistentResource<Child> childResource = new PersistentResource<>(child, null, goodScope);
// We do not want the update to one method to be called when we add the existing entity to the relation
funResource.addRelation("relation1", childResource);
verify(tx, never()).updateToManyRelation(eq(tx), eq(child), eq("relation1"), any(), any(), eq(goodScope));
}
use of example.Child in project elide by yahoo.
the class PersistentResourceNoopUpdateTest method testNOOPToOneAddRelation.
@Test
public void testNOOPToOneAddRelation() {
FunWithPermissions fun = new FunWithPermissions();
Child child = newChild(1);
fun.setRelation3(child);
DataStoreTransaction tx = mock(DataStoreTransaction.class);
RequestScope goodScope = new RequestScope(null, null, NO_VERSION, null, tx, goodUser, null, null, UUID.randomUUID(), elideSettings);
PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, "3", goodScope);
PersistentResource<Child> childResource = new PersistentResource<>(child, "1", goodScope);
when(tx.getToOneRelation(eq(tx), eq(fun), any(), any())).thenReturn(child);
// We do not want the update to one method to be called when we add the existing entity to the relation
funResource.addRelation("relation3", childResource);
verify(tx, never()).updateToOneRelation(eq(tx), eq(fun), any(), any(), eq(goodScope));
}
Aggregations