-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- ControllerTestSupport 수정 - AdminVehicleServiceImplTest 수정 #42
- Loading branch information
1 parent
e92bb82
commit 8b2bde8
Showing
5 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/kea/enter/enterbe/api/vehicle/controller/AdminVehicleControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package kea.enter.enterbe.api.vehicle.controller; | ||
|
||
import static org.mockito.BDDMockito.given; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.nio.charset.StandardCharsets; | ||
import kea.enter.enterbe.ControllerTestSupport; | ||
import kea.enter.enterbe.api.vehicle.controller.dto.request.AdminVehicleRequest; | ||
import kea.enter.enterbe.domain.vehicle.entity.VehicleFuel; | ||
import kea.enter.enterbe.domain.vehicle.entity.VehicleState; | ||
import org.apache.hc.core5.http.ContentType; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.mock.web.MockMultipartFile; | ||
|
||
class AdminVehicleControllerTest extends ControllerTestSupport { | ||
@DisplayName(value = "법인 차량 추가 Ctrl") | ||
@Test | ||
public void createVehicle() throws Exception { | ||
// given | ||
String vehicleNo = "12가3456"; | ||
String company = "현대"; | ||
String model = "그랜저"; | ||
int seats = 5; | ||
VehicleFuel fuel = VehicleFuel.GASOLINE; | ||
MockMultipartFile image = new MockMultipartFile("image", "test.jpg", | ||
ContentType.IMAGE_JPEG.getMimeType(), "test file".getBytes(StandardCharsets.UTF_8)); | ||
VehicleState state = VehicleState.AVAILABLE; | ||
|
||
AdminVehicleRequest request = new AdminVehicleRequest(vehicleNo, company, model, seats, fuel, state); | ||
|
||
MockMultipartFile data = new MockMultipartFile("data", "data.json", | ||
MediaType.APPLICATION_JSON_VALUE, objectMapper.writeValueAsString(request).getBytes(StandardCharsets.UTF_8)); | ||
|
||
given(fileUtil.isImageFile(image)).willReturn(true); | ||
|
||
// when | ||
mockMvc.perform( | ||
multipart("/admin/vehicles") | ||
.file(image) | ||
.file(data) | ||
).andExpect(status().isOk()); | ||
|
||
// then | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/test/java/kea/enter/enterbe/api/vehicle/service/AdminVehicleServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package kea.enter.enterbe.api.vehicle.service; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import kea.enter.enterbe.IntegrationTestSupport; | ||
import kea.enter.enterbe.api.vehicle.service.dto.CreateVehicleDto; | ||
import kea.enter.enterbe.domain.vehicle.entity.Vehicle; | ||
import kea.enter.enterbe.domain.vehicle.entity.VehicleFuel; | ||
import kea.enter.enterbe.domain.vehicle.entity.VehicleState; | ||
import kea.enter.enterbe.global.common.exception.CustomException; | ||
import kea.enter.enterbe.global.common.exception.ResponseCode; | ||
import kea.enter.enterbe.global.util.ObjectStorageUtil; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Slf4j | ||
class AdminVehicleServiceImplTest extends IntegrationTestSupport { | ||
|
||
@DisplayName(value = "법인 차량 추가 Serv (성공)") | ||
@Test | ||
public void createVehicleSuccess() { | ||
// given | ||
CreateVehicleDto dto = CreateVehicleDto.of("12가3456", "현대", "그랜저", | ||
5, VehicleFuel.GASOLINE, mock(MultipartFile.class), VehicleState.AVAILABLE); | ||
given(objectStorageUtil.uploadFileToS3(mock(MultipartFile.class))).willReturn("test.jpg"); | ||
// // 중복 아님 확인 | ||
// given(vehicleRepository.findByVehicleNoAndStateNot(dto.getVehicleNo(), VehicleState.INACTIVE)) | ||
// .willReturn(null); | ||
// | ||
// given(vehicleRepository.save(any(Vehicle.class))) | ||
// .willReturn(vehicle); | ||
|
||
// when | ||
adminVehicleService.createVehicle(dto); | ||
|
||
// then | ||
Vehicle vehicle = vehicleRepository.findByVehicleNoAndState(dto.getVehicleNo(), dto.getState()); | ||
// given(vehicleRepository.findByVehicleNoAndState(dto.getVehicleNo(), dto.getState())) | ||
// .willReturn(vehicle); | ||
|
||
// Vehicle findVehicle = vehicleRepository.findByVehicleNoAndState(dto.getVehicleNo(), dto.getState()); | ||
|
||
assertThat(vehicle.getVehicleNo()).isEqualTo(dto.getVehicleNo()); | ||
assertThat(vehicle.getState()).isEqualTo(dto.getState()); | ||
} | ||
|
||
@DisplayName(value = "법인 차량 추가 Serv (차량번호 형식 오류)") | ||
@Test | ||
public void createVehicleWithInvalidVehicleNo() { | ||
// given | ||
CreateVehicleDto dto = CreateVehicleDto.of("12가34567", "현대", "그랜저", | ||
5, VehicleFuel.GASOLINE, mock(MultipartFile.class), VehicleState.AVAILABLE); | ||
|
||
// when & then | ||
assertThatThrownBy(() -> adminVehicleService.createVehicle(dto)) | ||
.isInstanceOf(CustomException.class) | ||
.extracting("responseCode") | ||
.isEqualTo(ResponseCode.VEHICLE_NO_NOT_ALLOWED); | ||
} | ||
} |