-
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
Merged
Merged
[Test] 쿠폰 API 테스트 #228
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
37556d7
test #223: 쿠폰 api 테스트
Moses249 3f3ff85
test #223: 수정사항 반영
Moses249 7222b54
test #223: 수정사항 반영
Moses249 ed9d9d9
test #223: 수정사항 반영
Moses249 50c852b
test #223: 수정사항 반영
Moses249 16ddcb7
test #223: 테스트 코드 수정
Moses249 635b83b
test #223: 쿠폰 API 테스트
Moses249 bafd4f2
Merge branch 'develop' into test/#223-coupon-api
Moses249 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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[] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 8 additions & 1 deletion
9
src/main/java/com/irum/come2us/domain/coupon/presentation/dto/response/CouponResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
src/test/java/com/irum/come2us/domain/coupon/CouponControllerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| 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.domain.member.domain.entity.Member; | ||
| 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 com.irum.come2us.global.util.MemberUtil; | ||
| 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.test.web.servlet.MockMvc; | ||
|
|
||
| @WebMvcTest(CouponController.class) | ||
| @AutoConfigureRestDocs | ||
| @Import(SecurityTestConfig.class) | ||
| public class CouponControllerTest { | ||
| @Autowired private MockMvc mockMvc; | ||
| @Autowired private CouponService couponService; | ||
| @Autowired private MemberUtil memberUtil; | ||
| @Autowired private ObjectMapper objectMapper; | ||
|
|
||
| private void mockCurrentMemberId(long memberId) { | ||
| Member member = Mockito.mock(Member.class); | ||
| when(member.getMemberId()).thenReturn(memberId); | ||
| when(memberUtil.getCurrentMember()).thenReturn(member); | ||
| } | ||
|
|
||
| @TestConfiguration | ||
| static class TestConfig { | ||
|
|
||
| @Bean | ||
| public CouponService couponService() { | ||
| return Mockito.mock(CouponService.class); | ||
| } | ||
|
|
||
| @Bean | ||
| public MemberUtil memberUtil() { | ||
| return Mockito.mock(MemberUtil.class); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("쿠폰 생성 API") | ||
| void couponCreateApiTest() throws Exception { | ||
| mockCurrentMemberId(1L); | ||
| // 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 { | ||
| mockCurrentMemberId(1L); | ||
| // 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 { | ||
| mockCurrentMemberId(1L); | ||
| // 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/{couponId}", couponId).with(csrf()).with(user("1"))) | ||
| .andExpect(status().isNotFound()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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. 쿠폰 삭제로 바꾸시면 좋을것 같습니다.