@Test public void testJsonAPIErrorObjects() throws IOException { String request = "{ \"data\": { \"type\": \"nocreate\", \"id\": \"1\" } }"; JsonNode errors = objectMapper.readTree( RestAssured .given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .body(request) .post("/nocreate") .then() .statusCode(HttpStatus.SC_FORBIDDEN) .extract().body().asString()); for (JsonNode errorNode : errors.get("errors")) { Assert.assertTrue(errorNode.isObject(), "expected error should be object"); Assert.assertTrue(errorNode.has("detail"), "JsonAPI error should have 'detail'"); } }
@Test public void testGraphQLErrorObjects() throws IOException { String request = "mutation { nocreate(op: UPSERT, data:{id:\"1\"}) { edges { node { id } } } }"; JsonNode errors = objectMapper.readTree( RestAssured .given() .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) .body(request) .post("/graphQL") .then() .statusCode(HttpStatus.SC_FORBIDDEN) .extract().body().asString()); for (JsonNode errorNode : errors.get("errors")) { Assert.assertTrue(errorNode.isObject(), "expected error should be object"); Assert.assertTrue(errorNode.has("message"), "GraphQL error should have 'message'"); } } }
@Test(priority = 28) public void patchExtBadValue() throws IOException { // NOTE: This is a very hibernate/MySQL-centric test // TODO: If we want this test suite to be a universal suite for datastores, we need to refactor // these implementation details. String request = jsonParser.getJson("/ResourceIT/patchExtBadValue.req.json"); JsonNode result = jsonApiMapper.getObjectMapper().readTree(given() .contentType(JSONAPI_CONTENT_TYPE_WITH_JSON_PATCH_EXTENSION) .accept(JSONAPI_CONTENT_TYPE_WITH_JSON_PATCH_EXTENSION) .body(request) .patch("/") .then() .statusCode(HttpStatus.SC_LOCKED) .extract() .body() .asString()); JsonNode errors = result.get("errors"); assertNotNull(errors); assertEquals(errors.size(), 1); String error = errors.get(0).asText(); String expected = "TransactionException:"; assertTrue(error.startsWith(expected), "Error does not start with '" + expected + "' but found " + error); }
@Test(dataProvider = "like_queries") public void testSpecialCharacterLikeQuery(String filterParam, int noOfRecords) throws Exception { String actual = given().when().get(String.format("/book?%s", filterParam)).then().statusCode(HttpStatus.SC_OK) .extract().body().asString(); JsonApiDocument doc = jsonApiMapper.readJsonApiDocument(actual); assertEquals(doc.getData().get().size(), noOfRecords); }
@Test(priority = -1) public void testRootCollectionWithNoOperatorFilter() throws Exception { String actual = given().when().get("/parent?filter[parent.id][isnull]").then().statusCode(HttpStatus.SC_OK) .extract().body().asString(); JsonApiDocument doc = jsonApiMapper.readJsonApiDocument(actual); assertEquals(doc.getData().get().size(), 0); }
@Test public void testRootCollectionId() { String expected = jsonParser.getJson("/ResourceIT/testRootCollectionId.json"); String actual = given().when().get("/parent/1").then().statusCode(HttpStatus.SC_OK) .extract().body().asString(); assertEquals(actual, expected); }
@Test(priority = -1) public void testRootCollection() throws Exception { String actual = given().when().get("/parent").then().statusCode(HttpStatus.SC_OK) .extract().body().asString(); JsonApiDocument doc = jsonApiMapper.readJsonApiDocument(actual); assertEquals(doc.getData().get().size(), 4); }
@Test void testOne2One() { String expected = jsonParser.getJson("/EmbeddedIT/testOne2One.json"); String actual = given().when().get("/right/1") .then().statusCode(HttpStatus.SC_OK) .extract().body().asString(); assertEqualDocuments(actual, expected); } }
@Test(priority = 8) public void testGetReverseSortCollection() throws Exception { /* * /parent?sort=firstName * * {data: [ * parents sorted by name * ]} */ String expected = jsonParser.getJson("/ResourceIT/testGetReverseSortCollection.json"); String actual = given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .get("/parent?sort=-firstName") .then() .statusCode(HttpStatus.SC_OK) .extract().body().asString(); assertEquals(actual, expected); }
@Test(priority = 8) public void testGetNestedSingleInclude() throws IOException { String expected = jsonParser.getJson("/ResourceIT/testGetNestedSingleInclude.json"); String actual = given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .get("/parent/2?include=children.friends") .then() .statusCode(HttpStatus.SC_OK) .extract().body().asString(); assertEqualDocuments(actual, expected); }
@Test(priority = 8) public void testGetWithTrailingSlash() { String expected = jsonParser.getJson("/ResourceIT/testGetWithTrailingSlash.json"); String actual = given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .get("/parent/") .then() .statusCode(HttpStatus.SC_OK) .extract().body().asString(); assertEqualDocuments(actual, expected); }
@Test(priority = 0) public void testAuditOnCreate() throws Exception { String request = jsonParser.getJson("/AuditIT/createAuditEntity.req.json"); String expected = jsonParser.getJson("/AuditIT/createAuditEntity.resp.json"); String actual = given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .body(request) .post("/auditEntity") .then() .statusCode(HttpStatus.SC_CREATED) .extract().body().asString(); assertEqualDocuments(actual, expected); Assert.assertTrue(logger.logMessages.contains("old: null\n" + "new: Value: test abc relationship: null")); Assert.assertTrue(logger.logMessages.contains("Created with value: test abc")); }
@Test(priority = 8) public void testGetSortCollection() throws Exception { /* * /parent?sort=firstName * * {data: [ * parents sorted by name * ]} */ String expected = jsonParser.getJson("/ResourceIT/testGetSortCollection.json"); String actual = given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .get("/parent?sort=+firstName") .then() .statusCode(HttpStatus.SC_OK) .extract().body().asString(); assertEqualDocuments(actual, expected); }
@Test(priority = 30) public void createParentWithoutId() { String request = jsonParser.getJson("/ResourceIT/createParentWithoutId.req.json"); String expected = jsonParser.getJson("/ResourceIT/createParentWithoutId.json"); String actualResponse = given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .body(request) .post("/parent") .then() .statusCode(HttpStatus.SC_CREATED) .extract().body().asString(); assertEqualDocuments(actualResponse, expected); }
@Test(priority = 1) public void createParentNoRels() { String request = jsonParser.getJson("/ResourceIT/createParentNoRels.req.json"); String expected = jsonParser.getJson("/ResourceIT/createParentNoRels.json"); String actual = given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .body(request) .post("/parent") .then() .statusCode(HttpStatus.SC_CREATED) .extract().body().asString(); assertEqualDocuments(actual, expected); }
@Test(priority = 2) public void createParentWithRels() { String request = jsonParser.getJson("/ResourceIT/createParentWithRels.req.json"); String expected = jsonParser.getJson("/ResourceIT/createParentWithRels.json"); String actual = given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .body(request) .post("/parent") .then() .statusCode(HttpStatus.SC_CREATED) .extract().body().asString(); assertEqualDocuments(actual, expected); }
@Test(priority = 8) public void testGetSingleIncludeOnCollection() throws Exception { /* * /parent?include=children * * {data: [ * all the parents * ], include: [ * all the children belonging to a parent * ]} */ String expected = jsonParser.getJson("/ResourceIT/testGetSingleIncludeOnCollection.json"); String actual = given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .get("/parent?include=children") .then() .statusCode(HttpStatus.SC_OK) .extract().body().asString(); assertEqualDocuments(actual, expected); }
@Test(priority = 8) public void testGetMultipleIncludeOnCollection() throws Exception { /* * /parent?include=children,spouses * * {data: [ * all the parents * ], include: [ * all the children and spouses belonging to a parent * ]} */ String expected = jsonParser.getJson("/ResourceIT/testGetMultipleIncludeOnCollection.json"); String actual = given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .get("/parent?include=children,spouses") .then() .statusCode(HttpStatus.SC_OK) .extract().body().asString(); assertEqualDocuments(actual, expected); }
@Test(priority = 1) public void testAuditOnUpdate() { String request = jsonParser.getJson("/AuditIT/createAuditEntity2.req.json"); String expected = jsonParser.getJson("/AuditIT/createAuditEntity2.resp.json"); String actual = given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .body(request) .post("/auditEntity") .then() .statusCode(HttpStatus.SC_CREATED) .extract().body().asString(); assertEqualDocuments(actual, expected); request = jsonParser.getJson("/AuditIT/updateAuditEntity.req.json"); given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .body(request) .patch("/auditEntity/1") .then() .statusCode(HttpStatus.SC_NO_CONTENT); Assert.assertTrue(logger.logMessages.contains("Updated relationship (for id: 1): 2")); Assert.assertTrue(logger.logMessages.contains("Updated value (for id: 1): updated value")); }