Skip to content

Commit

Permalink
Merge pull request #238 from PLADI-ALM/test/PDS-202-update-equipment
Browse files Browse the repository at this point in the history
[PDS-202/test] 비품 정보 수정 API 테스트
  • Loading branch information
leeseunghakhello authored Nov 26, 2023
2 parents 118d57d + 36f4bcf commit fb97b6d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@Getter
@Builder
public class UpdateEquipmentReq {
@Schema(type = "String", description = "이름", example = "맥심커피", required = true)
@NotBlank(message = "E0003")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +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.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.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -13,42 +19,84 @@
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
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
private EquipmentService equipmentService;
@Mock
private EquipmentRepository equipmentRepository;
@Mock
private UserRepository userRepository;
@Mock
private EquipmentCategoryRepository equipmentCategoryRepository;
@Mock
BCryptPasswordEncoder passwordEncoder;

@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 updateEquipment() {
@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
@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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +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 {

Expand All @@ -16,10 +19,36 @@ 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();

}
}

0 comments on commit fb97b6d

Please sign in to comment.