committed by
Grzegorz Piwowarek
parent
48c0e41791
commit
8b028a2946
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.serenity;
|
||||
|
||||
import net.serenitybdd.jbehave.SerenityStory;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class GithubUserProfilePayloadTest extends SerenityStory {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.serenity;
|
||||
|
||||
import com.baeldung.serenity.membership.MemberStatusSteps;
|
||||
import net.serenitybdd.junit.runners.SerenityRunner;
|
||||
import net.thucydides.core.annotations.Steps;
|
||||
import net.thucydides.core.annotations.Title;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static com.baeldung.serenity.membership.Commodity.MacBookPro;
|
||||
import static com.baeldung.serenity.membership.MemberGrade.Bronze;
|
||||
import static com.baeldung.serenity.membership.MemberGrade.Gold;
|
||||
import static com.baeldung.serenity.membership.MemberGrade.Silver;
|
||||
|
||||
@RunWith(SerenityRunner.class)
|
||||
public class MemberStatusTest {
|
||||
|
||||
@Steps MemberStatusSteps memberSteps;
|
||||
|
||||
@Test
|
||||
public void membersShouldStartWithBronzeStatus() {
|
||||
memberSteps.aClientJoinsTheMemberProgram();
|
||||
memberSteps.theMemberShouldHaveAStatusOf(Bronze);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Title("Members earn Silver grade after 1000 points ($10,000)")
|
||||
public void earnsSilverAfterSpends$10000() {
|
||||
memberSteps.aClientJoinsTheMemberProgram();
|
||||
memberSteps.theMemberSpends(10_000);
|
||||
memberSteps.theMemberShouldHaveAStatusOf(Silver);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Title("Members with 2,000 points should earn Gold grade when added 3,000 points ($30,000)")
|
||||
public void memberWith2000PointsEarnsGoldAfterSpends$30000() {
|
||||
memberSteps.aMemberHasPointsOf(2000);
|
||||
memberSteps.theMemberSpends(30_000);
|
||||
memberSteps.theMemberShouldHaveAStatusOf(Gold);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Title("Members with 50,000 points can exchange a MacBook Pro")
|
||||
public void memberWith50000PointsCanExchangeAMacbookpro(){
|
||||
memberSteps.aMemberHasPointsOf(50_000);
|
||||
memberSteps.aMemberExchangeA(MacBookPro);
|
||||
memberSteps.memberShouldHavePointsLeft();
|
||||
}
|
||||
|
||||
/**
|
||||
* This test should fail, comment out <code>@Ignore</code> to see how failed test can be reflected in Serenity report. <br/>
|
||||
* Remember to add <code><testFailureIgnore>true</testFailureIgnore></code> under maven-surefire-plugin configuration.
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
@Title("Members with 500 points should have a Gold status when added 4,000 points ($40,000)")
|
||||
public void memberWith500PointsEarnsGoldAfterSpends$40000(){
|
||||
memberSteps.aMemberHasPointsOf(500);
|
||||
memberSteps.theMemberSpends(40_000);
|
||||
memberSteps.theMemberShouldHaveAStatusOf(Gold);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
@Title("Members with 100 points would have a Gold status when added 10,000 points ($100,000)")
|
||||
public void memberWith100EarnsGoldAfterSpends$100000(){
|
||||
memberSteps.aMemberHasPointsOf(100);
|
||||
memberSteps.theMemberSpends(100_000);
|
||||
memberSteps.theMemberShouldHaveAStatusOf(Gold);
|
||||
}
|
||||
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.serenity.github;
|
||||
|
||||
import net.thucydides.core.annotations.Step;
|
||||
import org.hamcrest.Matchers;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static net.serenitybdd.rest.SerenityRest.rest;
|
||||
import static net.serenitybdd.rest.SerenityRest.then;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class GithubRestAssuredUserAPISteps {
|
||||
|
||||
private String api;
|
||||
|
||||
@Step("Given the github REST API for user profile")
|
||||
public void withUserProfileAPIEndpoint() {
|
||||
api = "https://api.github.com/users/{username}";
|
||||
}
|
||||
|
||||
@Step("When looking for {0} via the api")
|
||||
public void getProfileOfUser(String username) throws IOException {
|
||||
rest().get(api, username);
|
||||
}
|
||||
|
||||
@Step("Then there should be a login field with value {0} in payload of user {0}")
|
||||
public void profilePayloadShouldContainLoginValue(String username) {
|
||||
then().body("login", Matchers.equalTo(username));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.serenity.github;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import net.thucydides.core.annotations.Step;
|
||||
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.apache.http.util.EntityUtils;
|
||||
import org.hamcrest.Matchers;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class GithubRestUserAPISteps {
|
||||
|
||||
private String api;
|
||||
private GitHubUser resource;
|
||||
|
||||
@Step("Given the github REST API for user profile")
|
||||
public void withUserProfileAPIEndpoint() {
|
||||
api = "https://api.github.com/users/%s";
|
||||
}
|
||||
|
||||
@Step("When looking for {0} via the api")
|
||||
public void getProfileOfUser(String username) throws IOException {
|
||||
HttpResponse httpResponse = getGithubUserProfile(api, username);
|
||||
resource = retrieveResourceFromResponse(httpResponse, GitHubUser.class);
|
||||
}
|
||||
|
||||
@Step("Then there should be a login field with value {0} in payload of user {0}")
|
||||
public void profilePayloadShouldContainLoginValue(String username) {
|
||||
assertThat(username, Matchers.is(resource.getLogin()));
|
||||
}
|
||||
|
||||
private 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);
|
||||
}
|
||||
|
||||
private static HttpResponse getGithubUserProfile(String api, String username) throws IOException {
|
||||
HttpUriRequest request = new HttpGet(String.format(api, username));
|
||||
return HttpClientBuilder
|
||||
.create()
|
||||
.build()
|
||||
.execute(request);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.serenity.github;
|
||||
|
||||
import net.thucydides.core.annotations.Steps;
|
||||
import org.jbehave.core.annotations.Given;
|
||||
import org.jbehave.core.annotations.Then;
|
||||
import org.jbehave.core.annotations.When;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class GithubUserProfilePayloadStepDefinitions {
|
||||
|
||||
// @Steps
|
||||
// GithubRestUserAPISteps userAPISteps;
|
||||
|
||||
@Steps
|
||||
GithubRestAssuredUserAPISteps userAPISteps;
|
||||
|
||||
@Given("github user profile api")
|
||||
public void givenGithubUserProfileApi() {
|
||||
userAPISteps.withUserProfileAPIEndpoint();
|
||||
}
|
||||
|
||||
@When("looking for $user via the api")
|
||||
public void whenLookingForProfileOf(String user) throws IOException {
|
||||
userAPISteps.getProfileOfUser(user);
|
||||
}
|
||||
|
||||
@Then("github's response contains a 'login' payload same as $user")
|
||||
public void thenGithubsResponseContainsAloginPayloadSameAs(String user) {
|
||||
userAPISteps.profilePayloadShouldContainLoginValue(user);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.serenity.membership;
|
||||
|
||||
import net.thucydides.core.annotations.Pending;
|
||||
import net.thucydides.core.annotations.Step;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class MemberStatusSteps {
|
||||
|
||||
Member member;
|
||||
|
||||
@Step("Given a member has {0} points")
|
||||
public void aMemberHasPointsOf(int points) {
|
||||
member = Member.withInitialPoints(points);
|
||||
}
|
||||
|
||||
@Step("Then the member grade should be {0}")
|
||||
public void theMemberShouldHaveAStatusOf(MemberGrade grade) {
|
||||
assertThat(member.getGrade(), equalTo(grade));
|
||||
}
|
||||
|
||||
@Step("When the member spends ${0} ")
|
||||
public void theMemberSpends(int moneySpent) {
|
||||
member.spend(moneySpent);
|
||||
}
|
||||
|
||||
@Step("Given client joins membership program")
|
||||
public void aClientJoinsTheMemberProgram() {
|
||||
member = Member.withInitialPoints(0);
|
||||
}
|
||||
|
||||
@Pending
|
||||
@Step("When the member exchange {}")
|
||||
public void aMemberExchangeA(Commodity commodity){
|
||||
//TODO
|
||||
}
|
||||
|
||||
@Pending
|
||||
@Step("Then the member should have points left")
|
||||
public void memberShouldHavePointsLeft() {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user