Rest-assured fix (#2759)

This commit is contained in:
Grzegorz Piwowarek 2017-10-19 17:32:02 +02:00 committed by GitHub
parent e6d82e8ced
commit fc0ed8fe3d
5 changed files with 218 additions and 262 deletions

View File

@ -1,29 +1,27 @@
package com.baeldung.restassured; package com.baeldung.restassured;
import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static io.restassured.RestAssured.get;
import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.hasItems;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static io.restassured.RestAssured.get;
import com.github.tomakehurst.wiremock.WireMockServer;
public class RestAssured2IntegrationTest { public class RestAssured2IntegrationTest {
private WireMockServer wireMockServer = new WireMockServer(); private static WireMockServer wireMockServer = new WireMockServer();
private static final String EVENTS_PATH = "/odds"; private static final String EVENTS_PATH = "/odds";
private static final String APPLICATION_JSON = "application/json"; private static final String APPLICATION_JSON = "application/json";
private static final String ODDS = getJson(); private static final String ODDS = getJson();
@Before @BeforeClass
public void before() throws Exception { public static void before() throws Exception {
System.out.println("Setting up!"); System.out.println("Setting up!");
wireMockServer.start(); wireMockServer.start();
configureFor("localhost", 8080); configureFor("localhost", 8080);
@ -41,15 +39,13 @@ public class RestAssured2IntegrationTest {
private static String getJson() { private static String getJson() {
return Util.inputStreamToString(new RestAssured2IntegrationTest().getClass() return Util.inputStreamToString(RestAssured2IntegrationTest.class
.getResourceAsStream("/odds.json")); .getResourceAsStream("/odds.json"));
} }
@After @AfterClass
public void after() throws Exception { public static void after() throws Exception {
System.out.println("Running: tearDown"); System.out.println("Running: tearDown");
wireMockServer.stop(); wireMockServer.stop();
} }
} }

View File

@ -1,5 +1,13 @@
package com.baeldung.restassured; package com.baeldung.restassured;
import com.github.fge.jsonschema.SchemaVersion;
import com.github.fge.jsonschema.cfg.ValidationConfiguration;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.get;
@ -11,24 +19,15 @@ import static io.restassured.module.jsv.JsonSchemaValidatorSettings.settings;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.hasItems;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.github.fge.jsonschema.SchemaVersion;
import com.github.fge.jsonschema.cfg.ValidationConfiguration;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.tomakehurst.wiremock.WireMockServer;
public class RestAssuredIntegrationTest { public class RestAssuredIntegrationTest {
private WireMockServer wireMockServer = new WireMockServer(); private static WireMockServer wireMockServer = new WireMockServer();
private static final String EVENTS_PATH = "/events?id=390"; private static final String EVENTS_PATH = "/events?id=390";
private static final String APPLICATION_JSON = "application/json"; private static final String APPLICATION_JSON = "application/json";
private static final String GAME_ODDS = getEventJson(); private static final String GAME_ODDS = getEventJson();
@Before @BeforeClass
public void before() throws Exception { public static void before() throws Exception {
System.out.println("Setting up!"); System.out.println("Setting up!");
wireMockServer.start(); wireMockServer.start();
configureFor("localhost", 8080); configureFor("localhost", 8080);
@ -49,7 +48,6 @@ public class RestAssuredIntegrationTest {
get("/events?id=390").then().statusCode(200).assertThat() get("/events?id=390").then().statusCode(200).assertThat()
.body("id", equalTo("390")); .body("id", equalTo("390"));
} }
@Test @Test
@ -79,7 +77,6 @@ public class RestAssuredIntegrationTest {
.assertThat() .assertThat()
.body(matchesJsonSchemaInClasspath("event_0.json").using( .body(matchesJsonSchemaInClasspath("event_0.json").using(
jsonSchemaFactory)); jsonSchemaFactory));
} }
@Test @Test
@ -92,8 +89,8 @@ public class RestAssuredIntegrationTest {
settings().with().checkedValidation(false))); settings().with().checkedValidation(false)));
} }
@After @AfterClass
public void after() throws Exception { public static void after() throws Exception {
System.out.println("Running: tearDown"); System.out.println("Running: tearDown");
wireMockServer.stop(); wireMockServer.stop();
} }
@ -102,5 +99,4 @@ public class RestAssuredIntegrationTest {
return Util.inputStreamToString(RestAssuredIntegrationTest.class return Util.inputStreamToString(RestAssuredIntegrationTest.class
.getResourceAsStream("/event_0.json")); .getResourceAsStream("/event_0.json"));
} }
} }

View File

@ -1,29 +1,27 @@
package com.baeldung.restassured; package com.baeldung.restassured;
import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static io.restassured.RestAssured.get;
import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.hasItems;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static io.restassured.RestAssured.get;
import com.github.tomakehurst.wiremock.WireMockServer;
public class RestAssuredXML2IntegrationTest { public class RestAssuredXML2IntegrationTest {
private WireMockServer wireMockServer = new WireMockServer(); private static WireMockServer wireMockServer = new WireMockServer();
private static final String EVENTS_PATH = "/teachers"; private static final String EVENTS_PATH = "/teachers";
private static final String APPLICATION_XML = "application/xml"; private static final String APPLICATION_XML = "application/xml";
private static final String TEACHERS = getXml(); private static final String TEACHERS = getXml();
@Before @BeforeClass
public void before() throws Exception { public static void before() throws Exception {
System.out.println("Setting up!"); System.out.println("Setting up!");
wireMockServer.start(); wireMockServer.start();
configureFor("localhost", 8080); configureFor("localhost", 8080);
@ -32,6 +30,7 @@ public class RestAssuredXML2IntegrationTest {
.withHeader("Content-Type", APPLICATION_XML) .withHeader("Content-Type", APPLICATION_XML)
.withBody(TEACHERS))); .withBody(TEACHERS)));
} }
@Test @Test
public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() { public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() {
get("/teachers") get("/teachers")
@ -39,16 +38,15 @@ public class RestAssuredXML2IntegrationTest {
.body("teachers.teacher.find { it.@department == 'science' }.subject", .body("teachers.teacher.find { it.@department == 'science' }.subject",
hasItems("math", "physics")); hasItems("math", "physics"));
} }
private static String getXml() { private static String getXml() {
return Util.inputStreamToString(RestAssuredXML2IntegrationTest.class
return Util .getResourceAsStream("/teachers.xml"));
.inputStreamToString(new RestAssuredXML2IntegrationTest().getClass().getResourceAsStream("/teachers.xml"));
} }
@After
public void after() throws Exception { @AfterClass
public static void after() throws Exception {
System.out.println("Running: tearDown"); System.out.println("Running: tearDown");
wireMockServer.stop(); wireMockServer.stop();
} }
} }

View File

@ -1,38 +1,28 @@
package com.baeldung.restassured; package com.baeldung.restassured;
import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static io.restassured.RestAssured.post; import static io.restassured.RestAssured.post;
import static io.restassured.RestAssured.get;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import static io.restassured.module.jsv.JsonSchemaValidatorSettings.settings;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.xml.HasXPath.hasXPath; import static org.hamcrest.xml.HasXPath.hasXPath;
import java.io.FileNotFoundException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.github.fge.jsonschema.SchemaVersion;
import com.github.fge.jsonschema.cfg.ValidationConfiguration;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.tomakehurst.wiremock.WireMockServer;
public class RestAssuredXMLIntegrationTest { public class RestAssuredXMLIntegrationTest {
private WireMockServer wireMockServer = new WireMockServer(); private static WireMockServer wireMockServer = new WireMockServer();
private static final String EVENTS_PATH = "/employees"; private static final String EVENTS_PATH = "/employees";
private static final String APPLICATION_XML = "application/xml"; private static final String APPLICATION_XML = "application/xml";
private static final String EMPLOYEES = getXml(); private static final String EMPLOYEES = getXml();
@Before @BeforeClass
public void before() throws Exception { public static void before() throws Exception {
System.out.println("Setting up!"); System.out.println("Setting up!");
wireMockServer.start(); wireMockServer.start();
configureFor("localhost", 8080); configureFor("localhost", 8080);
@ -41,6 +31,7 @@ public class RestAssuredXMLIntegrationTest {
.withHeader("Content-Type", APPLICATION_XML) .withHeader("Content-Type", APPLICATION_XML)
.withBody(EMPLOYEES))); .withBody(EMPLOYEES)));
} }
@Test @Test
public void givenUrl_whenXmlResponseValueTestsEqual_thenCorrect() { public void givenUrl_whenXmlResponseValueTestsEqual_thenCorrect() {
post("/employees").then().assertThat() post("/employees").then().assertThat()
@ -72,7 +63,6 @@ public class RestAssuredXMLIntegrationTest {
.assertThat() .assertThat()
.body(hasXPath("/employees/employee/first-name", .body(hasXPath("/employees/employee/first-name",
containsString("Ja"))); containsString("Ja")));
} }
@Test @Test
@ -81,18 +71,15 @@ public class RestAssuredXMLIntegrationTest {
.then() .then()
.assertThat() .assertThat()
.body(hasXPath("/employees/employee/first-name[text()='Jane']")); .body(hasXPath("/employees/employee/first-name[text()='Jane']"));
} }
private static String getXml() { private static String getXml() {
return Util return Util
.inputStreamToString(new RestAssuredXMLIntegrationTest().getClass().getResourceAsStream("/employees.xml")); .inputStreamToString(RestAssuredXMLIntegrationTest.class.getResourceAsStream("/employees.xml"));
} }
@After
public void after() throws Exception { @AfterClass
public static void after() throws Exception {
System.out.println("Running: tearDown"); System.out.println("Running: tearDown");
wireMockServer.stop(); wireMockServer.stop();
} }

View File

@ -1,36 +1,15 @@
package com.baeldung.restassured; package com.baeldung.restassured;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.util.Scanner;
public class Util { final class Util {
public static String inputStreamToString(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line; private Util() {
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
} }
} catch (IOException e) { static String inputStreamToString(InputStream is) {
e.printStackTrace(); Scanner s = new Scanner(is).useDelimiter("\\A");
} finally { return s.hasNext() ? s.next() : "";
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
} }
} }