Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API scenarios #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/test/java/com/odde/cucumber/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 8 additions & 0 deletions src/test/java/com/odde/cucumber/api/Api.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}
}
Expand Down
79 changes: 79 additions & 0 deletions src/test/java/com/odde/cucumber/step/ApiSteps.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
4 changes: 4 additions & 0 deletions src/test/java/com/odde/cucumber/step/CalculatorSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
17 changes: 17 additions & 0 deletions src/test/resources/feature/api.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Feature: API

Scenario: SignUp
Given has a user with email "[email protected]" 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 "[email protected]" 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
6 changes: 5 additions & 1 deletion src/test/resources/feature/calculator.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ Feature: Calculator

Scenario: Subtraction
When subtract 1 from 2
Then get 1
Then get 1

Scenario: Multiplication
When multiply 1 and 3
Then get 3