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:
committed by
GitHub
parent
b383d83bf4
commit
776a01429e
+10
@@ -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 CucumberIntegrationTest {
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package com.baeldung.rest.cucumber;
|
||||
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
import cucumber.api.java.en.Then;
|
||||
import cucumber.api.java.en.When;
|
||||
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 java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Scanner;
|
||||
|
||||
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.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.verify;
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class StepDefinition {
|
||||
|
||||
private static final String CREATE_PATH = "/create";
|
||||
private static final String APPLICATION_JSON = "application/json";
|
||||
|
||||
private final InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("cucumber.json");
|
||||
private final String jsonString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next();
|
||||
|
||||
private final WireMockServer wireMockServer = new WireMockServer();
|
||||
private final 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_PATH))
|
||||
.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_PATH))
|
||||
.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;
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.rest.jbehave;
|
||||
|
||||
import org.jbehave.core.configuration.Configuration;
|
||||
import org.jbehave.core.configuration.MostUsefulConfiguration;
|
||||
import org.jbehave.core.io.LoadFromClasspath;
|
||||
import org.jbehave.core.junit.JUnitStories;
|
||||
import org.jbehave.core.reporters.StoryReporterBuilder;
|
||||
import org.jbehave.core.steps.InjectableStepsFactory;
|
||||
import org.jbehave.core.steps.InstanceStepsFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jbehave.core.io.CodeLocations.codeLocationFromClass;
|
||||
import static org.jbehave.core.reporters.Format.CONSOLE;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public abstract class AbstractStory extends JUnitStories {
|
||||
|
||||
abstract String storyName();
|
||||
|
||||
@Override
|
||||
public Configuration configuration() {
|
||||
return new MostUsefulConfiguration()
|
||||
.useStoryLoader(new LoadFromClasspath(this.getClass()))
|
||||
.useStoryReporterBuilder(new StoryReporterBuilder()
|
||||
.withCodeLocation(codeLocationFromClass(this.getClass()))
|
||||
.withFormats(CONSOLE));
|
||||
}
|
||||
|
||||
abstract Object stepInstance();
|
||||
|
||||
@Override
|
||||
public InjectableStepsFactory stepsFactory() {
|
||||
return new InstanceStepsFactory(configuration(), stepInstance());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<String> storyPaths() {
|
||||
return Arrays.asList(storyName());
|
||||
}
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.rest.jbehave;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.jbehave.core.annotations.Given;
|
||||
import org.jbehave.core.annotations.Then;
|
||||
import org.jbehave.core.annotations.When;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.apache.http.HttpStatus.SC_NOT_FOUND;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class GithubUserNotFoundSteps {
|
||||
|
||||
private String api;
|
||||
private String nonExistentUser;
|
||||
private int githubResponseCode;
|
||||
|
||||
@Given("github user profile api")
|
||||
public void givenGithubUserProfileApi() {
|
||||
api = "https://api.github.com/users/%s";
|
||||
}
|
||||
|
||||
@Given("a random non-existent username")
|
||||
public void givenANonexistentUsername() {
|
||||
nonExistentUser = randomAlphabetic(8);
|
||||
}
|
||||
|
||||
@When("I look for the random user via the api")
|
||||
public void whenILookForTheUserViaTheApi() throws IOException {
|
||||
githubResponseCode = getGithubUserProfile(api, nonExistentUser)
|
||||
.getStatusLine()
|
||||
.getStatusCode();
|
||||
}
|
||||
|
||||
@When("I look for $user via the api")
|
||||
public void whenILookForSomeNonExistentUserViaTheApi(String user) throws IOException {
|
||||
githubResponseCode = getGithubUserProfile(api, user)
|
||||
.getStatusLine()
|
||||
.getStatusCode();
|
||||
}
|
||||
|
||||
static HttpResponse getGithubUserProfile(String api, String username) throws IOException {
|
||||
HttpUriRequest request = new HttpGet(String.format(api, username));
|
||||
return HttpClientBuilder
|
||||
.create()
|
||||
.build()
|
||||
.execute(request);
|
||||
}
|
||||
|
||||
@Then("github respond: 404 not found")
|
||||
public void thenGithubRespond404NotFound() {
|
||||
assertTrue(SC_NOT_FOUND == githubResponseCode);
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.rest.jbehave;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class GithubUserNotFoundStoryLiveTest extends AbstractStory {
|
||||
|
||||
@Override
|
||||
String storyName() {
|
||||
return "github_user_not_found.story";
|
||||
}
|
||||
|
||||
@Override
|
||||
Object stepInstance() {
|
||||
return new GithubUserNotFoundSteps();
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.rest.jbehave;
|
||||
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.jbehave.core.annotations.Given;
|
||||
import org.jbehave.core.annotations.Then;
|
||||
import org.jbehave.core.annotations.When;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static com.baeldung.rest.jbehave.GithubUserNotFoundSteps.getGithubUserProfile;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class GithubUserResponseMediaTypeSteps {
|
||||
|
||||
private String api;
|
||||
private String validUser;
|
||||
private String mediaType;
|
||||
|
||||
@Given("github user profile api")
|
||||
public void givenGithubUserProfileApi() {
|
||||
api = "https://api.github.com/users/%s";
|
||||
}
|
||||
|
||||
@Given("a valid username")
|
||||
public void givenAValidUsername() {
|
||||
validUser = "eugenp";
|
||||
}
|
||||
|
||||
@When("I look for the user via the api")
|
||||
public void whenILookForTheUserViaTheApi() throws IOException {
|
||||
mediaType = ContentType
|
||||
.getOrDefault(getGithubUserProfile(api, validUser).getEntity())
|
||||
.getMimeType();
|
||||
}
|
||||
|
||||
@Then("github respond data of type json")
|
||||
public void thenGithubRespondDataOfTypeJson() {
|
||||
assertEquals("application/json", mediaType);
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.rest.jbehave;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class GithubUserResponseMediaTypeStoryLiveTest extends AbstractStory {
|
||||
|
||||
@Override
|
||||
String storyName() {
|
||||
return "github_user_response_mediatype.story";
|
||||
}
|
||||
|
||||
@Override
|
||||
Object stepInstance() {
|
||||
return new GithubUserResponseMediaTypeSteps();
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.rest.jbehave;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.baeldung.rest.GitHubUser;
|
||||
import org.baeldung.rest.RetrieveUtil;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.jbehave.core.annotations.Given;
|
||||
import org.jbehave.core.annotations.Then;
|
||||
import org.jbehave.core.annotations.When;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static com.baeldung.rest.jbehave.GithubUserNotFoundSteps.getGithubUserProfile;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
public class GithubUserResponsePayloadSteps {
|
||||
|
||||
private String api;
|
||||
private GitHubUser resource;
|
||||
|
||||
@Given("github user profile api")
|
||||
public void givenGithubUserProfileApi() {
|
||||
api = "https://api.github.com/users/%s";
|
||||
}
|
||||
|
||||
@When("I look for $user via the api")
|
||||
public void whenILookForEugenpViaTheApi(String user) throws IOException {
|
||||
HttpResponse httpResponse = getGithubUserProfile(api, user);
|
||||
resource = RetrieveUtil.retrieveResourceFromResponse(httpResponse, GitHubUser.class);
|
||||
}
|
||||
|
||||
@Then("github's response contains a 'login' payload same as $username")
|
||||
public void thenGithubsResponseContainsAloginPayloadSameAsEugenp(String username) {
|
||||
assertThat(username, Matchers.is(resource.getLogin()));
|
||||
}
|
||||
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.rest.jbehave;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class GithubUserResponsePayloadStoryLiveTest extends AbstractStory {
|
||||
|
||||
@Override
|
||||
String storyName() {
|
||||
return "github_user_response_payload.story";
|
||||
}
|
||||
|
||||
@Override
|
||||
Object stepInstance() {
|
||||
return new GithubUserResponsePayloadSteps();
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.rest.jbehave;
|
||||
|
||||
import org.jbehave.core.annotations.Given;
|
||||
import org.jbehave.core.annotations.Then;
|
||||
import org.jbehave.core.annotations.When;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class IncreaseSteps {
|
||||
private int counter;
|
||||
private int previousValue;
|
||||
|
||||
@Given("a counter")
|
||||
public void aCounter() {
|
||||
}
|
||||
|
||||
@Given("the counter has any integral value")
|
||||
public void counterHasAnyIntegralValue() {
|
||||
counter = new Random().nextInt();
|
||||
previousValue = counter;
|
||||
}
|
||||
|
||||
@When("the user increases the counter")
|
||||
public void increasesTheCounter() {
|
||||
counter++;
|
||||
}
|
||||
|
||||
@Then("the value of the counter must be 1 greater than previous value")
|
||||
public void theValueOfTheCounterMustBe1Greater() {
|
||||
assertTrue(1 == counter - previousValue);
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.rest.jbehave;
|
||||
|
||||
import org.jbehave.core.configuration.Configuration;
|
||||
import org.jbehave.core.configuration.MostUsefulConfiguration;
|
||||
import org.jbehave.core.io.LoadFromClasspath;
|
||||
import org.jbehave.core.junit.JUnitStories;
|
||||
import org.jbehave.core.reporters.StoryReporterBuilder;
|
||||
import org.jbehave.core.steps.InjectableStepsFactory;
|
||||
import org.jbehave.core.steps.InstanceStepsFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jbehave.core.io.CodeLocations.codeLocationFromClass;
|
||||
import static org.jbehave.core.reporters.Format.CONSOLE;
|
||||
|
||||
public class IncreaseStoryLiveTest extends JUnitStories {
|
||||
|
||||
@Override
|
||||
public Configuration configuration() {
|
||||
return new MostUsefulConfiguration()
|
||||
.useStoryLoader(new LoadFromClasspath(this.getClass()))
|
||||
.useStoryReporterBuilder(new StoryReporterBuilder()
|
||||
.withCodeLocation(codeLocationFromClass(this.getClass()))
|
||||
.withFormats(CONSOLE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public InjectableStepsFactory stepsFactory() {
|
||||
return new InstanceStepsFactory(configuration(), new IncreaseSteps());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<String> storyPaths() {
|
||||
return Arrays.asList("increase.story");
|
||||
}
|
||||
|
||||
}
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
package com.baeldung.rest.wiremock.introduction;
|
||||
|
||||
import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
||||
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;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
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.get;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
|
||||
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.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class JUnitManagedIntegrationTest {
|
||||
|
||||
private static final String BAELDUNG_WIREMOCK_PATH = "/baeldung/wiremock";
|
||||
private static final String APPLICATION_JSON = "application/json";
|
||||
|
||||
@Rule
|
||||
public WireMockRule wireMockRule = new WireMockRule();
|
||||
|
||||
@Test
|
||||
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 = convertHttpResponseToString(httpResponse);
|
||||
|
||||
verify(getRequestedFor(urlEqualTo(BAELDUNG_WIREMOCK_PATH)));
|
||||
assertEquals(200, httpResponse.getStatusLine().getStatusCode());
|
||||
assertEquals(APPLICATION_JSON, httpResponse.getFirstHeader("Content-Type").getValue());
|
||||
assertEquals("\"testing-library\": \"WireMock\"", stringResponse);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJUnitManagedServer_whenMatchingHeaders_thenCorrect() throws IOException {
|
||||
stubFor(get(urlPathEqualTo(BAELDUNG_WIREMOCK_PATH))
|
||||
.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/html");
|
||||
HttpResponse httpResponse = httpClient.execute(request);
|
||||
String stringResponse = convertHttpResponseToString(httpResponse);
|
||||
|
||||
verify(getRequestedFor(urlEqualTo(BAELDUNG_WIREMOCK_PATH)));
|
||||
assertEquals(503, httpResponse.getStatusLine().getStatusCode());
|
||||
assertEquals("text/html", httpResponse.getFirstHeader("Content-Type").getValue());
|
||||
assertEquals("!!! Service Unavailable !!!", stringResponse);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJUnitManagedServer_whenMatchingBody_thenCorrect() throws IOException {
|
||||
stubFor(post(urlEqualTo(BAELDUNG_WIREMOCK_PATH))
|
||||
.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);
|
||||
|
||||
verify(postRequestedFor(urlEqualTo(BAELDUNG_WIREMOCK_PATH))
|
||||
.withHeader("Content-Type", equalTo(APPLICATION_JSON)));
|
||||
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJUnitManagedServer_whenNotUsingPriority_thenCorrect() throws IOException {
|
||||
stubFor(get(urlPathMatching("/baeldung/.*")).willReturn(aResponse().withStatus(200)));
|
||||
stubFor(get(urlPathEqualTo(BAELDUNG_WIREMOCK_PATH)).withHeader("Accept", matching("text/.*")).willReturn(aResponse().withStatus(503)));
|
||||
|
||||
HttpResponse httpResponse = generateClientAndReceiveResponseForPriorityTests();
|
||||
|
||||
verify(getRequestedFor(urlEqualTo(BAELDUNG_WIREMOCK_PATH)));
|
||||
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_PATH)).atPriority(2).withHeader("Accept", matching("text/.*")).willReturn(aResponse().withStatus(503)));
|
||||
|
||||
HttpResponse httpResponse = generateClientAndReceiveResponseForPriorityTests();
|
||||
|
||||
verify(getRequestedFor(urlEqualTo(BAELDUNG_WIREMOCK_PATH)));
|
||||
assertEquals(200, httpResponse.getStatusLine().getStatusCode());
|
||||
}
|
||||
|
||||
private static String convertHttpResponseToString(HttpResponse httpResponse) throws IOException {
|
||||
InputStream inputStream = httpResponse.getEntity().getContent();
|
||||
return convertInputStreamToString(inputStream);
|
||||
}
|
||||
|
||||
private static 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);
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.rest.wiremock.introduction;
|
||||
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
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 java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Scanner;
|
||||
|
||||
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;
|
||||
|
||||
public class ProgrammaticallyManagedLiveTest {
|
||||
|
||||
private static final String BAELDUNG_PATH = "/baeldung";
|
||||
|
||||
private WireMockServer wireMockServer = new WireMockServer();
|
||||
private CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
|
||||
@Test
|
||||
public void givenProgrammaticallyManagedServer_whenUsingSimpleStubbing_thenCorrect() throws IOException {
|
||||
wireMockServer.start();
|
||||
|
||||
configureFor("localhost", 8080);
|
||||
stubFor(get(urlEqualTo(BAELDUNG_PATH)).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_PATH)));
|
||||
assertEquals("Welcome to Baeldung!", stringResponse);
|
||||
|
||||
wireMockServer.stop();
|
||||
}
|
||||
|
||||
private static 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.baeldung.rest;
|
||||
|
||||
public class GitHubUser {
|
||||
|
||||
private String login;
|
||||
|
||||
public GitHubUser() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(final String login) {
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package org.baeldung.rest;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GithubBasicLiveTest {
|
||||
|
||||
// simple request - response
|
||||
|
||||
@Test
|
||||
public void givenUserDoesNotExists_whenUserInfoIsRetrieved_then404IsReceived() throws ClientProtocolException, IOException {
|
||||
// Given
|
||||
final String name = randomAlphabetic(8);
|
||||
final HttpUriRequest request = new HttpGet("https://api.github.com/users/" + name);
|
||||
|
||||
// When
|
||||
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
||||
|
||||
// Then
|
||||
assertThat(httpResponse.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_NOT_FOUND));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequestWithNoAcceptHeader_whenRequestIsExecuted_thenDefaultResponseContentTypeIsJson() throws ClientProtocolException, IOException {
|
||||
// Given
|
||||
final String jsonMimeType = "application/json";
|
||||
final HttpUriRequest request = new HttpGet("https://api.github.com/users/eugenp");
|
||||
|
||||
// When
|
||||
final HttpResponse response = HttpClientBuilder.create().build().execute(request);
|
||||
|
||||
// Then
|
||||
final String mimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
|
||||
assertEquals(jsonMimeType, mimeType);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserExists_whenUserInformationIsRetrieved_thenRetrievedResourceIsCorrect() throws ClientProtocolException, IOException {
|
||||
// Given
|
||||
final HttpUriRequest request = new HttpGet("https://api.github.com/users/eugenp");
|
||||
|
||||
// When
|
||||
final HttpResponse response = HttpClientBuilder.create().build().execute(request);
|
||||
|
||||
// Then
|
||||
final GitHubUser resource = RetrieveUtil.retrieveResourceFromResponse(response, GitHubUser.class);
|
||||
assertThat("eugenp", Matchers.is(resource.getLogin()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.baeldung.rest;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class RetrieveUtil {
|
||||
|
||||
// API
|
||||
|
||||
public static <T> T retrieveResourceFromResponse(final HttpResponse response, final Class<T> clazz) throws IOException {
|
||||
final String jsonFromResponse = EntityUtils.toString(response.getEntity());
|
||||
final ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
return mapper.readValue(jsonFromResponse, clazz);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user