|
| 1 | +package com.irum.come2us.domain.coupon; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.hasSize; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 6 | +import static org.mockito.Mockito.*; |
| 7 | +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; |
| 8 | +import static org.springframework.restdocs.payload.PayloadDocumentation.*; |
| 9 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
| 10 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; |
| 11 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 12 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 13 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 14 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 15 | + |
| 16 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 17 | +import com.irum.come2us.domain.coupon.application.service.CouponService; |
| 18 | +import com.irum.come2us.domain.coupon.presentation.controller.CouponController; |
| 19 | +import com.irum.come2us.domain.coupon.presentation.dto.request.CouponGenerateRequest; |
| 20 | +import com.irum.come2us.domain.coupon.presentation.dto.response.CouponResponse; |
| 21 | +import com.irum.come2us.global.config.SecurityTestConfig; |
| 22 | +import com.irum.come2us.global.presentation.advice.exception.CommonException; |
| 23 | +import com.irum.come2us.global.presentation.advice.exception.errorcode.CouponErrorCode; |
| 24 | +import java.time.LocalDateTime; |
| 25 | +import java.util.List; |
| 26 | +import java.util.UUID; |
| 27 | +import org.junit.jupiter.api.DisplayName; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.mockito.Mockito; |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; |
| 32 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 33 | +import org.springframework.boot.test.context.TestConfiguration; |
| 34 | +import org.springframework.context.annotation.Bean; |
| 35 | +import org.springframework.context.annotation.Import; |
| 36 | +import org.springframework.context.annotation.Primary; |
| 37 | +import org.springframework.test.web.servlet.MockMvc; |
| 38 | + |
| 39 | +@WebMvcTest(CouponController.class) |
| 40 | +@AutoConfigureRestDocs |
| 41 | +@Import({SecurityTestConfig.class, CouponControllerTest.TestConfig.class}) |
| 42 | +public class CouponControllerTest { |
| 43 | + @Autowired private MockMvc mockMvc; |
| 44 | + @Autowired private CouponService couponService; |
| 45 | + @Autowired private ObjectMapper objectMapper; |
| 46 | + |
| 47 | + @TestConfiguration |
| 48 | + static class TestConfig { |
| 49 | + |
| 50 | + @Bean |
| 51 | + @Primary |
| 52 | + public CouponService couponService() { |
| 53 | + return Mockito.mock(CouponService.class); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + @DisplayName("쿠폰 생성 API") |
| 59 | + void couponCreateApiTest() throws Exception { |
| 60 | + |
| 61 | + // Given |
| 62 | + CouponGenerateRequest request = |
| 63 | + new CouponGenerateRequest( |
| 64 | + "기간 한정 5000원 할인쿠폰", 5000, LocalDateTime.of(2028, 10, 10, 12, 12, 12)); |
| 65 | + String requestJson = objectMapper.writeValueAsString(request); |
| 66 | + |
| 67 | + doNothing().when(couponService).createCoupon(any(CouponGenerateRequest.class), anyLong()); |
| 68 | + |
| 69 | + // when |
| 70 | + mockMvc.perform( |
| 71 | + post("/coupons") |
| 72 | + .with(csrf()) |
| 73 | + .with(user("1")) |
| 74 | + .contentType("application/json") |
| 75 | + .content(requestJson)) |
| 76 | + .andExpect(status().isCreated()) |
| 77 | + .andDo( |
| 78 | + document( |
| 79 | + "coupon-create", |
| 80 | + requestFields( |
| 81 | + fieldWithPath("name").description("쿠폰명"), |
| 82 | + fieldWithPath("discountAmount").description("할인 금액"), |
| 83 | + fieldWithPath("expiration").description("유효기간")))); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + @DisplayName("쿠폰 정보 조회 API") |
| 88 | + void couponListTest() throws Exception { |
| 89 | + // Given |
| 90 | + LocalDateTime now = LocalDateTime.of(2028, 10, 10, 12, 12, 12); |
| 91 | + CouponResponse coupon1 = |
| 92 | + new CouponResponse( |
| 93 | + UUID.fromString("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), |
| 94 | + "기간 한정 5000원 할인쿠폰", |
| 95 | + 5000, |
| 96 | + now); |
| 97 | + |
| 98 | + CouponResponse coupon2 = |
| 99 | + new CouponResponse( |
| 100 | + UUID.fromString("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"), |
| 101 | + "신규가입 10% 할인", |
| 102 | + 10, |
| 103 | + now.plusDays(1)); |
| 104 | + |
| 105 | + List<CouponResponse> responses = List.of(coupon1, coupon2); |
| 106 | + when(couponService.getCouponByMember(anyLong())).thenReturn(responses); |
| 107 | + |
| 108 | + // When & Then |
| 109 | + mockMvc.perform(get("/coupons").with(user("1")).accept("application/json")) |
| 110 | + .andDo(print()) |
| 111 | + .andExpect(status().isOk()) |
| 112 | + .andExpect(jsonPath("$.success").value(true)) |
| 113 | + .andExpect(jsonPath("$.status").value(200)) |
| 114 | + .andExpect(jsonPath("$.data").isArray()) |
| 115 | + .andExpect(jsonPath("$.data", hasSize(2))) |
| 116 | + // coupon1 |
| 117 | + .andExpect(jsonPath("$.data[0].id").value("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")) |
| 118 | + .andExpect(jsonPath("$.data[0].name").value("기간 한정 5000원 할인쿠폰")) |
| 119 | + .andExpect(jsonPath("$.data[0].discountAmount").value(5000)) |
| 120 | + .andExpect(jsonPath("$.data[0].expiration").value("2028-10-10 12:12:12")) |
| 121 | + // coupon2 |
| 122 | + .andExpect(jsonPath("$.data[1].id").value("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb")) |
| 123 | + .andExpect(jsonPath("$.data[1].name").value("신규가입 10% 할인")) |
| 124 | + .andExpect(jsonPath("$.data[1].discountAmount").value(10)) |
| 125 | + .andExpect(jsonPath("$.data[1].expiration").value("2028-10-11 12:12:12")) |
| 126 | + .andExpect(jsonPath("$.timestamp").exists()) |
| 127 | + .andDo( |
| 128 | + document( |
| 129 | + "coupon-list", |
| 130 | + responseFields( |
| 131 | + fieldWithPath("success").description("성공 여부"), |
| 132 | + fieldWithPath("status").description("HTTP 상태 코드"), |
| 133 | + fieldWithPath("data").description("쿠폰 목록"), |
| 134 | + fieldWithPath("data[].id").description("쿠폰 ID"), |
| 135 | + fieldWithPath("data[].name").description("쿠폰명"), |
| 136 | + fieldWithPath("data[].discountAmount").description("할인 금액"), |
| 137 | + fieldWithPath("data[].expiration") |
| 138 | + .description("만료 일시 (yyyy-MM-dd HH:mm:ss)"), |
| 139 | + fieldWithPath("timestamp").description("응답 생성 시각")))); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + @DisplayName("쿠폰 삭제 API") |
| 144 | + void couponDeleteApiTest() throws Exception { |
| 145 | + // Given |
| 146 | + UUID couponId = UUID.fromString("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"); |
| 147 | + doNothing().when(couponService).deleteCoupon(couponId, 1L); |
| 148 | + |
| 149 | + // When & Then |
| 150 | + mockMvc.perform(delete("/coupons/{couponId}", couponId).with(csrf()).with(user("1"))) |
| 151 | + .andExpect(status().isNoContent()) |
| 152 | + .andDo(document("coupon-delete")); |
| 153 | + |
| 154 | + Mockito.verify(couponService).deleteCoupon(couponId, 1L); |
| 155 | + } |
| 156 | + |
| 157 | + // import static org.mockito.Mockito.doThrow; |
| 158 | + |
| 159 | + @Test |
| 160 | + @DisplayName("쿠폰 삭제 API - 쿠폰 없음") |
| 161 | + void couponDelete_NotFound() throws Exception { |
| 162 | + UUID couponId = UUID.fromString("cccccccc-bbbb-bbbb-bbbb-bbbbbbbbbbbb"); |
| 163 | + doThrow(new CommonException(CouponErrorCode.COUPON_NOT_FOUND)) |
| 164 | + .when(couponService) |
| 165 | + .deleteCoupon(couponId, 1L); |
| 166 | + |
| 167 | + mockMvc.perform(delete("/coupons/{coupon-id}", couponId).with(csrf()).with(user("1"))) |
| 168 | + .andExpect(status().isNotFound()); |
| 169 | + } |
| 170 | +} |
0 commit comments