From 6a230431d75b7b3fec56f65fd20376a04d98b524 Mon Sep 17 00:00:00 2001 From: Jarkko Pesonen <435495+jrkkp@users.noreply.github.com> Date: Mon, 14 Aug 2023 12:47:20 +0300 Subject: [PATCH] VKT(Backend): OPHVKTKEH-180 backend test for setting refund --- .../oph/vkt/service/ClerkPaymentService.java | 2 +- .../service/ClerkEnrollmentServiceTest.java | 20 +++-- .../vkt/service/ClerkPaymentServiceTest.java | 81 +++++++++++++++++++ 3 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 backend/vkt/src/test/java/fi/oph/vkt/service/ClerkPaymentServiceTest.java diff --git a/backend/vkt/src/main/java/fi/oph/vkt/service/ClerkPaymentService.java b/backend/vkt/src/main/java/fi/oph/vkt/service/ClerkPaymentService.java index ce57ca512..d34418115 100644 --- a/backend/vkt/src/main/java/fi/oph/vkt/service/ClerkPaymentService.java +++ b/backend/vkt/src/main/java/fi/oph/vkt/service/ClerkPaymentService.java @@ -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())); } } diff --git a/backend/vkt/src/test/java/fi/oph/vkt/service/ClerkEnrollmentServiceTest.java b/backend/vkt/src/test/java/fi/oph/vkt/service/ClerkEnrollmentServiceTest.java index 0c60628b9..b0417cd27 100644 --- a/backend/vkt/src/test/java/fi/oph/vkt/service/ClerkEnrollmentServiceTest.java +++ b/backend/vkt/src/test/java/fi/oph/vkt/service/ClerkEnrollmentServiceTest.java @@ -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); @@ -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) { @@ -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 @@ -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); } } diff --git a/backend/vkt/src/test/java/fi/oph/vkt/service/ClerkPaymentServiceTest.java b/backend/vkt/src/test/java/fi/oph/vkt/service/ClerkPaymentServiceTest.java new file mode 100644 index 000000000..dba0dfbf9 --- /dev/null +++ b/backend/vkt/src/test/java/fi/oph/vkt/service/ClerkPaymentServiceTest.java @@ -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); + } +}