BAEL-451 (#1610)
* BAEL-451 rest api test with JBehave * reformat pom * exclude live test from normal mvn test
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
1e8bcdccb2
commit
60332bb563
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user