Search in sources :

Example 1 with JsonApiDocument

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

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

the class JsonApiTest method writeEmptyList.

@Test
public void writeEmptyList() throws JsonProcessingException {
    String expected = "{\"data\":[]}";
    Data<Resource> empty = new Data<>(new ArrayList<>());
    JsonApiDocument jsonApiDocument = new JsonApiDocument();
    jsonApiDocument.setData(empty);
    Data<Resource> data = jsonApiDocument.getData();
    String doc = mapper.writeJsonApiDocument(jsonApiDocument);
    assertEquals(data, jsonApiDocument.getData());
    assertEquals(expected, doc);
    checkEquality(jsonApiDocument);
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Data(com.yahoo.elide.jsonapi.models.Data) Test(org.junit.jupiter.api.Test)

Example 3 with JsonApiDocument

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

the class JsonApiTest method compareNullAndEmpty.

@Test
public void compareNullAndEmpty() {
    Data<Resource> empty = new Data<>((Resource) null);
    JsonApiDocument jsonApiEmpty = new JsonApiDocument();
    jsonApiEmpty.setData(empty);
    JsonApiDocument jsonApiNull = new JsonApiDocument();
    jsonApiNull.setData(null);
    assertEquals(jsonApiEmpty, jsonApiNull);
    assertEquals(jsonApiEmpty.hashCode(), jsonApiNull.hashCode());
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Data(com.yahoo.elide.jsonapi.models.Data) Test(org.junit.jupiter.api.Test)

Example 4 with JsonApiDocument

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

the class JsonApiTest method writeEmptyObject.

@Test
public void writeEmptyObject() throws JsonProcessingException {
    String expected = "{\"data\":null}";
    Data<Resource> empty = new Data<>((Resource) null);
    JsonApiDocument jsonApiDocument = new JsonApiDocument();
    jsonApiDocument.setData(empty);
    Data<Resource> data = jsonApiDocument.getData();
    String doc = mapper.writeJsonApiDocument(jsonApiDocument);
    assertEquals(data, jsonApiDocument.getData());
    assertEquals(expected, doc);
    checkEquality(jsonApiDocument);
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Data(com.yahoo.elide.jsonapi.models.Data) Test(org.junit.jupiter.api.Test)

Example 5 with JsonApiDocument

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

the class JsonApiTest method readList.

@Test
public void readList() throws IOException {
    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\"}}}}]}";
    JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
    Data<Resource> list = jsonApiDocument.getData();
    Resource data = list.get().iterator().next();
    Map<String, Object> attributes = data.getAttributes();
    List<Resource> included = jsonApiDocument.getIncluded();
    assertEquals("parent", data.getType());
    assertEquals("123", data.getId());
    assertEquals("bob", attributes.get("firstName"));
    assertEquals("2", data.getRelationships().get("children").getData().getSingleValue().getId());
    assertNull(included);
    checkEquality(jsonApiDocument);
}
Also used : 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)

Aggregations

JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)51 Resource (com.yahoo.elide.jsonapi.models.Resource)31 Test (org.junit.jupiter.api.Test)26 PersistentResource (com.yahoo.elide.core.PersistentResource)25 RequestScope (com.yahoo.elide.core.RequestScope)15 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)9 Data (com.yahoo.elide.jsonapi.models.Data)8 IOException (java.io.IOException)7 TestRequestScope (com.yahoo.elide.core.TestRequestScope)6 Parent (example.Parent)6 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)5 EntityProjectionMaker (com.yahoo.elide.jsonapi.EntityProjectionMaker)4 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)4 Relationship (com.yahoo.elide.jsonapi.models.Relationship)4 BaseVisitor (com.yahoo.elide.jsonapi.parser.BaseVisitor)4 Child (example.Child)4 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)3 JsonApiMapper (com.yahoo.elide.jsonapi.JsonApiMapper)3 DocumentProcessor (com.yahoo.elide.jsonapi.document.processors.DocumentProcessor)3 IncludedProcessor (com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)3