Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refaktorert datoaritmetikk for personstatus #3670

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static no.nav.pdl.forvalter.utils.ArtifactUtils.getKilde;
import static no.nav.pdl.forvalter.utils.ArtifactUtils.getMaster;
Expand Down Expand Up @@ -193,19 +192,35 @@ private static LocalDateTime getBoadresseGyldigFraDato(PersonDTO person) {
protected static void setGyldigTilOgMed(List<FolkeregisterPersonstatusDTO> folkeregisterPersonstatus) {

for (var i = folkeregisterPersonstatus.size() - 1; i >= 0; i--) {
if (i - 1 >= 0 && isNull(folkeregisterPersonstatus.get(i).getGyldigTilOgMed())) {

folkeregisterPersonstatus.get(i)
.setGyldigTilOgMed(folkeregisterPersonstatus.get(i - 1).getGyldigFraOgMed().plusDays(1));
}
if (i - 1 >= 0 && folkeregisterPersonstatus.get(i - 1).getGyldigFraOgMed()
.isBefore(folkeregisterPersonstatus.get(i).getGyldigTilOgMed().plusDays(1))) {

folkeregisterPersonstatus.get(i - 1)
.setGyldigFraOgMed(folkeregisterPersonstatus.get(i).getGyldigTilOgMed().plusDays(1));
if (i - 1 >= 0) {
fixGyldigTilOgMed(folkeregisterPersonstatus.get(i), folkeregisterPersonstatus.get(i - 1));
fixGyldigFraOgMed(folkeregisterPersonstatus.get(i), folkeregisterPersonstatus.get(i - 1));
}
}

folkeregisterPersonstatus.getFirst().setGyldigTilOgMed(null);
}

private static void fixGyldigTilOgMed(FolkeregisterPersonstatusDTO statusA, FolkeregisterPersonstatusDTO statusB) {

if (statusA.getGyldigFraOgMed().isEqual(statusB.getGyldigFraOgMed())) {
statusA.setGyldigTilOgMed(statusB.getGyldigFraOgMed().plusDays(1));

} else if (statusA.getGyldigFraOgMed().plusDays(1).isBefore(statusB.getGyldigFraOgMed())) {
statusA.setGyldigTilOgMed(statusB.getGyldigFraOgMed().minusDays(1));

} else {
statusA.setGyldigTilOgMed(statusB.getGyldigFraOgMed());
}
}

private static void fixGyldigFraOgMed(FolkeregisterPersonstatusDTO statusA, FolkeregisterPersonstatusDTO statusB) {

if (statusA.getGyldigTilOgMed().isAfter(statusB.getGyldigFraOgMed()) ||
statusA.getGyldigTilOgMed().isEqual(statusB.getGyldigFraOgMed())) {

statusB.setGyldigFraOgMed(statusA.getGyldigTilOgMed().plusDays(1));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.nav.pdl.forvalter.service;

import no.nav.testnav.libs.data.pdlforvalter.v1.AdressebeskyttelseDTO;
import no.nav.testnav.libs.data.pdlforvalter.v1.BostedadresseDTO;
import no.nav.testnav.libs.data.pdlforvalter.v1.DoedsfallDTO;
import no.nav.testnav.libs.data.pdlforvalter.v1.FolkeregisterPersonstatusDTO;
Expand Down Expand Up @@ -27,11 +28,13 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

@ExtendWith(MockitoExtension.class)
class FolkeregisterPersonstatusServiceTest {

private static final String FNR_IDENT = "12044512345";
private static final LocalDateTime GYLDIG_FRA_OG_MED = LocalDateTime.of(1970, 1, 1, 0, 0);

@InjectMocks
private FolkeregisterPersonstatusService folkeregisterPersonstatusService;
Expand Down Expand Up @@ -174,4 +177,94 @@ void whenNoOtherInformation_thenUseDefault() {

assertThat(target.getStatus(), is(equalTo(FOEDSELSREGISTRERT)));
}

@Test
void whenFraDatoIsIdenticalOnTwoStatuses_FixIt() {

var target = folkeregisterPersonstatusService.convert(
PersonDTO.builder()
.ident(FNR_IDENT)
.adressebeskyttelse(List.of(AdressebeskyttelseDTO.builder()
.isNew(true)
.build()))
.folkeregisterPersonstatus(
List.of(FolkeregisterPersonstatusDTO.builder()
.status(FOEDSELSREGISTRERT)
.gyldigFraOgMed(GYLDIG_FRA_OG_MED)
.id(2)
.build(),
FolkeregisterPersonstatusDTO.builder()
.status(BOSATT)
.gyldigFraOgMed(GYLDIG_FRA_OG_MED)
.id(1)
.build()))
.build());

assertThat(target.getFirst().getStatus(), is(equalTo(FOEDSELSREGISTRERT)));
assertThat(target.getFirst().getGyldigFraOgMed(), is(equalTo(GYLDIG_FRA_OG_MED.plusDays(2))));
assertThat(target.getFirst().getGyldigTilOgMed(), is((nullValue())));
assertThat(target.getLast().getStatus(), is(equalTo(BOSATT)));
assertThat(target.getLast().getGyldigFraOgMed(), is(equalTo(GYLDIG_FRA_OG_MED)));
assertThat(target.getLast().getGyldigTilOgMed(), is(equalTo(GYLDIG_FRA_OG_MED.plusDays(1))));
}

@Test
void whenFraDatoIsArrangedProperly_DoNothing() {

var target = folkeregisterPersonstatusService.convert(
PersonDTO.builder()
.ident(FNR_IDENT)
.adressebeskyttelse(List.of(AdressebeskyttelseDTO.builder()
.isNew(true)
.build()))
.folkeregisterPersonstatus(
List.of(FolkeregisterPersonstatusDTO.builder()
.status(FOEDSELSREGISTRERT)
.gyldigFraOgMed(GYLDIG_FRA_OG_MED.plusYears(10))
.id(2)
.build(),
FolkeregisterPersonstatusDTO.builder()
.status(BOSATT)
.gyldigFraOgMed(GYLDIG_FRA_OG_MED)
.id(1)
.build()))
.build());

assertThat(target.getFirst().getStatus(), is(equalTo(FOEDSELSREGISTRERT)));
assertThat(target.getFirst().getGyldigFraOgMed(), is(equalTo(GYLDIG_FRA_OG_MED.plusYears(10))));
assertThat(target.getFirst().getGyldigTilOgMed(), is((nullValue())));
assertThat(target.getLast().getStatus(), is(equalTo(BOSATT)));
assertThat(target.getLast().getGyldigFraOgMed(), is(equalTo(GYLDIG_FRA_OG_MED)));
assertThat(target.getLast().getGyldigTilOgMed(), is(equalTo(GYLDIG_FRA_OG_MED.plusYears(10).minusDays(1))));
}

@Test
void whenFraDatoDiffersByOneDay_Fixit() {

var target = folkeregisterPersonstatusService.convert(
PersonDTO.builder()
.ident(FNR_IDENT)
.adressebeskyttelse(List.of(AdressebeskyttelseDTO.builder()
.isNew(true)
.build()))
.folkeregisterPersonstatus(
List.of(FolkeregisterPersonstatusDTO.builder()
.status(FOEDSELSREGISTRERT)
.gyldigFraOgMed(GYLDIG_FRA_OG_MED.plusDays(1))
.id(2)
.build(),
FolkeregisterPersonstatusDTO.builder()
.status(BOSATT)
.gyldigFraOgMed(GYLDIG_FRA_OG_MED)
.id(1)
.build()))
.build());

assertThat(target.getFirst().getStatus(), is(equalTo(FOEDSELSREGISTRERT)));
assertThat(target.getFirst().getGyldigFraOgMed(), is(equalTo(GYLDIG_FRA_OG_MED.plusDays(2))));
assertThat(target.getFirst().getGyldigTilOgMed(), is((nullValue())));
assertThat(target.getLast().getStatus(), is(equalTo(BOSATT)));
assertThat(target.getLast().getGyldigFraOgMed(), is(equalTo(GYLDIG_FRA_OG_MED)));
assertThat(target.getLast().getGyldigTilOgMed(), is(equalTo(GYLDIG_FRA_OG_MED.plusDays(1))));
}
}