Skip to content

Commit

Permalink
VKT(Backend): More backend tests for examiner exam event
Browse files Browse the repository at this point in the history
  • Loading branch information
jrkkp committed Jan 8, 2025
1 parent af02950 commit aba0e2f
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/vkt/src/test/java/fi/oph/vkt/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
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();
}
}

0 comments on commit aba0e2f

Please sign in to comment.