Skip to content

Commit

Permalink
Added display name for payment service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IhorTrokhymchuk committed May 3, 2024
1 parent 604dcb4 commit fe2a43e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.List;
import java.util.Optional;
import org.example.bookingappliation.dto.payment.responce.PaymentDto;
import org.example.bookingappliation.dto.payment.responce.PaymentInfoDto;
import org.example.bookingappliation.exception.payment.CantPaidBookingException;
import org.example.bookingappliation.exception.repository.EntityNotFoundException;
Expand All @@ -25,6 +26,7 @@
import org.example.bookingappliation.testutil.BookingSampleUtil;
import org.example.bookingappliation.testutil.PaymentSampleUtil;
import org.example.bookingappliation.testutil.UserSampleUtil;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -43,19 +45,21 @@ class PaymentServiceTest {
private PaymentServiceImpl paymentService;

@Test
@DisplayName("Create checkout session with not valid booking id")
void createPaymentCheckoutSession_createPaymentSessionWithNotValidId_throwException() {
User sampleUser = UserSampleUtil.createSampleUser(1L);

when(bookingRepository.findById(1L)).thenReturn(Optional.empty());
when(bookingRepository.findById(-1L)).thenReturn(Optional.empty());

assertThrows(EntityNotFoundException.class,
() -> paymentService.createPaymentCheckoutSession(1L, sampleUser.getEmail()));
() -> paymentService.createPaymentCheckoutSession(-1L, sampleUser.getEmail()));

verify(bookingRepository, times(1)).findById(1L);
verify(bookingRepository, times(1)).findById(-1L);
verifyNoMoreInteractions(bookingRepository);
}

@Test
@DisplayName("Create checkout session with not valid booking status")
void createPaymentCheckoutSession_createPaymentSessionWithNotValidStatus_throwException() {
User sampleUser = UserSampleUtil.createSampleUser(1L);
Booking sampleBooking = BookingSampleUtil.createSampleBooking(1L);
Expand All @@ -71,6 +75,7 @@ void createPaymentCheckoutSession_createPaymentSessionWithNotValidStatus_throwEx
}

@Test
@DisplayName("Create checkout session with not valid user")
void createPaymentCheckoutSession_createPaymentSessionWithNotValidUser_throwException() {
User sampleUser = UserSampleUtil.createSampleUser(1L);
sampleUser.setEmail("not valid email");
Expand All @@ -86,6 +91,7 @@ void createPaymentCheckoutSession_createPaymentSessionWithNotValidUser_throwExce
}

@Test
@DisplayName("Create checkout session with not valid payment status")
void createPaymentCheckoutSession_createWithNotValidPaymentStatus_throwException() {
Payment samplePayment = PaymentSampleUtil.createSamplePayment(1L);
samplePayment.getStatus().setName(PaymentStatus.PaymentStatusName.CANCEL);
Expand All @@ -103,6 +109,7 @@ void createPaymentCheckoutSession_createWithNotValidPaymentStatus_throwException
}

@Test
@DisplayName("Create checkout session with exist payment")
void createPaymentCheckoutSession_createPaymentSessionWithExistValidPayment_returnUrl() {
Payment samplePayment = PaymentSampleUtil.createSamplePayment(1L);

Expand All @@ -121,11 +128,48 @@ void createPaymentCheckoutSession_createPaymentSessionWithExistValidPayment_retu
}

@Test
@DisplayName("Success successes payment")
void successPayment_successSuccessPayment_returnPaidPayment() {
Payment samplePayment = PaymentSampleUtil.createSamplePayment(1L);
samplePayment.getStatus().setName(PaymentStatus.PaymentStatusName.PAID);
PaymentDto samplePaymentDto = PaymentSampleUtil.createSamplePaymentDto(1L);
samplePaymentDto.getStatus().setName(PaymentStatus.PaymentStatusName.PAID.name());

when(paymentRepository.findPaymentBySessionId(samplePayment.getSessionId()))
.thenReturn(Optional.of(samplePayment));
when(paymentMapper.toDto(samplePayment)).thenReturn(samplePaymentDto);
PaymentDto result = paymentService.successPayment(samplePayment.getSessionId());

assertNotNull(result);
assertEquals(samplePaymentDto, result);

verify(paymentRepository, times(1)).findPaymentBySessionId(samplePayment.getSessionId());
verify(paymentMapper, times(1)).toDto(samplePayment);
verifyNoMoreInteractions(bookingRepository, paymentRepository, paymentMapper);
}

@Test
@DisplayName("Success payment with not valid session id")
void successPayment_successPaymentWithNotValidSessionId_ThrowException() {
String nonValidSessionId = "nonValidSessionId";

when(paymentRepository.findPaymentBySessionId(nonValidSessionId))
.thenReturn(Optional.empty());

assertThrows(EntityNotFoundException.class,
() -> paymentService.successPayment(nonValidSessionId));

verify(paymentRepository, times(1)).findPaymentBySessionId(nonValidSessionId);
verifyNoMoreInteractions(bookingRepository, paymentRepository, paymentMapper);
}

@Test
@DisplayName("Find all payment with valid data")
void findAllPayments_findAllPaymentsWithValidData_returnPaymentsList() {
Payment samplePayment1 = PaymentSampleUtil.createSamplePayment(1L);
Payment samplePayment2 = PaymentSampleUtil.createSamplePayment(2L);
PaymentInfoDto samplePaymentDto1 = PaymentSampleUtil.createSamplePaymentDto(1L);
PaymentInfoDto samplePaymentDto2 = PaymentSampleUtil.createSamplePaymentDto(2L);
PaymentInfoDto samplePaymentDto1 = PaymentSampleUtil.createSamplePaymentInfoDto(1L);
PaymentInfoDto samplePaymentDto2 = PaymentSampleUtil.createSamplePaymentInfoDto(2L);

when(paymentRepository.findAll()).thenReturn(List.of(samplePayment1, samplePayment2));
when(paymentMapper.toInfoDto(samplePayment1)).thenReturn(samplePaymentDto1);
Expand All @@ -143,11 +187,12 @@ void findAllPayments_findAllPaymentsWithValidData_returnPaymentsList() {
}

@Test
@DisplayName("Find all payment by user email with valid data")
void findPaymentsByUserEmail_findUserPaymentsWithValidData_returnPaymentsList() {
Payment samplePayment1 = PaymentSampleUtil.createSamplePayment(1L);
Payment samplePayment2 = PaymentSampleUtil.createSamplePayment(2L);
PaymentInfoDto samplePaymentDto1 = PaymentSampleUtil.createSamplePaymentDto(1L);
PaymentInfoDto samplePaymentDto2 = PaymentSampleUtil.createSamplePaymentDto(2L);
PaymentInfoDto samplePaymentDto1 = PaymentSampleUtil.createSamplePaymentInfoDto(1L);
PaymentInfoDto samplePaymentDto2 = PaymentSampleUtil.createSamplePaymentInfoDto(2L);
String email = samplePayment1.getBooking().getUser().getEmail();

when(paymentRepository.findPaymentByBookingUserEmail(email))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;
import java.time.LocalDate;
import org.example.bookingappliation.dto.payment.responce.PaymentDto;
import org.example.bookingappliation.dto.payment.responce.PaymentInfoDto;
import org.example.bookingappliation.model.booking.Booking;
import org.example.bookingappliation.model.payment.Payment;
Expand All @@ -23,7 +24,7 @@ public static Payment createSamplePayment(Long param) {
return payment;
}

public static PaymentInfoDto createSamplePaymentDto(Long param) {
public static PaymentInfoDto createSamplePaymentInfoDto(Long param) {
Payment samplePayment = createSamplePayment(param);
PaymentInfoDto paymentDto = new PaymentInfoDto();
paymentDto.setStatus(PaymentStatusSampleUtil
Expand All @@ -32,4 +33,13 @@ public static PaymentInfoDto createSamplePaymentDto(Long param) {
paymentDto.setBookingId(samplePayment.getBooking().getId());
return paymentDto;
}

public static PaymentDto createSamplePaymentDto(Long param) {
Payment samplePayment = createSamplePayment(param);
PaymentDto paymentDto = new PaymentDto();
paymentDto.setStatus(PaymentStatusSampleUtil
.createSamplePaymentStatusDto(1L, PaymentStatus.PaymentStatusName.PENDING));
paymentDto.setSessionId(samplePayment.getSessionId());
return paymentDto;
}
}

0 comments on commit fe2a43e

Please sign in to comment.