Skip to content

Commit 60332bb

Browse files
Tian Baoqiangpivovarit
authored andcommitted
BAEL-451 (eugenp#1610)
* BAEL-451 rest api test with JBehave * reformat pom * exclude live test from normal mvn test
1 parent 1e8bcdc commit 60332bb

14 files changed

Lines changed: 556 additions & 179 deletions

‎rest-testing/pom.xml‎

Lines changed: 187 additions & 179 deletions
Large diffs are not rendered by default.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.rest.jbehave;
2+
3+
import org.jbehave.core.configuration.Configuration;
4+
import org.jbehave.core.configuration.MostUsefulConfiguration;
5+
import org.jbehave.core.io.LoadFromClasspath;
6+
import org.jbehave.core.junit.JUnitStories;
7+
import org.jbehave.core.reporters.StoryReporterBuilder;
8+
import org.jbehave.core.steps.InjectableStepsFactory;
9+
import org.jbehave.core.steps.InstanceStepsFactory;
10+
11+
import java.util.Arrays;
12+
import java.util.List;
13+
14+
import static org.jbehave.core.io.CodeLocations.codeLocationFromClass;
15+
import static org.jbehave.core.reporters.Format.CONSOLE;
16+
17+
/**
18+
* @author aiet
19+
*/
20+
public abstract class AbstractStory extends JUnitStories {
21+
22+
abstract String storyName();
23+
24+
@Override
25+
public Configuration configuration() {
26+
return new MostUsefulConfiguration()
27+
.useStoryLoader(new LoadFromClasspath(this.getClass()))
28+
.useStoryReporterBuilder(new StoryReporterBuilder()
29+
.withCodeLocation(codeLocationFromClass(this.getClass()))
30+
.withFormats(CONSOLE));
31+
}
32+
33+
abstract Object stepInstance();
34+
35+
@Override
36+
public InjectableStepsFactory stepsFactory() {
37+
return new InstanceStepsFactory(configuration(), stepInstance());
38+
}
39+
40+
@Override
41+
protected List<String> storyPaths() {
42+
return Arrays.asList(storyName());
43+
}
44+
45+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.rest.jbehave;
2+
3+
import org.apache.http.HttpResponse;
4+
import org.apache.http.client.methods.HttpGet;
5+
import org.apache.http.client.methods.HttpUriRequest;
6+
import org.apache.http.impl.client.HttpClientBuilder;
7+
import org.jbehave.core.annotations.Given;
8+
import org.jbehave.core.annotations.Then;
9+
import org.jbehave.core.annotations.When;
10+
11+
import java.io.IOException;
12+
13+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
14+
import static org.apache.http.HttpStatus.SC_NOT_FOUND;
15+
import static org.junit.Assert.assertTrue;
16+
17+
public class GithubUserNotFoundSteps {
18+
19+
private String api;
20+
private String nonExistentUser;
21+
private int githubResponseCode;
22+
23+
@Given("github user profile api")
24+
public void givenGithubUserProfileApi() {
25+
api = "https://api.github.com/users/%s";
26+
}
27+
28+
@Given("a random non-existent username")
29+
public void givenANonexistentUsername() {
30+
nonExistentUser = randomAlphabetic(8);
31+
}
32+
33+
@When("I look for the random user via the api")
34+
public void whenILookForTheUserViaTheApi() throws IOException {
35+
githubResponseCode = getGithubUserProfile(api, nonExistentUser)
36+
.getStatusLine()
37+
.getStatusCode();
38+
}
39+
40+
@When("I look for $user via the api")
41+
public void whenILookForSomeNonExistentUserViaTheApi(String user) throws IOException {
42+
githubResponseCode = getGithubUserProfile(api, user)
43+
.getStatusLine()
44+
.getStatusCode();
45+
}
46+
47+
static HttpResponse getGithubUserProfile(String api, String username) throws IOException {
48+
HttpUriRequest request = new HttpGet(String.format(api, username));
49+
return HttpClientBuilder
50+
.create()
51+
.build()
52+
.execute(request);
53+
}
54+
55+
@Then("github respond: 404 not found")
56+
public void thenGithubRespond404NotFound() {
57+
assertTrue(SC_NOT_FOUND == githubResponseCode);
58+
}
59+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.rest.jbehave;
2+
3+
/**
4+
* @author aiet
5+
*/
6+
public class GithubUserNotFoundStoryLiveTest extends AbstractStory {
7+
8+
@Override
9+
String storyName() {
10+
return "github_user_not_found.story";
11+
}
12+
13+
@Override
14+
Object stepInstance() {
15+
return new GithubUserNotFoundSteps();
16+
}
17+
18+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.rest.jbehave;
2+
3+
import org.apache.http.entity.ContentType;
4+
import org.jbehave.core.annotations.Given;
5+
import org.jbehave.core.annotations.Then;
6+
import org.jbehave.core.annotations.When;
7+
8+
import java.io.IOException;
9+
10+
import static com.baeldung.rest.jbehave.GithubUserNotFoundSteps.getGithubUserProfile;
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class GithubUserResponseMediaTypeSteps {
14+
15+
private String api;
16+
private String validUser;
17+
private String mediaType;
18+
19+
@Given("github user profile api")
20+
public void givenGithubUserProfileApi() {
21+
api = "https://api.github.com/users/%s";
22+
}
23+
24+
@Given("a valid username")
25+
public void givenAValidUsername() {
26+
validUser = "eugenp";
27+
}
28+
29+
@When("I look for the user via the api")
30+
public void whenILookForTheUserViaTheApi() throws IOException {
31+
mediaType = ContentType
32+
.getOrDefault(getGithubUserProfile(api, validUser).getEntity())
33+
.getMimeType();
34+
}
35+
36+
@Then("github respond data of type json")
37+
public void thenGithubRespondDataOfTypeJson() {
38+
assertEquals("application/json", mediaType);
39+
}
40+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.rest.jbehave;
2+
3+
/**
4+
* @author aiet
5+
*/
6+
public class GithubUserResponseMediaTypeStoryLiveTest extends AbstractStory {
7+
8+
@Override
9+
String storyName() {
10+
return "github_user_response_mediatype.story";
11+
}
12+
13+
@Override
14+
Object stepInstance() {
15+
return new GithubUserResponseMediaTypeSteps();
16+
}
17+
18+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.rest.jbehave;
2+
3+
import org.apache.http.HttpResponse;
4+
import org.apache.http.client.methods.HttpGet;
5+
import org.apache.http.client.methods.HttpUriRequest;
6+
import org.apache.http.impl.client.HttpClientBuilder;
7+
import org.baeldung.rest.GitHubUser;
8+
import org.baeldung.rest.RetrieveUtil;
9+
import org.hamcrest.Matchers;
10+
import org.jbehave.core.annotations.Given;
11+
import org.jbehave.core.annotations.Then;
12+
import org.jbehave.core.annotations.When;
13+
14+
import java.io.IOException;
15+
16+
import static com.baeldung.rest.jbehave.GithubUserNotFoundSteps.getGithubUserProfile;
17+
import static org.hamcrest.MatcherAssert.assertThat;
18+
19+
public class GithubUserResponsePayloadSteps {
20+
21+
private String api;
22+
private GitHubUser resource;
23+
24+
@Given("github user profile api")
25+
public void givenGithubUserProfileApi() {
26+
api = "https://api.github.com/users/%s";
27+
}
28+
29+
@When("I look for $user via the api")
30+
public void whenILookForEugenpViaTheApi(String user) throws IOException {
31+
HttpResponse httpResponse = getGithubUserProfile(api, user);
32+
resource = RetrieveUtil.retrieveResourceFromResponse(httpResponse, GitHubUser.class);
33+
}
34+
35+
@Then("github's response contains a 'login' payload same as $username")
36+
public void thenGithubsResponseContainsAloginPayloadSameAsEugenp(String username) {
37+
assertThat(username, Matchers.is(resource.getLogin()));
38+
}
39+
40+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.rest.jbehave;
2+
3+
/**
4+
* @author aiet
5+
*/
6+
public class GithubUserResponsePayloadStoryLiveTest extends AbstractStory {
7+
8+
@Override
9+
String storyName() {
10+
return "github_user_response_payload.story";
11+
}
12+
13+
@Override
14+
Object stepInstance() {
15+
return new GithubUserResponsePayloadSteps();
16+
}
17+
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.rest.jbehave;
2+
3+
import org.jbehave.core.annotations.Given;
4+
import org.jbehave.core.annotations.Then;
5+
import org.jbehave.core.annotations.When;
6+
7+
import java.util.Random;
8+
9+
import static org.junit.Assert.assertTrue;
10+
11+
public class IncreaseSteps {
12+
private int counter;
13+
private int previousValue;
14+
15+
@Given("a counter")
16+
public void aCounter() {
17+
}
18+
19+
@Given("the counter has any integral value")
20+
public void counterHasAnyIntegralValue() {
21+
counter = new Random().nextInt();
22+
previousValue = counter;
23+
}
24+
25+
@When("the user increases the counter")
26+
public void increasesTheCounter() {
27+
counter++;
28+
}
29+
30+
@Then("the value of the counter must be 1 greater than previous value")
31+
public void theValueOfTheCounterMustBe1Greater() {
32+
assertTrue(1 == counter - previousValue);
33+
}
34+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.rest.jbehave;
2+
3+
import org.jbehave.core.configuration.Configuration;
4+
import org.jbehave.core.configuration.MostUsefulConfiguration;
5+
import org.jbehave.core.io.LoadFromClasspath;
6+
import org.jbehave.core.junit.JUnitStories;
7+
import org.jbehave.core.reporters.StoryReporterBuilder;
8+
import org.jbehave.core.steps.InjectableStepsFactory;
9+
import org.jbehave.core.steps.InstanceStepsFactory;
10+
11+
import java.util.Arrays;
12+
import java.util.List;
13+
14+
import static org.jbehave.core.io.CodeLocations.codeLocationFromClass;
15+
import static org.jbehave.core.reporters.Format.CONSOLE;
16+
17+
public class IncreaseStoryLiveTest extends JUnitStories {
18+
19+
@Override
20+
public Configuration configuration() {
21+
return new MostUsefulConfiguration()
22+
.useStoryLoader(new LoadFromClasspath(this.getClass()))
23+
.useStoryReporterBuilder(new StoryReporterBuilder()
24+
.withCodeLocation(codeLocationFromClass(this.getClass()))
25+
.withFormats(CONSOLE));
26+
}
27+
28+
@Override
29+
public InjectableStepsFactory stepsFactory() {
30+
return new InstanceStepsFactory(configuration(), new IncreaseSteps());
31+
}
32+
33+
@Override
34+
protected List<String> storyPaths() {
35+
return Arrays.asList("increase.story");
36+
}
37+
38+
}

0 commit comments

Comments
 (0)