Skip to content

Commit

Permalink
VKT(Backend): OPHVKTKEH-180 backend test for setting refund
Browse files Browse the repository at this point in the history
  • Loading branch information
jrkkp committed Aug 14, 2023
1 parent 4d2f6fb commit 6a23043
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public ClerkPaymentDTO setRefunded(final long paymentId) {
final ClerkPaymentAuditDTO newAuditDto = ClerkPaymentUtil.createClerkPaymentAuditDTO(payment);
auditService.logUpdate(VktOperation.REFUND_PAYMENT, payment.getId(), oldAuditDto, newAuditDto);

return ClerkPaymentUtil.createClerkPaymentDTO(payment);
return ClerkPaymentUtil.createClerkPaymentDTO(paymentRepository.getReferenceById(payment.getId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public void testUpdate() {
entityManager.persist(person);
entityManager.persist(enrollment);

final ClerkEnrollmentAuditDTO oldEnrollmentDto = ClerkEnrollmentUtil.createClerkEnrollmentAuditDTO(enrollment);
final ClerkEnrollmentAuditDTO oldAuditDto = ClerkEnrollmentUtil.createClerkEnrollmentAuditDTO(enrollment);
final ClerkEnrollmentUpdateDTO dto = createUpdateDTOAddingOne(enrollment);
final ClerkEnrollmentDTO responseDTO = clerkEnrollmentService.update(dto);
final ClerkEnrollmentAuditDTO newEnrollmentDto = ClerkEnrollmentUtil.createClerkEnrollmentAuditDTO(enrollment);
final ClerkEnrollmentAuditDTO newAuditDto = ClerkEnrollmentUtil.createClerkEnrollmentAuditDTO(enrollment);

assertEquals(responseDTO.id(), dto.id());
assertEquals(responseDTO.version(), dto.version() + 1);
Expand All @@ -116,8 +116,7 @@ public void testUpdate() {
assertEquals(responseDTO.town(), dto.town());
assertEquals(responseDTO.country(), dto.country());

verify(auditService)
.logUpdate(VktOperation.UPDATE_ENROLLMENT, enrollment.getId(), oldEnrollmentDto, newEnrollmentDto);
verify(auditService).logUpdate(VktOperation.UPDATE_ENROLLMENT, enrollment.getId(), oldAuditDto, newAuditDto);
}

private ClerkEnrollmentUpdateDTO createUpdateDTOAddingOne(final Enrollment enrollment) {
Expand Down Expand Up @@ -203,18 +202,17 @@ public void testMove() {
entityManager.persist(person);
entityManager.persist(enrollment);

final ClerkEnrollmentAuditDTO oldEnrollmentDto = ClerkEnrollmentUtil.createClerkEnrollmentAuditDTO(enrollment);
final ClerkEnrollmentAuditDTO oldAuditDto = ClerkEnrollmentUtil.createClerkEnrollmentAuditDTO(enrollment);
final int originalEnrollmentVersion = enrollment.getVersion();

final ClerkEnrollmentMoveDTO moveDTO = createMoveDTO(enrollment, examEvent2);
final ClerkEnrollmentDTO responseDTO = clerkEnrollmentService.move(moveDTO);
final ClerkEnrollmentAuditDTO newEnrollmentDto = ClerkEnrollmentUtil.createClerkEnrollmentAuditDTO(enrollment);
final ClerkEnrollmentAuditDTO newAuditDto = ClerkEnrollmentUtil.createClerkEnrollmentAuditDTO(enrollment);

assertEquals(originalEnrollmentVersion + 1, responseDTO.version());
assertEquals(examEvent2.getId(), enrollmentRepository.getReferenceById(enrollment.getId()).getExamEvent().getId());

verify(auditService)
.logUpdate(VktOperation.MOVE_ENROLLMENT, enrollment.getId(), oldEnrollmentDto, newEnrollmentDto);
verify(auditService).logUpdate(VktOperation.MOVE_ENROLLMENT, enrollment.getId(), oldAuditDto, newAuditDto);
}

@Test
Expand Down Expand Up @@ -280,19 +278,19 @@ public void testCreatePaymentLink() {
entityManager.persist(person);
entityManager.persist(enrollment);

final ClerkEnrollmentAuditDTO oldEnrollmentDto = ClerkEnrollmentUtil.createClerkEnrollmentAuditDTO(enrollment);
final ClerkEnrollmentAuditDTO oldAuditDto = ClerkEnrollmentUtil.createClerkEnrollmentAuditDTO(enrollment);
final ClerkPaymentLinkDTO clerkPaymentLinkDTO = clerkEnrollmentService.createPaymentLink(enrollment.getId());
final String expectedUrl = String.format(
"http://localhost/examEvent/%d/redirect/269a2da4-58bb-45eb-b125-522b77e9167c",
examEvent.getId()
);
final ClerkEnrollmentAuditDTO newEnrollmentDto = ClerkEnrollmentUtil.createClerkEnrollmentAuditDTO(enrollment);
final ClerkEnrollmentAuditDTO newAuditDto = ClerkEnrollmentUtil.createClerkEnrollmentAuditDTO(enrollment);

assertEquals(expectedUrl, clerkPaymentLinkDTO.url());
assertEquals("269a2da4-58bb-45eb-b125-522b77e9167c", enrollment.getPaymentLinkHash());
assertTrue(enrollment.getPaymentLinkExpiresAt().isAfter(LocalDateTime.now()));

verify(auditService)
.logUpdate(VktOperation.UPDATE_ENROLLMENT_PAYMENT_LINK, enrollment.getId(), oldEnrollmentDto, newEnrollmentDto);
.logUpdate(VktOperation.UPDATE_ENROLLMENT_PAYMENT_LINK, enrollment.getId(), oldAuditDto, newAuditDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package fi.oph.vkt.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import fi.oph.vkt.Factory;
import fi.oph.vkt.api.dto.clerk.ClerkPaymentDTO;
import fi.oph.vkt.audit.AuditService;
import fi.oph.vkt.audit.VktOperation;
import fi.oph.vkt.audit.dto.ClerkPaymentAuditDTO;
import fi.oph.vkt.model.Enrollment;
import fi.oph.vkt.model.ExamEvent;
import fi.oph.vkt.model.Payment;
import fi.oph.vkt.model.Person;
import fi.oph.vkt.repository.PaymentRepository;
import fi.oph.vkt.util.ClerkPaymentUtil;
import fi.oph.vkt.util.UUIDSource;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.env.Environment;
import org.springframework.security.test.context.support.WithMockUser;

@WithMockUser
@DataJpaTest
public class ClerkPaymentServiceTest {

@Resource
private PaymentRepository paymentRepository;

@MockBean
private AuditService auditService;

private ClerkPaymentService clerkPaymentService;

@Resource
private TestEntityManager entityManager;

@BeforeEach
public void setup() {
final Environment environment = mock(Environment.class);
when(environment.getRequiredProperty("app.base-url.api")).thenReturn("http://localhost");

final UUIDSource uuidSource = mock(UUIDSource.class);
when(uuidSource.getRandomNonce()).thenReturn("269a2da4-58bb-45eb-b125-522b77e9167c");

clerkPaymentService = new ClerkPaymentService(paymentRepository, auditService);
}

@Test
public void testSetRefunded() {
final ExamEvent examEvent = Factory.examEvent();
final Person person = Factory.person();
final Enrollment enrollment = Factory.enrollment(examEvent, person);
final Payment payment1 = Factory.payment(enrollment);
final Payment payment2 = Factory.payment(enrollment);

entityManager.persist(examEvent);
entityManager.persist(person);
entityManager.persist(enrollment);
entityManager.persist(payment1);
entityManager.persist(payment2);

final ClerkPaymentAuditDTO oldAuditDto = ClerkPaymentUtil.createClerkPaymentAuditDTO(payment1);
final ClerkPaymentDTO responseDTO = clerkPaymentService.setRefunded(payment1.getId());
final ClerkPaymentAuditDTO newAuditDto = ClerkPaymentUtil.createClerkPaymentAuditDTO(payment1);

assertEquals(payment1.getId(), responseDTO.id());
assertNotNull(responseDTO.refundedAt());
assertNull(payment2.getRefundedAt());

verify(auditService).logUpdate(VktOperation.REFUND_PAYMENT, payment1.getId(), oldAuditDto, newAuditDto);
}
}

0 comments on commit 6a23043

Please sign in to comment.