Skip to content

Commit

Permalink
[REFACTOR] #419: Model 리팩토링 관련 Controller 테스트 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
tank3a committed Aug 23, 2023
1 parent 4f1c65c commit 1f9921f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 83 deletions.
19 changes: 13 additions & 6 deletions backend/src/test/java/autoever2/cartag/cars/CarControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,23 @@ void getTrimList() throws Exception {
//given
int carType = 1;
List<TrimDefaultOptionDto> trimDefaultOptionDtoList = new ArrayList<>();
trimDefaultOptionDtoList.add(TrimDefaultOptionDto.builder().optionName("안전 하차 보조").optionImage("image_1").optionDescription("좋은 보조 장치").OptionUsedCount(42).build());
trimDefaultOptionDtoList.add(TrimDefaultOptionDto.builder().optionName("후측방 충둘 경고").optionImage("image_2").optionDescription("좋은 보조 장치").OptionUsedCount(98).build());
trimDefaultOptionDtoList.add(TrimDefaultOptionDto.builder().optionName("후방 교차 충돌 보조 장치").optionImage("image_3").optionDescription("좋은 보조 장치").OptionUsedCount(41).build());
CarVo expected1 = CarVo.builder().carId(1).trim("Le Blanc").carDefaultPrice(123423).outerImage("image_1").innerImage("image_2").wheelImage("image_3").carDescription("Good").options(trimDefaultOptionDtoList).build();
CarVo expected2 = CarVo.builder().carId(2).trim("Exclusive").carDefaultPrice(123423).outerImage("image_a").innerImage("image_2").carDescription("Good").options(trimDefaultOptionDtoList).build();
trimDefaultOptionDtoList.add(TrimDefaultOptionDto.builder()
.optionName("안전 하차 보조").optionImage("image_1").optionDescription("좋은 보조 장치").OptionUsedCount(42).build());
trimDefaultOptionDtoList.add(TrimDefaultOptionDto.builder()
.optionName("후측방 충둘 경고").optionImage("image_2").optionDescription("좋은 보조 장치").OptionUsedCount(98).build());
trimDefaultOptionDtoList.add(TrimDefaultOptionDto.builder()
.optionName("후방 교차 충돌 보조 장치").optionImage("image_3").optionDescription("좋은 보조 장치").OptionUsedCount(41).build());

CarVo expected1 = CarVo.builder()
.carId(1).trim("Le Blanc").carDefaultPrice(123423).outerImage("image_1").innerImage("image_2").wheelImage("image_3").carDescription("Good").options(trimDefaultOptionDtoList).build();
CarVo expected2 = CarVo.builder()
.carId(2).trim("Exclusive").carDefaultPrice(123423).outerImage("image_a").innerImage("image_2").carDescription("Good").options(trimDefaultOptionDtoList).build();

given(service.getCarDtoByCarType(carType)).willReturn(List.of(expected1, expected2));

//when
ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/api/cars/types").param("cartype", String.valueOf(carType)));
ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/api/cars/types")
.param("cartype", String.valueOf(carType)));

//then
resultActions.andExpect(status().isOk())
Expand Down
113 changes: 36 additions & 77 deletions backend/src/test/java/autoever2/cartag/models/ModelControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import autoever2.cartag.models.dto.ModelDetailMappedDto;
import autoever2.cartag.models.dto.ModelEfficiencyDataDto;
import autoever2.cartag.models.dto.ModelShortDataDto;
import autoever2.cartag.models.ModelController;
import autoever2.cartag.models.ModelService;
import autoever2.cartag.models.dto.PowerTrainDataDto;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -22,6 +21,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(ModelController.class)
@DisplayName("UnitTest: ModelController")
class ModelControllerTest {

@Autowired
Expand All @@ -31,94 +31,56 @@ class ModelControllerTest {
private ModelService modelService;

@Test
@DisplayName("트림의 모델 타입 데이터 호출 API")
@DisplayName("트림의 모델타입 리스트 조회 API")
void getTrimModel() throws Exception {
List<ModelShortDataDto> trimModelList = new ArrayList<>();
//디젤 2.2 데이터
trimModelList.add(ModelShortDataDto.builder()
.modelId(1)
.modelName("디젤 2.2")
.modelPrice(0L)
.modelTypeName("파워트레인")
.percentage(65)
.build());

//가솔린 3.8 데이터
trimModelList.add(ModelShortDataDto.builder()
.modelId(2)
.modelName("가솔린 3.8")
.modelPrice(280000L)
.modelTypeName("파워트레인")
.percentage(35)
.build());

//7인승 데이터
trimModelList.add(ModelShortDataDto.builder()
.modelId(3)
.modelName("7인승")
.modelPrice(0L)
.modelTypeName("바디타입")
.percentage(70)
.build());

//8인승 데이터
trimModelList.add(ModelShortDataDto.builder()
.modelId(4)
.modelName("8인승")
.modelPrice(130000L)
.modelTypeName("바디타입")
.percentage(30)
.build());

//2WD 데이터
trimModelList.add(ModelShortDataDto.builder()
.modelId(5)
.modelName("2WD")
.modelPrice(0L)
.modelTypeName("구동방식")
.percentage(50)
.build());

//4WD 데이터
trimModelList.add(ModelShortDataDto.builder()
.modelId(6)
.modelName("4WD")
.modelPrice(237000L)
.modelTypeName("구동방식")
.percentage(50)
.build());

//given
List<ModelShortDataDto> modelTypeData = new ArrayList<>();

PowerTrainDataDto expectedHmg1 = PowerTrainDataDto.builder().maxPs("202/3800").maxKgfm("45.0/1750~2750").ratioPs(1.0).ratioKgfm(1.0).build();
modelTypeData.add(ModelShortDataDto.builder()
.modelId(1).modelName("디젤 2.2").modelPrice(1480000L).modelTypeName("파워트레인").percentage(40).modelImage("/model/diesel2-2.jpg").hmgData(expectedHmg1).build());

PowerTrainDataDto expectedHmg2 = PowerTrainDataDto.builder()
.maxPs("295/6000").maxKgfm("36.2/5200").ratioPs(0.9249174917491748).ratioKgfm(0.34807692307692306).build();
modelTypeData.add(ModelShortDataDto.builder()
.modelId(2).modelName("가솔린 3.8").modelPrice(280000L).modelTypeName("파워트레인").percentage(60).modelImage("/model/gasoline3-8.jpg").hmgData(expectedHmg2).build());

modelTypeData.add(ModelShortDataDto.builder()
.modelId(3).modelName("2WD").modelPrice(0L).modelTypeName("구동방식").percentage(25).modelImage("/model/2wd.png").build());
modelTypeData.add(ModelShortDataDto.builder()
.modelId(4).modelName("4WD").modelPrice(237000L).modelTypeName("구동방식").percentage(75).modelImage("/model/4wd.png").build());
modelTypeData.add(ModelShortDataDto.builder()
.modelId(5).modelName("7인승").modelPrice(0L).modelTypeName("바디타입").percentage(50).modelImage("/model/7seats.jpg").build());
modelTypeData.add(ModelShortDataDto.builder()
.modelId(6).modelName("8인승").modelPrice(0L).modelTypeName("바디타입").percentage(50).modelImage("/model/8seats.jpg").build());

int carId = 1;
given(modelService.getModelTypeData(carId)).willReturn(trimModelList);
given(modelService.getModelTypeData(carId)).willReturn(modelTypeData);

//when
ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/api/modeltypes/list").param("carid", String.valueOf(carId)));
ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/api/modeltypes/list")
.param("carid", String.valueOf(carId)));

//then
resultActions.andExpect(status().isOk())
.andExpect(jsonPath("$[0].modelId").value(1))
.andExpect(jsonPath("$[1].modelName").value("가솔린 3.8"))
.andExpect(jsonPath("$[3].modelPrice").value(130000))
.andExpect(jsonPath("$[2].modelImage").value("/model/2wd.png"))
.andExpect(jsonPath("$[3].modelPrice").value(237000))
.andExpect(jsonPath("$[4].percentage").value(50))
.andExpect(jsonPath("$[5].modelTypeName").value("구동방식"));
.andExpect(jsonPath("$[5].modelTypeName").value("바디타입"));
}

@Test
@DisplayName("모델의 상세 데이터 호출 API")
@DisplayName("모델의 상세 데이터 조회 API")
void getModelDetail() throws Exception {
int modelId = 1;

ModelDetailMappedDto model = ModelDetailMappedDto.builder()
.modelTypeName("파워트레인")
.modelName("디젤2.2")
.optionDescription("높은 토크로 파워풀한 드라이빙이 가능하며, 차급대비 연비 효율이 우수합니다")
.modelImage("/model/diesel2-2.jpg")
.build();
.modelTypeName("파워트레인").modelName("디젤2.2").optionDescription("높은 토크로 파워풀한 드라이빙이 가능하며, 차급대비 연비 효율이 우수합니다").modelImage("/model/diesel2-2.jpg").build();


given(modelService.getModelDetail(modelId)).willReturn(model);

ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/api/modeltypes/detail").param("modelid", String.valueOf(modelId)));
ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/api/modeltypes/detail")
.param("modelid", String.valueOf(modelId)));

resultActions.andExpect(status().isOk())
.andExpect(jsonPath("$.modelName").value("디젤2.2"))
Expand All @@ -128,15 +90,12 @@ void getModelDetail() throws Exception {
}

@Test
@DisplayName("연비와 cc HMG 데이터 호출 API")
@DisplayName("효율 HMG 데이터 조회 API")
void getEfficiencyData() throws Exception {
int powerTrainId = 1;
int operationId = 3;

ModelEfficiencyDataDto data = ModelEfficiencyDataDto.builder()
.averageFuel("12.16km/s")
.displacement("2,199cc")
.build();
ModelEfficiencyDataDto data = ModelEfficiencyDataDto.builder().averageFuel("12.16km/s").displacement("2,199cc").build();

given(modelService.getEfficiencyData(powerTrainId, operationId)).willReturn(data);

Expand Down

0 comments on commit 1f9921f

Please sign in to comment.