BAEL-451 (#1610)
* BAEL-451 rest api test with JBehave * reformat pom * exclude live test from normal mvn test
This commit is contained in:
parent
1e8bcdccb2
commit
60332bb563
|
@ -121,6 +121,13 @@
|
|||
<artifactId>cucumber-junit</artifactId>
|
||||
<version>${cucumber.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jbehave</groupId>
|
||||
<artifactId>jbehave-core</artifactId>
|
||||
<version>${jbehave.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -177,10 +184,10 @@
|
|||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*UnitTest.java</exclude>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*IntegrationTest.java</include>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
|
@ -220,6 +227,7 @@
|
|||
<httpcore.version>4.4.5</httpcore.version>
|
||||
<httpclient.version>4.5.2</httpclient.version>
|
||||
|
||||
<jbehave.version>4.1</jbehave.version>
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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()));
|
||||
}
|
||||
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
Meta:
|
||||
|
||||
Narrative:
|
||||
As a user
|
||||
I want to look up a non-existent user's profile on github
|
||||
So that I can be sure that the username can not be found on github
|
||||
|
||||
Scenario: when a user checks a non-existent user on github, github would respond 'not found'
|
||||
|
||||
Given github user profile api
|
||||
And a random non-existent username
|
||||
When I look for the random user via the api
|
||||
Then github respond: 404 not found
|
||||
|
||||
When I look for eugenp1 via the api
|
||||
Then github respond: 404 not found
|
||||
|
||||
When I look for eugenp2 via the api
|
||||
Then github respond: 404 not found
|
|
@ -0,0 +1,13 @@
|
|||
Meta:
|
||||
|
||||
Narrative:
|
||||
As a user
|
||||
I want to look up a valid user's profile on github
|
||||
So that I can know that github responds data of type json
|
||||
|
||||
Scenario: when a user checks a valid user's profile on github, github would respond json data
|
||||
|
||||
Given github user profile api
|
||||
And a valid username
|
||||
When I look for the user via the api
|
||||
Then github respond data of type json
|
|
@ -0,0 +1,12 @@
|
|||
Meta:
|
||||
|
||||
Narrative:
|
||||
As a user
|
||||
I want to look up a valid user's profile on github
|
||||
So that I can know the login payload should be the same as username
|
||||
|
||||
Scenario: when a user checks a valid user's profile on github, github's response json should include a login payload with the same username
|
||||
|
||||
Given github user profile api
|
||||
When I look for eugenp via the api
|
||||
Then github's response contains a 'login' payload same as eugenp
|
|
@ -0,0 +1,15 @@
|
|||
JBehave Story - An increase test
|
||||
|
||||
Meta:
|
||||
|
||||
Narrative:
|
||||
As a user
|
||||
I want to increase a counter
|
||||
So that I can have the counter's value increase by 1
|
||||
|
||||
Scenario: when a user increases a counter, its value is increased by 1
|
||||
|
||||
Given a counter
|
||||
And the counter has any integral value
|
||||
When the user increases the counter
|
||||
Then the value of the counter must be 1 greater than previous value
|
Loading…
Reference in New Issue