-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 changed file
with
121 additions
and
109 deletions.
There are no files selected for viewing
230 changes: 121 additions & 109 deletions
230
src/test/java/com/splanet/splanet/subscription/service/SubscriptionServiceTest.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 |
---|---|---|
@@ -1,109 +1,121 @@ | ||
//package com.splanet.splanet.subscription.service; | ||
// | ||
//import com.splanet.splanet.subscription.dao.SubscriptionDao; | ||
//import com.splanet.splanet.subscription.dto.SubscriptionDto; | ||
//import com.splanet.splanet.subscription.entity.Subscription; | ||
//import com.splanet.splanet.core.exception.BusinessException; | ||
//import org.junit.jupiter.api.BeforeEach; | ||
//import org.junit.jupiter.api.Test; | ||
//import org.mockito.InjectMocks; | ||
//import org.mockito.Mock; | ||
//import org.mockito.MockitoAnnotations; | ||
//import org.springframework.http.ResponseEntity; | ||
// | ||
//import java.time.LocalDateTime; | ||
//import java.util.Optional; | ||
// | ||
//import static org.junit.jupiter.api.Assertions.*; | ||
//import static org.mockito.Mockito.*; | ||
// | ||
//class SubscriptionServiceTest { | ||
// | ||
// @Mock | ||
// private SubscriptionDao subscriptionDao; | ||
// | ||
// @InjectMocks | ||
// private SubscriptionService subscriptionService; | ||
// | ||
// private Long userId; | ||
// private Subscription subscription; | ||
// private SubscriptionDto subscriptionDto; | ||
// | ||
// @BeforeEach | ||
// void setUp() { | ||
// MockitoAnnotations.openMocks(this); | ||
// userId = 1L; | ||
// | ||
// // 모킹할 Subscription 객체 | ||
// subscription = Subscription.builder() | ||
// .userId(userId) | ||
// .type(Subscription.Type.MONTHLY) | ||
// .status(Subscription.Status.ACTIVE) | ||
// .startDate(LocalDateTime.now()) | ||
// .endDate(LocalDateTime.now().plusMonths(1)) | ||
// .build(); | ||
// | ||
// // SubscriptionDto 객체 생성 (가정) | ||
// subscriptionDto = SubscriptionDto.fromSubscription(subscription); | ||
// } | ||
// | ||
// @Test | ||
// void 구독조회성공() { | ||
// // given | ||
// when(subscriptionDao.findLatestActiveSubscription(userId)).thenReturn(Optional.of(subscription)); | ||
// | ||
// // when | ||
// ResponseEntity<SubscriptionDto> response = subscriptionService.getSubscription(userId); | ||
// | ||
// // then | ||
// assertEquals(200, response.getStatusCodeValue()); | ||
// assertNotNull(response.getBody()); | ||
// assertEquals("구독 정보가 성공적으로 조회되었습니다.", response.getHeaders().getFirst("Message")); | ||
// } | ||
// | ||
// @Test | ||
// void 구독조회실패_사용자미인증() { | ||
// // given | ||
// userId = null; | ||
// | ||
// // when & then | ||
// assertThrows(BusinessException.class, () -> subscriptionService.getSubscription(userId)); | ||
// } | ||
// | ||
// @Test | ||
// void 구독취소성공() { | ||
// // given | ||
// when(subscriptionDao.findLatestActiveSubscription(userId)).thenReturn(Optional.of(subscription)); | ||
// | ||
// // when | ||
// ResponseEntity<String> response = subscriptionService.cancelSubscription(userId); | ||
// | ||
// // then | ||
// assertEquals(200, response.getStatusCodeValue()); | ||
// assertEquals("구독이 성공적으로 취소되었습니다.", response.getBody()); | ||
// } | ||
// | ||
// @Test | ||
// void 구독취소실패_이미취소됨() { | ||
// // given | ||
// subscription.setStatus(Subscription.Status.CANCELED); | ||
// when(subscriptionDao.findLatestActiveSubscription(userId)).thenReturn(Optional.of(subscription)); | ||
// | ||
// // when & then | ||
// assertThrows(BusinessException.class, () -> subscriptionService.cancelSubscription(userId)); | ||
// } | ||
// | ||
// @Test | ||
// void 구독하기성공() { | ||
// // given | ||
// when(subscriptionDao.saveSubscription(any(Subscription.class))).thenReturn(subscription); | ||
// | ||
// // when | ||
// ResponseEntity<SubscriptionDto> response = subscriptionService.subscribe(userId, Subscription.Type.MONTHLY); | ||
// | ||
// // then | ||
// assertEquals(200, response.getStatusCodeValue()); | ||
// assertNotNull(response.getBody()); | ||
// assertEquals("구독이 성공적으로 구매되었습니다.", response.getHeaders().getFirst("Message")); | ||
// } | ||
//} | ||
package com.splanet.splanet.subscription.service; | ||
|
||
import com.splanet.splanet.subscription.dto.SubscriptionRequest; | ||
import com.splanet.splanet.subscription.dto.SubscriptionResponse; | ||
import com.splanet.splanet.subscription.entity.Subscription; | ||
import com.splanet.splanet.subscription.repository.SubscriptionRepository; | ||
import com.splanet.splanet.core.exception.BusinessException; | ||
import com.splanet.splanet.core.exception.ErrorCode; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
class SubscriptionServiceTest { | ||
|
||
private SubscriptionRepository subscriptionRepository; | ||
private SubscriptionService subscriptionService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
subscriptionRepository = Mockito.mock(SubscriptionRepository.class); | ||
subscriptionService = new SubscriptionService(subscriptionRepository); | ||
} | ||
|
||
@Test | ||
void 구독조회성공() { | ||
Long userId = 1L; | ||
Subscription subscription = mock(Subscription.class); | ||
when(subscription.getId()).thenReturn(123L); | ||
when(subscription.getType()).thenReturn(Subscription.Type.MONTHLY); | ||
when(subscription.getStatus()).thenReturn(Subscription.Status.ACTIVE); | ||
when(subscription.getStartDate()).thenReturn(LocalDateTime.now()); | ||
when(subscription.getEndDate()).thenReturn(LocalDateTime.now().plusMonths(1)); | ||
|
||
when(subscriptionRepository.findTopByUserIdAndStatusOrderByStartDateDesc(userId, Subscription.Status.ACTIVE)) | ||
.thenReturn(java.util.Optional.of(subscription)); | ||
|
||
SubscriptionResponse response = subscriptionService.getSubscription(userId).getBody(); | ||
|
||
assertNotNull(response); | ||
assertEquals("구독 정보가 성공적으로 조회되었습니다.", response.getMessage()); | ||
assertEquals(123L, response.getSubscription().getId()); | ||
} | ||
|
||
@Test | ||
void 구독조회실패_구독미존재() { | ||
Long userId = 1L; | ||
|
||
when(subscriptionRepository.findTopByUserIdAndStatusOrderByStartDateDesc(userId, Subscription.Status.ACTIVE)) | ||
.thenReturn(java.util.Optional.empty()); | ||
|
||
BusinessException exception = assertThrows(BusinessException.class, () -> { | ||
subscriptionService.getSubscription(userId); | ||
}); | ||
|
||
assertEquals(ErrorCode.SUBSCRIPSTION_NOT_FOUND, exception.getErrorCode()); | ||
} | ||
|
||
@Test | ||
void 구독취소성공() { | ||
Long userId = 1L; | ||
Subscription subscription = mock(Subscription.class); | ||
when(subscription.getId()).thenReturn(123L); | ||
when(subscription.getUserId()).thenReturn(userId); | ||
when(subscription.getStatus()).thenReturn(Subscription.Status.ACTIVE); | ||
|
||
when(subscriptionRepository.findTopByUserIdAndStatusOrderByStartDateDesc(userId, Subscription.Status.ACTIVE)) | ||
.thenReturn(java.util.Optional.of(subscription)); | ||
|
||
subscriptionService.cancelSubscription(userId); | ||
|
||
verify(subscriptionRepository, times(1)).save(subscription); | ||
verify(subscription).cancel(); // cancel 메소드 호출 확인 | ||
} | ||
|
||
@Test | ||
void 구독취소실패_이미취소됨() { | ||
Long userId = 1L; | ||
Subscription subscription = mock(Subscription.class); | ||
when(subscription.getId()).thenReturn(123L); | ||
when(subscription.getUserId()).thenReturn(userId); | ||
when(subscription.getStatus()).thenReturn(Subscription.Status.CANCELED); | ||
|
||
when(subscriptionRepository.findTopByUserIdAndStatusOrderByStartDateDesc(userId, Subscription.Status.ACTIVE)) | ||
.thenReturn(java.util.Optional.of(subscription)); | ||
|
||
BusinessException exception = assertThrows(BusinessException.class, () -> { | ||
subscriptionService.cancelSubscription(userId); | ||
}); | ||
|
||
assertEquals(ErrorCode.ALREADY_CANCELED, exception.getErrorCode()); | ||
} | ||
|
||
@Test | ||
void 구독하기성공() { | ||
Long userId = 1L; | ||
SubscriptionRequest request = new SubscriptionRequest(); | ||
request.setType(Subscription.Type.MONTHLY); | ||
|
||
Subscription subscription = mock(Subscription.class); | ||
when(subscription.getId()).thenReturn(123L); | ||
when(subscription.getUserId()).thenReturn(userId); | ||
when(subscription.getType()).thenReturn(Subscription.Type.MONTHLY); | ||
when(subscription.getStatus()).thenReturn(Subscription.Status.ACTIVE); | ||
when(subscription.getStartDate()).thenReturn(LocalDateTime.now()); | ||
when(subscription.getEndDate()).thenReturn(LocalDateTime.now().plusMonths(1)); | ||
|
||
when(subscriptionRepository.save(any(Subscription.class))).thenReturn(subscription); | ||
|
||
SubscriptionResponse response = subscriptionService.subscribe(userId, request).getBody(); | ||
|
||
assertNotNull(response); | ||
assertEquals("구독이 성공적으로 구매되었습니다.", response.getMessage()); | ||
assertEquals(123L, response.getSubscription().getId()); | ||
} | ||
} |