-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VKT(Backend): More backend tests for examiner exam event
- Loading branch information
Showing
2 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
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
152 changes: 152 additions & 0 deletions
152
backend/vkt/src/test/java/fi/oph/vkt/service/ExaminerExamEventServiceTest.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 |
---|---|---|
@@ -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(); | ||
} | ||
} |