|
| 1 | +package in.koreatech.koin.unit.domain.ShopToOrderable; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.Mockito.verify; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import java.util.Optional; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.DisplayName; |
| 13 | +import org.junit.jupiter.api.Nested; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 16 | +import org.mockito.ArgumentCaptor; |
| 17 | +import org.mockito.InjectMocks; |
| 18 | +import org.mockito.Mock; |
| 19 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 20 | + |
| 21 | +import in.koreatech.koin.domain.shop.model.shop.Shop; |
| 22 | +import in.koreatech.koin.domain.shop.repository.shop.ShopRepository; |
| 23 | +import in.koreatech.koin.domain.shoptoOrderable.dto.ShopToOrderableRequest; |
| 24 | +import in.koreatech.koin.domain.shoptoOrderable.model.ShopToOrderable; |
| 25 | +import in.koreatech.koin.domain.shoptoOrderable.model.ShopToOrderableRequestStatus; |
| 26 | +import in.koreatech.koin.domain.shoptoOrderable.repository.ShopToOrderableRepository; |
| 27 | +import in.koreatech.koin.domain.shoptoOrderable.service.ShopToOrderableService; |
| 28 | +import in.koreatech.koin.domain.owner.model.Owner; |
| 29 | +import in.koreatech.koin.domain.user.model.User; |
| 30 | +import in.koreatech.koin.global.code.ApiResponseCode; |
| 31 | +import in.koreatech.koin.global.exception.CustomException; |
| 32 | +import in.koreatech.koin.unit.fixture.OwnerFixture; |
| 33 | +import in.koreatech.koin.unit.fixture.ShopFixture; |
| 34 | + |
| 35 | +import org.springframework.test.util.ReflectionTestUtils; |
| 36 | + |
| 37 | +@ExtendWith(MockitoExtension.class) |
| 38 | +class ShopToOrderableServiceTest { |
| 39 | + |
| 40 | + @InjectMocks |
| 41 | + private ShopToOrderableService shopToOrderableService; |
| 42 | + |
| 43 | + @Mock |
| 44 | + private ShopToOrderableRepository shopToOrderableRepository; |
| 45 | + |
| 46 | + @Mock |
| 47 | + private ShopRepository shopRepository; |
| 48 | + |
| 49 | + private Owner owner; |
| 50 | + private User user; |
| 51 | + private Shop shop; |
| 52 | + private ShopToOrderableRequest request; |
| 53 | + |
| 54 | + @BeforeEach |
| 55 | + void setUp() { |
| 56 | + owner = OwnerFixture.성빈_사장님(); |
| 57 | + ReflectionTestUtils.setField(owner, "id", 1); |
| 58 | + shop = ShopFixture.주문전환_이전_상점(owner); |
| 59 | + |
| 60 | + request = new ShopToOrderableRequest( |
| 61 | + 5000, |
| 62 | + true, |
| 63 | + "BOTH", |
| 64 | + 1000, |
| 65 | + 2000, |
| 66 | + true, |
| 67 | + "https://example.com/business_license.jpg", |
| 68 | + "https://example.com/business_certificate.jpg", |
| 69 | + "https://example.com/bank_copy.jpg", |
| 70 | + "국민은행", |
| 71 | + "123-456-789" |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + @Nested |
| 76 | + @DisplayName("주문 가능 상점 신청 생성 테스트") |
| 77 | + class CreateOrderableRequestTest { |
| 78 | + |
| 79 | + @Test |
| 80 | + @DisplayName("정상적으로 주문 가능 상점 신청이 생성된다") |
| 81 | + void createOrderableRequestSuccessfully() { |
| 82 | + // given |
| 83 | + when(shopRepository.findById(100)).thenReturn(Optional.of(shop)); |
| 84 | + when(shopToOrderableRepository.existsByShopId(100)).thenReturn(false); |
| 85 | + when(shopToOrderableRepository.existsByShopIdAndRequestStatus(100, ShopToOrderableRequestStatus.APPROVED)).thenReturn(false); |
| 86 | + when(shopToOrderableRepository.save(any(ShopToOrderable.class))).thenAnswer( |
| 87 | + invocation -> invocation.getArgument(0)); |
| 88 | + |
| 89 | + // when |
| 90 | + shopToOrderableService.createOrderableRequest(1, request, 100); |
| 91 | + |
| 92 | + // then |
| 93 | + ArgumentCaptor<ShopToOrderable> captor = ArgumentCaptor.forClass(ShopToOrderable.class); |
| 94 | + verify(shopToOrderableRepository).save(captor.capture()); |
| 95 | + |
| 96 | + ShopToOrderable saved = captor.getValue(); |
| 97 | + assertThat(saved.getShop()).isEqualTo(shop); |
| 98 | + assertThat(saved.getMinimumOrderAmount()).isEqualTo(5000); |
| 99 | + assertThat(saved.getTakeout()).isTrue(); |
| 100 | + assertThat(saved.getDeliveryOption()).isEqualTo("BOTH"); |
| 101 | + assertThat(saved.getCampusDeliveryTip()).isEqualTo(1000); |
| 102 | + assertThat(saved.getOutsideDeliveryTip()).isEqualTo(2000); |
| 103 | + assertThat(saved.getIsOpen()).isTrue(); |
| 104 | + assertThat(saved.getBank()).isEqualTo("국민은행"); |
| 105 | + assertThat(saved.getAccountNumber()).isEqualTo("123-456-789"); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + @DisplayName("존재하지 않는 상점 ID로 신청하면 예외가 발생한다") |
| 110 | + void throwExceptionWhenShopNotFound() { |
| 111 | + // given |
| 112 | + when(shopRepository.findById(999)).thenReturn(Optional.empty()); |
| 113 | + |
| 114 | + // when & then |
| 115 | + CustomException exception = assertThrows(CustomException.class, |
| 116 | + () -> shopToOrderableService.createOrderableRequest(1, request, 999)); |
| 117 | + |
| 118 | + assertThat(exception.getErrorCode()).isEqualTo(ApiResponseCode.NOT_FOUND_SHOP); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + @DisplayName("이미 신청한 내역이 있으면 예외가 발생한다") |
| 123 | + void throwExceptionWhenAlreadyRequested() { |
| 124 | + // given |
| 125 | + when(shopRepository.findById(100)).thenReturn(Optional.of(shop)); |
| 126 | + when(shopToOrderableRepository.existsByShopId(100)).thenReturn(true); |
| 127 | + |
| 128 | + // when & then |
| 129 | + CustomException exception = assertThrows(CustomException.class, |
| 130 | + () -> shopToOrderableService.createOrderableRequest(1, request, 100)); |
| 131 | + |
| 132 | + assertThat(exception.getErrorCode()).isEqualTo(ApiResponseCode.DUPLICATE_REQUESTED_ORDERABLE_SHOP); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + @DisplayName("상점 사장님이 아닌 경우 예외가 발생한다") |
| 137 | + void throwExceptionWhenNotShopOwner() { |
| 138 | + // given |
| 139 | + when(shopRepository.findById(100)).thenReturn(Optional.of(shop)); |
| 140 | + |
| 141 | + // when & then |
| 142 | + CustomException exception = assertThrows(CustomException.class, |
| 143 | + () -> shopToOrderableService.createOrderableRequest(2, request, 100)); |
| 144 | + assertThat(exception.getErrorCode()).isEqualTo(ApiResponseCode.FORBIDDEN_SHOP_OWNER); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + @DisplayName("이미 승인된 상점이 있으면 예외가 발생한다") |
| 149 | + void throwExceptionWhenAlreadyApproved() { |
| 150 | + // given |
| 151 | + when(shopRepository.findById(100)).thenReturn(Optional.of(shop)); |
| 152 | + when(shopToOrderableRepository.existsByShopId(100)).thenReturn(false); |
| 153 | + when(shopToOrderableRepository.existsByShopIdAndRequestStatus(100, ShopToOrderableRequestStatus.APPROVED)).thenReturn(true); |
| 154 | + |
| 155 | + // when & then |
| 156 | + CustomException exception = assertThrows(CustomException.class, |
| 157 | + () -> shopToOrderableService.createOrderableRequest(1, request, 100)); |
| 158 | + assertThat(exception.getErrorCode()).isEqualTo(ApiResponseCode.DUPLICATE_ORDERABLE_SHOP); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments