-
Notifications
You must be signed in to change notification settings - Fork 204
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
3단계 - 요금 정책 추가 #218
base: artium59
Are you sure you want to change the base?
3단계 - 요금 정책 추가 #218
Changes from all commits
11829db
34de02a
0aa34af
00da980
82eafc6
cad77e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,18 +5,23 @@ | |
import nextstep.member.domain.RoleType; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Component | ||
public class DataLoader { | ||
private MemberRepository memberRepository; | ||
private static final String PASSWORD ="password"; | ||
|
||
private final MemberRepository memberRepository; | ||
|
||
public DataLoader(MemberRepository memberRepository) { | ||
this.memberRepository = memberRepository; | ||
} | ||
|
||
public void loadData() { | ||
memberRepository.save(new Member("[email protected]", "password", 20, Arrays.asList(RoleType.ROLE_ADMIN.name()))); | ||
memberRepository.save(new Member("[email protected]", "password", 20, Arrays.asList(RoleType.ROLE_MEMBER.name()))); | ||
memberRepository.save(new Member("[email protected]", PASSWORD, 20, List.of(RoleType.ROLE_ADMIN.name()))); | ||
|
||
memberRepository.save(new Member("[email protected]", PASSWORD, 9, List.of(RoleType.ROLE_MEMBER.name()))); | ||
memberRepository.save(new Member("[email protected]", PASSWORD, 17, List.of(RoleType.ROLE_MEMBER.name()))); | ||
memberRepository.save(new Member("[email protected]", PASSWORD, 30, List.of(RoleType.ROLE_MEMBER.name()))); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package nextstep.subway.applicaion; | ||
|
||
import nextstep.member.application.MemberService; | ||
import nextstep.subway.builder.PathResponseBuilder; | ||
import nextstep.subway.applicaion.dto.PathResponse; | ||
import nextstep.subway.constant.SearchType; | ||
|
@@ -13,23 +14,26 @@ | |
|
||
@Service | ||
public class PathService { | ||
private LineService lineService; | ||
private StationService stationService; | ||
private PathResponseBuilder pathResponseBuilder; | ||
private final MemberService memberService; | ||
private final LineService lineService; | ||
private final StationService stationService; | ||
private final PathResponseBuilder pathResponseBuilder; | ||
|
||
public PathService(LineService lineService, StationService stationService, PathResponseBuilder pathResponseBuilder) { | ||
public PathService(MemberService memberService, LineService lineService, StationService stationService, PathResponseBuilder pathResponseBuilder) { | ||
this.memberService = memberService; | ||
this.lineService = lineService; | ||
this.stationService = stationService; | ||
this.pathResponseBuilder = pathResponseBuilder; | ||
} | ||
|
||
public PathResponse findPath(Long source, Long target, SearchType searchType) { | ||
public PathResponse findPath(String email, Long source, Long target, SearchType searchType) { | ||
int age = memberService.findMember(email).getAge(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 회원가입 되어 있는 회원이 로그인 상태가 아니라면 이 서비스는 이용을 못할 것 같네요 |
||
Station upStation = stationService.findById(source); | ||
Station downStation = stationService.findById(target); | ||
List<Line> lines = lineService.findLines(); | ||
SubwayMap subwayMap = new SubwayMap(lines); | ||
Path path = subwayMap.findPath(upStation, downStation, searchType); | ||
|
||
return pathResponseBuilder.build(path); | ||
return pathResponseBuilder.build(path, age); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class AcceptanceTest { | ||
private static final String EMAIL = "[email protected]"; | ||
private static final String PASSWORD = "password"; | ||
public static final String PASSWORD = "password"; | ||
|
||
@LocalServerPort | ||
int port; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,21 @@ | ||
package nextstep.subway.acceptance; | ||
|
||
import io.restassured.RestAssured; | ||
import io.restassured.response.ExtractableResponse; | ||
import io.restassured.response.Response; | ||
import nextstep.subway.constant.SearchType; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.http.MediaType; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import static nextstep.subway.acceptance.LineSteps.지하철_노선에_지하철_구간_생성_요청; | ||
import static nextstep.subway.acceptance.MemberSteps.로그인_되어_있음; | ||
import static nextstep.subway.acceptance.PathSteps.searchType에_따른_두_역의_최단_경로_조회를_요청; | ||
import static nextstep.subway.acceptance.StationSteps.지하철역_생성_요청; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
|
@@ -42,48 +45,60 @@ public void setUp() { | |
양재역 = 지하철역_생성_요청(관리자, "양재역").jsonPath().getLong("id"); | ||
남부터미널역 = 지하철역_생성_요청(관리자, "남부터미널역").jsonPath().getLong("id"); | ||
|
||
이호선 = 지하철_노선_생성_요청("2호선", "green", 교대역, 강남역, 10, 4); | ||
신분당선 = 지하철_노선_생성_요청("신분당선", "red", 강남역, 양재역, 10, 4); | ||
삼호선 = 지하철_노선_생성_요청("3호선", "orange", 교대역, 남부터미널역, 2, 10); | ||
이호선 = 지하철_노선_생성_요청("2호선", "green", 교대역, 강남역, 10, 4, 200); | ||
신분당선 = 지하철_노선_생성_요청("신분당선", "red", 강남역, 양재역, 10, 4, 1000); | ||
삼호선 = 지하철_노선_생성_요청("3호선", "orange", 교대역, 남부터미널역, 2, 10, 300); | ||
|
||
지하철_노선에_지하철_구간_생성_요청(관리자, 삼호선, createSectionCreateParams(남부터미널역, 양재역, 3, 1)); | ||
} | ||
|
||
@DisplayName("두 역의 최단 거리 경로를 조회한다.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 테스트 내용이 변경됨에 따라 DisplayName도 변경하면 어떨까요? |
||
@Test | ||
void findPathByDistance() { | ||
@ParameterizedTest(name = "{index}: {2}") | ||
@MethodSource("유저와_최단_거리_경로_요금") | ||
void findPathByDistance(String email, int price, String message) { | ||
// when | ||
ExtractableResponse<Response> response = searchType에_따른_두_역의_최단_경로_조회를_요청(교대역, 양재역, SearchType.DISTANCE); | ||
String accessToken = 로그인_되어_있음(email, PASSWORD); | ||
ExtractableResponse<Response> response = searchType에_따른_두_역의_최단_경로_조회를_요청(accessToken, 교대역, 양재역, SearchType.DISTANCE); | ||
|
||
// then | ||
assertThat(response.jsonPath().getList("stations.id", Long.class)).containsExactly(교대역, 남부터미널역, 양재역); | ||
assertThat(response.jsonPath().getInt("distance")).isEqualTo(5); | ||
assertThat(response.jsonPath().getInt("duration")).isEqualTo(11); | ||
assertThat(response.jsonPath().getInt("fare")).isEqualTo(1250); | ||
assertThat(response.jsonPath().getInt("fare")).isEqualTo(price); | ||
} | ||
|
||
@DisplayName("두 역의 최단 시간 경로를 조회한다.") | ||
@Test | ||
void findPathByDuration() { | ||
@ParameterizedTest(name = "{index}: {2}") | ||
@MethodSource("유저와_최단_시간_경로_요금") | ||
void findPathByDuration(String email, int price, String message) { | ||
// when | ||
ExtractableResponse<Response> response = searchType에_따른_두_역의_최단_경로_조회를_요청(교대역, 양재역, SearchType.DURATION); | ||
String accessToken = 로그인_되어_있음(email, PASSWORD); | ||
ExtractableResponse<Response> response = searchType에_따른_두_역의_최단_경로_조회를_요청(accessToken, 교대역, 양재역, SearchType.DURATION); | ||
|
||
// then | ||
assertThat(response.jsonPath().getList("stations.id", Long.class)).containsExactly(교대역, 강남역, 양재역); | ||
assertThat(response.jsonPath().getInt("distance")).isEqualTo(20); | ||
assertThat(response.jsonPath().getInt("duration")).isEqualTo(8); | ||
assertThat(response.jsonPath().getInt("fare")).isEqualTo(1450); | ||
assertThat(response.jsonPath().getInt("fare")).isEqualTo(price); | ||
} | ||
|
||
private ExtractableResponse<Response> searchType에_따른_두_역의_최단_경로_조회를_요청(Long source, Long target, SearchType searchType) { | ||
return RestAssured | ||
.given().log().all() | ||
.accept(MediaType.APPLICATION_JSON_VALUE) | ||
.when().get("/paths?source={sourceId}&target={targetId}&searchType={searchType}", source, target, searchType.name()) | ||
.then().log().all().extract(); | ||
private static Stream<Arguments> 유저와_최단_거리_경로_요금() { | ||
return Stream.of( | ||
Arguments.of("[email protected]", 600, "어린이 사용자는 350원을 공제한 금액의 50%를 할인받는다."), | ||
Arguments.of("[email protected]", 960, "청소년 사용자는 350원을 공제한 금액의 20%를 할인받는다."), | ||
Arguments.of("[email protected]", 1550, "성인 사용자는 할인 안받는다.") | ||
); | ||
} | ||
|
||
private Long 지하철_노선_생성_요청(String name, String color, Long upStation, Long downStation, int distance, int duration) { | ||
private static Stream<Arguments> 유저와_최단_시간_경로_요금() { | ||
return Stream.of( | ||
Arguments.of("[email protected]", 1050, "어린이 사용자는 350원을 공제한 금액의 50%를 할인받는다."), | ||
Arguments.of("[email protected]", 1680, "청소년 사용자는 350원을 공제한 금액의 20%를 할인받는다."), | ||
Arguments.of("[email protected]", 2450, "성인 사용자는 할인 안받는다.") | ||
); | ||
} | ||
|
||
private Long 지하철_노선_생성_요청(String name, String color, Long upStation, Long downStation, int distance, int duration, int extraFare) { | ||
Map<String, String> lineCreateParams; | ||
lineCreateParams = new HashMap<>(); | ||
lineCreateParams.put("name", name); | ||
|
@@ -92,6 +107,7 @@ void findPathByDuration() { | |
lineCreateParams.put("downStationId", downStation + ""); | ||
lineCreateParams.put("distance", distance + ""); | ||
lineCreateParams.put("duration", duration + ""); | ||
lineCreateParams.put("extraFare", extraFare + ""); | ||
|
||
return LineSteps.지하철_노선_생성_요청(관리자, lineCreateParams).jsonPath().getLong("id"); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package nextstep.subway.acceptance; | ||
|
||
import io.restassured.response.ExtractableResponse; | ||
import io.restassured.response.Response; | ||
import nextstep.subway.constant.SearchType; | ||
import org.springframework.http.MediaType; | ||
|
||
public class PathSteps extends AcceptanceTestSteps { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PathSteps를 분리해 주셨네요 👍 |
||
|
||
public static ExtractableResponse<Response> searchType에_따른_두_역의_최단_경로_조회를_요청(String accessToken, Long source, Long target, SearchType searchType) { | ||
return given(accessToken) | ||
.accept(MediaType.APPLICATION_JSON_VALUE) | ||
.when().get("/paths?source={sourceId}&target={targetId}&searchType={searchType}", source, target, searchType.name()) | ||
.then().log().all().extract(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
주석 없이는 쉽게 이해하기 힘든 코드라고 생각드네요 😢
(
객체지향 생활 체조 규칙 2: else 예약어를 쓰지 않는다.
)할인 정책이 추가되거나 변경되면 조건문이 무한히 늘어날 수 을 것 같아요
정책을 효과적으로 관리할 수 있는 방법은 어떤게 있을까요?