Skip to content

Commit

Permalink
test: 테스트 코드 체크 스타일 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
hongdosan committed Nov 28, 2023
1 parent 5c55268 commit a14e084
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.moabam.api.application.notification.NotificationService;
import com.moabam.api.domain.coupon.Coupon;
import com.moabam.api.domain.coupon.CouponWallet;
import com.moabam.api.domain.coupon.repository.CouponManageRepository;
Expand All @@ -26,35 +27,54 @@
public class CouponManageService {

private static final long ISSUE_SIZE = 10;
private static final long FIRST_INDEX = 0;
private static final String SUCCESS_ISSUE_BODY = "%s 쿠폰 발행을 성공했습니다. 축하드립니다!";
private static final String FAIL_ISSUE_BODY = "%s 쿠폰 발행을 실패했습니다. 다음 기회에!";

private final ClockHolder clockHolder;
private final NotificationService notificationService;

private final CouponRepository couponRepository;
private final CouponManageRepository couponManageRepository;
private final CouponWalletRepository couponWalletRepository;

private long start = FIRST_INDEX;
private long end = ISSUE_SIZE;

@Scheduled(cron = "0 0 0 * * *")
public void init() {
start = FIRST_INDEX;
end = ISSUE_SIZE;
}

@Scheduled(fixedDelay = 1000)
public void issue() {
LocalDate now = clockHolder.date();
Optional<Coupon> isCoupon = couponRepository.findByStartAt(now);
Optional<Coupon> optionalCoupon = couponRepository.findByStartAt(now);

if (!canIssue(isCoupon)) {
if (optionalCoupon.isEmpty()) {
return;
}

Coupon coupon = isCoupon.get();
Set<Long> membersId = couponManageRepository.popMinQueue(coupon.getName(), ISSUE_SIZE);
Coupon coupon = optionalCoupon.get();
String couponName = coupon.getName();
Set<Long> membersId = couponManageRepository.range(couponName, start, end);

membersId.forEach(memberId -> {
for (Long memberId : membersId) {
int nextStock = couponManageRepository.increaseIssuedStock(coupon.getName());

if (coupon.getStock() < nextStock) {
return;
notificationService.sendCouponIssueResult(memberId, coupon.getName(), FAIL_ISSUE_BODY);
continue;
}

CouponWallet couponWallet = CouponWallet.create(memberId, coupon);
couponWalletRepository.save(couponWallet);
});
notificationService.sendCouponIssueResult(memberId, coupon.getName(), SUCCESS_ISSUE_BODY);
}

start = end;
end = Math.min(couponManageRepository.queueSize(couponName), end + ISSUE_SIZE);
}

public void register(AuthMember authMember, String couponName) {
Expand All @@ -76,16 +96,4 @@ private void validateRegister(String couponName) {
throw new BadRequestException(ErrorMessage.INVALID_COUPON_PERIOD);
}
}

private boolean canIssue(Optional<Coupon> coupon) {
if (coupon.isEmpty()) {
return false;
}

Coupon currentCoupon = coupon.get();
int currentStock = couponManageRepository.getIssuedStock(currentCoupon.getName());
int maxStock = currentCoupon.getStock();

return currentStock < maxStock;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/static/docs/coupon.html
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ <h4 id="_응답_7" class="discrete">응답</h4>
<div id="footer">
<div id="footer-text">
Version 0.0.1-SNAPSHOT<br>
Last updated 2023-11-27 17:06:34 +0900
Last updated 2023-11-27 17:38:59 +0900
</div>
</div>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
import static org.mockito.BDDMockito.*;

import java.time.LocalDate;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.moabam.api.application.notification.NotificationService;
import com.moabam.api.domain.coupon.Coupon;
import com.moabam.api.domain.coupon.CouponWallet;
import com.moabam.api.domain.coupon.repository.CouponManageRepository;
Expand All @@ -36,6 +38,9 @@ class CouponManageServiceTest {
@InjectMocks
CouponManageService couponManageService;

@Mock
NotificationService notificationService;

@Mock
CouponRepository couponRepository;

Expand All @@ -48,24 +53,33 @@ class CouponManageServiceTest {
@Mock
ClockHolder clockHolder;

@DisplayName("쿠폰 발행이 성공적으로 된다.")
@DisplayName("쿠폰 관리 인덱스를 성공적으로 초기화한다.")
@Test
void issue_all_success() {
void init_success() {
// When & Then
assertThatNoException().isThrownBy(() -> couponManageService.init());
}

@DisplayName("10명ㅗ두 쿠폰 발행이 성공적으로 된다.")
@MethodSource("com.moabam.support.fixture.CouponFixture#provideValues_Long")
@ParameterizedTest
void issue_all_success(Set<Long> values) {
// Given
Coupon coupon = CouponFixture.coupon(1000, 100);
Set<Long> membersId = new HashSet<>(Set.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L));

given(clockHolder.date()).willReturn(LocalDate.now());
given(couponRepository.findByStartAt(any(LocalDate.class))).willReturn(Optional.of(coupon));
given(couponManageRepository.getIssuedStock(any(String.class))).willReturn(10);
given(couponManageRepository.popMinQueue(any(String.class), any(long.class))).willReturn(membersId);
given(couponManageRepository.increaseIssuedStock(any(String.class))).willReturn(99);
given(couponManageRepository.range(any(String.class), any(long.class), any(long.class))).willReturn(values);
given(couponManageRepository.increaseIssuedStock(any(String.class))).willReturn(100);

// When
couponManageService.issue();

// Then
verify(couponManageRepository).queueSize(any(String.class));
verify(couponWalletRepository, times(10)).save(any(CouponWallet.class));
verify(notificationService, times(10))
.sendCouponIssueResult(any(Long.class), any(String.class), any(String.class));
}

@DisplayName("발행 가능한 쿠폰이 없다.")
Expand All @@ -79,49 +93,37 @@ void issue_notStartAt() {
couponManageService.issue();

// Then
verify(couponManageRepository, times(0)).getIssuedStock(any(String.class));
verify(couponManageRepository, times(0)).popMinQueue(any(String.class), any(long.class));
verify(couponManageRepository, times(0)).increaseIssuedStock(any(String.class));
verify(couponWalletRepository, times(0)).save(any(CouponWallet.class));
verify(couponManageRepository, times(0)).queueSize(any(String.class));
verify(couponManageRepository, times(0))
.range(any(String.class), any(long.class), any(long.class));
verify(notificationService, times(0))
.sendCouponIssueResult(any(Long.class), any(String.class), any(String.class));
}

@DisplayName("해당 쿠폰은 재고가 마감된 쿠폰이다.")
@Test
void issue_stockEnd() {
// Given
Coupon coupon = CouponFixture.coupon(1000, 100);

given(clockHolder.date()).willReturn(LocalDate.now());
given(couponRepository.findByStartAt(any(LocalDate.class))).willReturn(Optional.of(coupon));
given(couponManageRepository.getIssuedStock(any(String.class))).willReturn(coupon.getStock());

// When
couponManageService.issue();

// Then
verify(couponManageRepository, times(0)).popMinQueue(any(String.class), any(long.class));
verify(couponManageRepository, times(0)).increaseIssuedStock(any(String.class));
verify(couponWalletRepository, times(0)).save(any(CouponWallet.class));
}

@DisplayName("대기열에 남은 인원이 모두 발급받지 못한다.")
@Test
void issue_queue_stockENd() {
@MethodSource("com.moabam.support.fixture.CouponFixture#provideValues_Long")
@ParameterizedTest
void issue_stockEnd(Set<Long> values) {
// Given
Coupon coupon = CouponFixture.coupon(1000, 100);
Set<Long> membersId = new HashSet<>(Set.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L));

given(clockHolder.date()).willReturn(LocalDate.now());
given(couponRepository.findByStartAt(any(LocalDate.class))).willReturn(Optional.of(coupon));
given(couponManageRepository.getIssuedStock(any(String.class))).willReturn(10);
given(couponManageRepository.popMinQueue(any(String.class), any(long.class))).willReturn(membersId);
given(couponManageRepository.range(any(String.class), any(long.class), any(long.class))).willReturn(values);
given(couponManageRepository.increaseIssuedStock(any(String.class))).willReturn(101);

// When
couponManageService.issue();

// Then
verify(couponManageRepository).queueSize(any(String.class));
verify(couponManageRepository, times(10)).increaseIssuedStock(any(String.class));
verify(couponWalletRepository, times(0)).save(any(CouponWallet.class));
verify(notificationService, times(10))
.sendCouponIssueResult(any(Long.class), any(String.class), any(String.class));

}

@WithMember
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ class NotificationServiceTest {
@Mock
ClockHolder clockHolder;

String SUCCESS_ISSUE_BODY = "%s 쿠폰 발행을 성공했습니다. 축하드립니다!";
String FAIL_ISSUE_BODY = "%s 쿠폰 발행을 실패했습니다. 다음 기회에!";
String successIssueResult = "%s 쿠폰 발행을 성공했습니다. 축하드립니다!";

@WithMember
@DisplayName("상대에게 콕 알림을 성공적으로 보낸다. - Void")
Expand Down Expand Up @@ -130,7 +129,7 @@ void sendCouponIssueResult_success() {
given(fcmService.findTokenByMemberId(any(Long.class))).willReturn(Optional.of("FCM-TOKEN"));

// When
notificationService.sendCouponIssueResult(1L, "couponName", SUCCESS_ISSUE_BODY);
notificationService.sendCouponIssueResult(1L, "couponName", successIssueResult);

// Then
verify(fcmService).sendAsync(any(String.class), any(String.class));
Expand All @@ -143,7 +142,7 @@ void sendCouponIssueResult_fcmToken_null() {
given(fcmService.findTokenByMemberId(any(Long.class))).willReturn(Optional.empty());

// When
notificationService.sendCouponIssueResult(1L, "couponName", SUCCESS_ISSUE_BODY);
notificationService.sendCouponIssueResult(1L, "couponName", successIssueResult);

// Then
verify(fcmService).sendAsync(isNull(), any(String.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ZSetRedisRepositoryTest {

String key = "key";
Long value = 1L;
int expire_days = 2;
int expireDays = 2;

@AfterEach
void afterEach() {
Expand All @@ -41,7 +41,7 @@ void afterEach() {
@Test
void addIfAbsent_success() {
// When
zSetRedisRepository.addIfAbsent(key, value, 1, expire_days);
zSetRedisRepository.addIfAbsent(key, value, 1, expireDays);

// Then
assertThat(valueRedisRepository.hasKey(key)).isTrue();
Expand All @@ -51,8 +51,8 @@ void addIfAbsent_success() {
@Test
void setRedisRepository_addIfAbsent_not_update() {
// When
zSetRedisRepository.addIfAbsent(key, value, 1, expire_days);
zSetRedisRepository.addIfAbsent(key, value, 5, expire_days);
zSetRedisRepository.addIfAbsent(key, value, 1, expireDays);
zSetRedisRepository.addIfAbsent(key, value, 5, expireDays);

// Then
assertThat(redisTemplate.opsForZSet().score(key, value)).isEqualTo(1);
Expand All @@ -63,9 +63,9 @@ void setRedisRepository_addIfAbsent_not_update() {
@Test
void range_same_success() {
// Given
zSetRedisRepository.addIfAbsent(key, value + 1, 1, expire_days);
zSetRedisRepository.addIfAbsent(key, value + 2, 2, expire_days);
zSetRedisRepository.addIfAbsent(key, value + 3, 3, expire_days);
zSetRedisRepository.addIfAbsent(key, value + 1, 1, expireDays);
zSetRedisRepository.addIfAbsent(key, value + 2, 2, expireDays);
zSetRedisRepository.addIfAbsent(key, value + 3, 3, expireDays);

// When
Set<Object> actual = zSetRedisRepository.range(key, 0, 3);
Expand All @@ -79,8 +79,8 @@ void range_same_success() {
@Test
void range_more_success() {
// Given
zSetRedisRepository.addIfAbsent(key, value + 1, 1, expire_days);
zSetRedisRepository.addIfAbsent(key, value + 2, 2, expire_days);
zSetRedisRepository.addIfAbsent(key, value + 1, 1, expireDays);
zSetRedisRepository.addIfAbsent(key, value + 2, 2, expireDays);

// When
Set<Object> actual = zSetRedisRepository.range(key, 0, 3);
Expand All @@ -94,11 +94,11 @@ void range_more_success() {
@Test
void range_less_success() {
// Given
zSetRedisRepository.addIfAbsent(key, value + 1, 1, expire_days);
zSetRedisRepository.addIfAbsent(key, value + 2, 2, expire_days);
zSetRedisRepository.addIfAbsent(key, value + 3, 3, expire_days);
zSetRedisRepository.addIfAbsent(key, value + 4, 4, expire_days);
zSetRedisRepository.addIfAbsent(key, value + 5, 5, expire_days);
zSetRedisRepository.addIfAbsent(key, value + 1, 1, expireDays);
zSetRedisRepository.addIfAbsent(key, value + 2, 2, expireDays);
zSetRedisRepository.addIfAbsent(key, value + 3, 3, expireDays);
zSetRedisRepository.addIfAbsent(key, value + 4, 4, expireDays);
zSetRedisRepository.addIfAbsent(key, value + 5, 5, expireDays);

// When
Set<Object> actual = zSetRedisRepository.range(key, 0, 3);
Expand All @@ -111,8 +111,8 @@ void range_less_success() {
@Test
void size_success() {
// Given
zSetRedisRepository.addIfAbsent(key, value + 1, 1, expire_days);
zSetRedisRepository.addIfAbsent(key, value + 2, 2, expire_days);
zSetRedisRepository.addIfAbsent(key, value + 1, 1, expireDays);
zSetRedisRepository.addIfAbsent(key, value + 2, 2, expireDays);

// When
Long actual = zSetRedisRepository.size(key);
Expand Down

0 comments on commit a14e084

Please sign in to comment.