-
Notifications
You must be signed in to change notification settings - Fork 1
[Test] 쿠폰 API 테스트 #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Test] 쿠폰 API 테스트 #228
Changes from 1 commit
37556d7
3f3ff85
7222b54
ed9d9d9
50c852b
16ddcb7
635b83b
bafd4f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| == 쿠폰 (Coupons) API | ||
|
|
||
| === 1. 쿠폰 생성 | ||
| `POST /coupons` | ||
|
|
||
| 요청 본문: | ||
| include::{snippets}/coupon-create/request-fields.adoc[] | ||
|
|
||
| 응답: | ||
| include::{snippets}/coupon-create/http-response.adoc[] | ||
|
|
||
|
|
||
| === 2. 쿠폰 조회 | ||
| `GET /coupons` | ||
|
|
||
| 응답 본문: | ||
| include::{snippets}/coupon-list/response-fields.adoc[] | ||
|
|
||
| 응답: | ||
| include::{snippets}/coupon-list/http-response.adoc[] | ||
|
|
||
|
|
||
| === 7. 쿠폰 삭제 | ||
| `DELETE /coupons` | ||
|
|
||
| 응답: | ||
| include::{snippets}/coupon-delete/http-response.adoc[] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
|
|
||
| include::_member.adoc[] | ||
| include::_manager.adoc[] | ||
| include::_coupon.adoc[] | ||
|
|
||
| // include::_product.adoc[] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,13 +32,12 @@ public ResponseEntity<CouponGenerateRequest> createCoupon( | |
| public ResponseEntity<List<CouponResponse>> getCoupon( | ||
| @AuthenticationPrincipal UserDetails userDetails) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q(P1) 해당 어노테이션은 SecurityContextHolder에서 유저 인증 정보를 꺼내오는 것인데, 저희는 MemberUtil을 통해 Service 레어어에서 이를 해결하고 있습니다. 혹시 특별히 사용하신 이유가 있는지 궁금합니다! P3: 또한 공통 응답 포맷도 신경 써주시면 좋겠습니다!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 포맷이 member util을 가져오는 걸로 잘못 이해하고 있었습니다. 내일은 신경쓰고 작성하겠습니다! |
||
| Long memberId = Long.parseLong(userDetails.getUsername()); | ||
| List<CouponResponse> coupons = couponService.getCouponByMember(memberId); | ||
| return ResponseEntity.ok(coupons); | ||
| return ResponseEntity.ok(couponService.getCouponByMember(memberId)); | ||
| } | ||
|
|
||
| @DeleteMapping("/{coupon-id}") | ||
| @DeleteMapping("/{couponId}") | ||
| public ResponseEntity<Void> deleteCoupon( | ||
| @PathVariable("coupon-id") UUID couponId, | ||
| @PathVariable("couponId") UUID couponId, | ||
| @AuthenticationPrincipal UserDetails userDetails) { | ||
|
|
||
| Long memberId = Long.parseLong(userDetails.getUsername()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| package com.irum.come2us.domain.coupon; | ||
|
|
||
| import static org.hamcrest.Matchers.hasSize; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyLong; | ||
| import static org.mockito.Mockito.*; | ||
| import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; | ||
| import static org.springframework.restdocs.payload.PayloadDocumentation.*; | ||
| import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; | ||
| import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
| 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 com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.irum.come2us.domain.coupon.application.service.CouponService; | ||
| import com.irum.come2us.domain.coupon.presentation.controller.CouponController; | ||
| import com.irum.come2us.domain.coupon.presentation.dto.request.CouponGenerateRequest; | ||
| import com.irum.come2us.domain.coupon.presentation.dto.response.CouponResponse; | ||
| import com.irum.come2us.global.config.SecurityTestConfig; | ||
| import com.irum.come2us.global.presentation.advice.exception.CommonException; | ||
| import com.irum.come2us.global.presentation.advice.exception.errorcode.CouponErrorCode; | ||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mockito; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
| import org.springframework.boot.test.context.TestConfiguration; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Import; | ||
| import org.springframework.context.annotation.Primary; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
|
|
||
| @WebMvcTest(CouponController.class) | ||
| @AutoConfigureRestDocs | ||
| @Import({SecurityTestConfig.class, CouponControllerTest.TestConfig.class}) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: 아마 제가 #211 에 올린 커밋을 참고하신 것 같습니다! 테스트 중앙화를 위해 TestConfig를 별도로 분리하였는데, 아직 Develop 머지 전이라 이너 클래스를 참조하도록 자동완성된 듯 합니다! |
||
| public class CouponControllerTest { | ||
| @Autowired private MockMvc mockMvc; | ||
| @Autowired private CouponService couponService; | ||
| @Autowired private ObjectMapper objectMapper; | ||
|
|
||
| @TestConfiguration | ||
| static class TestConfig { | ||
|
|
||
| @Bean | ||
| @Primary | ||
| public CouponService couponService() { | ||
| return Mockito.mock(CouponService.class); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("쿠폰 생성 API") | ||
| void couponCreateApiTest() throws Exception { | ||
|
|
||
| // Given | ||
| CouponGenerateRequest request = | ||
| new CouponGenerateRequest( | ||
| "기간 한정 5000원 할인쿠폰", 5000, LocalDateTime.of(2028, 10, 10, 12, 12, 12)); | ||
| String requestJson = objectMapper.writeValueAsString(request); | ||
|
|
||
| doNothing().when(couponService).createCoupon(any(CouponGenerateRequest.class), anyLong()); | ||
|
|
||
| // when | ||
| mockMvc.perform( | ||
| post("/coupons") | ||
| .with(csrf()) | ||
| .with(user("1")) | ||
| .contentType("application/json") | ||
| .content(requestJson)) | ||
| .andExpect(status().isCreated()) | ||
| .andDo( | ||
| document( | ||
| "coupon-create", | ||
| requestFields( | ||
| fieldWithPath("name").description("쿠폰명"), | ||
| fieldWithPath("discountAmount").description("할인 금액"), | ||
| fieldWithPath("expiration").description("유효기간")))); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("쿠폰 정보 조회 API") | ||
| void couponListTest() throws Exception { | ||
| // Given | ||
| LocalDateTime now = LocalDateTime.of(2028, 10, 10, 12, 12, 12); | ||
| CouponResponse coupon1 = | ||
| new CouponResponse( | ||
| UUID.fromString("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), | ||
| "기간 한정 5000원 할인쿠폰", | ||
| 5000, | ||
| now); | ||
|
|
||
| CouponResponse coupon2 = | ||
| new CouponResponse( | ||
| UUID.fromString("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"), | ||
| "신규가입 10% 할인", | ||
| 10, | ||
| now.plusDays(1)); | ||
|
|
||
| List<CouponResponse> responses = List.of(coupon1, coupon2); | ||
| when(couponService.getCouponByMember(anyLong())).thenReturn(responses); | ||
|
|
||
| // When & Then | ||
| mockMvc.perform(get("/coupons").with(user("1")).accept("application/json")) | ||
| .andDo(print()) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.success").value(true)) | ||
| .andExpect(jsonPath("$.status").value(200)) | ||
| .andExpect(jsonPath("$.data").isArray()) | ||
| .andExpect(jsonPath("$.data", hasSize(2))) | ||
| // coupon1 | ||
| .andExpect(jsonPath("$.data[0].id").value("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")) | ||
| .andExpect(jsonPath("$.data[0].name").value("기간 한정 5000원 할인쿠폰")) | ||
| .andExpect(jsonPath("$.data[0].discountAmount").value(5000)) | ||
| .andExpect(jsonPath("$.data[0].expiration").value("2028-10-10 12:12:12")) | ||
| // coupon2 | ||
| .andExpect(jsonPath("$.data[1].id").value("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb")) | ||
| .andExpect(jsonPath("$.data[1].name").value("신규가입 10% 할인")) | ||
| .andExpect(jsonPath("$.data[1].discountAmount").value(10)) | ||
| .andExpect(jsonPath("$.data[1].expiration").value("2028-10-11 12:12:12")) | ||
| .andExpect(jsonPath("$.timestamp").exists()) | ||
| .andDo( | ||
| document( | ||
| "coupon-list", | ||
| responseFields( | ||
| fieldWithPath("success").description("성공 여부"), | ||
| fieldWithPath("status").description("HTTP 상태 코드"), | ||
| fieldWithPath("data").description("쿠폰 목록"), | ||
| fieldWithPath("data[].id").description("쿠폰 ID"), | ||
| fieldWithPath("data[].name").description("쿠폰명"), | ||
| fieldWithPath("data[].discountAmount").description("할인 금액"), | ||
| fieldWithPath("data[].expiration") | ||
| .description("만료 일시 (yyyy-MM-dd HH:mm:ss)"), | ||
| fieldWithPath("timestamp").description("응답 생성 시각")))); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("쿠폰 삭제 API") | ||
| void couponDeleteApiTest() throws Exception { | ||
| // Given | ||
| UUID couponId = UUID.fromString("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"); | ||
| doNothing().when(couponService).deleteCoupon(couponId, 1L); | ||
|
|
||
| // When & Then | ||
| mockMvc.perform(delete("/coupons/{couponId}", couponId).with(csrf()).with(user("1"))) | ||
| .andExpect(status().isNoContent()) | ||
| .andDo(document("coupon-delete")); | ||
|
|
||
| Mockito.verify(couponService).deleteCoupon(couponId, 1L); | ||
| } | ||
|
|
||
| // import static org.mockito.Mockito.doThrow; | ||
|
|
||
| @Test | ||
| @DisplayName("쿠폰 삭제 API - 쿠폰 없음") | ||
| void couponDelete_NotFound() throws Exception { | ||
| UUID couponId = UUID.fromString("cccccccc-bbbb-bbbb-bbbb-bbbbbbbbbbbb"); | ||
| doThrow(new CommonException(CouponErrorCode.COUPON_NOT_FOUND)) | ||
| .when(couponService) | ||
| .deleteCoupon(couponId, 1L); | ||
|
|
||
| mockMvc.perform(delete("/coupons/{coupon-id}", couponId).with(csrf()).with(user("1"))) | ||
| .andExpect(status().isNotFound()); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
p1 :
3. 쿠폰 삭제로 바꾸시면 좋을것 같습니다.