Skip to content

Commit

Permalink
Update tests to handle potential insufficient identifiers
Browse files Browse the repository at this point in the history
#deploy-testnav-ident-pool

Modified the tests to ensure that identifier generation does not throw an exception when the desired amount of identifiers cannot be generated. Instead, the tests now assert that the resulting set size is less than the requested amount.
  • Loading branch information
krharum committed Oct 9, 2024
1 parent 7242c34 commit 029baed
Showing 1 changed file with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class IdentGeneratorServiceTest {
private static final int START_1900 = 0;
private static final int GENERATE_SIZE = 100;

private final LocalDate LOCAL_DATE = LocalDate.now();
private final LocalDate timeNow = LocalDate.now();

private final IdentGeneratorService identGeneratorService = new IdentGeneratorService();

Expand All @@ -41,7 +41,7 @@ class IdentGeneratorServiceTest {
void shouldThrowToDateAfterFromDate() {

var request = createRequest(Identtype.FNR, Kjoenn.MANN)
.foedtFoer(LOCAL_DATE.minusDays(2))
.foedtFoer(timeNow.minusDays(2))
.build();

Set<String> result = new HashSet<>();
Expand All @@ -51,24 +51,24 @@ void shouldThrowToDateAfterFromDate() {
}

@Test
@DisplayName("Skal feile når ønsket antall ikke kan genereres")
void shouldThrowToFewIdents() {
@DisplayName("Skal ikke feile når ønsket antall ikke kan genereres")
void shouldNotThrowTooFewIdents() {

var requestedAmount = 500;
var request = createRequest(Identtype.FNR, Kjoenn.MANN)
.antall(500)
.antall(requestedAmount)
.build();

Set<String> result = new HashSet<>();
var result = identGeneratorService.genererIdenter(request, new HashSet<>());

assertThrows(IllegalArgumentException.class,
() -> identGeneratorService.genererIdenter(request, result));
assertThat(result.size(), is(lessThan(requestedAmount)));
}

@Test
@DisplayName("Skal ikke generere identer i sortert rekkefølge")
void fnrGenererDescendingTest() {
// This test will stop working 1. Jan 2040 :(
LocalDate localDate = LocalDate.now();
var localDate = LocalDate.now();
Map<LocalDate, List<String>> pinMap =
identGeneratorService.genererIdenterMap(localDate, localDate.plusDays(1), Identtype.FNR, false);
assertThat(pinMap.size(), is(equalTo(1)));
Expand All @@ -82,9 +82,9 @@ void fnrGenererKjonnKriterier() {
Set<String> kvinner = generateIdents(Identtype.FNR, Kjoenn.KVINNE);

assertThat(menn.size(), is(equalTo(GENERATE_SIZE)));
menn.forEach(fnr -> assertFnrValues(fnr, Kjoenn.MANN, LOCAL_DATE));
menn.forEach(fnr -> assertFnrValues(fnr, Kjoenn.MANN, timeNow));
assertThat(kvinner.size(), is(equalTo(GENERATE_SIZE)));
kvinner.forEach(fnr -> assertFnrValues(fnr, Kjoenn.KVINNE, LOCAL_DATE));
kvinner.forEach(fnr -> assertFnrValues(fnr, Kjoenn.KVINNE, timeNow));
}

@Test
Expand All @@ -94,9 +94,9 @@ void dnrGenererKjonnKriterier() {
Set<String> kvinner = generateIdents(Identtype.DNR, Kjoenn.KVINNE);

assertThat(menn.size(), is(equalTo(GENERATE_SIZE)));
menn.forEach(dnr -> assertDnrValues(dnr, Kjoenn.MANN, LOCAL_DATE));
menn.forEach(dnr -> assertDnrValues(dnr, Kjoenn.MANN, timeNow));
assertThat(kvinner.size(), is(equalTo(GENERATE_SIZE)));
kvinner.forEach(dnr -> assertDnrValues(dnr, Kjoenn.KVINNE, LOCAL_DATE));
kvinner.forEach(dnr -> assertDnrValues(dnr, Kjoenn.KVINNE, timeNow));
}

@Test
Expand All @@ -106,22 +106,22 @@ void bostGenererKjonnKriterier() {
Set<String> kvinner = generateIdents(Identtype.BOST, Kjoenn.KVINNE);

assertThat(menn.size(), is(equalTo(GENERATE_SIZE)));
menn.forEach(bnr -> assertBnrValues(bnr, Kjoenn.MANN, LOCAL_DATE));
menn.forEach(bnr -> assertBnrValues(bnr, Kjoenn.MANN, timeNow));
assertThat(kvinner.size(), is(equalTo(GENERATE_SIZE)));
kvinner.forEach(bnr -> assertBnrValues(bnr, Kjoenn.KVINNE, LOCAL_DATE));
kvinner.forEach(bnr -> assertBnrValues(bnr, Kjoenn.KVINNE, timeNow));
}

private Set<String> generateIdents(Identtype identtype, Kjoenn kjoenn) {
return identGeneratorService.genererIdenter(
createRequest(identtype, kjoenn).build(), new HashSet<String>());
createRequest(identtype, kjoenn).build(), new HashSet<>());
}

private HentIdenterRequest.HentIdenterRequestBuilder createRequest(Identtype identtype, Kjoenn kjoenn) {
return HentIdenterRequest.builder()
.identtype(identtype)
.antall(GENERATE_SIZE)
.foedtEtter(LOCAL_DATE)
.foedtFoer(LOCAL_DATE)
.foedtEtter(timeNow)
.foedtFoer(timeNow)
.kjoenn(kjoenn);
}

Expand Down

0 comments on commit 029baed

Please sign in to comment.