private List<TodoView> search(String value) { try { return Arrays.asList(objectMapper.readValue(given() .queryParam("searchValue", value) .when() .get(todoViewServiceBaseUrl() + "/todoviews") .then() .statusCode(200) .extract() .response() .asString(), TodoView[].class)); } catch (IOException e) { throw new RuntimeException(e); } }
@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'"); } }
.statusCode(200) .extract() .path("id") .toString();
/** * Should return the specification UI. * * @param testContext * @throws Exception */ @Test public void testGetSpecUi(TestContext testContext) throws Exception { final String body = given() .log().everything() .accept(ContentType.TEXT) .when() .get(OpenApiPluginImpl.SPECIFICATION_PATH + "/") .then() .log().everything() .statusCode(HttpUtil.HTTP_OK) .extract().asString(); testContext.assertTrue(body.contains("</html>")); }
@Test public void testCreateBuilding() throws InterruptedException { // given String payload = "{\"name\":\"testBuilding\"}"; // expect String createdId = given() .body(payload) .contentType("application/json") .when() .post("/building") .then() .statusCode(201) .extract().header("X-Created-Id"); // and when() .get("/building/" + createdId) .then() .statusCode(200) .body("name", is("testBuilding")); }
.statusCode(200) .extract() .path("id") .toString();
/** * Should return a combined specification. * * @param testContext * @throws Exception */ @Test public void testGetCombinedSpec(TestContext testContext) throws Exception { final String body = given() .log().everything() .accept(ContentType.JSON) .when() .get(OpenApiPluginImpl.COMBINED_SPECIFICATION_PATH) .then() .log().everything() .statusCode(HttpUtil.HTTP_OK) .extract().asString(); testContext.assertNotNull(body); final Swagger combined = new SwaggerParser().parse(body); testContext.assertNotNull(combined.getInfo()); testContext.assertEquals("Imposter Mock APIs", combined.getInfo().getTitle()); // should contain combination of both specs' endpoints testContext.assertEquals(4, combined.getPaths().size()); testContext.assertTrue(combined.getPaths().keySet().contains("/simple/apis")); testContext.assertTrue(combined.getPaths().keySet().contains("/api/pets")); }
private List<TodoView> search(String value) { try { return Arrays.asList(objectMapper.readValue(given() .queryParam("searchValue", value) .when() .get(baseUrl() + "/todoviews") .then() .statusCode(200) .extract() .response() .asString(), TodoView[].class)); } catch (IOException e) { throw new RuntimeException(e); } }
@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'"); } } }
/** * Should return the example from the specification for the default HTTP 200 status code, since the * content type in the 'Accept' matches that in the specification example. * * @param testContext * @throws Exception */ @Test public void testServeDefaultExampleMatchContentType(TestContext testContext) throws Exception { final String body = given() .log().everything() // JSON content type in 'Accept' header matches specification example .accept(ContentType.JSON) .when() .get("/simple/apis") .then() .log().everything() .statusCode(HttpUtil.HTTP_OK) .extract().asString(); assertBody(testContext, body); }
.statusCode(isSuccessful()) .and().contentType(not(TEXT_TURTLE)) .extract().response();
.body("data.relationships.myStuff.data.type", equalTo("tractor")) .extract() .path("data.id");
@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); }
/** * Should return the example from the specification for the default HTTP 200 status code, even though the * content type in the 'Accept' header does not match that in the specification example. * * @param testContext * @throws Exception * @see OpenApiPluginConfig#isPickFirstIfNoneMatch() */ @Test public void testServeDefaultExampleNoExactMatch(TestContext testContext) throws Exception { final String body = given() .log().everything() // do not set JSON content type in 'Accept' header, to force mismatch against specification example .accept(ContentType.TEXT) .when() .get("/simple/apis") .then() .log().everything() .statusCode(HttpUtil.HTTP_OK) .extract().asString(); assertBody(testContext, body); }
.then() .statusCode(HttpStatus.SC_OK) .extract().response().asString();
@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); }