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

[FEAT] 버스 노선, 정류장, 노선정류장 서비스 테스트 추가 #50

Merged
merged 10 commits into from
Aug 8, 2024
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.talkka.server.bus.dto;

import com.talkka.server.bus.dao.BusStationEntity;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -20,7 +18,6 @@ public class BusRouteStationCreateDto {

private String apiRouteId;
private String apiStationId;
private BusStationEntity station;
private Short stationSeq;
private String stationName;
}
169 changes: 169 additions & 0 deletions server/src/test/java/com/talkka/server/bus/service/BusFactory.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 팩토리는 테스트에서 사용할 객체 생성 factory 두신 것이라고 이해하면 될까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 맞습니다.
BusRouteService, BusStationService, BusRouteStationService가 사용하는 entity와 dto의 일관성을 위해 분리했습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package com.talkka.server.bus.service;

import java.math.BigDecimal;

import com.talkka.server.bus.dao.BusRouteEntity;
import com.talkka.server.bus.dao.BusRouteStationEntity;
import com.talkka.server.bus.dao.BusStationEntity;
import com.talkka.server.bus.dto.BusRouteCreateDto;
import com.talkka.server.bus.dto.BusRouteRespDto;
import com.talkka.server.bus.dto.BusRouteStationCreateDto;
import com.talkka.server.bus.dto.BusRouteStationRespDto;
import com.talkka.server.bus.dto.BusStationCreateDto;
import com.talkka.server.bus.dto.BusStationRespDto;
import com.talkka.server.bus.enums.BusRouteType;
import com.talkka.server.bus.enums.CenterStation;
import com.talkka.server.bus.enums.DistrictCode;
import com.talkka.server.bus.enums.TurnStation;

public class BusFactory {

protected static BusRouteEntity getBusRouteEntity(Long id) {
return BusRouteEntity.builder()
.id(id)
.apiRouteId("BRT" + id)
.routeName("7800" + id)
.routeTypeCd(BusRouteType.DIRECT_SEAT_CITY_BUS)
.routeTypeName(BusRouteType.DIRECT_SEAT_CITY_BUS.getName())
.companyId("COMP123")
.companyName("수형운수")
.companyTel("02-123-4567")
.districtCd(DistrictCode.DONGDUCHEON)
.upFirstTime("05:30")
.upLastTime("23:00")
.downFirstTime("06:00")
.downLastTime("00:35")
.startMobileNo("101")
.startStationId(1001L)
.startStationName("기점 정류소")
.endStationId(2002L)
.endMobileNo("202")
.endStationName("종점 정류소")
.regionName("서울")
.peekAlloc(15)
.nPeekAlloc(25)
.build();
}

protected static BusRouteCreateDto getBusRouteCreateDto(Long id) {

return BusRouteCreateDto.builder()
.apiRouteId("BRT" + id)
.routeName("7800")
.routeTypeCd(BusRouteType.DIRECT_SEAT_CITY_BUS)
.routeTypeName(BusRouteType.DIRECT_SEAT_CITY_BUS.getName())
.companyId("COMP123")
.companyName("수형운수")
.companyTel("02-123-4567")
.districtCd(DistrictCode.DONGDUCHEON)
.upFirstTime("05:30")
.upLastTime("23:00")
.downFirstTime("06:00")
.downLastTime("00:35")
.startMobileNo("101")
.startStationId(1001L)
.startStationName("기점 정류소")
.endStationId(2002L)
.endMobileNo("202")
.endStationName("종점 정류소")
.regionName("서울")
.peekAlloc(15)
.nPeekAlloc(25)
.build();
}

protected static BusRouteRespDto getBusRouteRespDto(Long id) {
return BusRouteRespDto.builder()
.routeId(id)
.routeName("7800" + id)
.routeTypeCd(BusRouteType.DIRECT_SEAT_CITY_BUS)
.routeTypeName(BusRouteType.DIRECT_SEAT_CITY_BUS.getName())
.districtCd(DistrictCode.DONGDUCHEON)
.upFirstTime("05:30")
.upLastTime("23:00")
.downFirstTime("06:00")
.downLastTime("00:35")
.startMobileNo("101")
.startStationId(1001L)
.startStationName("기점 정류소")
.endStationId(2002L)
.endMobileNo("202")
.endStationName("종점 정류소")
.regionName("서울")
.peekAlloc(15)
.nPeekAlloc(25)
.build();
}

protected static BusStationEntity getBusStationEntity(Long id) {
return BusStationEntity.builder()
.id(id)
.apiStationId("BST" + id)
.stationName("정거장" + id)
.regionName("서울")
.districtCd(DistrictCode.DONGDUCHEON)
.centerYn(CenterStation.CENTER_STATION)
.turnYn(TurnStation.TURN_STATION)
.longitude(BigDecimal.valueOf(127.123456))
.latitude(BigDecimal.valueOf(37.123456))
.build();
}

protected static BusStationCreateDto getBusStationCreateDto(Long id) {
return BusStationCreateDto.builder()
.apiStationId("BST" + id)
.stationName("정거장" + id)
.regionName("서울")
.districtCd(DistrictCode.DONGDUCHEON)
.centerYn(CenterStation.CENTER_STATION)
.turnYn(TurnStation.TURN_STATION)
.longitude(BigDecimal.valueOf(127.123456))
.latitude(BigDecimal.valueOf(37.123456))
.build();
}

protected static BusStationRespDto getBusStationRespDto(Long id) {
return BusStationRespDto.builder()
.stationId(id)
.stationName("정거장" + id)
.regionName("서울")
.districtCd(DistrictCode.DONGDUCHEON)
.centerYn(CenterStation.CENTER_STATION)
.turnYn(TurnStation.TURN_STATION)
.longitude(BigDecimal.valueOf(127.123456))
.latitude(BigDecimal.valueOf(37.123456))
.build();
}

protected static BusRouteStationEntity getBusRouteStationEntity(Long id, BusRouteEntity routeEntity,
BusStationEntity stationEntity) {
return BusRouteStationEntity.builder()
.id(id)
.route(routeEntity)
.station(stationEntity)
.stationName("정거장" + id)
.stationSeq(Short.valueOf(String.valueOf(id)))
.build();
}

protected static BusRouteStationCreateDto getBusRouteStationCreateDto(Long id) {
return BusRouteStationCreateDto.builder()
.apiRouteId("BRT" + id)
.apiStationId("BST" + id)
.stationName("정거장" + id)
.stationSeq(Short.valueOf(String.valueOf(id)))
.build();
}

protected static BusRouteStationRespDto getBusRouteStationRespDto(Long id, BusRouteRespDto routeRespDto,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protected 로 하게된 배경이 궁금합니다

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bus관련 테스트에서만 사용하는 객체라고 생각해서 protected로 설정했습니다.

BusStationRespDto stationRespDto) {
return BusRouteStationRespDto.builder()
.busRouteStationId(id)
.route(routeRespDto)
.station(stationRespDto)
.stationName("정거장" + id)
.stationSeq(Short.valueOf(String.valueOf(id)))
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.talkka.server.bus.service;

import static org.assertj.core.api.AssertionsForClassTypes.*;
import static com.talkka.server.bus.service.BusFactory.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -15,8 +19,6 @@
import com.talkka.server.bus.dao.BusRouteRepository;
import com.talkka.server.bus.dto.BusRouteCreateDto;
import com.talkka.server.bus.dto.BusRouteRespDto;
import com.talkka.server.bus.enums.BusRouteType;
import com.talkka.server.bus.enums.DistrictCode;
import com.talkka.server.common.exception.http.BadRequestException;

@ExtendWith(MockitoExtension.class)
Expand All @@ -28,88 +30,15 @@ public class BusRouteServiceTest {
@Mock
private BusRouteRepository busRouteRepository;

private BusRouteCreateDto getBusRouteReqDto(String id) {

return BusRouteCreateDto.builder()
.apiRouteId(id)
.routeName("7800" + id)
.routeTypeCd(BusRouteType.DIRECT_SEAT_CITY_BUS)
.routeTypeName(BusRouteType.DIRECT_SEAT_CITY_BUS.getName())
.companyId("COMP123")
.companyName("수형운수")
.companyTel("02-123-4567")
.districtCd(DistrictCode.DONGDUCHEON)
.upFirstTime("05:30")
.upLastTime("23:00")
.downFirstTime("06:00")
.downLastTime("00:35")
.startMobileNo("101")
.startStationId(1001L)
.startStationName("기점 정류소")
.endStationId(2002L)
.endMobileNo("202")
.endStationName("종점 정류소")
.regionName("서울")
.peekAlloc(15)
.nPeekAlloc(25)
.build();
}

private BusRouteRespDto getBusRouteRespDto(Long id) {
return BusRouteRespDto.builder()
.routeId(id)
.routeName("7800" + id)
.routeTypeCd(BusRouteType.DIRECT_SEAT_CITY_BUS)
.routeTypeName(BusRouteType.DIRECT_SEAT_CITY_BUS.getName())
.districtCd(DistrictCode.DONGDUCHEON)
.upFirstTime("05:30")
.upLastTime("23:00")
.downFirstTime("06:00")
.downLastTime("00:35")
.startMobileNo("101")
.startStationId(1001L)
.startStationName("기점 정류소")
.endStationId(2002L)
.endMobileNo("202")
.endStationName("종점 정류소")
.regionName("서울")
.peekAlloc(15)
.nPeekAlloc(25)
.build();
}

@Nested
@DisplayName("createBusRoute method")
public class CreateBusRouteTest {
@Test
void 버스_노선을_생성한다() {
// given
BusRouteCreateDto busRouteCreateDto = getBusRouteReqDto("1");
BusRouteCreateDto busRouteCreateDto = getBusRouteCreateDto(1L);
BusRouteRespDto busRouteRespDto = getBusRouteRespDto(1L);
BusRouteEntity busRouteEntity = BusRouteEntity.builder()
.id(1L)
.apiRouteId(busRouteCreateDto.getApiRouteId())
.routeName(busRouteCreateDto.getRouteName())
.routeTypeCd(BusRouteType.DIRECT_SEAT_CITY_BUS)
.routeTypeName(BusRouteType.DIRECT_SEAT_CITY_BUS.getName())
.companyId("COMP123")
.companyName("수형운수")
.companyTel("02-123-4567")
.districtCd(DistrictCode.DONGDUCHEON)
.upFirstTime("05:30")
.upLastTime("23:00")
.downFirstTime("06:00")
.downLastTime("00:35")
.startMobileNo("101")
.startStationId(1001L)
.startStationName("기점 정류소")
.endStationId(2002L)
.endMobileNo("202")
.endStationName("종점 정류소")
.regionName("서울")
.peekAlloc(15)
.nPeekAlloc(25)
.build();
BusRouteEntity busRouteEntity = getBusRouteEntity(1L);
given(busRouteRepository.save(any(BusRouteEntity.class))).willReturn(busRouteEntity);
given(busRouteRepository.existsByApiRouteId(any(String.class))).willReturn(true);
// when
Expand All @@ -121,7 +50,7 @@ public class CreateBusRouteTest {
@Test
void 존재하는_버스_노선일_경우_Exception을_발생시킨다() {
// given
BusRouteCreateDto reqDto = getBusRouteReqDto("1");
BusRouteCreateDto reqDto = getBusRouteCreateDto(1L);
given(busRouteRepository.existsByApiRouteId(any(String.class))).willReturn(false);
// when
// then
Expand All @@ -131,4 +60,58 @@ public class CreateBusRouteTest {
.hasMessage("이미 등록된 버스 노선입니다.");
}
}

@Nested
@DisplayName("findByRouteId method")
public class FindByRouteIdTest {
@Test
void ID를_기반으로_버스_노선을_요청하면_레포지토리를_통해_결과를_DTO로_반환한다() {
// given
Long routeId = 1L;
BusRouteEntity foundEntity = getBusRouteEntity(routeId);
given(busRouteRepository.findById(anyLong())).willReturn(Optional.of(foundEntity));
// when
var BusRouteRespDto = busRouteService.findByRouteId(routeId);
// then
verify(busRouteRepository).findById(anyLong());
assertThat(BusRouteRespDto).isEqualTo(getBusRouteRespDto(routeId));
}

@Test
void ID가_존재하지_않으면_Exception을_throw한다() {
// given
Class<?> exceptionClass = BadRequestException.class;
given(busRouteRepository.findById(anyLong())).willReturn(Optional.empty());
// when
// then
assertThatThrownBy(
() -> busRouteService.findByRouteId(1L)
).isInstanceOf(exceptionClass)
.hasMessage("존재하지 않는 노선입니다.");
verify(busRouteRepository, times(1)).findById(anyLong());
}
}

@Nested
@DisplayName("findByRouteName method")
public class FindByRouteNameTest {

@Test
void 버스노선이름을_요청으로_받아_repository에서_조회하고_해당_이름으로_시작하는_노선들을_리스트로_반환한다() {

// given
String routeName = "7800";
var entityList = List.of(getBusRouteEntity(1L), getBusRouteEntity(2L));
var expectedList = List.of(getBusRouteRespDto(1L), getBusRouteRespDto(2L));
given(busRouteRepository.findByRouteNameLikeOrderByRouteNameAsc(any(String.class))).willReturn(entityList);

// when
var resultList = busRouteService.findByRouteName(routeName);

// then
verify(busRouteRepository, times(1)).findByRouteNameLikeOrderByRouteNameAsc(anyString());
assertThat(resultList).containsAll(expectedList);

}
}
}
Loading
Loading