-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from FINTLabs/FF-431
FF-431 #comment Add tests for case controller and arkivressurs mapper
- Loading branch information
Showing
3 changed files
with
287 additions
and
14 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
223 changes: 223 additions & 0 deletions
223
src/test/java/no/fintlabs/arkiv/kodeverk/ArkivressursDisplayNameMapperTest.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,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<String, PersonalressursResource> personalressursResourceCache; | ||
|
||
@Mock | ||
FintCache<String, PersonResource> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> personBrukernavn = arkivressursDisplayNameMapper.findPersonalressursBrukernavn(arkivressursResource); | ||
|
||
assertThat(personBrukernavn).isEmpty(); | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
src/test/java/no/fintlabs/arkiv/sak/CaseControllerTest.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,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<CaseTitle> 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); | ||
} | ||
|
||
} |