Search in sources :

Example 6 with Resource

use of com.yahoo.elide.jsonapi.models.Resource 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 7 with Resource

use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.

the class JsonApiTest method readSingleIncluded.

@Test
public void readSingleIncluded() throws Exception {
    String doc = "{\"data\":{\"type\":\"parent\",\"id\":\"123\",\"attributes\":{\"firstName\":\"bob\"},\"relationships\":{\"children\":{\"links\":{\"self\":\"/parent/123/relationships/child\",\"related\":\"/parent/123/child\"},\"data\":{\"type\":\"child\",\"id\":\"2\"}}}},\"included\":[{\"type\":\"child\",\"id\":\"2\",\"relationships\":{\"parents\":{\"links\":{\"self\":\"/parent/123/relationships/child\",\"related\":\"/parent/123/child\"},\"data\":{\"type\":\"parent\",\"id\":\"123\"}}}}]}";
    JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
    Data<Resource> dataObj = jsonApiDocument.getData();
    Resource data = dataObj.getSingleValue();
    Map<String, Object> attributes = data.getAttributes();
    List<Resource> included = jsonApiDocument.getIncluded();
    Resource includedChild = IterableUtils.first(included);
    ResourceIdentifier parent = includedChild.getRelationships().get("parents").getResourceIdentifierData().getSingleValue();
    assertEquals("parent", data.getType());
    assertEquals("123", data.getId());
    assertEquals("bob", attributes.get("firstName"));
    assertEquals("child", includedChild.getType());
    assertEquals("2", includedChild.getId());
    assertEquals("123", parent.getId());
    checkEquality(jsonApiDocument);
}
Also used : ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Test(org.junit.jupiter.api.Test)

Example 8 with Resource

use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.

the class IncludedProcessorTest method testExecuteSingleNestedRelation.

@Test
public void testExecuteSingleNestedRelation() throws Exception {
    JsonApiDocument jsonApiDocument = new JsonApiDocument();
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.put(INCLUDE, Collections.singletonList("children.friends"));
    testScope.setQueryParams(queryParams);
    includedProcessor.execute(jsonApiDocument, parentRecord1, queryParams);
    List<Resource> expectedIncluded = Arrays.asList(childRecord1.toResource(), childRecord2.toResource());
    List<Resource> actualIncluded = jsonApiDocument.getIncluded();
    assertEquals(expectedIncluded, actualIncluded, "Included Processor added single nested requested resources from 'include' query param");
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Test(org.junit.jupiter.api.Test)

Example 9 with Resource

use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.

the class IncludedProcessorTest method testExecuteSingleRelation.

@Test
public void testExecuteSingleRelation() throws Exception {
    JsonApiDocument jsonApiDocument = new JsonApiDocument();
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.put(INCLUDE, Collections.singletonList("children"));
    testScope.setQueryParams(queryParams);
    includedProcessor.execute(jsonApiDocument, parentRecord1, queryParams);
    List<Resource> expectedIncluded = Collections.singletonList(childRecord1.toResource());
    List<Resource> actualIncluded = jsonApiDocument.getIncluded();
    assertEquals(expectedIncluded, actualIncluded, "Included Processor added single requested resource from 'include' query param");
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Test(org.junit.jupiter.api.Test)

Example 10 with Resource

use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.

the class IncludedProcessorTest method testExecuteMultipleNestedRelations.

@Test
public void testExecuteMultipleNestedRelations() throws Exception {
    JsonApiDocument jsonApiDocument = new JsonApiDocument();
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.put(INCLUDE, Collections.singletonList("children.friends"));
    testScope.setQueryParams(queryParams);
    includedProcessor.execute(jsonApiDocument, parentRecord3, queryParams);
    Set<Resource> expectedIncluded = Sets.newHashSet(childRecord1.toResource(), childRecord2.toResource(), childRecord3.toResource(), childRecord4.toResource());
    Set<Resource> actualIncluded = new HashSet<>(jsonApiDocument.getIncluded());
    assertEquals(expectedIncluded, actualIncluded, "Included Processor added multiple nested requested resource collections from 'include' query param");
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

Resource (com.yahoo.elide.jsonapi.models.Resource)55 Test (org.junit.jupiter.api.Test)35 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)31 PersistentResource (com.yahoo.elide.core.PersistentResource)28 Relationship (com.yahoo.elide.jsonapi.models.Relationship)21 ArrayList (java.util.ArrayList)17 ResourceIdentifier (com.yahoo.elide.jsonapi.models.ResourceIdentifier)16 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)13 Data (com.yahoo.elide.jsonapi.models.Data)13 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)9 LinkedHashSet (java.util.LinkedHashSet)9 Parent (example.Parent)8 HashSet (java.util.HashSet)8 EntityProjection (com.yahoo.elide.core.request.EntityProjection)7 Child (example.Child)7 LinkedHashMap (java.util.LinkedHashMap)7 RequestScope (com.yahoo.elide.core.RequestScope)6 TestUser (com.yahoo.elide.core.security.TestUser)6 User (com.yahoo.elide.core.security.User)6 TestRequestScope (com.yahoo.elide.core.TestRequestScope)5