From e8bbe6eba606d8661ec691249f2eedd921b2c48e Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Thu, 26 May 2016 17:36:32 +0700 Subject: [PATCH 1/5] initial commit for wiremock --- rest-testing/pom.xml | 285 +++++++++--------- .../wiremock/introduction/JUnitManaged.java | 53 ++++ .../introduction/ProgrammaticallyManaged.java | 52 ++++ 3 files changed, 251 insertions(+), 139 deletions(-) create mode 100644 rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java create mode 100644 rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/ProgrammaticallyManaged.java diff --git a/rest-testing/pom.xml b/rest-testing/pom.xml index e159af0b77..3bd7efb4d5 100644 --- a/rest-testing/pom.xml +++ b/rest-testing/pom.xml @@ -1,173 +1,180 @@ - - 4.0.0 - com.baeldung - rest-testing - 0.1-SNAPSHOT + + 4.0.0 + com.baeldung + rest-testing + 0.1-SNAPSHOT - rest-testing + rest-testing - + - + - - com.google.guava - guava - ${guava.version} - + + com.google.guava + guava + ${guava.version} + - - commons-io - commons-io - 2.4 - + + commons-io + commons-io + 2.4 + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - + - - org.apache.httpcomponents - httpclient - ${httpclient.version} - - - org.apache.httpcomponents - httpcore - ${httpcore.version} - + + org.apache.httpcomponents + httpclient + ${httpclient.version} + + + org.apache.httpcomponents + httpcore + ${httpcore.version} + - + - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - + - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - org.slf4j - jcl-over-slf4j - ${org.slf4j.version} - runtime - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + runtime + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + - + - - junit - junit - ${junit.version} - test - + + junit + junit + ${junit.version} + test + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - - org.mockito - mockito-core - ${mockito.version} - test - + + org.mockito + mockito-core + ${mockito.version} + test + + + com.github.tomakehurst + wiremock + 1.58 + test + - + - - rest-testing - - - src/main/resources - true - - + + rest-testing + + + src/main/resources + true + + - + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + - + - + - - - 2.7.2 + + + 2.7.2 - - 1.7.13 - 1.1.3 + + 1.7.13 + 1.1.3 - - 5.1.3.Final + + 5.1.3.Final - - 19.0 - 3.4 + + 19.0 + 3.4 - - 1.3 - 4.12 - 1.10.19 + + 1.3 + 4.12 + 1.10.19 - 4.4.1 - 4.5 + 4.4.1 + 4.5 - 2.9.0 + 2.9.0 - - 3.5.1 - 2.6 - 2.19.1 - 2.7 - 1.4.18 + + 3.5.1 + 2.6 + 2.19.1 + 2.7 + 1.4.18 - + \ No newline at end of file diff --git a/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java new file mode 100644 index 0000000000..9fef6017c1 --- /dev/null +++ b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java @@ -0,0 +1,53 @@ +package com.baeldung.rest.wiremock.introduction; + +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.getRequestedFor; +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.urlPathMatching; +import static com.github.tomakehurst.wiremock.client.WireMock.verify; +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Scanner; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.Rule; +import org.junit.Test; + +import com.github.tomakehurst.wiremock.junit.WireMockRule; + +public class JUnitManaged { + @Rule + public WireMockRule wireMockRule = new WireMockRule(); + + @Test + public void givenJUnitManagedServer_whenStubbingURL_thenCorrect() throws IOException { + configureFor("localhost", 8080); + stubFor(get(urlPathMatching("/baeldung/.*")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("\"testing-library\": \"WireMock\""))); + + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock"); + HttpResponse httpResponse = httpClient.execute(request); + String stringResponse = convertResponseToString(httpResponse); + + verify(getRequestedFor(urlEqualTo("/baeldung/wiremock"))); + assertEquals(200, httpResponse.getStatusLine().getStatusCode()); + assertEquals("application/json", httpResponse.getFirstHeader("Content-Type").getValue()); + assertEquals("\"testing-library\": \"WireMock\"", stringResponse); + } + + private String convertResponseToString(HttpResponse response) throws IOException { + InputStream responseStream = response.getEntity().getContent(); + Scanner scanner = new Scanner(responseStream, "UTF-8"); + String stringResponse = scanner.useDelimiter("\\Z").next(); + scanner.close(); + return stringResponse; + } +} diff --git a/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/ProgrammaticallyManaged.java b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/ProgrammaticallyManaged.java new file mode 100644 index 0000000000..34b445ac0f --- /dev/null +++ b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/ProgrammaticallyManaged.java @@ -0,0 +1,52 @@ +package com.baeldung.rest.wiremock.introduction; + +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.getRequestedFor; +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.verify; +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Scanner; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.Test; + +import com.github.tomakehurst.wiremock.WireMockServer; + +public class ProgrammaticallyManaged { + WireMockServer wireMockServer = new WireMockServer(); + CloseableHttpClient httpClient = HttpClients.createDefault(); + + @Test + public void givenProgrammaticallyManagedServer_whenUsingSimpleStubbing_thenCorrect() throws IOException { + wireMockServer.start(); + + configureFor("localhost", 8080); + stubFor(get(urlEqualTo("/baeldung")).willReturn(aResponse().withBody("Welcome to Baeldung!"))); + + HttpGet request = new HttpGet("http://localhost:8080/baeldung"); + HttpResponse httpResponse = httpClient.execute(request); + String stringResponse = convertResponseToString(httpResponse); + + verify(getRequestedFor(urlEqualTo("/baeldung"))); + assertEquals("Welcome to Baeldung!", stringResponse); + + wireMockServer.stop(); + } + + private String convertResponseToString(HttpResponse response) throws IOException { + InputStream responseStream = response.getEntity().getContent(); + Scanner scanner = new Scanner(responseStream, "UTF-8"); + String stringResponse = scanner.useDelimiter("\\Z").next(); + scanner.close(); + return stringResponse; + } +} \ No newline at end of file From db9ecdb2392f5a7f4bc300be063a5c970644b1a5 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Sun, 29 May 2016 14:02:06 +0700 Subject: [PATCH 2/5] introduction to wiremock --- .../wiremock/introduction/JUnitManaged.java | 96 ++++++++++++++++--- 1 file changed, 85 insertions(+), 11 deletions(-) diff --git a/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java index 9fef6017c1..6f1ad640cb 100644 --- a/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java +++ b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java @@ -1,12 +1,17 @@ package com.baeldung.rest.wiremock.introduction; 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.equalTo; import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; 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.urlPathEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; +import static com.github.tomakehurst.wiremock.client.WireMock.matching; +import static com.github.tomakehurst.wiremock.client.WireMock.containing; import static com.github.tomakehurst.wiremock.client.WireMock.verify; import static org.junit.Assert.assertEquals; @@ -16,6 +21,8 @@ import java.util.Scanner; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.junit.Rule; @@ -28,14 +35,13 @@ public class JUnitManaged { public WireMockRule wireMockRule = new WireMockRule(); @Test - public void givenJUnitManagedServer_whenStubbingURL_thenCorrect() throws IOException { - configureFor("localhost", 8080); + public void givenJUnitManagedServer_whenMatchingURL_thenCorrect() throws IOException { stubFor(get(urlPathMatching("/baeldung/.*")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("\"testing-library\": \"WireMock\""))); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock"); HttpResponse httpResponse = httpClient.execute(request); - String stringResponse = convertResponseToString(httpResponse); + String stringResponse = convertHttpResponseToString(httpResponse); verify(getRequestedFor(urlEqualTo("/baeldung/wiremock"))); assertEquals(200, httpResponse.getStatusLine().getStatusCode()); @@ -43,11 +49,79 @@ public class JUnitManaged { assertEquals("\"testing-library\": \"WireMock\"", stringResponse); } - private String convertResponseToString(HttpResponse response) throws IOException { - InputStream responseStream = response.getEntity().getContent(); - Scanner scanner = new Scanner(responseStream, "UTF-8"); - String stringResponse = scanner.useDelimiter("\\Z").next(); - scanner.close(); - return stringResponse; + @Test + public void givenJUnitManagedServer_whenMatchingHeaders_thenCorrect() throws IOException { + stubFor(get(urlPathEqualTo("/baeldung/wiremock")).withHeader("Accept", matching("text/.*")).willReturn(aResponse().withStatus(503).withBody("!!! Service Unavailable !!!"))); + + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock"); + request.addHeader("Accept", "text/xml"); + HttpResponse httpResponse = httpClient.execute(request); + String stringResponse = convertHttpResponseToString(httpResponse); + + verify(getRequestedFor(urlEqualTo("/baeldung/wiremock"))); + assertEquals(503, httpResponse.getStatusLine().getStatusCode()); + assertEquals("!!! Service Unavailable !!!", stringResponse); } -} + + // @Test + public void givenJUnitManagedServer_whenMatchingBody_thenCorrect() throws IOException { + stubFor(post(urlEqualTo("/baeldung/wiremock")).withHeader("Content-Type", equalTo("application/json")).withRequestBody(containing("\"testing-library\": \"WireMock\"")).withRequestBody(containing("\"creator\": \"Tom Akehurst\"")) + .withRequestBody(containing("\"website\": \"wiremock.org\"")).willReturn(aResponse().withStatus(200))); + + InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("wiremock_intro.json"); + String jsonString = convertInputStreamToString(jsonInputStream); + StringEntity entity = new StringEntity(jsonString); + + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost request = new HttpPost("http://localhost:8080/baeldung/wiremock"); + + request.addHeader("Content-Type", "application/json"); + request.setEntity(entity); + HttpResponse response = httpClient.execute(request); + + assertEquals(200, response.getStatusLine().getStatusCode()); + verify(postRequestedFor(urlEqualTo("/baeldung/wiremock")).withHeader("Content-Type", equalTo("application/json"))); + } + + @Test + public void givenJUnitManagedServer_whenNotUsingPriority_thenCorrect() throws IOException { + stubFor(get(urlPathMatching("/baeldung/.*")).willReturn(aResponse().withStatus(200))); + stubFor(get(urlPathEqualTo("/baeldung/wiremock")).withHeader("Accept", matching("text/.*")).willReturn(aResponse().withStatus(503))); + + HttpResponse httpResponse = generateClientAndReceiveResponseForPriorityTests(); + + verify(getRequestedFor(urlEqualTo("/baeldung/wiremock"))); + assertEquals(503, httpResponse.getStatusLine().getStatusCode()); + } + + @Test + public void givenJUnitManagedServer_whenUsingPriority_thenCorrect() throws IOException { + stubFor(get(urlPathMatching("/baeldung/.*")).atPriority(1).willReturn(aResponse().withStatus(200))); + stubFor(get(urlPathEqualTo("/baeldung/wiremock")).atPriority(2).withHeader("Accept", matching("text/.*")).willReturn(aResponse().withStatus(503))); + + HttpResponse httpResponse = generateClientAndReceiveResponseForPriorityTests(); + + verify(getRequestedFor(urlEqualTo("/baeldung/wiremock"))); + assertEquals(200, httpResponse.getStatusLine().getStatusCode()); + } + + private String convertHttpResponseToString(HttpResponse httpResponse) throws IOException { + InputStream inputStream = httpResponse.getEntity().getContent(); + return convertInputStreamToString(inputStream); + } + + private String convertInputStreamToString(InputStream inputStream) { + Scanner scanner = new Scanner(inputStream, "UTF-8"); + String string = scanner.useDelimiter("\\Z").next(); + scanner.close(); + return string; + } + + private HttpResponse generateClientAndReceiveResponseForPriorityTests() throws IOException { + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock"); + request.addHeader("Accept", "text/xml"); + return httpClient.execute(request); + } +} \ No newline at end of file From 1cf09ba2a8b8f0d14b3c218b66af54f43dd5cccd Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Sun, 29 May 2016 23:48:59 +0700 Subject: [PATCH 3/5] minor fixes --- .../rest/wiremock/introduction/JUnitManaged.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java index 6f1ad640cb..f6a46256ac 100644 --- a/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java +++ b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java @@ -51,20 +51,21 @@ public class JUnitManaged { @Test public void givenJUnitManagedServer_whenMatchingHeaders_thenCorrect() throws IOException { - stubFor(get(urlPathEqualTo("/baeldung/wiremock")).withHeader("Accept", matching("text/.*")).willReturn(aResponse().withStatus(503).withBody("!!! Service Unavailable !!!"))); + stubFor(get(urlPathEqualTo("/baeldung/wiremock")).withHeader("Accept", matching("text/.*")).willReturn(aResponse().withStatus(503).withHeader("Content-Type", "text/html").withBody("!!! Service Unavailable !!!"))); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock"); - request.addHeader("Accept", "text/xml"); + request.addHeader("Accept", "text/html"); HttpResponse httpResponse = httpClient.execute(request); String stringResponse = convertHttpResponseToString(httpResponse); verify(getRequestedFor(urlEqualTo("/baeldung/wiremock"))); assertEquals(503, httpResponse.getStatusLine().getStatusCode()); + assertEquals("text/html", httpResponse.getFirstHeader("Content-Type").getValue()); assertEquals("!!! Service Unavailable !!!", stringResponse); } - // @Test + @Test public void givenJUnitManagedServer_whenMatchingBody_thenCorrect() throws IOException { stubFor(post(urlEqualTo("/baeldung/wiremock")).withHeader("Content-Type", equalTo("application/json")).withRequestBody(containing("\"testing-library\": \"WireMock\"")).withRequestBody(containing("\"creator\": \"Tom Akehurst\"")) .withRequestBody(containing("\"website\": \"wiremock.org\"")).willReturn(aResponse().withStatus(200))); @@ -75,13 +76,12 @@ public class JUnitManaged { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost request = new HttpPost("http://localhost:8080/baeldung/wiremock"); - request.addHeader("Content-Type", "application/json"); request.setEntity(entity); HttpResponse response = httpClient.execute(request); - assertEquals(200, response.getStatusLine().getStatusCode()); verify(postRequestedFor(urlEqualTo("/baeldung/wiremock")).withHeader("Content-Type", equalTo("application/json"))); + assertEquals(200, response.getStatusLine().getStatusCode()); } @Test From f53ac9d4a17365f652202064704b8b278c55b464 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Mon, 6 Jun 2016 23:19:05 +0700 Subject: [PATCH 4/5] adds the wire_mock.json file --- rest-testing/src/main/resources/wiremock_intro.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 rest-testing/src/main/resources/wiremock_intro.json diff --git a/rest-testing/src/main/resources/wiremock_intro.json b/rest-testing/src/main/resources/wiremock_intro.json new file mode 100644 index 0000000000..ece2d35907 --- /dev/null +++ b/rest-testing/src/main/resources/wiremock_intro.json @@ -0,0 +1,5 @@ +{ + "testing-library": "WireMock", + "creator": "Tom Akehurst", + "website": "wiremock.org" +} \ No newline at end of file From c38e6c187681113ad5403d3dcc501756538077c2 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Tue, 7 Jun 2016 00:22:22 +0700 Subject: [PATCH 5/5] initial commit for rest testing with cucumber --- rest-testing/pom.xml | 11 +++ .../main/resources/Feature/cucumber.feature | 10 +++ rest-testing/src/main/resources/cucumber.json | 14 ++++ .../baeldung/rest/cucumber/CucumberTest.java | 10 +++ .../rest/cucumber/StepDefinition.java | 83 +++++++++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 rest-testing/src/main/resources/Feature/cucumber.feature create mode 100644 rest-testing/src/main/resources/cucumber.json create mode 100644 rest-testing/src/test/java/com/baeldung/rest/cucumber/CucumberTest.java create mode 100644 rest-testing/src/test/java/com/baeldung/rest/cucumber/StepDefinition.java diff --git a/rest-testing/pom.xml b/rest-testing/pom.xml index 3bd7efb4d5..652f2ab601 100644 --- a/rest-testing/pom.xml +++ b/rest-testing/pom.xml @@ -110,6 +110,17 @@ test + + info.cukes + cucumber-java + 1.2.4 + test + + + info.cukes + cucumber-junit + 1.2.4 + diff --git a/rest-testing/src/main/resources/Feature/cucumber.feature b/rest-testing/src/main/resources/Feature/cucumber.feature new file mode 100644 index 0000000000..99dd8249fe --- /dev/null +++ b/rest-testing/src/main/resources/Feature/cucumber.feature @@ -0,0 +1,10 @@ +Feature: Testing a REST API + Users should be able to submit GET and POST requests to a web service, represented by WireMock + + Scenario: Data Upload to a web service + When users upload data on a project + Then the server should handle it and return a success status + + Scenario: Data retrieval from a web service + When users want to get information on the Cucumber project + Then the requested data is returned \ No newline at end of file diff --git a/rest-testing/src/main/resources/cucumber.json b/rest-testing/src/main/resources/cucumber.json new file mode 100644 index 0000000000..38ebe066ac --- /dev/null +++ b/rest-testing/src/main/resources/cucumber.json @@ -0,0 +1,14 @@ +{ + "testing-framework": "cucumber", + "supported-language": + [ + "Ruby", + "Java", + "Javascript", + "PHP", + "Python", + "C++" + ], + + "website": "cucumber.io" +} \ No newline at end of file diff --git a/rest-testing/src/test/java/com/baeldung/rest/cucumber/CucumberTest.java b/rest-testing/src/test/java/com/baeldung/rest/cucumber/CucumberTest.java new file mode 100644 index 0000000000..041de592e9 --- /dev/null +++ b/rest-testing/src/test/java/com/baeldung/rest/cucumber/CucumberTest.java @@ -0,0 +1,10 @@ +package com.baeldung.rest.cucumber; + +import org.junit.runner.RunWith; +import cucumber.api.CucumberOptions; +import cucumber.api.junit.Cucumber; + +@RunWith(Cucumber.class) +@CucumberOptions(features = "classpath:Feature") +public class CucumberTest { +} \ No newline at end of file diff --git a/rest-testing/src/test/java/com/baeldung/rest/cucumber/StepDefinition.java b/rest-testing/src/test/java/com/baeldung/rest/cucumber/StepDefinition.java new file mode 100644 index 0000000000..0d72c60950 --- /dev/null +++ b/rest-testing/src/test/java/com/baeldung/rest/cucumber/StepDefinition.java @@ -0,0 +1,83 @@ +package com.baeldung.rest.cucumber; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.containsString; +import static com.github.tomakehurst.wiremock.client.WireMock.*; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Scanner; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; + +import com.github.tomakehurst.wiremock.WireMockServer; + +import cucumber.api.java.en.Then; +import cucumber.api.java.en.When; + +public class StepDefinition { + InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("cucumber.json"); + String jsonString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next(); + + WireMockServer wireMockServer = new WireMockServer(); + CloseableHttpClient httpClient = HttpClients.createDefault(); + + @When("^users upload data on a project$") + public void usersUploadDataOnAProject() throws IOException { + wireMockServer.start(); + + configureFor("localhost", 8080); + stubFor(post(urlEqualTo("/create")).withHeader("content-type", equalTo("application/json")).withRequestBody(containing("testing-framework")).willReturn(aResponse().withStatus(200))); + + HttpPost request = new HttpPost("http://localhost:8080/create"); + StringEntity entity = new StringEntity(jsonString); + request.addHeader("content-type", "application/json"); + request.setEntity(entity); + HttpResponse response = httpClient.execute(request); + + assertEquals(200, response.getStatusLine().getStatusCode()); + verify(postRequestedFor(urlEqualTo("/create")).withHeader("content-type", equalTo("application/json"))); + + wireMockServer.stop(); + } + + @When("^users want to get information on the (.+) project$") + public void usersGetInformationOnAProject(String projectName) throws IOException { + wireMockServer.start(); + + configureFor("localhost", 8080); + stubFor(get(urlEqualTo("/projects/cucumber")).withHeader("accept", equalTo("application/json")).willReturn(aResponse().withBody(jsonString))); + + HttpGet request = new HttpGet("http://localhost:8080/projects/" + projectName.toLowerCase()); + request.addHeader("accept", "application/json"); + HttpResponse httpResponse = httpClient.execute(request); + String responseString = convertResponseToString(httpResponse); + + assertThat(responseString, containsString("\"testing-framework\": \"cucumber\"")); + assertThat(responseString, containsString("\"website\": \"cucumber.io\"")); + verify(getRequestedFor(urlEqualTo("/projects/cucumber")).withHeader("accept", equalTo("application/json"))); + + wireMockServer.stop(); + } + + @Then("^the server should handle it and return a success status$") + public void theServerShouldReturnASuccessStatus() { + } + + @Then("^the requested data is returned$") + public void theRequestedDataIsReturned() { + } + + private String convertResponseToString(HttpResponse response) throws IOException { + InputStream responseStream = response.getEntity().getContent(); + Scanner scanner = new Scanner(responseStream, "UTF-8"); + String responseString = scanner.useDelimiter("\\Z").next(); + scanner.close(); + return responseString; + } +} \ No newline at end of file