From aba0e2f5521c124f180fbcba1b53c658bd6c61e9 Mon Sep 17 00:00:00 2001 From: Jarkko Pesonen <435495+jrkkp@users.noreply.github.com> Date: Wed, 8 Jan 2025 09:51:38 +0200 Subject: [PATCH] VKT(Backend): More backend tests for examiner exam event --- .../vkt/src/test/java/fi/oph/vkt/Factory.java | 1 + .../service/ExaminerExamEventServiceTest.java | 152 ++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 backend/vkt/src/test/java/fi/oph/vkt/service/ExaminerExamEventServiceTest.java diff --git a/backend/vkt/src/test/java/fi/oph/vkt/Factory.java b/backend/vkt/src/test/java/fi/oph/vkt/Factory.java index bb7a52931..533f4350e 100644 --- a/backend/vkt/src/test/java/fi/oph/vkt/Factory.java +++ b/backend/vkt/src/test/java/fi/oph/vkt/Factory.java @@ -58,6 +58,7 @@ public static ExaminerExamEvent examinerExamEvent( examEvent.setRegistrationCloses(LocalDate.now().plusDays(4)); examEvent.setHidden(false); examEvent.setMaxParticipants(10L); + examEvent.setLocation("Mordor"); return examEvent; } diff --git a/backend/vkt/src/test/java/fi/oph/vkt/service/ExaminerExamEventServiceTest.java b/backend/vkt/src/test/java/fi/oph/vkt/service/ExaminerExamEventServiceTest.java new file mode 100644 index 000000000..b87591795 --- /dev/null +++ b/backend/vkt/src/test/java/fi/oph/vkt/service/ExaminerExamEventServiceTest.java @@ -0,0 +1,152 @@ +package fi.oph.vkt.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +import fi.oph.vkt.Factory; +import fi.oph.vkt.api.dto.MunicipalityDTO; +import fi.oph.vkt.api.dto.examiner.ExaminerExamEventDTO; +import fi.oph.vkt.api.dto.examiner.ExaminerExamEventUpsertDTO; +import fi.oph.vkt.audit.AuditService; +import fi.oph.vkt.model.Examiner; +import fi.oph.vkt.model.ExaminerExamEvent; +import fi.oph.vkt.model.Municipality; +import fi.oph.vkt.repository.*; +import fi.oph.vkt.util.UUIDSource; +import fi.oph.vkt.util.exception.APIException; +import fi.oph.vkt.util.exception.APIExceptionType; +import jakarta.annotation.Resource; +import java.util.List; +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 ExaminerExamEventServiceTest { + + @Resource + private ExaminerExamEventRepository examinerExamEventRepository; + + @Resource + private ExaminerRepository examinerRepository; + + @MockBean + private AuditService auditService; + + @Resource + private TestEntityManager entityManager; + + private ExaminerExamEventService examinerExamEventService; + + @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"); + + examinerExamEventService = + new ExaminerExamEventService(examinerExamEventRepository, environment, auditService, examinerRepository); + } + + @Test + public void testGetExamEvent() { + final Examiner examiner = Factory.examiner(); + final Municipality municipality = Factory.municipality(); + final ExaminerExamEvent examEvent = Factory.examinerExamEvent(examiner, municipality); + + entityManager.persist(examiner); + entityManager.persist(municipality); + entityManager.persist(examEvent); + + final ExaminerExamEventDTO dto = examinerExamEventService.getExamEvent(examiner.getOid(), examEvent.getId()); + + assertEquals(examEvent.getId(), dto.id()); + assertEquals(examEvent.getVersion(), dto.version()); + assertEquals(examEvent.getLanguage(), dto.language()); + assertEquals(examEvent.getDate(), dto.date()); + assertEquals(examEvent.getLocation(), dto.location()); + assertEquals(examEvent.getExamTime(), dto.examTime()); + assertEquals(examEvent.getOtherInformation(), dto.otherInformation()); + assertEquals(examEvent.getRegistrationCloses(), dto.registrationCloses()); + assertEquals(examEvent.getMaxParticipants(), dto.maxParticipants()); + } + + @Test + public void testGetExamEventOidMismatch() { + final Examiner examiner = Factory.examiner(); + final Municipality municipality = Factory.municipality(); + final ExaminerExamEvent examEvent = Factory.examinerExamEvent(examiner, municipality); + + entityManager.persist(examiner); + entityManager.persist(municipality); + entityManager.persist(examEvent); + + final APIException ex = assertThrows( + APIException.class, + () -> examinerExamEventService.getExamEvent("5.4.3.2.1", examEvent.getId()) + ); + + assertEquals(APIExceptionType.EXAMINER_EXAM_EVENT_EXAMINER_MISMATCH, ex.getExceptionType()); + verifyNoInteractions(auditService); + } + + @Test + public void testUpdate() { + final Examiner examiner = Factory.examiner(); + final Municipality municipality = Factory.municipality(); + final ExaminerExamEvent examEvent = Factory.examinerExamEvent(examiner, municipality); + + examiner.setMunicipalities(List.of(municipality)); + + entityManager.persist(municipality); + entityManager.persist(examiner); + entityManager.persist(examEvent); + + final ExaminerExamEventUpsertDTO dto = createUpdateDTOAddingOne(examEvent); + final ExaminerExamEventDTO responseDTO = examinerExamEventService.updateExamEvent( + examiner.getOid(), + examEvent.getId(), + dto + ); + + assertEquals(examEvent.getLocation(), "MordorX"); + assertEquals(responseDTO.id(), dto.id()); + assertEquals(responseDTO.language(), dto.language()); + assertEquals(responseDTO.date(), dto.date()); + assertEquals(responseDTO.location(), dto.location()); + assertEquals(responseDTO.examTime(), dto.examTime()); + assertEquals(responseDTO.otherInformation(), dto.otherInformation()); + assertEquals(responseDTO.registrationCloses(), dto.registrationCloses()); + assertEquals(responseDTO.maxParticipants(), dto.maxParticipants()); + } + + private ExaminerExamEventUpsertDTO createUpdateDTOAddingOne(final ExaminerExamEvent examEvent) { + return ExaminerExamEventUpsertDTO + .builder() + .id(examEvent.getId()) + .date(examEvent.getDate()) + .examTime(examEvent.getExamTime() + "X") + .isHidden(examEvent.isHidden()) + .location(examEvent.getLocation() + "X") + .maxParticipants(examEvent.getMaxParticipants() + 1L) + .registrationCloses(examEvent.getRegistrationCloses()) + .otherInformation(examEvent.getOtherInformation() + "X") + .language(examEvent.getLanguage()) + .municipality(createMunicipalityDTO(examEvent.getMunicipality())) + .build(); + } + + private MunicipalityDTO createMunicipalityDTO(final Municipality municipality) { + return MunicipalityDTO.builder().code(municipality.getCode()).build(); + } +}