-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
816 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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,35 @@ | ||
== 쿠폰(Coupon) | ||
|
||
쿠폰에 대해 생성/삭제/조회/발급/사용 기능을 제공합니다. | ||
|
||
=== 쿠폰 생성 | ||
|
||
관리자가 쿠폰을 생성합니다. | ||
|
||
[discrete] | ||
==== 요청 | ||
|
||
include::{snippets}/coupons/http-request.adoc[] | ||
|
||
[discrete] | ||
==== 응답 | ||
|
||
include::{snippets}/coupons/http-response.adoc[] | ||
|
||
=== 쿠폰 삭제 (진행 중) | ||
|
||
관리자가 쿠폰을 삭제합니다. | ||
|
||
=== 쿠폰 조회 (진행 중) | ||
|
||
관리자 혹은 사용자가 쿠폰들을 조회합니다. | ||
|
||
사용자가 자신의 보관함에 있는 쿠폰들을 조회합니다. | ||
|
||
=== 쿠폰 발급 (진행 중) | ||
|
||
사용자가 발급 가능한 쿠폰을 선착순으로 발급 받습니다. | ||
|
||
=== 쿠폰 사용 (진행 중) | ||
|
||
사용자가 자신의 보관함에 있는 쿠폰들을 사용합니다. |
This file contains 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
89 changes: 89 additions & 0 deletions
89
src/test/java/com/moabam/api/application/CouponServiceTest.java
This file contains 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,89 @@ | ||
package com.moabam.api.application; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.mockito.BDDMockito.*; | ||
|
||
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; | ||
|
||
import com.moabam.api.domain.entity.Coupon; | ||
import com.moabam.api.domain.entity.enums.CouponType; | ||
import com.moabam.api.domain.repository.CouponRepository; | ||
import com.moabam.api.dto.CreateCouponRequest; | ||
import com.moabam.global.error.exception.BadRequestException; | ||
import com.moabam.global.error.exception.ConflictException; | ||
import com.moabam.global.error.exception.NotFoundException; | ||
import com.moabam.global.error.model.ErrorMessage; | ||
import com.moabam.support.fixture.CouponFixture; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class CouponServiceTest { | ||
|
||
@InjectMocks | ||
private CouponService couponService; | ||
|
||
@Mock | ||
private CouponRepository couponRepository; | ||
|
||
@DisplayName("쿠폰을 성공적으로 발행한다. - Void") | ||
@Test | ||
void couponService_createCoupon() { | ||
// Given | ||
String couponType = CouponType.GOLDEN_COUPON.getTypeName(); | ||
CreateCouponRequest request = CouponFixture.createCouponRequest(couponType, 1, 2); | ||
|
||
given(couponRepository.existsByName(any(String.class))).willReturn(false); | ||
|
||
// When | ||
couponService.createCoupon(1L, request); | ||
|
||
// Then | ||
verify(couponRepository).save(any(Coupon.class)); | ||
} | ||
|
||
@DisplayName("중복된 쿠폰명을 발행한다. - ConflictException") | ||
@Test | ||
void couponService_createCoupon_ConflictException() { | ||
// Given | ||
String couponType = CouponType.GOLDEN_COUPON.getTypeName(); | ||
CreateCouponRequest request = CouponFixture.createCouponRequest(couponType, 1, 2); | ||
|
||
given(couponRepository.existsByName(any(String.class))).willReturn(true); | ||
|
||
// When & Then | ||
assertThatThrownBy(() -> couponService.createCoupon(1L, request)) | ||
.isInstanceOf(ConflictException.class) | ||
.hasMessage(ErrorMessage.CONFLICT_COUPON_NAME.getMessage()); | ||
} | ||
|
||
@DisplayName("존재하지 않는 쿠폰 종류를 발행한다. - NotFoundException") | ||
@Test | ||
void couponService_createCoupon_NotFoundException() { | ||
// Given | ||
CreateCouponRequest request = CouponFixture.createCouponRequest("UNKNOWN", 1, 2); | ||
given(couponRepository.existsByName(any(String.class))).willReturn(false); | ||
|
||
// When & Then | ||
assertThatThrownBy(() -> couponService.createCoupon(1L, request)) | ||
.isInstanceOf(NotFoundException.class) | ||
.hasMessage(ErrorMessage.NOT_FOUND_COUPON_TYPE.getMessage()); | ||
} | ||
|
||
@DisplayName("쿠폰 발급 종료 기간이 시작 기간보다 더 이전인 쿠폰을 발행한다. - BadRequestException") | ||
@Test | ||
void couponService_createCoupon_BadRequestException() { | ||
// Given | ||
String couponType = CouponType.GOLDEN_COUPON.getTypeName(); | ||
CreateCouponRequest request = CouponFixture.createCouponRequest(couponType, 2, 1); | ||
given(couponRepository.existsByName(any(String.class))).willReturn(false); | ||
|
||
// When & Then | ||
assertThatThrownBy(() -> couponService.createCoupon(1L, request)) | ||
.isInstanceOf(BadRequestException.class) | ||
.hasMessage(ErrorMessage.INVALID_COUPON_PERIOD.getMessage()); | ||
} | ||
} |
This file contains 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 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
31 changes: 31 additions & 0 deletions
31
src/test/java/com/moabam/api/dto/CreateCouponRequestTest.java
This file contains 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,31 @@ | ||
package com.moabam.api.dto; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
|
||
class CreateCouponRequestTest { | ||
|
||
@DisplayName("쿠폰 발급 가능 시작 날짜가 올바른 형식으로 입력된다. - yyyy-MM-dd'T'HH:mm") | ||
@Test | ||
void createCouponRequest_StartAt() throws JsonProcessingException { | ||
// Given | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.registerModule(new JavaTimeModule()); | ||
|
||
String json = "{\"startAt\":\"2023-11-09T10:10\"}"; | ||
|
||
// When | ||
CreateCouponRequest actual = objectMapper.readValue(json, CreateCouponRequest.class); | ||
|
||
// Then | ||
assertThat(actual.startAt()).isEqualTo(LocalDateTime.of(2023, 11, 9, 10, 10)); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/test/java/com/moabam/api/presentation/CouponControllerTest.java
This file contains 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,80 @@ | ||
package com.moabam.api.presentation; | ||
|
||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*; | ||
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.moabam.api.domain.entity.enums.CouponType; | ||
import com.moabam.api.domain.repository.CouponRepository; | ||
import com.moabam.api.dto.CouponMapper; | ||
import com.moabam.api.dto.CreateCouponRequest; | ||
import com.moabam.support.fixture.CouponFixture; | ||
import com.moabam.support.fixture.CouponSnippetFixture; | ||
|
||
@Transactional | ||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@AutoConfigureRestDocs | ||
class CouponControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
private CouponRepository couponRepository; | ||
|
||
@DisplayName("쿠폰을 성공적으로 발행한다. - Void") | ||
@Test | ||
void couponController_createCoupon() throws Exception { | ||
// Given | ||
String couponType = CouponType.GOLDEN_COUPON.getTypeName(); | ||
CreateCouponRequest request = CouponFixture.createCouponRequest(couponType, 1, 2); | ||
|
||
// When & Then | ||
mockMvc.perform(post("/admins/coupons") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(request))) | ||
.andDo(print()) | ||
.andDo(document("coupons", | ||
preprocessRequest(prettyPrint()), | ||
preprocessResponse(prettyPrint()), | ||
CouponSnippetFixture.CREATE_COUPON_REQUEST)) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@DisplayName("쿠폰명이 중복된 쿠폰을 발행한다. - ConflictException") | ||
@Test | ||
void couponController_createCoupon_ConflictException() throws Exception { | ||
// Given | ||
String couponType = CouponType.GOLDEN_COUPON.getTypeName(); | ||
CreateCouponRequest request = CouponFixture.createCouponRequest(couponType, 1, 2); | ||
couponRepository.save(CouponMapper.toEntity(1L, request)); | ||
|
||
// When & Then | ||
mockMvc.perform(post("/admins/coupons") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(request))) | ||
.andDo(print()) | ||
.andDo(document("coupons", | ||
preprocessRequest(prettyPrint()), | ||
preprocessResponse(prettyPrint()), | ||
CouponSnippetFixture.CREATE_COUPON_REQUEST)) | ||
.andExpect(status().isConflict()); | ||
} | ||
} |
This file contains 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
19 changes: 19 additions & 0 deletions
19
src/test/java/com/moabam/support/fixture/CouponSnippetFixture.java
This file contains 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,19 @@ | ||
package com.moabam.support.fixture; | ||
|
||
import static org.springframework.restdocs.payload.JsonFieldType.*; | ||
import static org.springframework.restdocs.payload.PayloadDocumentation.*; | ||
|
||
import org.springframework.restdocs.payload.RequestFieldsSnippet; | ||
|
||
public final class CouponSnippetFixture { | ||
|
||
public static final RequestFieldsSnippet CREATE_COUPON_REQUEST = requestFields( | ||
fieldWithPath("name").type(STRING).description("쿠폰명"), | ||
fieldWithPath("description").type(STRING).description("쿠폰 간단 소개 (NULL 가능)"), | ||
fieldWithPath("type").type(STRING).description("쿠폰 종류 (아침, 저녁, 황금, 할인)"), | ||
fieldWithPath("point").type(NUMBER).description("쿠폰 사용 시, 제공하는 포인트량"), | ||
fieldWithPath("stock").type(NUMBER).description("쿠폰을 발급 받을 수 있는 수"), | ||
fieldWithPath("startAt").type(STRING).description("쿠폰 발급 시작 날짜 (Ex: yyyy-MM-dd'T'HH:mm)"), | ||
fieldWithPath("endAt").type(STRING).description("쿠폰 발급 종료 날짜 (Ex: yyyy-MM-dd'T'HH:mm)") | ||
); | ||
} |