diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index e49433230e09..3dbb73973200 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -49,6 +49,7 @@ parallel-tests-junit powermock rest-assured + rest-assured-2 rest-testing selenide selenium diff --git a/testing-modules/rest-assured-2/.gitignore b/testing-modules/rest-assured-2/.gitignore new file mode 100644 index 000000000000..862f46031e08 --- /dev/null +++ b/testing-modules/rest-assured-2/.gitignore @@ -0,0 +1,16 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear + +.externalToolBuilders +.settings \ No newline at end of file diff --git a/testing-modules/rest-assured-2/pom.xml b/testing-modules/rest-assured-2/pom.xml new file mode 100644 index 000000000000..0edf87dc8d45 --- /dev/null +++ b/testing-modules/rest-assured-2/pom.xml @@ -0,0 +1,199 @@ + + + 4.0.0 + rest-assured-2 + 1.0 + rest-assured-2 + + + com.baeldung + parent-boot-3 + 0.0.1-SNAPSHOT + ../../parent-boot-3 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-json + + + org.springframework.boot + spring-boot-starter-test + test + + + jakarta.servlet + jakarta.servlet-api + ${jakarta.servlet-api.version} + provided + + + org.eclipse.jetty + jetty-security + ${jetty.version} + + + org.eclipse.jetty + jetty-servlet + ${jetty.version} + + + org.eclipse.jetty + jetty-servlets + ${jetty.version} + + + org.eclipse.jetty + jetty-io + + + org.eclipse.jetty + jetty-http + ${jetty.version} + + + org.eclipse.jetty + jetty-server + ${jetty.version} + + + org.eclipse.jetty + jetty-util + ${jetty.version} + + + org.apache.httpcomponents.core5 + httpcore5 + ${httpcore5.version} + + + org.apache.commons + commons-lang3 + + + javax.mail + mail + ${javax.mail.version} + + + com.fasterxml.jackson.core + jackson-annotations + + + com.fasterxml.jackson.core + jackson-databind + + + org.apache.httpcomponents.client5 + httpclient5 + ${httpclient5.version} + + + org.wiremock + wiremock-standalone + ${wiremock.version} + + + commons-collections + commons-collections + ${commons-collections.version} + + + + io.rest-assured + rest-assured + ${rest-assured.version} + test + + + io.rest-assured + spring-mock-mvc + ${rest-assured.version} + test + + + io.rest-assured + rest-assured-all + ${rest-assured.version} + test + + + com.github.scribejava + scribejava-apis + ${scribejava.version} + test + + + org.skyscreamer + jsonassert + ${json.assert.version} + test + + + net.javacrumbs.json-unit + json-unit-assertj + ${json.unit.version} + test + + + net.javacrumbs.json-unit + json-unit + ${json.unit.version} + test + + + uk.org.webcompere + model-assert + ${model.assert.version} + + + uk.co.datumedge + hamcrest-json + ${hamcrest.json.version} + + + io.rest-assured + json-schema-validator + ${rest-assured.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + -parameters + + + + + + + + 6.1.0 + 1.4.7 + 9.4.0.v20161208 + 3.2.2 + 5.2.5 + 5.2.3 + 3.9.1 + 2.5.3 + 5.5.0 + 1.5.3 + 3.4.1 + 1.0.3 + 0.2 + + + \ No newline at end of file diff --git a/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/Application.java b/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/Application.java new file mode 100644 index 000000000000..8b53a9c63de3 --- /dev/null +++ b/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/Application.java @@ -0,0 +1,13 @@ +package com.baeldung.restassured; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/controller/AppController.java b/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/controller/AppController.java new file mode 100644 index 000000000000..96fe632b8c0a --- /dev/null +++ b/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/controller/AppController.java @@ -0,0 +1,100 @@ +package com.baeldung.restassured.controller; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.util.Set; +import java.util.UUID; + +import jakarta.servlet.http.Cookie; +import jakarta.servlet.http.HttpServletResponse; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.InputStreamResource; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.restassured.model.Movie; +import com.baeldung.restassured.service.AppService; + +@RestController +public class AppController { + + @Autowired + AppService appService; + + @GetMapping("/movies") + public ResponseEntity getMovies() { + + Set result = appService.getAll(); + + return ResponseEntity.ok() + .body(result); + } + + @PostMapping("/movie") + @ResponseStatus(HttpStatus.CREATED) + public Movie addMovie(@RequestBody Movie movie) { + + appService.add(movie); + return movie; + } + + @GetMapping("/movie/{id}") + public ResponseEntity getMovie(@PathVariable int id) { + + Movie movie = appService.findMovie(id); + if (movie == null) { + return ResponseEntity.badRequest() + .body("Invalid movie id"); + } + + return ResponseEntity.ok(movie); + } + + @GetMapping("/welcome") + public ResponseEntity welcome(HttpServletResponse response) { + + HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8"); + headers.add("sessionId", UUID.randomUUID() + .toString()); + + Cookie cookie = new Cookie("token", "some-token"); + cookie.setDomain("localhost"); + + response.addCookie(cookie); + + return ResponseEntity.noContent() + .headers(headers) + .build(); + } + + @GetMapping("/download/{id}") + public ResponseEntity getFile(@PathVariable int id) throws FileNotFoundException { + + File file = appService.getFile(id); + + if (file == null) { + return ResponseEntity.notFound() + .build(); + } + + InputStreamResource resource = new InputStreamResource(new FileInputStream(file)); + + return ResponseEntity.ok() + .contentLength(file.length()) + .contentType(MediaType.parseMediaType("application/octet-stream")) + .body(resource); + } + +} diff --git a/testing-modules/rest-assured/src/main/java/com/baeldung/restassured/learner/Course.java b/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/learner/Course.java similarity index 100% rename from testing-modules/rest-assured/src/main/java/com/baeldung/restassured/learner/Course.java rename to testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/learner/Course.java diff --git a/testing-modules/rest-assured/src/main/java/com/baeldung/restassured/learner/CourseController.java b/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/learner/CourseController.java similarity index 100% rename from testing-modules/rest-assured/src/main/java/com/baeldung/restassured/learner/CourseController.java rename to testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/learner/CourseController.java diff --git a/testing-modules/rest-assured/src/main/java/com/baeldung/restassured/learner/CourseControllerExceptionHandler.java b/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/learner/CourseControllerExceptionHandler.java similarity index 100% rename from testing-modules/rest-assured/src/main/java/com/baeldung/restassured/learner/CourseControllerExceptionHandler.java rename to testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/learner/CourseControllerExceptionHandler.java diff --git a/testing-modules/rest-assured/src/main/java/com/baeldung/restassured/learner/CourseNotFoundException.java b/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/learner/CourseNotFoundException.java similarity index 100% rename from testing-modules/rest-assured/src/main/java/com/baeldung/restassured/learner/CourseNotFoundException.java rename to testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/learner/CourseNotFoundException.java diff --git a/testing-modules/rest-assured/src/main/java/com/baeldung/restassured/learner/CourseService.java b/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/learner/CourseService.java similarity index 100% rename from testing-modules/rest-assured/src/main/java/com/baeldung/restassured/learner/CourseService.java rename to testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/learner/CourseService.java diff --git a/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/model/Movie.java b/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/model/Movie.java new file mode 100644 index 000000000000..00a446fc6539 --- /dev/null +++ b/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/model/Movie.java @@ -0,0 +1,58 @@ +package com.baeldung.restassured.model; + +public class Movie { + + private Integer id; + + private String name; + + private String synopsis; + + public Movie() { + } + + public Movie(Integer id, String name, String synopsis) { + super(); + this.id = id; + this.name = name; + this.synopsis = synopsis; + } + + public Integer getId() { + return id; + } + + public String getName() { + return name; + } + + public String getSynopsis() { + return synopsis; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Movie other = (Movie) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + +} diff --git a/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/service/AppService.java b/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/service/AppService.java new file mode 100644 index 000000000000..15685f29241f --- /dev/null +++ b/testing-modules/rest-assured-2/src/main/java/com/baeldung/restassured/service/AppService.java @@ -0,0 +1,45 @@ +package com.baeldung.restassured.service; + +import java.io.File; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.stereotype.Service; + +import com.baeldung.restassured.model.Movie; + +@Service +public class AppService { + + private Set movieSet = new HashSet<>(); + + public Set getAll() { + return movieSet; + } + + public void add(Movie movie) { + movieSet.add(movie); + } + + public Movie findMovie(int id) { + return movieSet.stream() + .filter(movie -> movie.getId() + .equals(id)) + .findFirst() + .orElse(null); + } + + public File getFile(int id) { + File file = null; + try { + file = new ClassPathResource(String.valueOf(id)).getFile(); + } catch (IOException e) { + e.printStackTrace(); + } + + return file; + } + +} diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/Odd.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/Odd.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/Odd.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/Odd.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssured2IntegrationTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/RestAssured2IntegrationTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssured2IntegrationTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/RestAssured2IntegrationTest.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredAdvancedLiveTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/RestAssuredAdvancedLiveTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredAdvancedLiveTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/RestAssuredAdvancedLiveTest.java diff --git a/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/RestAssuredIntegrationTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/RestAssuredIntegrationTest.java new file mode 100644 index 000000000000..99f7274aabba --- /dev/null +++ b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/RestAssuredIntegrationTest.java @@ -0,0 +1,79 @@ +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.*; +import static io.restassured.RestAssured.get; +import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath; +import static io.restassured.module.jsv.JsonSchemaValidatorSettings.settings; + + +public class RestAssuredIntegrationTest { + private static WireMockServer wireMockServer; + + 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() { + System.out.println("Setting up!"); + final int port = Util.getAvailablePort(); + wireMockServer = new WireMockServer(port); + wireMockServer.start(); + RestAssured.port = port; + configureFor("localhost", port); + stubFor(com.github.tomakehurst.wiremock.client.WireMock.get(urlEqualTo(EVENTS_PATH)) + .willReturn(aResponse().withStatus(200) + .withHeader("Content-Type", APPLICATION_JSON) + .withBody(GAME_ODDS))); + } + + + @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() { + System.out.println("Running: tearDown"); + wireMockServer.stop(); + } + + private static String getEventJson() { + return Util.inputStreamToString(RestAssuredIntegrationTest.class.getResourceAsStream("/event_0.json")); + } +} diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredMultipartIntegrationTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/RestAssuredMultipartIntegrationTest.java similarity index 97% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredMultipartIntegrationTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/RestAssuredMultipartIntegrationTest.java index 677a205986d2..5b79c2f59232 100644 --- a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredMultipartIntegrationTest.java +++ b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/RestAssuredMultipartIntegrationTest.java @@ -1,107 +1,107 @@ -package com.baeldung.restassured; - -import static com.github.tomakehurst.wiremock.client.WireMock.aMultipart; -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.containing; -import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; -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.given; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.core.io.ClassPathResource; - -import com.github.tomakehurst.wiremock.WireMockServer; -import com.github.tomakehurst.wiremock.matching.MultipartValuePatternBuilder; - -import io.restassured.RestAssured; -import io.restassured.builder.MultiPartSpecBuilder; -import io.restassured.specification.MultiPartSpecification; - -class RestAssuredMultipartIntegrationTest { - - private WireMockServer wireMockServer; - - @BeforeEach - void startServer() { - int port = Util.getAvailablePort(); - wireMockServer = new WireMockServer(port); - wireMockServer.start(); - configureFor("localhost", port); - RestAssured.port = port; - } - - @AfterEach - void stopServer() { - wireMockServer.stop(); - } - - @Test - void whenUploadOneFile_ThenSuccess() throws IOException { - stubFor(post(urlEqualTo("/upload")).withHeader("Content-Type", containing("multipart/form-data")) - .withRequestBody(containing("file")) - .withRequestBody(containing(getFileContent("baeldung.txt"))) - .willReturn(aResponse().withStatus(200))); - - given().multiPart("file", getFile("baeldung.txt")) - .when() - .post("/upload") - .then() - .statusCode(200); - } - - @Test - void whenUploadTwoFiles_ThenSuccess() throws IOException { - stubFor(post(urlEqualTo("/upload")).withHeader("Content-Type", containing("multipart/form-data")) - .withRequestBody(containing(getFileContent("baeldung.txt"))) - .withRequestBody(containing(getFileContent("helloworld.txt"))) - .willReturn(aResponse().withStatus(200))); - - given().multiPart("file", getFile("baeldung.txt")) - .multiPart("helloworld", getFile("helloworld.txt")) - .when() - .post("/upload") - .then() - .statusCode(200); - } - - @Test - void whenBuildingMultipartSpecification_ThenSuccess() { - MultipartValuePatternBuilder multipartValuePatternBuilder = aMultipart().withName("file") - .withHeader("Content-Disposition", containing("file.txt")) - .withBody(equalTo("File content")) - .withHeader("Content-Type", containing("text/plain")); - - stubFor(post(urlEqualTo("/upload")).withMultipartRequestBody(multipartValuePatternBuilder) - .willReturn(aResponse().withStatus(200))); - - MultiPartSpecification multiPartSpecification = new MultiPartSpecBuilder("File content".getBytes()).fileName("file.txt") - .controlName("file") - .mimeType("text/plain") - .build(); - - given().multiPart(multiPartSpecification) - .when() - .post("/upload") - .then() - .statusCode(200); - } - - private String getFileContent(String fileName) throws IOException { - return new String(Files.readAllBytes(Paths.get(getFile(fileName).getPath()))); - } - - private File getFile(String fileName) throws IOException { - return new ClassPathResource(fileName).getFile(); - } - -} +package com.baeldung.restassured; + +import static com.github.tomakehurst.wiremock.client.WireMock.aMultipart; +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.containing; +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; +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.given; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.core.io.ClassPathResource; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.matching.MultipartValuePatternBuilder; + +import io.restassured.RestAssured; +import io.restassured.builder.MultiPartSpecBuilder; +import io.restassured.specification.MultiPartSpecification; + +class RestAssuredMultipartIntegrationTest { + + private WireMockServer wireMockServer; + + @BeforeEach + void startServer() { + int port = Util.getAvailablePort(); + wireMockServer = new WireMockServer(port); + wireMockServer.start(); + configureFor("localhost", port); + RestAssured.port = port; + } + + @AfterEach + void stopServer() { + wireMockServer.stop(); + } + + @Test + void whenUploadOneFile_ThenSuccess() throws IOException { + stubFor(post(urlEqualTo("/upload")).withHeader("Content-Type", containing("multipart/form-data")) + .withRequestBody(containing("file")) + .withRequestBody(containing(getFileContent("baeldung.txt"))) + .willReturn(aResponse().withStatus(200))); + + given().multiPart("file", getFile("baeldung.txt")) + .when() + .post("/upload") + .then() + .statusCode(200); + } + + @Test + void whenUploadTwoFiles_ThenSuccess() throws IOException { + stubFor(post(urlEqualTo("/upload")).withHeader("Content-Type", containing("multipart/form-data")) + .withRequestBody(containing(getFileContent("baeldung.txt"))) + .withRequestBody(containing(getFileContent("helloworld.txt"))) + .willReturn(aResponse().withStatus(200))); + + given().multiPart("file", getFile("baeldung.txt")) + .multiPart("helloworld", getFile("helloworld.txt")) + .when() + .post("/upload") + .then() + .statusCode(200); + } + + @Test + void whenBuildingMultipartSpecification_ThenSuccess() { + MultipartValuePatternBuilder multipartValuePatternBuilder = aMultipart().withName("file") + .withHeader("Content-Disposition", containing("file.txt")) + .withBody(equalTo("File content")) + .withHeader("Content-Type", containing("text/plain")); + + stubFor(post(urlEqualTo("/upload")).withMultipartRequestBody(multipartValuePatternBuilder) + .willReturn(aResponse().withStatus(200))); + + MultiPartSpecification multiPartSpecification = new MultiPartSpecBuilder("File content".getBytes()).fileName("file.txt") + .controlName("file") + .mimeType("text/plain") + .build(); + + given().multiPart(multiPartSpecification) + .when() + .post("/upload") + .then() + .statusCode(200); + } + + private String getFileContent(String fileName) throws IOException { + return new String(Files.readAllBytes(Paths.get(getFile(fileName).getPath()))); + } + + private File getFile(String fileName) throws IOException { + return new ClassPathResource(fileName).getFile(); + } + +} diff --git a/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/Util.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/Util.java new file mode 100644 index 000000000000..70c595f56262 --- /dev/null +++ b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/Util.java @@ -0,0 +1,38 @@ +package com.baeldung.restassured; + +import java.io.IOException; +import java.io.InputStream; +import java.net.ServerSocket; +import java.util.Random; +import java.util.Scanner; + +final class Util { + + private static final int DEFAULT_PORT = 8069; + + private Util() { + } + + static String inputStreamToString(InputStream is) { + Scanner s = new Scanner(is).useDelimiter("\\A"); + return s.hasNext() ? s.next() : ""; + } + + static int getAvailablePort() { + return new Random() + .ints(6000, 9000) + .filter(Util::isFree) + .findFirst() + .orElse(DEFAULT_PORT); + } + + + private static boolean isFree(int port) { + try { + new ServerSocket(port).close(); + return true; + } catch (IOException e) { + return false; + } + } +} diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonUnitTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonUnitTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonUnitTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonUnitTest.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithHamcrestUnitTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithHamcrestUnitTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithHamcrestUnitTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithHamcrestUnitTest.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithJsonAssertUnitTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithJsonAssertUnitTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithJsonAssertUnitTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithJsonAssertUnitTest.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithJsonUnitUnitTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithJsonUnitUnitTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithJsonUnitUnitTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithJsonUnitUnitTest.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithModelAssertUnitTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithModelAssertUnitTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithModelAssertUnitTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/assertjson/RestAssuredAssertJsonWithModelAssertUnitTest.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/assertjson/WebsitePojo.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/assertjson/WebsitePojo.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/assertjson/WebsitePojo.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/assertjson/WebsitePojo.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/assertjson/WireMockTestBase.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/assertjson/WireMockTestBase.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/assertjson/WireMockTestBase.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/assertjson/WireMockTestBase.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/authentication/BasicAuthenticationLiveTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/authentication/BasicAuthenticationLiveTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/authentication/BasicAuthenticationLiveTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/authentication/BasicAuthenticationLiveTest.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/authentication/BasicPreemtiveAuthenticationLiveTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/authentication/BasicPreemtiveAuthenticationLiveTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/authentication/BasicPreemtiveAuthenticationLiveTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/authentication/BasicPreemtiveAuthenticationLiveTest.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/authentication/DigestAuthenticationLiveTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/authentication/DigestAuthenticationLiveTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/authentication/DigestAuthenticationLiveTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/authentication/DigestAuthenticationLiveTest.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/authentication/FormAuthenticationLiveTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/authentication/FormAuthenticationLiveTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/authentication/FormAuthenticationLiveTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/authentication/FormAuthenticationLiveTest.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/authentication/FormAutoconfAuthenticationLiveTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/authentication/FormAutoconfAuthenticationLiveTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/authentication/FormAutoconfAuthenticationLiveTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/authentication/FormAutoconfAuthenticationLiveTest.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/authentication/OAuth2AuthenticationLiveTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/authentication/OAuth2AuthenticationLiveTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/authentication/OAuth2AuthenticationLiveTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/authentication/OAuth2AuthenticationLiveTest.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/authentication/OAuthAuthenticationLiveTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/authentication/OAuthAuthenticationLiveTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/authentication/OAuthAuthenticationLiveTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/authentication/OAuthAuthenticationLiveTest.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/learner/CourseControllerIntegrationTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/learner/CourseControllerIntegrationTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/learner/CourseControllerIntegrationTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/learner/CourseControllerIntegrationTest.java diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/learner/CourseControllerUnitTest.java b/testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/learner/CourseControllerUnitTest.java similarity index 100% rename from testing-modules/rest-assured/src/test/java/com/baeldung/restassured/learner/CourseControllerUnitTest.java rename to testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/learner/CourseControllerUnitTest.java diff --git a/testing-modules/rest-assured/src/test/resources/baeldung.txt b/testing-modules/rest-assured-2/src/test/resources/baeldung.txt similarity index 100% rename from testing-modules/rest-assured/src/test/resources/baeldung.txt rename to testing-modules/rest-assured-2/src/test/resources/baeldung.txt diff --git a/testing-modules/rest-assured-2/src/test/resources/event_0.json b/testing-modules/rest-assured-2/src/test/resources/event_0.json new file mode 100644 index 000000000000..a6e45239ec40 --- /dev/null +++ b/testing-modules/rest-assured-2/src/test/resources/event_0.json @@ -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" + } + + ] +} \ No newline at end of file diff --git a/testing-modules/rest-assured/src/test/resources/expected-build.json b/testing-modules/rest-assured-2/src/test/resources/expected-build.json similarity index 100% rename from testing-modules/rest-assured/src/test/resources/expected-build.json rename to testing-modules/rest-assured-2/src/test/resources/expected-build.json diff --git a/testing-modules/rest-assured/src/test/resources/expected-website-different-field-order.json b/testing-modules/rest-assured-2/src/test/resources/expected-website-different-field-order.json similarity index 100% rename from testing-modules/rest-assured/src/test/resources/expected-website-different-field-order.json rename to testing-modules/rest-assured-2/src/test/resources/expected-website-different-field-order.json diff --git a/testing-modules/rest-assured/src/test/resources/expected-website.json b/testing-modules/rest-assured-2/src/test/resources/expected-website.json similarity index 100% rename from testing-modules/rest-assured/src/test/resources/expected-website.json rename to testing-modules/rest-assured-2/src/test/resources/expected-website.json diff --git a/testing-modules/rest-assured/src/test/resources/helloworld.txt b/testing-modules/rest-assured-2/src/test/resources/helloworld.txt similarity index 100% rename from testing-modules/rest-assured/src/test/resources/helloworld.txt rename to testing-modules/rest-assured-2/src/test/resources/helloworld.txt diff --git a/testing-modules/rest-assured-2/src/test/resources/logback.xml b/testing-modules/rest-assured-2/src/test/resources/logback.xml new file mode 100644 index 000000000000..ec0dc2469ae0 --- /dev/null +++ b/testing-modules/rest-assured-2/src/test/resources/logback.xml @@ -0,0 +1,19 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testing-modules/rest-assured/src/test/resources/odds.json b/testing-modules/rest-assured-2/src/test/resources/odds.json similarity index 100% rename from testing-modules/rest-assured/src/test/resources/odds.json rename to testing-modules/rest-assured-2/src/test/resources/odds.json diff --git a/testing-modules/rest-assured/pom.xml b/testing-modules/rest-assured/pom.xml index f258dc7d5159..6a83cd6c3095 100644 --- a/testing-modules/rest-assured/pom.xml +++ b/testing-modules/rest-assured/pom.xml @@ -137,64 +137,12 @@ ${rest-assured.version} test - - io.rest-assured - spring-mock-mvc - ${rest-assured.version} - test - - - io.rest-assured - json-schema-validator - ${rest-assured.version} - test - io.rest-assured xml-path ${rest-assured.version} test - - io.rest-assured - rest-assured-all - ${rest-assured.version} - test - - - com.github.scribejava - scribejava-apis - ${scribejava.version} - test - - - org.skyscreamer - jsonassert - ${json.assert.version} - test - - - net.javacrumbs.json-unit - json-unit-assertj - ${json.unit.version} - test - - - net.javacrumbs.json-unit - json-unit - ${json.unit.version} - test - - - uk.org.webcompere - model-assert - ${model.assert.version} - - - uk.co.datumedge - hamcrest-json - ${hamcrest.json.version} - @@ -224,12 +172,7 @@ 1.1 1.2 3.9.1 - 2.5.3 5.5.0 - 1.5.3 - 3.4.1 - 1.0.3 - 0.2 \ No newline at end of file diff --git a/testing-modules/rest-assured/src/main/resources/1 b/testing-modules/rest-assured/src/main/resources/1 deleted file mode 100644 index 49351eb5b7e3..000000000000 --- a/testing-modules/rest-assured/src/main/resources/1 +++ /dev/null @@ -1 +0,0 @@ -File 1 \ No newline at end of file diff --git a/testing-modules/rest-assured/src/main/resources/2 b/testing-modules/rest-assured/src/main/resources/2 deleted file mode 100644 index 9fbb45ed0828..000000000000 --- a/testing-modules/rest-assured/src/main/resources/2 +++ /dev/null @@ -1 +0,0 @@ -File 2 \ No newline at end of file diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredIntegrationTest.java b/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredIntegrationTest.java index 3c3e1cc39f7e..ae998583b981 100644 --- a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredIntegrationTest.java +++ b/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredIntegrationTest.java @@ -5,8 +5,7 @@ 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; @@ -14,9 +13,6 @@ import org.junit.BeforeClass; 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; import io.restassured.RestAssured; @@ -65,36 +61,6 @@ public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect .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() { System.out.println("Running: tearDown");