Skip to content

Commit

Permalink
Readme update and convenience method for single random fnr generation (
Browse files Browse the repository at this point in the history
…#7)

* Example of usage

* Convenience method for get single random fnr and update README.md

* Better method names
  • Loading branch information
oyri authored May 4, 2022
1 parent fff1d5b commit dc395c0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# idporten-identifikatorer
# idporten-generate-fnr

![Maven build status](https://github.com/felleslosninger/idporten-generate-fnr/actions/workflows/call-maventests.yml/badge.svg)
[![Latest Stable Version](https://img.shields.io/github/v/release/felleslosninger/idporten-generate-fnr?display_name=tag)](https://github.com/felleslosninger/idporten-generate-fnr/releases)
Expand All @@ -19,3 +19,23 @@ From the command line:
mvn install
```
See [GenerateSynteticFodselsnummerTest.java](/src/test/java/no/idporten/test/generate/fnr/GenerateSynteticFodselsnummerTest.java)

## To use it to generate test-user in another projects test

Include in pom.xml
```
<dependency>
<groupId>no.idporten.test.generate</groupId>
<artifactId>idporten-fnr-generate</artifactId>
<version>0.1.5</version>
<scope>test</scope>
</dependency>
```
Simple usage in UnitTest:
```
public void test(){
SyntheticFodselsnummerGenerator synFnrGenerator = new SyntheticFodselsnummerGenerator();
String personIdentifier = synFnrGenerator.fodselsnummer();
// Use it and do your testing
}
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package no.idporten.test.generate.fnr;

public class PersonIdentifiserChecksum {
public class FodselsnummerChecksum {

public static int getChecksum1(int[] n) {
int checksum1 = 11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@
import java.util.List;
import java.util.Random;

public class GenerateSynteticFodselsnummer {
public class SyntheticFodselsnummerGenerator {
final int ILLEGAL_CHECKSUM_VALUE = 10;


public String generateOneSynteticFodselsnummerFromYear(final int year) {
String fnr = generateValidFodselsnummer(year);
System.out.println(fnr);
return fnr;
public String fodselsnummerFromYear(final int year) {
return generateValidFodselsnummer(year);
}

public List<String> generateListOfSynteticFodselsnummers(final int count) {
public String fodselsnummer() {
int randomYear = getRandomNumberInRange(1855, Year.now().getValue());
return fodselsnummerFromYear(randomYear);
}

public List<String> listOfFodselsnummers(final int count) {

List<String> fodselsnummers = new ArrayList<>();
for (int i = 0; i < count; i++) {
int randomYear = getRandomNumberInRange(1855, Year.now().getValue());
String fnr = generateValidFodselsnummer(randomYear);
fodselsnummers.add(fnr);
System.out.println(fnr);
}
return fodselsnummers;

Expand All @@ -44,13 +46,13 @@ private String generateValidFodselsnummer(final int year) {

private String generateFodselsnummer(int year) {
int[] digits = getFodselsnummerWithEmptyChecksums(year);
int checksum1 = PersonIdentifiserChecksum.getChecksum1(digits);
int checksum1 = FodselsnummerChecksum.getChecksum1(digits);

if (checksum1 == ILLEGAL_CHECKSUM_VALUE) {
throw new RuntimeException("Invalid checksum1 " + checksum1 + ", please try different individNumber.");
}
digits[9] = checksum1;
int checksum2 = PersonIdentifiserChecksum.getChecksum2(digits, checksum1);
int checksum2 = FodselsnummerChecksum.getChecksum2(digits, checksum1);
if (checksum2 == ILLEGAL_CHECKSUM_VALUE) {
throw new RuntimeException("Invalid checksum2: " + checksum2 + ", please try different individNumber.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,41 @@

import static org.junit.jupiter.api.Assertions.*;

class GenerateSynteticFodselsnummerTest {
class SyntheticFodselsnummerGeneratorTest {

public void setup(){
PersonIdentifierValidator.setSyntheticPersonIdentifiersAllowed(true);
PersonIdentifierValidator.setRealPersonIdentifiersAllowed(false);
}

@Test
public void when_generate_one_fnr_then_one_valid_fnr_is_returned(){
GenerateSynteticFodselsnummer generator = new GenerateSynteticFodselsnummer();
public void when_generate_one_fnr_then_one_valid_fnr_is_returned_for_year(){
SyntheticFodselsnummerGenerator generator = new SyntheticFodselsnummerGenerator();
int birthYear = 2001;
String fnr = generator.generateOneSynteticFodselsnummerFromYear(birthYear);
String fnr = generator.fodselsnummerFromYear(birthYear);
System.out.println(fnr);
assertNotNull(fnr);
assertEquals(11, fnr.length());
assertEquals(4, fnr.indexOf("01")); //ddMMyy.....
PersonIdentifierValidator.isValid(fnr);
}

@Test
public void when_generate_one_fnr_then_one_valid_fnr_is_returned(){
SyntheticFodselsnummerGenerator generator = new SyntheticFodselsnummerGenerator();
String fnr = generator.fodselsnummer();
System.out.println(fnr);
assertNotNull(fnr);
PersonIdentifierValidator.isValid(fnr);
}

@Test
public void when_generate_10_fnr_then_10_valid_fnr_is_returned(){
GenerateSynteticFodselsnummer generator = new GenerateSynteticFodselsnummer();
List<String> fnrs = generator.generateListOfSynteticFodselsnummers(10);
SyntheticFodselsnummerGenerator generator = new SyntheticFodselsnummerGenerator();
List<String> fnrs = generator.listOfFodselsnummers(10);
assertEquals(10, fnrs.size());
for(String fnr: fnrs){
System.out.println(fnr);
assertNotNull(fnr);
PersonIdentifierValidator.isValid(fnr);
}
Expand Down

0 comments on commit dc395c0

Please sign in to comment.