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

실습 - cucumber 전환 #422

Open
wants to merge 6 commits into
base: default-sample
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
21 changes: 21 additions & 0 deletions src/test/java/nextstep/cucumber/AcceptanceContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package nextstep.cucumber;

import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Profile("test")
@Component
public class AcceptanceContext {
public Map<String, Object> store = new HashMap<>();
public ExtractableResponse<Response> response;

public void clear() {
store.clear();
response = null;
}
}
25 changes: 25 additions & 0 deletions src/test/java/nextstep/cucumber/steps/BeforeStepDef.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nextstep.cucumber.steps;

import io.cucumber.java8.En;
import nextstep.cucumber.AcceptanceContext;
import nextstep.utils.DataLoader;
import nextstep.utils.DatabaseCleanup;
import org.springframework.beans.factory.annotation.Autowired;

public class BeforeStepDef implements En {
@Autowired
private DatabaseCleanup databaseCleanup;
@Autowired
private DataLoader dataLoader;

@Autowired
private AcceptanceContext acceptanceContext;

public BeforeStepDef() {
Before(() -> {
databaseCleanup.execute();
dataLoader.loadData();
acceptanceContext.clear();
});
}
}
77 changes: 77 additions & 0 deletions src/test/java/nextstep/cucumber/steps/PathStepDef.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package nextstep.cucumber.steps;

import io.cucumber.datatable.DataTable;
import io.cucumber.java8.En;
import io.restassured.RestAssured;
import nextstep.cucumber.AcceptanceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static nextstep.subway.acceptance.LineSteps.*;
import static nextstep.subway.acceptance.StationSteps.지하철역_생성_요청;
import static org.assertj.core.api.Assertions.assertThat;

public class PathStepDef implements En {

@Autowired
private AcceptanceContext acceptanceContext;

public PathStepDef() {
Given("지하철 역들을 생성 요청하고", (DataTable table) -> {
List<Map<String, String>> maps = table.asMaps();
for (Map<String, String> map : maps) {
String name = map.get("name");
Long stationId = 지하철역_생성_요청(name).jsonPath().getLong("id");
acceptanceContext.store.put(name, stationId);
}
});

Given("지하철 노선들을 생성 요청하고", (DataTable table) -> {
for (Map<String, String> map : table.asMaps()) {
String name = map.get("name");
String color = map.get("color");
String upStationName = map.get("upStationName");
String downStationName = map.get("downStationName");
int distance = Integer.parseInt(map.get("distance"));

Long lineId = 지하철_노선_생성_요청(name, color, (Long) acceptanceContext.store.get(upStationName), (Long) acceptanceContext.store.get(downStationName), distance);
acceptanceContext.store.put(name, lineId);
}
});

Given("지하철 노선에 지하철 구간 생성 요청하고", (DataTable table) -> {
for (Map<String, String> map : table.asMaps()) {
String name = map.get("name");
String upStationName = map.get("upStationName");
String downStationName = map.get("downStationName");
int distance = Integer.parseInt(map.get("distance"));

지하철_노선에_지하철_구간_생성_요청((Long) acceptanceContext.store.get(name), createSectionCreateParams((Long) acceptanceContext.store.get(upStationName), (Long) acceptanceContext.store.get(downStationName), distance));
}
});

When("{string}과 {string}의 최단 거리 경로를 조회하면", (String source, String target) -> {
acceptanceContext.response = RestAssured
.given().log().all()
.accept(MediaType.APPLICATION_JSON_VALUE)
.when().get("/paths?source={sourceId}&target={targetId}", acceptanceContext.store.get(source), acceptanceContext.store.get(target))
.then().log().all().extract();
});

Then("최단 거리 경로를 응답 받는다", (DataTable table) -> {
List<List<String>> rows = table.asLists(String.class);

List<Long> expected = rows.stream()
.map(it -> it.get(0))
.map(it -> (Long) acceptanceContext.store.get(it))
.collect(Collectors.toList());

assertThat(acceptanceContext.response.jsonPath().getList("stations.id", Long.class)).containsExactly(expected.toArray(new Long[0]));
});
}

}
21 changes: 21 additions & 0 deletions src/test/resources/features/path.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Feature: 지하철 경로 검색
Scenario: 두 역의 최단 거리 경로를 조회한다.
Given 지하철 역들을 생성 요청하고
| name |
| 교대역 |
| 강남역 |
| 양재역 |
| 남부터미널역 |
Given 지하철 노선들을 생성 요청하고
| name | color | upStationName | downStationName | distance |
| 2호선 | bg-red | 교대역 | 강남역 | 10 |
| 3호선 | bg-green | 강남역 | 양재역 | 10 |
| 신분당선 | bg-yellow | 교대역 | 남부터미널역 | 2 |
Given 지하철 노선에 지하철 구간 생성 요청하고
| name | upStationName | downStationName | distance |
| 3호선 | 남부터미널역 | 양재역 | 3 |
When "교대역"과 "양재역"의 최단 거리 경로를 조회하면
Then 최단 거리 경로를 응답 받는다
| 교대역 |
| 남부터미널역 |
| 양재역 |