diff --git a/src/main/java/bc1/gream/domain/coupon/service/qeury/CouponQueryService.java b/src/main/java/bc1/gream/domain/coupon/service/qeury/CouponQueryService.java index 6fe8aa08..8395d5cd 100644 --- a/src/main/java/bc1/gream/domain/coupon/service/qeury/CouponQueryService.java +++ b/src/main/java/bc1/gream/domain/coupon/service/qeury/CouponQueryService.java @@ -24,11 +24,7 @@ public class CouponQueryService { public Coupon checkCoupon(Long couponId, User buyer, CouponStatus status) { - Coupon coupon = findByCouponId(couponId); - - if (!isCheckCouponUser(coupon, buyer)) { - throw new GlobalException(ResultCase.NOT_AUTHORIZED); - } + Coupon coupon = findCouponById(couponId, buyer); if (!isCheckCouponStatus(coupon, status)) { throw new GlobalException(ResultCase.COUPON_STATUS_CHANGE_FAIL); @@ -37,16 +33,6 @@ public Coupon checkCoupon(Long couponId, User buyer, CouponStatus status) { return coupon; } - private Coupon findByCouponId(Long couponId) { - return couponRepository.findById(couponId).orElseThrow( - () -> new GlobalException(ResultCase.COUPON_NOT_FOUND) - ); - } - - private boolean isCheckCouponUser(Coupon coupon, User user) { - return coupon.getUser().getLoginId().equals(user.getLoginId()); - } - private boolean isCheckCouponStatus(Coupon coupon, CouponStatus status) { return coupon.getStatus().equals(status); } diff --git a/src/test/java/bc1/gream/domain/admin/controller/AdminControllerTest.java b/src/test/java/bc1/gream/domain/admin/controller/AdminControllerTest.java index 11a50432..4a36d260 100644 --- a/src/test/java/bc1/gream/domain/admin/controller/AdminControllerTest.java +++ b/src/test/java/bc1/gream/domain/admin/controller/AdminControllerTest.java @@ -1,19 +1,31 @@ package bc1.gream.domain.admin.controller; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import bc1.gream.domain.admin.dto.request.AdminCreateCouponRequestDto; +import bc1.gream.domain.admin.dto.request.AdminGetRefundRequestDto; +import bc1.gream.domain.admin.dto.request.AdminProductRequestDto; +import bc1.gream.domain.admin.dto.request.AdminRefundPassResponseDto; import bc1.gream.domain.coupon.entity.DiscountType; import bc1.gream.domain.coupon.provider.CouponProvider; import bc1.gream.domain.product.service.command.ProductCommandService; +import bc1.gream.domain.user.entity.Refund; import bc1.gream.domain.user.entity.UserRole; import bc1.gream.domain.user.service.command.RefundCommandService; import bc1.gream.domain.user.service.query.RefundQueryService; import bc1.gream.global.security.WithMockCustomUser; +import bc1.gream.test.RefundTest; import bc1.gream.test.UserTest; import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.ArrayList; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -28,7 +40,7 @@ @WebMvcTest(controllers = AdminController.class) @WithMockCustomUser(loginId = "adminId12", password = "adminPw12!", userRole = UserRole.ADMIN) @ActiveProfiles("test") -class AdminControllerTest { +class AdminControllerTest implements RefundTest { @Autowired WebApplicationContext context; @@ -49,9 +61,83 @@ class AdminControllerTest { void setUp() { this.mockMvc = MockMvcBuilders .webAppContextSetup(context) + .alwaysDo(print()) .build(); } + @Test + void 신청된_환급_리스트를_관리자가_조회하는_컨트롤러_기능_성공_테스트() throws Exception { + + // given + AdminGetRefundRequestDto requestDto = new AdminGetRefundRequestDto(); + + String json = objectMapper.writeValueAsString(requestDto); + + List responseList = new ArrayList<>(); + + for (int i = 0; i < 5; i++) { + responseList.add(TEST_REFUND); + } + + given(refundQueryService.getRefunds()).willReturn(responseList); + + // when - then + mockMvc.perform(get("/api/admin/refunds") + .content(json) + .contentType(MediaType.APPLICATION_JSON)) + .andExpectAll( + status().isOk(), + jsonPath("$.code").value(0), + jsonPath("$.message").value("정상 처리 되었습니다"), + jsonPath("$.data[0].refundBank").value("농협은행"), + jsonPath("$.data[1].refundBank").value("농협은행"), + jsonPath("$.data[2].refundBank").value("농협은행"), + jsonPath("$.data[3].refundBank").value("농협은행"), + jsonPath("$.data[4].refundBank").value("농협은행") + ); + } + + @Test + void 관리자가_새로운_상품을_추가하는_컨트롤러_기능_성공_테스트() throws Exception { + + // given + AdminProductRequestDto requestDto = AdminProductRequestDto.builder() + .brand("스타벅스") + .name("콜드브루") + .imageUrl("C:/") + .description("콜드브루") + .price(6000L) + .build(); + + String json = objectMapper.writeValueAsString(requestDto); + + // when - then + mockMvc.perform(post("/api/admin/products") + .content(json) + .contentType(MediaType.APPLICATION_JSON)) + .andExpectAll( + status().isOk(), + jsonPath("$.code").value(0), + jsonPath("$.message").value("정상 처리 되었습니다") + ); + } + + @Test + void 유저가_요청한_환급내역_관리자가_승인하는_컨트롤러_기능_성공_테스트() throws Exception { + + // given + AdminRefundPassResponseDto responseDto = new AdminRefundPassResponseDto(); + given(refundCommandService.approveRefund(any(Long.class))).willReturn(responseDto); + + // when - then + mockMvc.perform(delete("/api/admin/refund/{id}", TEST_REFUND_ID)) + .andExpectAll( + status().isOk(), + jsonPath("$.code").value(0), + jsonPath("$.message").value("정상 처리 되었습니다") + ); + } + @Test public void 새로운_쿠폰_추가_정상요청() throws Exception { // GIVEN diff --git a/src/test/java/bc1/gream/domain/buy/service/query/BuyQueryServiceTest.java b/src/test/java/bc1/gream/domain/buy/service/query/BuyQueryServiceTest.java index aa3036a2..8c1a8de8 100644 --- a/src/test/java/bc1/gream/domain/buy/service/query/BuyQueryServiceTest.java +++ b/src/test/java/bc1/gream/domain/buy/service/query/BuyQueryServiceTest.java @@ -141,7 +141,7 @@ class BuyQueryServiceTest implements BuyTest, CouponTest { } @Test - void 현재_진행중인_유저의_구매입찰_전체_조회_서비스_기능_성공_테스트() { + void 현재_진행중인_유저의_구매입찰에_쿠폰이_존재할때_전체_조회_서비스_기능_성공_테스트() { // given List responseDtoList = new ArrayList<>(); @@ -150,25 +150,56 @@ class BuyQueryServiceTest implements BuyTest, CouponTest { .buyId((long) i) .price(1000L * i) .discountPrice(1000L * i) - .coupon((i % 2 == 0) ? TEST_COUPON_FIX : null) + .coupon(TEST_COUPON_FIX) .build(); responseDtoList.add(responseDto); } - given(buyRepository.findAllBuyBidCoupon(any(User.class), any(LocalDateTime.class))).willReturn(responseDtoList); + given(buyRepository.findAllBuyBidCoupon(any(User.class))).willReturn(responseDtoList); // when List resultList = buyQueryService.findAllBuyBidCoupon(TEST_USER); // then assertThat(resultList.size()).isEqualTo(5); - assertThat(resultList.get(0).discountPrice()).isEqualTo(responseDtoList.get(0).discountPrice()); + assertThat(resultList.get(0).discountPrice()).isEqualTo( + CouponCalculator.calculateDiscount(TEST_COUPON_FIX, responseDtoList.get(0).discountPrice())); assertThat(resultList.get(1).discountPrice()).isEqualTo( CouponCalculator.calculateDiscount(TEST_COUPON_FIX, responseDtoList.get(1).discountPrice())); - assertThat(resultList.get(2).discountPrice()).isEqualTo(responseDtoList.get(2).discountPrice()); + assertThat(resultList.get(2).discountPrice()).isEqualTo( + CouponCalculator.calculateDiscount(TEST_COUPON_FIX, responseDtoList.get(2).discountPrice())); assertThat(resultList.get(3).discountPrice()).isEqualTo( CouponCalculator.calculateDiscount(TEST_COUPON_FIX, responseDtoList.get(3).discountPrice())); - assertThat(resultList.get(4).discountPrice()).isEqualTo(responseDtoList.get(4).discountPrice()); + assertThat(resultList.get(4).discountPrice()).isEqualTo( + CouponCalculator.calculateDiscount(TEST_COUPON_FIX, responseDtoList.get(4).discountPrice())); + } + + @Test + void 현재_진행중인_유저의_구매입찰에_쿠폰이_존재하지_않을때_전체_조회_서비스_기능_성공_테스트() { + + // given + List responseDtoList = new ArrayList<>(); + for (int i = 1; i <= 5; i++) { + BuyCheckBidResponseDto responseDto = BuyCheckBidResponseDto.builder() + .buyId((long) i) + .price(1000L * i) + .discountPrice(1000L * i) + .coupon(null) + .build(); + + responseDtoList.add(responseDto); + } + given(buyRepository.findAllBuyBidCoupon(any(User.class), any(LocalDateTime.class))).willReturn(responseDtoList); + + // when + List resultList = buyQueryService.findAllBuyBidCoupon(TEST_USER); + + // then + assertThat(resultList.size()).isEqualTo(5); + assertThat(resultList.get(0).discountPrice()).isEqualTo(responseDtoList.get(0).discountPrice()); + assertThat(resultList.get(1).discountPrice()).isEqualTo(responseDtoList.get(1).discountPrice()); + assertThat(resultList.get(2).discountPrice()).isEqualTo(responseDtoList.get(2).discountPrice()); + assertThat(resultList.get(3).discountPrice()).isEqualTo(responseDtoList.get(3).discountPrice()); } @Test diff --git a/src/test/java/bc1/gream/domain/coupon/controller/CouponControllerTest.java b/src/test/java/bc1/gream/domain/coupon/controller/CouponControllerTest.java new file mode 100644 index 00000000..fbb2b2fa --- /dev/null +++ b/src/test/java/bc1/gream/domain/coupon/controller/CouponControllerTest.java @@ -0,0 +1,95 @@ +package bc1.gream.domain.coupon.controller; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import bc1.gream.domain.buy.service.query.BuyQueryService; +import bc1.gream.domain.coupon.entity.Coupon; +import bc1.gream.domain.coupon.service.qeury.CouponQueryService; +import bc1.gream.global.security.UserDetailsImpl; +import bc1.gream.global.security.WithMockCustomUser; +import bc1.gream.test.CouponTest; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +@WebMvcTest(controllers = CouponController.class) +@WithMockCustomUser +@ActiveProfiles("test") +class CouponControllerTest implements CouponTest { + + @Autowired + protected ObjectMapper objectMapper; + @MockBean + CouponQueryService couponQueryService; + @MockBean + BuyQueryService buyQueryService; + @Autowired + private MockMvc mockMvc; + @Autowired + private WebApplicationContext context; + + @BeforeEach + void setUp() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(context) + .alwaysDo(print()).build(); + } + + @Test + void 로그인_한_유저가_사용가능한_쿠폰을_조회하는_컨트롤러_기능_성공_테스트() throws Exception { + + // given + List couponList = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + couponList.add((i % 2 == 0) ? TEST_COUPON_FIX : TEST_COUPON_RATE); + } + + given(couponQueryService.availableCouponList(any(UserDetailsImpl.class))).willReturn(couponList); + + // when - then + mockMvc.perform(get("/api/coupons")) + .andExpectAll( + status().isOk(), + jsonPath("$.code").value(0), + jsonPath("$.message").value("정상 처리 되었습니다"), + jsonPath("$.data.size()").value(5), + jsonPath("$.data[0].discountType").value("FIX"), + jsonPath("$.data[1].discountType").value("RATE") + ); + } + + @Test + void 로그인_한_유저가_이미_사용한_쿠폰을_조회하는_컨트롤러_기능_성공_테스트() throws Exception { + + // given + List couponList = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + couponList.add((i % 2 == 0) ? TEST_COUPON_RATE_USED : TEST_COUPON_FIX_USED); + } + given(couponQueryService.unavailableCouponList(any(UserDetailsImpl.class))).willReturn(couponList); + + // when - then + mockMvc.perform(get("/api/coupons/used")) + .andExpectAll( + status().isOk(), + jsonPath("$.code").value(0), + jsonPath("$.message").value("정상 처리 되었습니다"), + jsonPath("$.data.size()").value(5), + jsonPath("$.data[0].discountType").value("RATE"), + jsonPath("$.data[1].discountType").value("FIX") + ); + } +} \ No newline at end of file diff --git a/src/test/java/bc1/gream/domain/coupon/provider/CouponProviderTest.java b/src/test/java/bc1/gream/domain/coupon/provider/CouponProviderTest.java new file mode 100644 index 00000000..4fbcb627 --- /dev/null +++ b/src/test/java/bc1/gream/domain/coupon/provider/CouponProviderTest.java @@ -0,0 +1,54 @@ +package bc1.gream.domain.coupon.provider; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; + +import bc1.gream.domain.admin.dto.request.AdminCreateCouponRequestDto; +import bc1.gream.domain.coupon.entity.Coupon; +import bc1.gream.domain.coupon.entity.DiscountType; +import bc1.gream.domain.coupon.service.command.CouponCommandService; +import bc1.gream.domain.user.entity.User; +import bc1.gream.domain.user.repository.UserRepository; +import bc1.gream.test.CouponTest; +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class CouponProviderTest implements CouponTest { + + @InjectMocks + private CouponProvider couponProvider; + @Mock + private UserRepository userRepository; + @Mock + private CouponCommandService couponCommandService; + + @Test + void 관리자가_사용자에게_쿠폰을_등록해주는_Provider_성공_테스트() { + + // given + AdminCreateCouponRequestDto requestDto = AdminCreateCouponRequestDto.builder() + .name("TEST COUPON") + .discountType(DiscountType.FIX) + .discount(500L) + .userLoginId(TEST_USER_LOGIN_ID) + .build(); + + given(userRepository.findByLoginId(any(String.class))).willReturn(Optional.of(TEST_USER)); + given(couponCommandService.createCoupon(any(User.class), any(AdminCreateCouponRequestDto.class))).willReturn(TEST_COUPON_FIX); + + // when + Coupon resultCoupon = couponProvider.createCoupon(requestDto); + + // then + assertThat(resultCoupon.getName()).isEqualTo(requestDto.name()); + assertThat(resultCoupon.getDiscount()).isEqualTo(requestDto.discount()); + assertThat(resultCoupon.getDiscountType()).isEqualTo(requestDto.discountType()); + + } +} \ No newline at end of file diff --git a/src/test/java/bc1/gream/domain/coupon/service/qeury/CouponQueryServiceTest.java b/src/test/java/bc1/gream/domain/coupon/service/qeury/CouponQueryServiceTest.java new file mode 100644 index 00000000..97e22059 --- /dev/null +++ b/src/test/java/bc1/gream/domain/coupon/service/qeury/CouponQueryServiceTest.java @@ -0,0 +1,215 @@ +package bc1.gream.domain.coupon.service.qeury; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import bc1.gream.domain.coupon.entity.Coupon; +import bc1.gream.domain.coupon.entity.CouponStatus; +import bc1.gream.domain.coupon.repository.CouponRepository; +import bc1.gream.domain.user.entity.User; +import bc1.gream.global.common.ResultCase; +import bc1.gream.global.exception.GlobalException; +import bc1.gream.global.security.UserDetailsImpl; +import bc1.gream.test.CouponTest; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class CouponQueryServiceTest implements CouponTest { + + @InjectMocks + private CouponQueryService couponQueryService; + + @Mock + private CouponRepository couponRepository; + + @Test + void 쿠폰이_상태가_원하는_상태인지_체크하는_서비스_기능_성공_테스트() { + + // given + given(couponRepository.findById(any(Long.class))).willReturn(Optional.of(TEST_COUPON_OF_TEST_USER)); + + // when + Coupon coupon = couponQueryService.checkCoupon(TEST_COUPON_ID, TEST_USER, CouponStatus.AVAILABLE); + + // then + assertThat(coupon.getName()).isEqualTo(TEST_COUPON_OF_TEST_USER.getName()); + assertThat(coupon.getDiscount()).isEqualTo(TEST_COUPON_OF_TEST_USER.getDiscount()); + assertThat(coupon.getDiscountType()).isEqualTo(TEST_COUPON_OF_TEST_USER.getDiscountType()); + } + + @Test + void 쿠폰이_상태가_원하는_상태인지_체크하는_서비스_기능_쿠폰이_존재하지_않으므로_인한_테스트() { + + // given + given(couponRepository.findById(any(Long.class))).willReturn(Optional.empty()); + + // when + GlobalException exception = assertThrows(GlobalException.class, () -> { + couponQueryService.checkCoupon(TEST_COUPON_ID, TEST_BUYER, CouponStatus.AVAILABLE); + }); + + // then + assertThat(exception.getResultCase()).isEqualTo(ResultCase.COUPON_NOT_FOUND); + assertThat(exception.getResultCase().getCode()).isEqualTo(ResultCase.COUPON_NOT_FOUND.getCode()); + assertThat(exception.getResultCase().getMessage()).isEqualTo(ResultCase.COUPON_NOT_FOUND.getMessage()); + } + + @Test + void 쿠폰이_상태가_원하는_상태인지_체크하는_서비스_기능_로그인한_유저의_쿠폰이_아님으로_인한_실패_테스트() { + + // given + given(couponRepository.findById(any(Long.class))).willReturn(Optional.of(TEST_COUPON_OF_TEST_USER)); + + // when + GlobalException exception = assertThrows(GlobalException.class, () -> { + couponQueryService.checkCoupon(TEST_COUPON_ID, TEST_BUYER, CouponStatus.AVAILABLE); + }); + + // then + assertThat(exception.getResultCase()).isEqualTo(ResultCase.NOT_AUTHORIZED); + assertThat(exception.getResultCase().getCode()).isEqualTo(ResultCase.NOT_AUTHORIZED.getCode()); + assertThat(exception.getResultCase().getMessage()).isEqualTo(ResultCase.NOT_AUTHORIZED.getMessage()); + } + + @Test + void 쿠폰이_사용가능한지_체크하는_서비스_기능_이미_사용한_쿠폰으로_인한_실패_테스트() { + + // given + given(couponRepository.findById(any(Long.class))).willReturn(Optional.of(TEST_COUPON_OF_TEST_USER)); + + // when + GlobalException exception = assertThrows(GlobalException.class, () -> { + couponQueryService.checkCoupon(TEST_COUPON_ID, TEST_USER, CouponStatus.ALREADY_USED); + }); + + // then + assertThat(exception.getResultCase()).isEqualTo(ResultCase.COUPON_STATUS_CHANGE_FAIL); + assertThat(exception.getResultCase().getCode()).isEqualTo(ResultCase.COUPON_STATUS_CHANGE_FAIL.getCode()); + assertThat(exception.getResultCase().getMessage()).isEqualTo(ResultCase.COUPON_STATUS_CHANGE_FAIL.getMessage()); + } + + @Test + void 사용_가능한_유저의_쿠폰_리스트_조회_성공_테스트() { + + // given + List couponList = new ArrayList<>(); + + for (int i = 0; i < 3; i++) { + couponList.add(TEST_COUPON_FIX); + } + + given(couponRepository.availableCoupon(any(User.class))).willReturn(couponList); + UserDetailsImpl userDetails = new UserDetailsImpl(TEST_USER); + + // when + List resultList = couponQueryService.availableCouponList(userDetails); + + // then + assertThat(resultList.get(0)).isEqualTo(couponList.get(0)); + assertThat(resultList.get(1)).isEqualTo(couponList.get(1)); + assertThat(resultList.get(2)).isEqualTo(couponList.get(2)); + } + + @Test + void unavailableCouponList() { + + // given + List couponList = new ArrayList<>(); + + for (int i = 0; i < 3; i++) { + couponList.add(TEST_COUPON_FIX_USED); + } + + given(couponRepository.unavailable(any(User.class))).willReturn(couponList); + UserDetailsImpl userDetails = new UserDetailsImpl(TEST_USER); + + // when + List resultList = couponQueryService.unavailableCouponList(userDetails); + + // then + assertThat(resultList.get(0)).isEqualTo(couponList.get(0)); + assertThat(resultList.get(1)).isEqualTo(couponList.get(1)); + assertThat(resultList.get(2)).isEqualTo(couponList.get(2)); + } + + @Test + void 쿠폰을_아이디와_유저로_체크하는_서비스_기능_성공_테스트() { + + // given + given(couponRepository.findById(any(Long.class))).willReturn(Optional.of(TEST_COUPON_OF_TEST_USER)); + + // when + Coupon coupon = couponQueryService.findCouponById(TEST_COUPON_ID, TEST_USER); + + // then + assertThat(coupon.getName()).isEqualTo(TEST_COUPON_OF_TEST_USER.getName()); + assertThat(coupon.getDiscount()).isEqualTo(TEST_COUPON_OF_TEST_USER.getDiscount()); + assertThat(coupon.getStatus()).isEqualTo(TEST_COUPON_OF_TEST_USER.getStatus()); + } + + @Test + void 쿠폰을_아이디와_유저로_체크하는_서비스_기능_쿠폰이_존재하지_않으므로_인한_실패_테스트() { + + // given + given(couponRepository.findById(any(Long.class))).willReturn(Optional.empty()); + + // when + GlobalException exception = assertThrows(GlobalException.class, () -> { + couponQueryService.findCouponById(TEST_COUPON_ID, TEST_USER); + }); + + // then + assertThat(exception.getResultCase()).isEqualTo(ResultCase.COUPON_NOT_FOUND); + assertThat(exception.getResultCase().getCode()).isEqualTo(ResultCase.COUPON_NOT_FOUND.getCode()); + assertThat(exception.getResultCase().getMessage()).isEqualTo(ResultCase.COUPON_NOT_FOUND.getMessage()); + } + + @Test + void 쿠폰을_아이디와_유저로_체크하는_서비스_기능_쿠폰이_유저의_소유가_아니므로_인한_실패_테스트() { + + // given + given(couponRepository.findById(any(Long.class))).willReturn(Optional.of(TEST_COUPON_OF_TEST_USER)); + + // when + GlobalException exception = assertThrows(GlobalException.class, () -> { + couponQueryService.findCouponById(TEST_COUPON_ID, TEST_BUYER); + }); + + // then + assertThat(exception.getResultCase()).isEqualTo(ResultCase.NOT_AUTHORIZED); + assertThat(exception.getResultCase().getCode()).isEqualTo(ResultCase.NOT_AUTHORIZED.getCode()); + assertThat(exception.getResultCase().getMessage()).isEqualTo(ResultCase.NOT_AUTHORIZED.getMessage()); + } + + @Test + @DisplayName("쿠폰에 대한 회원 접근권한을 검증합니다.") + public void 쿠폰_회원접근권한_검증() { + // GIVEN + User mockUser = mock(User.class); + given(mockUser.getLoginId()).willReturn(TEST_USER_LOGIN_ID); + Coupon mockCoupon = mock(Coupon.class); + given(mockCoupon.getUser()).willReturn(mockUser); + + // WHEN + boolean matchCouponUser = couponQueryService.isMatchCouponUser(mockUser, mockCoupon); + + // THEN + assertTrue(matchCouponUser); + verify(mockUser, times(2)).getLoginId(); // verify getLoginId() has called on mockUser + verify(mockCoupon, times(1)).getUser(); // verify getUser() has called on mockCoupon + } +} \ No newline at end of file diff --git a/src/test/java/bc1/gream/domain/gifticon/service/command/GifticonCommandServiceTest.java b/src/test/java/bc1/gream/domain/gifticon/service/command/GifticonCommandServiceTest.java new file mode 100644 index 00000000..0efb7e08 --- /dev/null +++ b/src/test/java/bc1/gream/domain/gifticon/service/command/GifticonCommandServiceTest.java @@ -0,0 +1,41 @@ +package bc1.gream.domain.gifticon.service.command; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import bc1.gream.domain.gifticon.entity.Gifticon; +import bc1.gream.domain.gifticon.repository.GifticonRepository; +import bc1.gream.test.GifticonTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class GifticonCommandServiceTest implements GifticonTest { + + @InjectMocks + private GifticonCommandService gifticonCommandService; + @Mock + private GifticonRepository gifticonRepository; + + @Test + void 새로운_기프티콘을_등록하는_서비스_기능_성공_테스트() { + // given - when + gifticonCommandService.saveGifticon(TEST_GIFTICON_URL, TEST_ORDER); + + // then + verify(gifticonRepository, times(1)).save(any(Gifticon.class)); + } + + @Test + void 등록되어있는_기프티콘을_삭제하는_서비스_기능_성공_테스트() { + // given - when + gifticonCommandService.delete(TEST_GIFTICON); + + // then + verify(gifticonRepository, times(1)).delete(any(Gifticon.class)); + } +} \ No newline at end of file diff --git a/src/test/java/bc1/gream/domain/gifticon/service/query/GifticonQueryServiceTest.java b/src/test/java/bc1/gream/domain/gifticon/service/query/GifticonQueryServiceTest.java new file mode 100644 index 00000000..817c7d65 --- /dev/null +++ b/src/test/java/bc1/gream/domain/gifticon/service/query/GifticonQueryServiceTest.java @@ -0,0 +1,71 @@ +package bc1.gream.domain.gifticon.service.query; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; + +import bc1.gream.domain.buy.dto.response.BuyCheckOrderResponseDto; +import bc1.gream.domain.gifticon.entity.Gifticon; +import bc1.gream.domain.gifticon.repository.GifticonRepository; +import bc1.gream.domain.user.entity.User; +import bc1.gream.test.GifticonTest; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class GifticonQueryServiceTest implements GifticonTest { + + @Mock + GifticonRepository gifticonRepository; + @InjectMocks + private GifticonQueryService gifticonQueryService; + + @Test + void 판매자_기준_거래가_완료된_기프티콘_전체_조회하는_서비스_기능_성공_테스트() { + // given + List gifticonList = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + gifticonList.add(TEST_GIFTICON); + } + + given(gifticonRepository.findAllSoldBySeller(any(User.class))).willReturn(gifticonList); + + // when + List resultList = gifticonQueryService.findAllSoldBySeller(TEST_USER); + + // then + assertThat(resultList.get(0)).isEqualTo(gifticonList.get(0)); + assertThat(resultList.get(1)).isEqualTo(gifticonList.get(1)); + assertThat(resultList.get(2)).isEqualTo(gifticonList.get(2)); + assertThat(resultList.get(3)).isEqualTo(gifticonList.get(3)); + } + + @Test + void 구매자_기준_거래가_완료된_기프티콘_전체_조회하는_서비스_기능_성공_테스트() { + // given + List gifticonList = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + Gifticon gifticon = Gifticon.builder() + .gifticonUrl(TEST_GIFTICON_URL) + .order(TEST_ORDER) + .build(); + gifticonList.add(gifticon); + } + + given(gifticonRepository.findAllBoughtByBuyer(any(User.class))).willReturn(gifticonList); + + // when + List resultList = gifticonQueryService.getBoughtOrder(TEST_USER); + + // then + assertThat(resultList.get(0).gifticonUrl()).isEqualTo(TEST_GIFTICON_URL); + assertThat(resultList.get(1).gifticonUrl()).isEqualTo(TEST_GIFTICON_URL); + assertThat(resultList.get(2).gifticonUrl()).isEqualTo(TEST_GIFTICON_URL); + assertThat(resultList.get(3).gifticonUrl()).isEqualTo(TEST_GIFTICON_URL); + } +} \ No newline at end of file diff --git a/src/test/java/bc1/gream/domain/order/service/command/OrderCommandServiceTest.java b/src/test/java/bc1/gream/domain/order/service/command/OrderCommandServiceTest.java new file mode 100644 index 00000000..109cb23e --- /dev/null +++ b/src/test/java/bc1/gream/domain/order/service/command/OrderCommandServiceTest.java @@ -0,0 +1,62 @@ +package bc1.gream.domain.order.service.command; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import bc1.gream.domain.order.entity.Order; +import bc1.gream.domain.order.repository.OrderRepository; +import bc1.gream.test.BuyTest; +import bc1.gream.test.CouponTest; +import bc1.gream.test.OrderTest; +import bc1.gream.test.SellTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class OrderCommandServiceTest implements OrderTest, BuyTest, CouponTest, SellTest { + + @InjectMocks + private OrderCommandService orderCommandService; + @Mock + private OrderRepository orderRepository; + + @Test + void 즉시판매로_인하여_새로운_주문이_생기는_서비스_기능_성공_테스트_쿠폰이_있을떄() { + // given - when + orderCommandService.saveOrderOfBuy(TEST_BUY_COUPON, TEST_SELLER, TEST_COUPON_FIX); + + // then + verify(orderRepository, times(1)).save(any(Order.class)); + } + + @Test + void 즉시판매로_인하여_새로운_주문이_생기는_서비스_기능_성공_테스트_쿠폰이_없을때() { + // given - when + orderCommandService.saveOrderOfBuyNotCoupon(TEST_BUY, TEST_SELLER); + + // then + verify(orderRepository, times(1)).save(any(Order.class)); + } + + @Test + void 즉시구매로_인하여_새로운_주문이_생기는_서비스_기능_성공_테스트_쿠폰이_있을때() { + // given - when + orderCommandService.saveOrderOfSell(TEST_SELL, TEST_BUYER, TEST_COUPON_FIX); + + // then + verify(orderRepository, times(1)).save(any(Order.class)); + } + + @Test + void 즉시구매로_인하여_새로운_주문이_생기는_서비스_기능_성공_테스트_쿠폰이_없을때() { + // given - when + orderCommandService.saveOrderOfSellNotCoupon(TEST_SELL, TEST_BUYER); + + // then + verify(orderRepository, times(1)).save(any(Order.class)); + } +} \ No newline at end of file diff --git a/src/test/java/bc1/gream/domain/order/service/query/OrderQueryServiceTest.java b/src/test/java/bc1/gream/domain/order/service/query/OrderQueryServiceTest.java index 7b9f6e9f..a11843d0 100644 --- a/src/test/java/bc1/gream/domain/order/service/query/OrderQueryServiceTest.java +++ b/src/test/java/bc1/gream/domain/order/service/query/OrderQueryServiceTest.java @@ -1,14 +1,19 @@ package bc1.gream.domain.order.service.query; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import bc1.gream.domain.order.entity.Order; import bc1.gream.domain.order.repository.OrderRepository; import bc1.gream.domain.product.entity.Product; +import bc1.gream.domain.user.entity.User; import bc1.gream.test.OrderTest; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -50,4 +55,34 @@ class OrderQueryServiceTest implements OrderTest { assertEquals(recentOrder, trades.get(0)); assertEquals(pastOrder, trades.get(1)); } + + @Test + void 판매자_기준_주문_전체_조회하는_서비스_기능_성공_테스트() { + // given + List orderList = new ArrayList<>(); + + for (int i = 0; i < 5; i++) { + Order order = Order.builder() + .buyer(TEST_BUYER) + .seller(TEST_SELLER) + .product(TEST_PRODUCT) + .finalPrice(1000L * i) + .expectedPrice(1000L * i) + .build(); + + orderList.add(order); + } + + given(orderRepository.findAllBySellerOrderByCreatedAtDesc(any(User.class))).willReturn(orderList); + // when + List resultList = orderQueryService.findAllOrderBySeller(TEST_SELLER); + + // then + assertThat(resultList.get(0).getExpectedPrice()).isEqualTo(orderList.get(0).getExpectedPrice()); + assertThat(resultList.get(1).getExpectedPrice()).isEqualTo(orderList.get(1).getExpectedPrice()); + assertThat(resultList.get(2).getExpectedPrice()).isEqualTo(orderList.get(2).getExpectedPrice()); + assertThat(resultList.get(3).getExpectedPrice()).isEqualTo(orderList.get(3).getExpectedPrice()); + assertThat(resultList.get(4).getExpectedPrice()).isEqualTo(orderList.get(4).getExpectedPrice()); + + } } \ No newline at end of file diff --git a/src/test/java/bc1/gream/domain/user/service/CouponQueryServiceTest.java b/src/test/java/bc1/gream/domain/user/service/CouponQueryServiceTest.java deleted file mode 100644 index ecf77ad6..00000000 --- a/src/test/java/bc1/gream/domain/user/service/CouponQueryServiceTest.java +++ /dev/null @@ -1,47 +0,0 @@ -package bc1.gream.domain.user.service; - -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -import bc1.gream.domain.coupon.entity.Coupon; -import bc1.gream.domain.coupon.repository.CouponRepository; -import bc1.gream.domain.coupon.service.qeury.CouponQueryService; -import bc1.gream.domain.user.entity.User; -import bc1.gream.test.CouponTest; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -class CouponQueryServiceTest implements CouponTest { - - @Mock - private CouponRepository couponRepository; - - @InjectMocks - private CouponQueryService couponQueryService; - - @Test - @DisplayName("쿠폰에 대한 회원 접근권한을 검증합니다.") - public void 쿠폰_회원접근권한_검증() { - // GIVEN - User mockUser = mock(User.class); - given(mockUser.getLoginId()).willReturn(TEST_USER_LOGIN_ID); - Coupon mockCoupon = mock(Coupon.class); - given(mockCoupon.getUser()).willReturn(mockUser); - - // WHEN - boolean matchCouponUser = couponQueryService.isMatchCouponUser(mockUser, mockCoupon); - - // THEN - assertTrue(matchCouponUser); - verify(mockUser, times(2)).getLoginId(); // verify getLoginId() has called on mockUser - verify(mockCoupon, times(1)).getUser(); // verify getUser() has called on mockCoupon - } -} \ No newline at end of file diff --git a/src/test/java/bc1/gream/test/CouponTest.java b/src/test/java/bc1/gream/test/CouponTest.java index 6b0135a7..add0ce49 100644 --- a/src/test/java/bc1/gream/test/CouponTest.java +++ b/src/test/java/bc1/gream/test/CouponTest.java @@ -51,4 +51,18 @@ public interface CouponTest extends UserTest { .status(TEST_COUPON_STATUS_AVAILABLE) .user(TEST_USER) .build(); + + Coupon TEST_COUPON_FIX_USED = Coupon.builder() + .name(TEST_COUPON_NAME) + .discountType(TEST_DISCOUNT_TYPE_WON) + .discount(TEST_DISCOUNT) + .status(TEST_COUPON_STATUS_ALREADY_USED) + .build(); + + Coupon TEST_COUPON_RATE_USED = Coupon.builder() + .name(TEST_COUPON_NAME) + .discountType(TEST_DISCOUNT_TYPE_PERCENT) + .discount(TEST_DISCOUNT_PERCENT) + .status(TEST_COUPON_STATUS_ALREADY_USED) + .build(); } diff --git a/src/test/java/bc1/gream/test/RefundTest.java b/src/test/java/bc1/gream/test/RefundTest.java new file mode 100644 index 00000000..b876b1ca --- /dev/null +++ b/src/test/java/bc1/gream/test/RefundTest.java @@ -0,0 +1,21 @@ +package bc1.gream.test; + +import bc1.gream.domain.user.entity.Refund; + +public interface RefundTest extends UserTest { + + Long TEST_REFUND_ID = 1L; + + Long TEST_REFUND_POINT = 1000L; + + String TEST_REFUND_BANK = "농협은행"; + + String TEST_REFUND_ACCOUNT_NUMBER = "111-1111-1111111"; + + Refund TEST_REFUND = Refund.builder() + .point(TEST_REFUND_POINT) + .bank(TEST_REFUND_BANK) + .accountNumber(TEST_REFUND_ACCOUNT_NUMBER) + .user(TEST_USER) + .build(); +} diff --git a/src/test/java/bc1/gream/test/UserTest.java b/src/test/java/bc1/gream/test/UserTest.java index f527bd15..f229b34c 100644 --- a/src/test/java/bc1/gream/test/UserTest.java +++ b/src/test/java/bc1/gream/test/UserTest.java @@ -44,4 +44,17 @@ public interface UserTest { .role(UserRole.USER) .provider(Provider.LOCAL) .build(); + + Long TEST_ADMIN_USER_ID = 3L; + String TEST_ADMIN_LOGIN_ID = "admin01"; + String TEST_ADMIN_NICKNAME = "admin01"; + String TEST_ADMIN_PASSWORD = "ABcd5678#&"; + + User TEST_ADMIN_USER = User.builder() + .loginId(TEST_ADMIN_LOGIN_ID) + .nickname(TEST_ADMIN_NICKNAME) + .password(TEST_ADMIN_PASSWORD) + .role(UserRole.ADMIN) + .provider(Provider.LOCAL) + .build(); }