Skip to content

Commit

Permalink
Merge pull request #14 from FINTLabs/FF-431
Browse files Browse the repository at this point in the history
FF-431 #comment Add tests for case controller and arkivressurs mapper
  • Loading branch information
Battlestad authored Sep 4, 2023
2 parents 1b86a5b + 8007829 commit 5f9b73f
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 14 deletions.
14 changes: 0 additions & 14 deletions src/test/groovy/no/fintlabs/ApplicationSpec.groovy

This file was deleted.

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 src/test/java/no/fintlabs/arkiv/sak/CaseControllerTest.java
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);
}

}

0 comments on commit 5f9b73f

Please sign in to comment.