|
| 1 | +package com.example.pace.domain.schedule.service.command; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.ArgumentMatchers.anyString; |
| 6 | +import static org.mockito.BDDMockito.given; |
| 7 | + |
| 8 | +import com.example.pace.domain.schedule.dto.request.DirectionRequestDTO; |
| 9 | +import com.example.pace.domain.schedule.dto.request.RouteSaveReqDto; |
| 10 | +import com.example.pace.domain.schedule.dto.response.RouteApiResDto; |
| 11 | +import com.example.pace.domain.schedule.dto.response.RouteListResDTO; |
| 12 | +import com.example.pace.domain.schedule.dto.response.info.TransitRouteDetailInfoResDTO; |
| 13 | +import com.example.pace.domain.schedule.enums.TransitType; |
| 14 | +import com.example.pace.domain.schedule.infrastructure.GoogleDirectionApiClient; |
| 15 | +import com.example.pace.domain.schedule.infrastructure.dto.GoogleDirectionApiResponse; |
| 16 | +import com.example.pace.domain.transit.dto.response.SubwayStationResDTO; |
| 17 | +import com.example.pace.domain.transit.service.SubwayNetworkService; |
| 18 | +import java.math.BigDecimal; |
| 19 | +import java.util.List; |
| 20 | +import org.junit.jupiter.api.DisplayName; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | +import org.mockito.InjectMocks; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 26 | +import org.springframework.test.util.ReflectionTestUtils; |
| 27 | + |
| 28 | +@ExtendWith(MockitoExtension.class) |
| 29 | +class RouteCommandServiceUnitTest { |
| 30 | + |
| 31 | + @InjectMocks |
| 32 | + private RouteCommandService routeCommandService; |
| 33 | + |
| 34 | + @Mock |
| 35 | + private GoogleDirectionApiClient googleDirectionApiClient; |
| 36 | + |
| 37 | + @Mock |
| 38 | + private SubwayNetworkService subwayNetworkService; |
| 39 | + |
| 40 | + @Test |
| 41 | + @DisplayName("지하철 하행 경로 탐색 시 downNext 필드에 하행 방향이 세팅된다.") |
| 42 | + void searchRoute_Subway_Downbound_Success() { |
| 43 | + // given: 하행 경로 조건 세팅 |
| 44 | + RouteSaveReqDto.CreateRouteDTO request = new RouteSaveReqDto.CreateRouteDTO( |
| 45 | + BigDecimal.valueOf(37.1), BigDecimal.valueOf(127.1), |
| 46 | + BigDecimal.valueOf(37.2), BigDecimal.valueOf(127.2), |
| 47 | + null, null, TransitType.SUBWAY, null |
| 48 | + ); |
| 49 | + |
| 50 | + GoogleDirectionApiResponse googleRes = createMockGoogleResponse("1호선", "시청", "서울역"); |
| 51 | + given(googleDirectionApiClient.getDirections(any(DirectionRequestDTO.class))) |
| 52 | + .willReturn(googleRes); |
| 53 | + |
| 54 | + // 하행 조건: 출발역의 nextStations 리스트에 도착역 포함 |
| 55 | + SubwayStationResDTO startStation = new SubwayStationResDTO(); |
| 56 | + ReflectionTestUtils.setField(startStation, "stationName", "시청"); |
| 57 | + ReflectionTestUtils.setField(startStation, "nextStations", List.of("서울역")); |
| 58 | + ReflectionTestUtils.setField(startStation, "prevStations", List.of("종각")); |
| 59 | + |
| 60 | + SubwayStationResDTO secondStation = new SubwayStationResDTO(); |
| 61 | + ReflectionTestUtils.setField(secondStation, "stationName", "서울역"); |
| 62 | + |
| 63 | + given(subwayNetworkService.getStationsBetween(anyString(), anyString(), anyString())) |
| 64 | + .willReturn(List.of(startStation, secondStation)); |
| 65 | + |
| 66 | + // when: 서비스 메서드 실행 |
| 67 | + RouteListResDTO result = routeCommandService.searchRoute(request); |
| 68 | + |
| 69 | + // then: 결과 검증 |
| 70 | + assertThat(result.getRouteApiResDtoList()).hasSize(1); |
| 71 | + RouteApiResDto route = result.getRouteApiResDtoList().getFirst(); |
| 72 | + |
| 73 | + TransitRouteDetailInfoResDTO transitDetail = route.getRouteDetailInfoResDTOList().getFirst().getTransitDetail(); |
| 74 | + |
| 75 | + assertThat(transitDetail.getDownNext()).isEqualTo("하행 서울역"); |
| 76 | + assertThat(transitDetail.getUpNext()).isNull(); |
| 77 | + assertThat(transitDetail.getStationPath()).containsExactly("시청", "서울역"); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + @DisplayName("지하철 상행 경로 탐색 시 upNext 필드에 상행 방향이 세팅된다.") |
| 82 | + void searchRoute_Subway_Upbound_Success() { |
| 83 | + // given: 상행 경로 조건 세팅 |
| 84 | + RouteSaveReqDto.CreateRouteDTO request = new RouteSaveReqDto.CreateRouteDTO( |
| 85 | + BigDecimal.valueOf(37.1), BigDecimal.valueOf(127.1), |
| 86 | + BigDecimal.valueOf(37.2), BigDecimal.valueOf(127.2), |
| 87 | + null, null, TransitType.SUBWAY, null |
| 88 | + ); |
| 89 | + |
| 90 | + GoogleDirectionApiResponse googleRes = createMockGoogleResponse("1호선", "서울역", "시청"); |
| 91 | + given(googleDirectionApiClient.getDirections(any(DirectionRequestDTO.class))) |
| 92 | + .willReturn(googleRes); |
| 93 | + |
| 94 | + // 상행 조건: 출발역의 prevStations 리스트에 도착역 포함 |
| 95 | + SubwayStationResDTO startStation = new SubwayStationResDTO(); |
| 96 | + ReflectionTestUtils.setField(startStation, "stationName", "서울역"); |
| 97 | + ReflectionTestUtils.setField(startStation, "nextStations", List.of("남영")); |
| 98 | + ReflectionTestUtils.setField(startStation, "prevStations", List.of("시청")); |
| 99 | + |
| 100 | + SubwayStationResDTO secondStation = new SubwayStationResDTO(); |
| 101 | + ReflectionTestUtils.setField(secondStation, "stationName", "시청"); |
| 102 | + |
| 103 | + given(subwayNetworkService.getStationsBetween(anyString(), anyString(), anyString())) |
| 104 | + .willReturn(List.of(startStation, secondStation)); |
| 105 | + |
| 106 | + // when: 서비스 메서드 실행 |
| 107 | + RouteListResDTO result = routeCommandService.searchRoute(request); |
| 108 | + |
| 109 | + // then: 결과 검증 |
| 110 | + TransitRouteDetailInfoResDTO transitDetail = result.getRouteApiResDtoList().getFirst() |
| 111 | + .getRouteDetailInfoResDTOList().getFirst().getTransitDetail(); |
| 112 | + |
| 113 | + assertThat(transitDetail.getUpNext()).isEqualTo("상행 시청"); |
| 114 | + assertThat(transitDetail.getDownNext()).isNull(); |
| 115 | + } |
| 116 | + |
| 117 | + private GoogleDirectionApiResponse createMockGoogleResponse(String lineName, String startStop, String endStop) { |
| 118 | + GoogleDirectionApiResponse.EncodedLine line = new GoogleDirectionApiResponse.EncodedLine(); |
| 119 | + ReflectionTestUtils.setField(line, "name", lineName); |
| 120 | + |
| 121 | + GoogleDirectionApiResponse.Vehicle vehicle = new GoogleDirectionApiResponse.Vehicle(); |
| 122 | + ReflectionTestUtils.setField(vehicle, "type", "SUBWAY"); |
| 123 | + ReflectionTestUtils.setField(line, "vehicle", vehicle); |
| 124 | + |
| 125 | + GoogleDirectionApiResponse.TransitDetails transitDetails = new GoogleDirectionApiResponse.TransitDetails(); |
| 126 | + ReflectionTestUtils.setField(transitDetails, "encodedLine", line); |
| 127 | + |
| 128 | + GoogleDirectionApiResponse.Step step = new GoogleDirectionApiResponse.Step(); |
| 129 | + ReflectionTestUtils.setField(step, "travelMode", "TRANSIT"); |
| 130 | + ReflectionTestUtils.setField(step, "transitDetails", transitDetails); |
| 131 | + |
| 132 | + GoogleDirectionApiResponse.Leg leg = new GoogleDirectionApiResponse.Leg(); |
| 133 | + ReflectionTestUtils.setField(leg, "steps", List.of(step)); |
| 134 | + |
| 135 | + GoogleDirectionApiResponse.Route route = new GoogleDirectionApiResponse.Route(); |
| 136 | + ReflectionTestUtils.setField(route, "legs", List.of(leg)); |
| 137 | + |
| 138 | + GoogleDirectionApiResponse response = new GoogleDirectionApiResponse(); |
| 139 | + ReflectionTestUtils.setField(response, "routes", List.of(route)); |
| 140 | + |
| 141 | + GoogleDirectionApiResponse.DepartureStop departureStop = new GoogleDirectionApiResponse.DepartureStop(); |
| 142 | + ReflectionTestUtils.setField(departureStop, "encodedName", startStop); |
| 143 | + ReflectionTestUtils.setField(transitDetails, "departureStop", departureStop); |
| 144 | + |
| 145 | + GoogleDirectionApiResponse.ArrivalStop arrivalStop = new GoogleDirectionApiResponse.ArrivalStop(); |
| 146 | + ReflectionTestUtils.setField(arrivalStop, "encodedName", endStop); |
| 147 | + ReflectionTestUtils.setField(transitDetails, "arrivalStop", arrivalStop); |
| 148 | + |
| 149 | + return response; |
| 150 | + } |
| 151 | +} |
0 commit comments