Search in sources :

Example 1 with Child

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);
}
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 Child

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);
}
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 Child

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

Example 4 with 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));
}
Also used : DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) FunWithPermissions(example.FunWithPermissions) Child(example.Child) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with Child

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));
}
Also used : DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) FunWithPermissions(example.FunWithPermissions) Child(example.Child) Test(org.junit.jupiter.api.Test)

Aggregations

Child (example.Child)60 Test (org.junit.jupiter.api.Test)54 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)43 FunWithPermissions (example.FunWithPermissions)27 Parent (example.Parent)27 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)16 HashSet (java.util.HashSet)8 Resource (com.yahoo.elide.jsonapi.models.Resource)7 LinkedHashSet (java.util.LinkedHashSet)6 PersistentResource (com.yahoo.elide.core.PersistentResource)5 RequestScope (com.yahoo.elide.core.RequestScope)5 TestRequestScope (com.yahoo.elide.core.TestRequestScope)5 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)5 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)4 Relationship (com.yahoo.elide.jsonapi.models.Relationship)3 ResourceIdentifier (com.yahoo.elide.jsonapi.models.ResourceIdentifier)3 ArrayList (java.util.ArrayList)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 LogMessage (com.yahoo.elide.core.audit.LogMessage)2 TestAuditLogger (com.yahoo.elide.core.audit.TestAuditLogger)2