Search in sources :

Example 1 with Parent

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);
}
Also used : TestRequestScope(com.yahoo.elide.core.TestRequestScope) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Parent(example.Parent) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Child(example.Child) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Test(org.junit.jupiter.api.Test)

Example 2 with Parent

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);
}
Also used : TestRequestScope(com.yahoo.elide.core.TestRequestScope) PersistentResource(com.yahoo.elide.core.PersistentResource) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Parent(example.Parent) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Child(example.Child) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Test(org.junit.jupiter.api.Test)

Example 3 with Parent

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;
}
Also used : Parent(example.Parent)

Example 4 with 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"));
}
Also used : Parent(example.Parent) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 5 with Parent

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");
}
Also used : Left(example.Left) Parent(example.Parent) Right(example.Right) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Child(example.Child) Test(org.junit.jupiter.api.Test)

Aggregations

Parent (example.Parent)40 Test (org.junit.jupiter.api.Test)34 Child (example.Child)27 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)26 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)11 Resource (com.yahoo.elide.jsonapi.models.Resource)8 PersistentResource (com.yahoo.elide.core.PersistentResource)7 RequestScope (com.yahoo.elide.core.RequestScope)7 TestRequestScope (com.yahoo.elide.core.TestRequestScope)7 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)6 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)4 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)3 TestUser (com.yahoo.elide.core.security.TestUser)3 Relationship (com.yahoo.elide.jsonapi.models.Relationship)3 ResourceIdentifier (com.yahoo.elide.jsonapi.models.ResourceIdentifier)3 FunWithPermissions (example.FunWithPermissions)3 ArrayList (java.util.ArrayList)3 LogMessage (com.yahoo.elide.core.audit.LogMessage)2 TestAuditLogger (com.yahoo.elide.core.audit.TestAuditLogger)2