Group testing modules (#3014)

* move security content from spring-security-rest-full

* swagger update

* move query language to new module

* rename spring-security-rest-full to spring-rest-full

* group persistence modules

* group testing modules

* try fix conflict
This commit is contained in:
Grzegorz Piwowarek
2017-11-12 11:16:46 +01:00
committed by GitHub
parent b383d83bf4
commit 776a01429e
236 changed files with 37 additions and 18 deletions
@@ -0,0 +1,53 @@
package com.baeldung.restassured;
import com.github.tomakehurst.wiremock.WireMockServer;
import io.restassured.RestAssured;
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.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static io.restassured.RestAssured.get;
import static org.hamcrest.Matchers.hasItems;
public class RestAssured2IntegrationTest {
private static final int PORT = 8084;
private static WireMockServer wireMockServer = new WireMockServer(PORT);
private static final String EVENTS_PATH = "/odds";
private static final String APPLICATION_JSON = "application/json";
private static final String ODDS = getJson();
@BeforeClass
public static void before() throws Exception {
System.out.println("Setting up!");
wireMockServer.start();
configureFor("localhost", PORT);
RestAssured.port = PORT;
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
aResponse().withStatus(200)
.withHeader("Content-Type", APPLICATION_JSON)
.withBody(ODDS)));
}
@Test
public void givenUrl_whenVerifiesOddPricesAccuratelyByStatus_thenCorrect() {
get("/odds").then().body("odds.findAll { it.status > 0 }.price",
hasItems(5.25f, 1.2f));
}
private static String getJson() {
return Util.inputStreamToString(RestAssured2IntegrationTest.class
.getResourceAsStream("/odds.json"));
}
@AfterClass
public static void after() throws Exception {
System.out.println("Running: tearDown");
wireMockServer.stop();
}
}
@@ -0,0 +1,105 @@
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 io.restassured.RestAssured;
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.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
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;
public class RestAssuredIntegrationTest {
private static final int PORT = 8083;
private static WireMockServer wireMockServer = new WireMockServer(PORT);
private static final String EVENTS_PATH = "/events?id=390";
private static final String APPLICATION_JSON = "application/json";
private static final String GAME_ODDS = getEventJson();
@BeforeClass
public static void before() throws Exception {
System.out.println("Setting up!");
wireMockServer.start();
RestAssured.port = PORT;
configureFor("localhost", PORT);
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
aResponse().withStatus(200)
.withHeader("Content-Type", APPLICATION_JSON)
.withBody(GAME_ODDS)));
}
@Test
public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() {
get("/events?id=390").then().assertThat()
.body("odd.ck", equalTo(12.2f));
}
@Test
public void givenUrl_whenSuccessOnGetsResponseAndJsonHasRequiredKV_thenCorrect() {
get("/events?id=390").then().statusCode(200).assertThat()
.body("id", equalTo("390"));
}
@Test
public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect() {
get("/events?id=390").then().assertThat()
.body("odds.price", hasItems("1.30", "5.25", "2.70", "1.20"));
}
@Test
public void givenUrl_whenJsonResponseConformsToSchema_thenCorrect() {
get("/events?id=390").then().assertThat()
.body(matchesJsonSchemaInClasspath("event_0.json"));
}
@Test
public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() {
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory
.newBuilder()
.setValidationConfiguration(
ValidationConfiguration.newBuilder()
.setDefaultVersion(SchemaVersion.DRAFTV4)
.freeze()).freeze();
get("/events?id=390")
.then()
.assertThat()
.body(matchesJsonSchemaInClasspath("event_0.json").using(
jsonSchemaFactory));
}
@Test
public void givenUrl_whenValidatesResponseWithStaticSettings_thenCorrect() {
get("/events?id=390")
.then()
.assertThat()
.body(matchesJsonSchemaInClasspath("event_0.json").using(
settings().with().checkedValidation(false)));
}
@AfterClass
public static void after() throws Exception {
System.out.println("Running: tearDown");
wireMockServer.stop();
}
private static String getEventJson() {
return Util.inputStreamToString(RestAssuredIntegrationTest.class
.getResourceAsStream("/event_0.json"));
}
}
@@ -0,0 +1,55 @@
package com.baeldung.restassured;
import com.github.tomakehurst.wiremock.WireMockServer;
import io.restassured.RestAssured;
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.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static io.restassured.RestAssured.get;
import static org.hamcrest.Matchers.hasItems;
public class RestAssuredXML2IntegrationTest {
private static final int PORT = 8082;
private static WireMockServer wireMockServer = new WireMockServer(PORT);
private static final String EVENTS_PATH = "/teachers";
private static final String APPLICATION_XML = "application/xml";
private static final String TEACHERS = getXml();
@BeforeClass
public static void before() throws Exception {
System.out.println("Setting up!");
wireMockServer.start();
RestAssured.port = PORT;
configureFor("localhost", PORT);
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
aResponse().withStatus(200)
.withHeader("Content-Type", APPLICATION_XML)
.withBody(TEACHERS)));
}
@Test
public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() {
get("/teachers")
.then()
.body("teachers.teacher.find { it.@department == 'science' }.subject",
hasItems("math", "physics"));
}
private static String getXml() {
return Util.inputStreamToString(RestAssuredXML2IntegrationTest.class
.getResourceAsStream("/teachers.xml"));
}
@AfterClass
public static void after() throws Exception {
System.out.println("Running: tearDown");
wireMockServer.stop();
}
}
@@ -0,0 +1,90 @@
package com.baeldung.restassured;
import com.github.tomakehurst.wiremock.WireMockServer;
import io.restassured.RestAssured;
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.configureFor;
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.urlEqualTo;
import static io.restassured.RestAssured.post;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.xml.HasXPath.hasXPath;
public class RestAssuredXMLIntegrationTest {
private static final int PORT = 8081;
private static WireMockServer wireMockServer = new WireMockServer(PORT);
private static final String EVENTS_PATH = "/employees";
private static final String APPLICATION_XML = "application/xml";
private static final String EMPLOYEES = getXml();
@BeforeClass
public static void before() throws Exception {
System.out.println("Setting up!");
wireMockServer.start();
configureFor("localhost", PORT);
RestAssured.port = PORT;
stubFor(post(urlEqualTo(EVENTS_PATH)).willReturn(
aResponse().withStatus(200)
.withHeader("Content-Type", APPLICATION_XML)
.withBody(EMPLOYEES)));
}
@Test
public void givenUrl_whenXmlResponseValueTestsEqual_thenCorrect() {
post("/employees").then().assertThat()
.body("employees.employee.first-name", equalTo("Jane"));
}
@Test
public void givenUrl_whenMultipleXmlValuesTestEqual_thenCorrect() {
post("/employees").then().assertThat()
.body("employees.employee.first-name", equalTo("Jane"))
.body("employees.employee.last-name", equalTo("Daisy"))
.body("employees.employee.sex", equalTo("f"));
}
@Test
public void givenUrl_whenMultipleXmlValuesTestEqualInShortHand_thenCorrect() {
post("/employees")
.then()
.assertThat()
.body("employees.employee.first-name", equalTo("Jane"),
"employees.employee.last-name", equalTo("Daisy"),
"employees.employee.sex", equalTo("f"));
}
@Test
public void givenUrl_whenValidatesXmlUsingXpath_thenCorrect() {
post("/employees")
.then()
.assertThat()
.body(hasXPath("/employees/employee/first-name",
containsString("Ja")));
}
@Test
public void givenUrl_whenValidatesXmlUsingXpath2_thenCorrect() {
post("/employees")
.then()
.assertThat()
.body(hasXPath("/employees/employee/first-name[text()='Jane']"));
}
private static String getXml() {
return Util
.inputStreamToString(RestAssuredXMLIntegrationTest.class.getResourceAsStream("/employees.xml"));
}
@AfterClass
public static void after() throws Exception {
System.out.println("Running: tearDown");
wireMockServer.stop();
}
}
@@ -0,0 +1,15 @@
package com.baeldung.restassured;
import java.io.InputStream;
import java.util.Scanner;
final class Util {
private Util() {
}
static String inputStreamToString(InputStream is) {
Scanner s = new Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
}
@@ -0,0 +1,7 @@
<employees>
<employee category="skilled">
<first-name>Jane</first-name>
<last-name>Daisy</last-name>
<sex>f</sex>
</employee>
</employees>
@@ -0,0 +1,43 @@
{
"id": "390",
"odd": {
"price": "1.20",
"status": 2,
"ck": 12.2,
"name": "2"
},
"data": {
"countryId": 35,
"countryName": "Norway",
"leagueName": "Norway 3",
"status": 0,
"sportName": "Soccer",
"time": "2016-06-12T12:00:00Z"
},
"odds": [{
"price": "1.30",
"status": 0,
"ck": 12.2,
"name": "1"
},
{
"price":"5.25",
"status": 1,
"ck": 13.1,
"name": "X"
},
{
"price": "2.70",
"status": 0,
"ck": 12.2,
"name": "0"
},
{
"price": "1.20",
"status": 2,
"ck": 13.1,
"name": "2"
}
]
}
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -0,0 +1,28 @@
{
"odds": [{
"price": 1.30,
"status": 0,
"ck": 12.2,
"name": "1"
},
{
"price": 5.25,
"status": 1,
"ck": 13.1,
"name": "X"
},
{
"price": 2.70,
"status": 0,
"ck": 12.2,
"name": "0"
},
{
"price": 1.20,
"status": 2,
"ck": 13.1,
"name": "2"
}
]
}
@@ -0,0 +1,10 @@
<teachers>
<teacher department="science" id='309'>
<subject>math</subject>
<subject>physics</subject>
</teacher>
<teacher department="arts" id='310'>
<subject>political education</subject>
<subject>english</subject>
</teacher>
</teachers>