JsonApiResponse { public static RequiredEntity createGetResponse(UriInfo uriInfo) { Assert.assertNotNull(uriInfo); return new Builder(new JsonApiResponse(uriInfo, Response.Status.OK)); } private  JsonApiResponse(UriInfo uriInfo, Response.StatusType statusCode); private  JsonApiResponse(Response.StatusType statusCode); static RequiredEntity createGetResponse(UriInfo uriInfo); static RequiredEntity createPostResponse(UriInfo uriInfo); static RequiredEntity createPutResponse(UriInfo uriInfo); static BuildableException createExceptionResponse(Response.Status code); static final String JSONAPI_TYPE; }
@Test public void testGetResponse() { setUpExpectations(); JsonApiIdentifiable minimalEntity = createMinimalEntity(); Response result = JsonApiResponse .createGetResponse(uriInfo) .data(minimalEntity) .build(); Assert.assertEquals(200, result.getStatus()); assertIsValidJsonApiDataResponse(result); JsonNode jEntity = extractJsonEntity(result); Assert.assertEquals(TEST_ID, jEntity.get("data").get("id").textValue()); Assert.assertEquals(ENTITY_PATH, jEntity.get("links").get("self").textValue()); Assert.assertTrue(jEntity.get("data").has("attributes")); }
@Test public void testGetResponseCollection() { setUpExpectations(); new Expectations() {{ uriInfo.getAbsolutePath(); result = URI.create(COLLECTION_PATH); }}; List<JsonApiIdentifiable> entityList = new ArrayList<>(); for (int i = 0; i < NR_ENTITIES; i++) { entityList.add(createCustomMinimalEntity(String.valueOf(i))); } Response result = JsonApiResponse .createGetResponse(uriInfo) .data(entityList) .build(); Assert.assertEquals(200, result.getStatus()); assertIsValidJsonApiDataResponse(result); JsonNode jArray = extractJsonEntity(result); Assert.assertTrue(jArray.get("data").isArray()); }
@Test public void testGetResponseRestrictTo() { setUpExpectations(); JsonApiIdentifiable entity = createEntityWithAttributes(); Response result = JsonApiResponse .createGetResponse(uriInfo) .data(entity) .restrictTo("intAttribute") .build(); assertIsValidJsonApiDataResponse(result); JsonNode resultJson = extractJsonEntity(result).get("data"); Assert.assertEquals(3, resultJson.size()); Assert.assertTrue(resultJson.has("attributes")); Assert.assertEquals(1, resultJson.get("attributes").size()); Assert.assertEquals(1, resultJson.get("attributes").get("intAttribute").asInt()); }
@Test public void testAddLinks() { setUpExpectations(); JsonApiIdentifiable minimalEntity = createMinimalEntity(); String linkUrl = "http: Response result = JsonApiResponse .createGetResponse(uriInfo) .data(minimalEntity) .addLink("TestLink", URI.create(linkUrl)) .build(); JsonNode resultAsJson = extractJsonEntity(result); Assert.assertTrue(resultAsJson.get("links").has("TestLink")); Assert.assertEquals(linkUrl, resultAsJson.get("links").get("TestLink").asText()); }
@Test public void testAddRelationship() { setUpExpectations(); JsonApiIdentifiable minimalEntity = createMinimalEntity(); JsonApiIdentifiable relatedEntity = createCustomMinimalEntity("related"); Response result = JsonApiResponse .createGetResponse(uriInfo) .data(minimalEntity) .addRelationship("related", relatedEntity, uriInfo.getAbsolutePath()) .build(); assertIsValidJsonApiDataResponse(result); JsonNode resultJson = extractJsonEntity(result).get("data"); Assert.assertTrue(resultJson.has("relationships")); Assert.assertEquals(1, resultJson.get("relationships").size()); Assert.assertTrue(resultJson.get("relationships").has("related")); JsonNode relationshipNode = resultJson.get("relationships").get("related"); assertHasValidRelationshipData(relationshipNode); Assert.assertEquals("related", relationshipNode.get("data").get("id").textValue()); }
@Test public void testAddRelationshipCollection() { setUpExpectations(); JsonApiIdentifiable minimalEntity = createMinimalEntity(); Collection<JsonApiIdentifiable> relatedCollection = new LinkedList<>(); for (int i = 0; i < NR_ENTITIES; i++) { relatedCollection.add(createCustomMinimalEntity(String.valueOf(i))); } Response result = JsonApiResponse .createGetResponse(uriInfo) .data(minimalEntity) .addRelationship("related", relatedCollection, uriInfo.getAbsolutePath()) .build(); assertIsValidJsonApiDataResponse(result); JsonNode resultJson = extractJsonEntity(result).get("data"); Assert.assertEquals(1, resultJson.get("relationships").size()); Assert.assertTrue(resultJson.get("relationships").has("related")); JsonNode relationshipNode = resultJson.get("relationships").get("related"); assertHasValidRelationshipData(relationshipNode); for (int i = 0; i < NR_ENTITIES; i++) { Assert.assertEquals(String.valueOf(i), relationshipNode.get("data").get(i).get("id").textValue()); } }
@Test public void testAddIncluded() { setUpExpectations(); JsonApiIdentifiable entityWithAttributes = createEntityWithAttributes(); JsonApiIdentifiable relatedEntity = createEntityWithAttributes("related"); Response result = JsonApiResponse .createGetResponse(uriInfo) .data(entityWithAttributes) .addRelationship("related", relatedEntity, uriInfo.getAbsolutePath()) .addIncluded(relatedEntity) .build(); assertIsValidJsonApiDataResponse(result); JsonNode resultJson = extractJsonEntity(result); Assert.assertTrue(resultJson.has("included")); Assert.assertEquals(1, resultJson.get("included").size()); JsonNode includedData = resultJson.get("included").get(0); assertIsValidDataNode(includedData); Assert.assertEquals("related", includedData.get("id").textValue()); }
@Test public void testAddIncludedWithCollection() { setUpExpectations(); JsonApiIdentifiable minimalEntity = createMinimalEntity(); List<JsonApiIdentifiable> relatedCollection = new LinkedList<>(); for (int i = 0; i < NR_ENTITIES; i++) { relatedCollection.add(createCustomMinimalEntity(String.valueOf(i))); } Response result = JsonApiResponse .createGetResponse(uriInfo) .data(minimalEntity) .addRelationship("relatedCollection", relatedCollection, uriInfo.getAbsolutePath()) .addIncluded(relatedCollection.get(0)) .build(); assertIsValidJsonApiDataResponse(result); JsonNode resultJson = extractJsonEntity(result); Assert.assertTrue(resultJson.has("included")); Assert.assertEquals(1, resultJson.get("included").size()); JsonNode includedData = resultJson.get("included").get(0); assertIsValidDataNode(includedData); Assert.assertEquals("0", includedData.get("id").textValue()); }
@Test(expected = IllegalArgumentException.class) public void testAddIncludedWithDifferentIdThrowsIllegalArgumentException() { setUpExpectations(); JsonApiIdentifiable minimalEntity = createMinimalEntity(); JsonApiIdentifiable relatedEntity = createCustomMinimalEntity("related"); JsonApiIdentifiable unrelatedEntity = createCustomMinimalEntity("unrelated"); JsonApiResponse .createGetResponse(uriInfo) .data(minimalEntity) .addRelationship("related", relatedEntity, uriInfo.getAbsolutePath()) .addIncluded(unrelatedEntity) .build(); }
@Test(expected = IllegalArgumentException.class) public void testAddIncludedWithDifferentTypeThrowsIllegalArgumentException() { setUpExpectations(); JsonApiIdentifiable minimalEntity = createMinimalEntity(); JsonApiIdentifiable relatedEntity = createCustomMinimalEntity(TEST_ID); JsonApiIdentifiable unrelatedEntity = createEntityWithAttributes(); JsonApiResponse .createGetResponse(uriInfo) .data(minimalEntity) .addRelationship("related", relatedEntity, uriInfo.getAbsolutePath()) .addIncluded(unrelatedEntity) .build(); }
@Test public void testFromRepositoryURI() { setUpExpectations(); URI collectionURI = URI.create("http: List<JsonApiIdentifiable> entities = new ArrayList<>(); for (int i = 0; i < NR_ENTITIES; i++) { entities.add(createCustomMinimalEntity(String.valueOf(i))); } Response result = JsonApiResponse .createGetResponse(uriInfo) .data(entities) .fromRepositoryURI(collectionURI) .build(); assertIsValidJsonApiDataResponse(result); JsonNode resultNode = extractJsonEntity(result); assertIsValidDataNode(resultNode.get("data")); for (int i = 0; i < NR_ENTITIES; i++) { JsonNode links = resultNode.get("data").get(i).get("links"); Assert.assertEquals(1, links.size()); Assert.assertEquals(collectionURI.toString() + "/" + i, links.get("self").textValue()); } }