diff --git a/src/test/java/com/odde/cucumber/Calculator.java b/src/test/java/com/odde/cucumber/Calculator.java index 2e465de..af9571f 100644 --- a/src/test/java/com/odde/cucumber/Calculator.java +++ b/src/test/java/com/odde/cucumber/Calculator.java @@ -17,4 +17,8 @@ public int getResult() { public void subtract(int a, int b) { result = a - b; } + + public void multiply(int a, int b) { + result = a * b; + } } diff --git a/src/test/java/com/odde/cucumber/api/Api.java b/src/test/java/com/odde/cucumber/api/Api.java index 9288c84..b59e9b8 100644 --- a/src/test/java/com/odde/cucumber/api/Api.java +++ b/src/test/java/com/odde/cucumber/api/Api.java @@ -1,11 +1,14 @@ package com.odde.cucumber.api; +import com.github.dockerjava.api.exception.UnauthorizedException; import com.odde.cucumber.api.client.AccountsClient; import com.odde.cucumber.api.client.UsersClient; import com.odde.cucumber.api.dto.Account; import com.odde.cucumber.api.dto.User; +import com.sun.jdi.InternalException; import feign.Response; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import java.util.List; @@ -32,6 +35,11 @@ public void signUp(User user) { public void signIn(User user) { try (Response response = usersClient.signIn(user)) { + if(response.status() == HttpStatus.UNAUTHORIZED.value()) { + throw new UnauthorizedException(response.reason()); + } else if (response.status() != HttpStatus.OK.value()) { + throw new InternalException(response.reason()); + } Feign.authorization = response.headers().get("Authorization").stream().findFirst().get(); } } diff --git a/src/test/java/com/odde/cucumber/step/ApiSteps.java b/src/test/java/com/odde/cucumber/step/ApiSteps.java new file mode 100644 index 0000000..b8af96c --- /dev/null +++ b/src/test/java/com/odde/cucumber/step/ApiSteps.java @@ -0,0 +1,79 @@ +package com.odde.cucumber.step; + +import com.github.dockerjava.api.exception.UnauthorizedException; +import com.odde.cucumber.api.Api; +import com.odde.cucumber.api.dto.Account; +import com.odde.cucumber.api.dto.User; +import io.cucumber.java.en.Given; +import io.cucumber.java.en.Then; +import io.cucumber.java.en.When; +import org.springframework.beans.factory.annotation.Autowired; + +import static org.junit.Assert.*; + +public class ApiSteps { + + @Autowired + private Api api; + + User user = null; + Throwable throwable = null; + + @Given("has a user with email {string} and password {string}") + public void hasAUserWithEmailAndPassword(String email, String password) { + this.user = new User(email, password); + } + + @When("signup with the given user") + public void signupWithTheGivenUser() { + api.signUp(this.user); + } + + @Then("the user can signin") + public void theUserCanSignin() { + api.signIn(user); + } + + @When("signup with the existed user again") + public void signupWithTheExistedUserAgain() { + try { + api.signUp(this.user); + api.signUp(this.user); + } catch (Exception e) { + this.throwable = e; + } + } + + @Then("should success") + public void shouldSuccess() { + assertNull(throwable); + } + + @When("add an account with name {string} and balance {int}") + public void addAnAccountWithNameAndBalance(String name, int balance) { + api.addAccount(new Account(name, balance)); + } + + @Then("get {string} and balance {int}") + public void getAndBalance(String name, int balance) { + Account account = api.getAccounts().get(0); + assertEquals(name, account.getName()); + assertEquals((Integer) balance, account.getBalance()); + } + + @When("signin with email {string} and password {string}") + public void signinWithEmailAndPassword(String name, String password) { + try { + api.signIn(new User(name, password)); + } catch (Exception ex) { + throwable = ex; + } + } + + @Then("throw unauthorized exception") + public void throwUnauthorizedException() { + assertNotNull(throwable); + System.out.println(throwable.getMessage()); + assertEquals(UnauthorizedException.class, throwable.getClass()); + } +} diff --git a/src/test/java/com/odde/cucumber/step/CalculatorSteps.java b/src/test/java/com/odde/cucumber/step/CalculatorSteps.java index 5f4929b..960a83c 100644 --- a/src/test/java/com/odde/cucumber/step/CalculatorSteps.java +++ b/src/test/java/com/odde/cucumber/step/CalculatorSteps.java @@ -25,4 +25,8 @@ public void get(int expected) { public void subtract(int a, int b) { calculator.subtract(b, a); } + + @When("multiply {int} and {int}") + public void multiply(int a, int b) { calculator.multiply(a, b); + } } diff --git a/src/test/resources/feature/api.feature b/src/test/resources/feature/api.feature new file mode 100644 index 0000000..af3afcb --- /dev/null +++ b/src/test/resources/feature/api.feature @@ -0,0 +1,17 @@ +Feature: API + + Scenario: SignUp + Given has a user with email "yong.wang@lseg.com" and password "1234" + When signup with the given user + Then the user can signin + + When signup with the existed user again + Then should success + + Scenario: SignIn + When signin with email "not.exist@example.com" and password "1234" + Then throw unauthorized exception + + Scenario: Account + When add an account with name "Yong Wang" and balance 1000 + Then get "Yong Wang" and balance 1000 \ No newline at end of file diff --git a/src/test/resources/feature/calculator.feature b/src/test/resources/feature/calculator.feature index 0076fe5..aacc491 100644 --- a/src/test/resources/feature/calculator.feature +++ b/src/test/resources/feature/calculator.feature @@ -6,4 +6,8 @@ Feature: Calculator Scenario: Subtraction When subtract 1 from 2 - Then get 1 \ No newline at end of file + Then get 1 + + Scenario: Multiplication + When multiply 1 and 3 + Then get 3 \ No newline at end of file