From 2d8c4ce361dd672fbd8a8fab44e37d2cd17575cd Mon Sep 17 00:00:00 2001 From: dangnak2 <80161984+dangnak2@users.noreply.github.com> Date: Sun, 26 Nov 2023 19:11:09 +0900 Subject: [PATCH] =?UTF-8?q?[PDS-202]=20test:=20=EB=B9=84=ED=92=88=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=EC=88=98=EC=A0=95=20API=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/request/UpdateEquipmentReq.java | 2 + .../service/EquipmentServiceTest.java | 63 ++++++++++++++----- .../service/model/TestEquipmentInfo.java | 37 +++++++++++ 3 files changed, 86 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/example/pladialmserver/equipment/dto/request/UpdateEquipmentReq.java b/src/main/java/com/example/pladialmserver/equipment/dto/request/UpdateEquipmentReq.java index 1984c97a..83fabfab 100644 --- a/src/main/java/com/example/pladialmserver/equipment/dto/request/UpdateEquipmentReq.java +++ b/src/main/java/com/example/pladialmserver/equipment/dto/request/UpdateEquipmentReq.java @@ -1,6 +1,7 @@ package com.example.pladialmserver.equipment.dto.request; import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Builder; import lombok.Getter; import javax.validation.constraints.NotBlank; @@ -8,6 +9,7 @@ import javax.validation.constraints.NotNull; @Getter +@Builder public class UpdateEquipmentReq { @Schema(type = "String", description = "이름", example = "맥심커피", required = true) @NotBlank(message = "E0003") diff --git a/src/test/java/com/example/pladialmserver/equipment/service/EquipmentServiceTest.java b/src/test/java/com/example/pladialmserver/equipment/service/EquipmentServiceTest.java index 85bac53d..32c28625 100644 --- a/src/test/java/com/example/pladialmserver/equipment/service/EquipmentServiceTest.java +++ b/src/test/java/com/example/pladialmserver/equipment/service/EquipmentServiceTest.java @@ -1,14 +1,16 @@ package com.example.pladialmserver.equipment.service; import com.example.pladialmserver.equipment.dto.request.RegisterEquipmentReq; +import com.example.pladialmserver.equipment.dto.request.UpdateEquipmentReq; +import com.example.pladialmserver.equipment.entity.Equipment; +import com.example.pladialmserver.equipment.entity.EquipmentCategory; import com.example.pladialmserver.equipment.repository.EquipmentCategoryRepository; import com.example.pladialmserver.equipment.repository.EquipmentRepository; -import com.example.pladialmserver.global.utils.JwtUtil; +import com.example.pladialmserver.global.exception.BaseException; +import com.example.pladialmserver.global.exception.BaseResponseCode; import com.example.pladialmserver.user.entity.Role; import com.example.pladialmserver.user.entity.User; import com.example.pladialmserver.user.repository.user.UserRepository; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -20,13 +22,15 @@ import java.util.Optional; -import static com.example.pladialmserver.equipment.service.model.TestEquipmentInfo.setUpRegisterEquipmentInfo; +import static com.example.pladialmserver.equipment.service.model.TestEquipmentInfo.*; import static com.example.pladialmserver.user.service.model.TestUserInfo.*; import static com.example.pladialmserver.user.service.model.TestUserInfo.PASSWORD; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) -public class EquipmentServiceTest { +class EquipmentServiceTest { @Spy @InjectMocks @@ -34,39 +38,66 @@ public class EquipmentServiceTest { @Mock private EquipmentRepository equipmentRepository; @Mock + private UserRepository userRepository; + @Mock private EquipmentCategoryRepository equipmentCategoryRepository; @Mock BCryptPasswordEncoder passwordEncoder; - @BeforeEach - void setUp() { - } - - @AfterEach - void tearDown() { - } - @Test @DisplayName("[성공] 구매 비품 추가") - void registerEquipmentSuccess() { + void registerEquipment_SUCCESS() { // given User user = setUpUser(1L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD)); RegisterEquipmentReq req = setUpRegisterEquipmentInfo("맥심커피", "10박스", "기타", "S1305", "맥심커피입니다.", "photo/maxim.png"); + EquipmentCategory category = setUpEquipmentCategory(req.getCategory()); // when + doReturn(category).when(equipmentCategoryRepository).findByName(req.getCategory()); equipmentService.registerEquipment(req, user); + // then + assertThat(req.getCategory()).isEqualTo(category.getName()); + // verify + verify(equipmentCategoryRepository, times(1)).findByName(any(String.class)); verify(equipmentService, times(1)).registerEquipment(any(RegisterEquipmentReq.class), any(User.class)); verify(passwordEncoder, times(1)).encode(any(String.class)); } @Test - void searchEquipment() { + @DisplayName("[성공] 비품 정보 수정") + void updateEquipment_SUCCESS() { + // given + Long equipmentId = 1L; + User user = setUpUser(1L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD)); + UpdateEquipmentReq req = setUpUpdateEquipmentReq("비품2", "10개", 1L, 2L, "S1350", "비품2입니다.", "기타", "photo/maxim.png"); + EquipmentCategory category = setUpEquipmentCategory(req.getCategory()); + Equipment equipment = setUpEquipment("비품1", "10개", "photo/maxim.png", "S1350", "비품1입니다.", category, user); + + // when + doReturn(Optional.of(equipment)).when(equipmentRepository).findByEquipmentIdAndIsEnable(equipmentId, true); + doReturn(Optional.of(user)).when(userRepository).findByUserIdAndIsEnable(req.getUserId(), true); + equipmentService.updateEquipment(equipmentId, req, user); + + // verify + verify(equipmentService, times(1)).updateEquipment(any(Long.class), any(UpdateEquipmentReq.class), any(User.class)); + verify(passwordEncoder, times(1)).encode(any(String.class)); } @Test - void updateEquipment() { + @DisplayName("[실패] 비품 정보 수정 - 존재하지 않는 비품일 경우") + void updateEquipment_EQUIPMENT_NOT_FOUND() { + // given + User user = setUpUser(1L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD)); + UpdateEquipmentReq req = setUpUpdateEquipmentReq("비품2", "10개", 1L, 2L, "S1350", "비품2입니다.", "기타", "photo/maxim.png"); + Long fakeId = 9999L; + // when + BaseException exception = assertThrows(BaseException.class, () -> { + equipmentService.updateEquipment(fakeId, req, user); + }); + // then + assertThat(exception.getBaseResponseCode()).isEqualTo(BaseResponseCode.EQUIPMENT_NOT_FOUND); } @Test diff --git a/src/test/java/com/example/pladialmserver/equipment/service/model/TestEquipmentInfo.java b/src/test/java/com/example/pladialmserver/equipment/service/model/TestEquipmentInfo.java index cf23181b..4cf17de0 100644 --- a/src/test/java/com/example/pladialmserver/equipment/service/model/TestEquipmentInfo.java +++ b/src/test/java/com/example/pladialmserver/equipment/service/model/TestEquipmentInfo.java @@ -1,6 +1,10 @@ package com.example.pladialmserver.equipment.service.model; import com.example.pladialmserver.equipment.dto.request.RegisterEquipmentReq; +import com.example.pladialmserver.equipment.dto.request.UpdateEquipmentReq; +import com.example.pladialmserver.equipment.entity.Equipment; +import com.example.pladialmserver.equipment.entity.EquipmentCategory; +import com.example.pladialmserver.user.entity.User; public class TestEquipmentInfo { @@ -15,4 +19,37 @@ public static RegisterEquipmentReq setUpRegisterEquipmentInfo(String name, Strin .build(); } + + public static Equipment setUpEquipment(String name, String quantity, String imgKey, String location, String description, EquipmentCategory category, User user) { + return Equipment.builder() + .name(name) + .quantity(quantity) + .imgKey(imgKey) + .location(location) + .description(description) + .category(category) + .user(user) + .build(); + } + + public static EquipmentCategory setUpEquipmentCategory(String name) { + return EquipmentCategory.builder() + .name(name) + .build(); + } + + public static UpdateEquipmentReq setUpUpdateEquipmentReq(String name, String quantity, Long register, Long userId, String location, String description, String category, String imgKey) { + return UpdateEquipmentReq.builder() + .name(name) + .quantity(quantity) + .register(register) + .userId(userId) + .location(location) + .description(description) + .category(category) + .imgKey(imgKey) + .build(); + + } + }