Skip to content

Commit

Permalink
[FEAT] 버스 노선, 정류장, 노선정류장 서비스 테스트 추가 (#50)
Browse files Browse the repository at this point in the history
- BusRouteServiceTest 작성
- BusStationServiceTest 작성
- BusRouteStationServiceTest 작성
- 테스트를 위한 entity, dto factory : BusFactory 추가

---------

Co-authored-by: Photogrammer <[email protected]>
  • Loading branch information
Gyaak and JuneParkCode authored Aug 8, 2024
1 parent 0f67b83 commit fd089c2
Show file tree
Hide file tree
Showing 5 changed files with 517 additions and 82 deletions.
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
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,
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

0 comments on commit fd089c2

Please sign in to comment.