diff --git a/src/test/groovy/no/fintlabs/ApplicationSpec.groovy b/src/test/groovy/no/fintlabs/ApplicationSpec.groovy deleted file mode 100644 index 7f8fd9a..0000000 --- a/src/test/groovy/no/fintlabs/ApplicationSpec.groovy +++ /dev/null @@ -1,14 +0,0 @@ -package no.fintlabs - -import spock.lang.Specification - -class ApplicationSpec extends Specification { - - def "Application is created"() { - when: - def application = new Application() - - then: - application - } -} diff --git a/src/test/java/no/fintlabs/arkiv/kodeverk/ArkivressursDisplayNameMapperTest.java b/src/test/java/no/fintlabs/arkiv/kodeverk/ArkivressursDisplayNameMapperTest.java new file mode 100644 index 0000000..2318656 --- /dev/null +++ b/src/test/java/no/fintlabs/arkiv/kodeverk/ArkivressursDisplayNameMapperTest.java @@ -0,0 +1,223 @@ +package no.fintlabs.arkiv.kodeverk; + +import no.fint.model.felles.kompleksedatatyper.Identifikator; +import no.fint.model.felles.kompleksedatatyper.Personnavn; +import no.fint.model.resource.Link; +import no.fint.model.resource.administrasjon.personal.PersonalressursResource; +import no.fint.model.resource.arkiv.noark.ArkivressursResource; +import no.fint.model.resource.felles.PersonResource; +import no.fintlabs.cache.FintCache; +import no.fintlabs.cache.exceptions.NoSuchCacheEntryException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.*; + + +@ExtendWith(MockitoExtension.class) +class ArkivressursDisplayNameMapperTest { + + @Mock + FintCache personalressursResourceCache; + + @Mock + FintCache personResourceCache; + + ArkivressursDisplayNameMapper arkivressursDisplayNameMapper; + + @BeforeEach + public void setUp() { + arkivressursDisplayNameMapper = new ArkivressursDisplayNameMapper( + personalressursResourceCache, + personResourceCache + ); + } + + private ArkivressursResource setupMocksForPersonnavn(Personnavn personnavn) { + ArkivressursResource arkivressursResource = mock(ArkivressursResource.class); + when(arkivressursResource.getPersonalressurs()).thenReturn(List.of(Link.with("testPersonalressursLink1"))); + + PersonalressursResource personalressursResource = mock(PersonalressursResource.class); + when(personalressursResourceCache.get("testPersonalressursLink1")).thenReturn(personalressursResource); + when(personalressursResource.getPerson()).thenReturn(List.of(Link.with("testPersonLink1"))); + + PersonResource personResource = mock(PersonResource.class); + when(personResourceCache.get("testPersonLink1")).thenReturn(personResource); + + when(personResource.getNavn()).thenReturn(personnavn); + + + return arkivressursResource; + } + + private ArkivressursResource setupMocksForPersonBrukernavn() { + ArkivressursResource arkivressursResource = mock(ArkivressursResource.class); + when(arkivressursResource.getPersonalressurs()).thenReturn(List.of(Link.with("testPersonalressursLink1"))); + + PersonalressursResource personalressursResource = mock(PersonalressursResource.class); + when(personalressursResourceCache.get("testPersonalressursLink1")).thenReturn(personalressursResource); + + Identifikator identifikator = new Identifikator(); + identifikator.setIdentifikatorverdi("12345"); + when(personalressursResource.getBrukernavn()).thenReturn(identifikator); + + return arkivressursResource; + + } + + @Test + public void findPersonNavn_givenFirstMiddleAndLastName_shouldReturnFullNameWithSpaceSeparations() { + Personnavn personnavn = mock(Personnavn.class); + when(personnavn.getFornavn()).thenReturn("testFornavn"); + when(personnavn.getMellomnavn()).thenReturn("testMellomnavn"); + when(personnavn.getEtternavn()).thenReturn("testEtternavn"); + + ArkivressursResource arkivressursResource = setupMocksForPersonnavn(personnavn); + + Optional personNavn = arkivressursDisplayNameMapper.findPersonNavn(arkivressursResource); + + assertThat(personNavn).isPresent(); + assertThat(personNavn).contains("testFornavn testMellomnavn testEtternavn"); + } + + @Test + public void findPersonNavn_givenFirstAndLastName_shouldReturnFullNameWithSpaceSeparations() { + Personnavn personnavn = mock(Personnavn.class); + when(personnavn.getFornavn()).thenReturn("testFornavn"); + when(personnavn.getMellomnavn()).thenReturn(null); + when(personnavn.getEtternavn()).thenReturn("testEtternavn"); + + ArkivressursResource arkivressursResource = setupMocksForPersonnavn(personnavn); + + Optional personNavn = arkivressursDisplayNameMapper.findPersonNavn(arkivressursResource); + + assertThat(personNavn).isPresent(); + assertThat(personNavn).contains("testFornavn testEtternavn"); + } + + @Test + public void findPersonNavn_givenNoName_shouldReturnNoName() { + Personnavn personnavn = mock(Personnavn.class); + when(personnavn.getFornavn()).thenReturn(null); + when(personnavn.getMellomnavn()).thenReturn(null); + when(personnavn.getEtternavn()).thenReturn(null); + + ArkivressursResource arkivressursResource = setupMocksForPersonnavn(personnavn); + + Optional personNavn = arkivressursDisplayNameMapper.findPersonNavn(arkivressursResource); + + assertThat(personNavn).isPresent(); + assertThat(personNavn).contains(""); + } + + @Test + public void findPersonNavn_givenNullName_shouldThrowIllegalStateException() { + ArkivressursResource arkivressursResource = setupMocksForPersonnavn(null); + + IllegalStateException illegalStateException = assertThrows( + IllegalStateException.class, + () -> arkivressursDisplayNameMapper.findPersonNavn(arkivressursResource) + ); + + assertThat(illegalStateException).hasMessage("Person resource contains no name"); + } + + @Test + public void findPersonNavn_givenNoMatchingPersonalressursInCache_shouldReturnEmpty() { + ArkivressursResource arkivressursResource = mock(ArkivressursResource.class); + when(arkivressursResource.getPersonalressurs()).thenReturn(List.of(Link.with("testPersonalressursLink1"))); + + when(personalressursResourceCache.get("testPersonalressursLink1")).thenThrow( + new NoSuchCacheEntryException("testPersonalressursLink1") + ); + + Optional personNavn = arkivressursDisplayNameMapper.findPersonNavn(arkivressursResource); + + assertThat(personNavn).isEmpty(); + } + + @Test + public void findPersonNavn_givenNoMatchingPersonInCache_shouldReturnEmpty() { + ArkivressursResource arkivressursResource = mock(ArkivressursResource.class); + when(arkivressursResource.getPersonalressurs()).thenReturn(List.of(Link.with("testPersonalressursLink1"))); + + PersonalressursResource personalressursResource = mock(PersonalressursResource.class); + when(personalressursResourceCache.get("testPersonalressursLink1")).thenReturn(personalressursResource); + when(personalressursResource.getPerson()).thenReturn(List.of(Link.with("testPersonLink1"))); + + when(personResourceCache.get("testPersonLink1")).thenThrow( + new NoSuchCacheEntryException("testPersonLink1") + ); + + Optional personNavn = arkivressursDisplayNameMapper.findPersonNavn(arkivressursResource); + + assertThat(personNavn).isEmpty(); + } + + @Test + public void findPersonNavn_shouldUseFirstLinksToGetFromCache() { + ArkivressursResource arkivressursResource = mock(ArkivressursResource.class); + when(arkivressursResource.getPersonalressurs()).thenReturn(List.of( + Link.with("testPersonalressursLink1"), + Link.with("testPersonalressursLink2"), + Link.with("testPersonalressursLink3") + )); + + PersonalressursResource personalressursResource = mock(PersonalressursResource.class); + when(personalressursResourceCache.get("testPersonalressursLink1")).thenReturn(personalressursResource); + when(personalressursResource.getPerson()).thenReturn(List.of( + Link.with("testPersonLink1"), + Link.with("testPersonLink2"), + Link.with("testPersonLink3") + )); + PersonResource personResource = mock(PersonResource.class); + when(personResourceCache.get("testPersonLink1")).thenReturn(personResource); + + Personnavn personnavn = mock(Personnavn.class); + when(personnavn.getFornavn()).thenReturn(null); + when(personnavn.getMellomnavn()).thenReturn(null); + when(personnavn.getEtternavn()).thenReturn(null); + when(personResource.getNavn()).thenReturn(personnavn); + + arkivressursDisplayNameMapper.findPersonNavn(arkivressursResource); + + verify(personalressursResourceCache, times(1)).get("testPersonalressursLink1"); + verifyNoMoreInteractions(personalressursResourceCache); + + verify(personResourceCache, times(1)).get("testPersonLink1"); + verifyNoMoreInteractions(personResourceCache); + } + + @Test + public void getPersonalressursBrukernavn_shouldReturnIdentifikator() { + ArkivressursResource arkivressursResource = setupMocksForPersonBrukernavn(); + + Optional personBrukernavn = arkivressursDisplayNameMapper.findPersonalressursBrukernavn(arkivressursResource); + + assertThat(personBrukernavn).isPresent(); + assertThat(personBrukernavn).contains("12345"); + } + + @Test + public void getPersonalressursBrukernavn_shouldReturnEmpty() { + ArkivressursResource arkivressursResource = mock(ArkivressursResource.class); + when(arkivressursResource.getPersonalressurs()).thenReturn(List.of(Link.with("testPersonalressursLink1"))); + + when(personalressursResourceCache.get("testPersonalressursLink1")).thenThrow( + new NoSuchCacheEntryException("testPersonalressursLink1") + ); + + Optional personBrukernavn = arkivressursDisplayNameMapper.findPersonalressursBrukernavn(arkivressursResource); + + assertThat(personBrukernavn).isEmpty(); + } + +} diff --git a/src/test/java/no/fintlabs/arkiv/sak/CaseControllerTest.java b/src/test/java/no/fintlabs/arkiv/sak/CaseControllerTest.java new file mode 100644 index 0000000..0e307c4 --- /dev/null +++ b/src/test/java/no/fintlabs/arkiv/sak/CaseControllerTest.java @@ -0,0 +1,64 @@ +package no.fintlabs.arkiv.sak; + +import no.fint.model.resource.arkiv.noark.SakResource; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.server.ResponseStatusException; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class CaseControllerTest { + + @Mock + CaseRequestService caseRequestService; + + @Mock + SakResource sakResource; + + CaseController caseController; + + @BeforeEach + public void setUp() { + caseController = new CaseController(caseRequestService); + } + + @Test + public void getCaseTitle_givenFoundCase_shouldReturn200WithCaseTitle() { + when(sakResource.getTittel()).thenReturn("Test tittel"); + when(caseRequestService.getByMappeId("2023/102")).thenReturn(Optional.of(sakResource)); + + + ResponseEntity response = caseController.getCaseTitle("2023", "102"); + + assertTrue(response.getStatusCode().is2xxSuccessful()); + assertNotNull(response.getBody()); + assertEquals(response.getBody().getValue(), "Test tittel"); + verify(caseRequestService, times(1)).getByMappeId("2023/102"); + verifyNoMoreInteractions(caseRequestService); + } + + @Test + public void getCaseTitle_givenNoFoundCase_shouldReturn404() { + when(caseRequestService.getByMappeId("2023/101")).thenReturn(Optional.empty()); + + ResponseStatusException responseStatusException = assertThrows( + ResponseStatusException.class, + () -> caseController.getCaseTitle("2023", "101") + ); + + assertEquals(HttpStatus.NOT_FOUND, responseStatusException.getStatus()); + assertEquals("Case with mappeId=2023/101 could not be found", responseStatusException.getReason()); + verify(caseRequestService, times(1)).getByMappeId("2023/101"); + verifyNoMoreInteractions(caseRequestService); + } + +}