From 777ece33411be46d067234b9bcbefefb7a7f40c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eivind=20Bergst=C3=B8l?= Date: Wed, 30 Sep 2020 22:25:23 +0200 Subject: [PATCH 1/3] =?UTF-8?q?St=C3=B8tt=20syntetiske=20fnr=20og=20dnr=20?= =?UTF-8?q?for=20folkeregisteret=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Folkeregisteret vil innføre syntetiske fnr og dnr fra nov 2020. Dette gjør de for å ikke ha mulighet for å ha skikkelige fnr og dnr i testmiljøene. Skatteetaten foreslår at man gir en mulighet til å slå av og på om en skal validere truthy eller falsy på syntetiske fnr/dnr (test/prod). Jeg har lagt inn en enkelt statisk flagg for å styre dette. public static boolean FAIL_ON_SYNTHETIC_NUMBER = true; --- pom.xml | 8 +- .../bekk/bekkopen/person/Fodselsnummer.java | 38 ++- .../person/FodselsnummerCalculator.java | 47 ++- .../person/FodselsnummerValidator.java | 28 +- .../person/FodselsnummerCalculatorTest.java | 16 +- .../bekkopen/person/FodselsnummerTest.java | 298 +++++++++--------- .../person/FodselsnummerValidatorTest.java | 260 +++++++-------- .../SyntheticFodselsnummerCalculatorTest.java | 50 +++ .../SyntheticFodselsnummerValidatorTest.java | 27 ++ 9 files changed, 456 insertions(+), 316 deletions(-) mode change 100755 => 100644 src/main/java/no/bekk/bekkopen/person/Fodselsnummer.java mode change 100755 => 100644 src/test/java/no/bekk/bekkopen/person/FodselsnummerCalculatorTest.java mode change 100755 => 100644 src/test/java/no/bekk/bekkopen/person/FodselsnummerTest.java create mode 100644 src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerCalculatorTest.java create mode 100644 src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerValidatorTest.java diff --git a/pom.xml b/pom.xml index 506f959..aae50e3 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ com.jcabi parent - 0.49.2 + 0.50.5 @@ -141,12 +141,6 @@ ${junit.version} test - - org.hibernate - hibernate-validator - 4.3.1.Final - test - diff --git a/src/main/java/no/bekk/bekkopen/person/Fodselsnummer.java b/src/main/java/no/bekk/bekkopen/person/Fodselsnummer.java old mode 100755 new mode 100644 index e69cc73..dedfbd0 --- a/src/main/java/no/bekk/bekkopen/person/Fodselsnummer.java +++ b/src/main/java/no/bekk/bekkopen/person/Fodselsnummer.java @@ -21,7 +21,7 @@ public class Fodselsnummer extends StringNumber { * @return A String containing the date and month of birth. */ public String getDateAndMonth() { - return parseDNumber(getValue()).substring(0, 4); + return parseSynthenticNumber(parseDNumber(getValue())).substring(0, 4); } /** @@ -31,7 +31,7 @@ public String getDateAndMonth() { * @return A String containing the date of birth */ public String getDayInMonth() { - return parseDNumber(getValue()).substring(0, 2); + return parseSynthenticNumber(parseDNumber(getValue())).substring(0, 2); } /** @@ -41,7 +41,7 @@ public String getDayInMonth() { * @return A String containing the date of birth */ public String getMonth() { - return parseDNumber(getValue()).substring(2, 4); + return parseSynthenticNumber(parseDNumber(getValue())).substring(2, 4); } /** @@ -86,7 +86,7 @@ public String get2DigitBirthYear() { * @return A String containing the date and month of birth. */ public String getDateOfBirth() { - return parseDNumber(getValue()).substring(0, 6); + return parseSynthenticNumber(parseDNumber(getValue())).substring(0, 6); } /** @@ -155,10 +155,18 @@ public boolean isFemale() { return !isMale(); } - static boolean isDNumber(String fodselsnummer) { + static String parseSynthenticNumber(String fodselsnummer) { + if (!isSynthetic(fodselsnummer)) { + return fodselsnummer; + } else { + return fodselsnummer.substring(0, 2) + (getThirdDigit(fodselsnummer) - 8) + fodselsnummer.substring(3); + } + } + + static boolean isSynthetic(String fodselsnummer) { try { - int firstDigit = getFirstDigit(fodselsnummer); - if (firstDigit > 3 && firstDigit < 8) { + int thirdDigit = getThirdDigit(fodselsnummer); + if (thirdDigit == 8 || thirdDigit == 9) { return true; } } catch (IllegalArgumentException e) { @@ -167,6 +175,18 @@ static boolean isDNumber(String fodselsnummer) { return false; } + static boolean isDNumber(String fodselsnummer) { + try { + int firstDigit = getFirstDigit(fodselsnummer); + if (firstDigit > 3 && firstDigit < 8) { + return true; + } + } catch (IllegalArgumentException e) { + // ignore + } + return false; + } + static String parseDNumber(String fodselsnummer) { if (!isDNumber(fodselsnummer)) { return fodselsnummer; @@ -179,6 +199,10 @@ private static int getFirstDigit(String fodselsnummer) { return Integer.parseInt(fodselsnummer.substring(0, 1)); } + private static int getThirdDigit(String fodselsnummer) { + return Integer.parseInt(fodselsnummer.substring(2, 3)); + } + public KJONN getKjonn() { if (isFemale()) { return KJONN.KVINNE; diff --git a/src/main/java/no/bekk/bekkopen/person/FodselsnummerCalculator.java b/src/main/java/no/bekk/bekkopen/person/FodselsnummerCalculator.java index d8e84bd..a44e5a9 100644 --- a/src/main/java/no/bekk/bekkopen/person/FodselsnummerCalculator.java +++ b/src/main/java/no/bekk/bekkopen/person/FodselsnummerCalculator.java @@ -45,10 +45,49 @@ public static List getManyDNumberFodselsnummerForDate(Date date) DateFormat df = new SimpleDateFormat("ddMMyy"); String centuryString = getCentury(date); String dateString = df.format(date); - dateString = new StringBuilder() - .append(Character.toChars(dateString.charAt(0) + 4)[0]) - .append(dateString.substring(1)) - .toString(); + dateString = Character.toChars(dateString.charAt(0) + 4)[0] + + dateString.substring(1); + return generateFodselsnummerForDate(dateString, centuryString); + } + + /** + * Returns a List with with VALID DNumber Fodselsnummer instances for a given Date. + * + * @param date The Date instance + * @return A List with Fodelsnummer instances + */ + + public static List getManySynteticFodselsnummerForDate(Date date) { + if (date == null) { + throw new IllegalArgumentException(); + } + DateFormat df = new SimpleDateFormat("ddMMyy"); + String centuryString = getCentury(date); + String dateString = df.format(date); + dateString = dateString.substring(0, 2) + + Character.toChars(dateString.charAt(2) + 8)[0] + + dateString.substring(3); + return generateFodselsnummerForDate(dateString, centuryString); + } + + /** + * Returns a List with with VALID DNumber Fodselsnummer instances for a given Date. + * + * @param date The Date instance + * @return A List with Fodelsnummer instances + */ + + public static List getManySynteticDNumberFodselsnummerForDate(Date date) { + if (date == null) { + throw new IllegalArgumentException(); + } + DateFormat df = new SimpleDateFormat("ddMMyy"); + String centuryString = getCentury(date); + String dateString = df.format(date); + dateString = Character.toChars(dateString.charAt(0) + 4)[0] + + dateString.substring(1, 2) + + Character.toChars(dateString.charAt(2) + 8)[0] + + dateString.substring(3); return generateFodselsnummerForDate(dateString, centuryString); } diff --git a/src/main/java/no/bekk/bekkopen/person/FodselsnummerValidator.java b/src/main/java/no/bekk/bekkopen/person/FodselsnummerValidator.java index 4b60e5a..9e331fd 100644 --- a/src/main/java/no/bekk/bekkopen/person/FodselsnummerValidator.java +++ b/src/main/java/no/bekk/bekkopen/person/FodselsnummerValidator.java @@ -14,6 +14,14 @@ /** * Provides methods that validates if a Fodselsnummer is valid with respect to * syntax, Individnummer, Date and checksum digits. + * + * This validator also validates DNummer as valid. + * Birth date is modified by adding 4 on the first digit + * + * The Norwegian registry Det Norske Folkeregister has decided + * that it will create syntetic ssn for its testpopulation. + * This validator will can now validate true for Fodselsnummer that has month +80 + * for synthetic fnr/dnr */ public class FodselsnummerValidator extends StringNumberValidator implements ConstraintValidator { @@ -28,9 +36,16 @@ public class FodselsnummerValidator extends StringNumberValidator implements Con protected static final String ERROR_INVALID_INDIVIDNUMMER = "Invalid individnummer in fødselsnummer : "; + /** + * Truthy validation of synthetic fnr/dnr is false. You are adviced to + * set til value to false in test environments where you expect + * syntetic fnr/dnr to appair. + */ + public static boolean FAIL_ON_SYNTHETIC_NUMBER = true; + /** * Returns an object that represents a Fodselsnummer. - * + * * @param fodselsnummer * A String containing a Fodselsnummer * @return A Fodselsnummer instance @@ -42,12 +57,19 @@ public static no.bekk.bekkopen.person.Fodselsnummer getFodselsnummer(String fods validateIndividnummer(fodselsnummer); validateDate(fodselsnummer); validateChecksums(fodselsnummer); + validateSynthetic(fodselsnummer); return new no.bekk.bekkopen.person.Fodselsnummer(fodselsnummer); } - /** + private static void validateSynthetic(String fodselsnummer) { + if (Fodselsnummer.isSynthetic(fodselsnummer) && FAIL_ON_SYNTHETIC_NUMBER) { + throw new IllegalArgumentException("Syntetic fødselsnummer is not allowd" + fodselsnummer); + } + } + + /** * Return true if the provided String is a valid Fodselsnummer. - * + * * @param fodselsnummer * A String containing a Fodselsnummer * @return true or false diff --git a/src/test/java/no/bekk/bekkopen/person/FodselsnummerCalculatorTest.java b/src/test/java/no/bekk/bekkopen/person/FodselsnummerCalculatorTest.java old mode 100755 new mode 100644 index 15b2363..7a55134 --- a/src/test/java/no/bekk/bekkopen/person/FodselsnummerCalculatorTest.java +++ b/src/test/java/no/bekk/bekkopen/person/FodselsnummerCalculatorTest.java @@ -3,15 +3,15 @@ import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class FodselsnummerCalculatorTest { @@ -24,12 +24,6 @@ public void setUpDate() throws Exception { date = df.parse("09062006"); } - @Test - public void testGetFodselsnummerForDateAndGender() throws ParseException { - List options = FodselsnummerCalculator.getFodselsnummerForDateAndGender(date, KJONN.KVINNE); - assertTrue("Forventet minst 10 fødselsnumre, men fikk " + options.size(), options.size() > 10); - } - @Test public void testGetFodselsnummerForDate() { List options = FodselsnummerCalculator.getManyFodselsnummerForDate(date); @@ -52,7 +46,7 @@ public void testThatAllGeneratedNumbersAreValid() { @Test public void testThatAllGeneratedNumbersAreNotDNumbers() { for(Fodselsnummer fnr : FodselsnummerCalculator.getManyFodselsnummerForDate(date)) { - assertTrue("Ugyldig fødselsnummer: " + fnr, Fodselsnummer.isDNumber(fnr.toString()) != true); + assertFalse("Ugyldig fødselsnummer: " + fnr, Fodselsnummer.isDNumber(fnr.toString())); } } @@ -76,7 +70,7 @@ public void testThatAllGeneratedDNumbersAreValid() { @Test public void testThatAllGeneratedDNumbersAreDNumbers() { for(Fodselsnummer dnr : FodselsnummerCalculator.getManyDNumberFodselsnummerForDate(date)) { - assertTrue("Ugyldig D-nummer: " + dnr, Fodselsnummer.isDNumber(dnr.toString()) == true); + assertTrue("Ugyldig D-nummer: " + dnr, Fodselsnummer.isDNumber(dnr.toString())); } } diff --git a/src/test/java/no/bekk/bekkopen/person/FodselsnummerTest.java b/src/test/java/no/bekk/bekkopen/person/FodselsnummerTest.java old mode 100755 new mode 100644 index 3eec244..80de9dc --- a/src/test/java/no/bekk/bekkopen/person/FodselsnummerTest.java +++ b/src/test/java/no/bekk/bekkopen/person/FodselsnummerTest.java @@ -1,154 +1,144 @@ -package no.bekk.bekkopen.person; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import org.junit.Before; -import org.junit.Test; - -public class FodselsnummerTest { - - private static final String VALID_FODSELSNUMMER = "01010123476"; - - private static final String D_FODSELSNUMMER = "41010123476";//Invalid but valid is tested in FodselsnummerValidatorTest - - private Fodselsnummer sut = null; - - @Before - public void setUpValidFodselsnummer() throws Exception { - sut = new Fodselsnummer(VALID_FODSELSNUMMER); - } - - @Test - public void testGetDateAndMonth() { - assertEquals("0101", sut.getDateAndMonth()); - } - - @Test - public void testGetDayInMonth() { - assertEquals("01", sut.getDayInMonth()); - sut = new Fodselsnummer(D_FODSELSNUMMER); - assertEquals("01", sut.getDayInMonth()); - } - - @Test - public void testGetMonth() { - assertEquals("01", sut.getMonth()); - } - - @Test - public void testGetDateAndMonthDNumber() { - sut = new Fodselsnummer(D_FODSELSNUMMER); - assertEquals("0101", sut.getDateAndMonth()); - } - - @Test - public void testGetCentury() { - sut = new Fodselsnummer("01016666609"); - assertEquals("18", sut.getCentury()); - - sut = new Fodselsnummer("01015466609"); - assertEquals("18", sut.getCentury()); - - sut = new Fodselsnummer("01016633301"); - assertEquals("19", sut.getCentury()); - - sut = new Fodselsnummer("01019196697"); - assertEquals("19", sut.getCentury()); - - sut = new Fodselsnummer("01013366671"); - assertEquals("20", sut.getCentury()); - - // DNumber... - sut = new Fodselsnummer("41016666609"); - assertEquals("18", sut.getCentury()); - - sut = new Fodselsnummer("41016633301"); - assertEquals("19", sut.getCentury()); - - sut = new Fodselsnummer("41019196697"); - assertEquals("19", sut.getCentury()); - - sut = new Fodselsnummer("41013366671"); - assertEquals("20", sut.getCentury()); - } - - @Test - public void testGet2DigitBirthYear() { - assertEquals("01", sut.get2DigitBirthYear()); - } - - @Test - public void testGetBirthYear() { - assertEquals("1901", sut.getBirthYear()); - sut = new Fodselsnummer(D_FODSELSNUMMER); - assertEquals("1901", sut.getBirthYear()); - } - - @Test - public void testGetDateOfBirth() { - assertEquals("010101", sut.getDateOfBirth()); - } - - @Test - public void testGetDateOfBirthDNumber() { - sut = new Fodselsnummer(D_FODSELSNUMMER); - assertEquals("010101", sut.getDateOfBirth()); - } - - @Test - public void testGetPersonnummer() { - assertEquals("23476", sut.getPersonnummer()); - } - - @Test - public void testGetIndividnummer() { - assertEquals("234", sut.getIndividnummer()); - } - - @Test - public void testGetGenderDigit() { - assertEquals(4, sut.getGenderDigit()); - } - - @Test - public void testGetChecksumDigits() { - assertEquals(7, sut.getChecksumDigit1()); - assertEquals(6, sut.getChecksumDigit2()); - } - - @Test - public void testIsMale() { - assertEquals(false, sut.isMale()); - } - - @Test - public void testIsMaleDNumber() { - sut = new Fodselsnummer(D_FODSELSNUMMER); - assertEquals(false, sut.isMale()); - } - - @Test - public void testIsFemale() { - assertEquals(true, sut.isFemale()); - } - - @Test - public void testIsFemaleDNumber() { - sut = new Fodselsnummer(D_FODSELSNUMMER); - assertEquals(true, sut.isFemale()); - } - - @Test - public void testIsDNumber() { - assertFalse(Fodselsnummer.isDNumber("01010101006")); - assertFalse(Fodselsnummer.isDNumber("80000000000")); - assertTrue(Fodselsnummer.isDNumber("47086303651")); - } - - @Test - public void testParseDNumber() { - assertEquals("07086303651", Fodselsnummer.parseDNumber("47086303651")); - } -} +package no.bekk.bekkopen.person; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class FodselsnummerTest { + + private static final String VALID_FODSELSNUMMER = "01010123476"; + + private static final String D_FODSELSNUMMER = "41010123476";//Invalid but valid is tested in FodselsnummerValidatorTest + + private Fodselsnummer sut = null; + + @Before + public void setUpValidFodselsnummer() { + sut = new Fodselsnummer(VALID_FODSELSNUMMER); + } + + @Test + public void testGetDateAndMonth() { + assertEquals("0101", sut.getDateAndMonth()); + } + + @Test + public void testGetDayInMonth() { + assertEquals("01", sut.getDayInMonth()); + sut = new Fodselsnummer(D_FODSELSNUMMER); + assertEquals("01", sut.getDayInMonth()); + } + + @Test + public void testGetMonth() { + assertEquals("01", sut.getMonth()); + } + + @Test + public void testGetDateAndMonthDNumber() { + sut = new Fodselsnummer(D_FODSELSNUMMER); + assertEquals("0101", sut.getDateAndMonth()); + } + + @Test + public void testGetCentury() { + sut = new Fodselsnummer("01016666609"); + assertEquals("18", sut.getCentury()); + + sut = new Fodselsnummer("01015466609"); + assertEquals("18", sut.getCentury()); + + sut = new Fodselsnummer("01016633301"); + assertEquals("19", sut.getCentury()); + + sut = new Fodselsnummer("01019196697"); + assertEquals("19", sut.getCentury()); + + sut = new Fodselsnummer("01013366671"); + assertEquals("20", sut.getCentury()); + + // DNumber... + sut = new Fodselsnummer("41016666609"); + assertEquals("18", sut.getCentury()); + + sut = new Fodselsnummer("41016633301"); + assertEquals("19", sut.getCentury()); + + sut = new Fodselsnummer("41019196697"); + assertEquals("19", sut.getCentury()); + + sut = new Fodselsnummer("41013366671"); + assertEquals("20", sut.getCentury()); + } + + @Test + public void testGet2DigitBirthYear() { + assertEquals("01", sut.get2DigitBirthYear()); + } + + @Test + public void testGetBirthYear() { + assertEquals("1901", sut.getBirthYear()); + sut = new Fodselsnummer(D_FODSELSNUMMER); + assertEquals("1901", sut.getBirthYear()); + } + + @Test + public void testGetDateOfBirth() { + assertEquals("010101", sut.getDateOfBirth()); + } + + @Test + public void testGetDateOfBirthDNumber() { + sut = new Fodselsnummer(D_FODSELSNUMMER); + assertEquals("010101", sut.getDateOfBirth()); + } + + @Test + public void testGetPersonnummer() { + assertEquals("23476", sut.getPersonnummer()); + } + + @Test + public void testGetIndividnummer() { + assertEquals("234", sut.getIndividnummer()); + } + + @Test + public void testGetGenderDigit() { + assertEquals(4, sut.getGenderDigit()); + } + + @Test + public void testGetChecksumDigits() { + assertEquals(7, sut.getChecksumDigit1()); + assertEquals(6, sut.getChecksumDigit2()); + } + + @Test + public void testIsDNumber() { + assertFalse(Fodselsnummer.isDNumber("01010101006")); + assertFalse(Fodselsnummer.isDNumber("80000000000")); + assertTrue(Fodselsnummer.isDNumber("47086303651")); + } + + @Test + public void testParseDNumber() { + assertEquals("07086303651", Fodselsnummer.parseDNumber("47086303651")); + } + + @Test + public void testParseSynteticNumber() { + assertEquals("07086303651", Fodselsnummer.parseSynthenticNumber("07886303651")); + } + + @Test + public void testIsSynteticNumber() { + assertFalse(Fodselsnummer.isSynthetic("01010101006")); + assertFalse(Fodselsnummer.isSynthetic("80000000000")); + assertTrue(Fodselsnummer.isSynthetic("07886303651")); + } +} diff --git a/src/test/java/no/bekk/bekkopen/person/FodselsnummerValidatorTest.java b/src/test/java/no/bekk/bekkopen/person/FodselsnummerValidatorTest.java index 7e5f40c..234a2ab 100644 --- a/src/test/java/no/bekk/bekkopen/person/FodselsnummerValidatorTest.java +++ b/src/test/java/no/bekk/bekkopen/person/FodselsnummerValidatorTest.java @@ -1,130 +1,130 @@ -package no.bekk.bekkopen.person; - -import static no.bekk.bekkopen.common.Checksums.ERROR_INVALID_CHECKSUM; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import no.bekk.bekkopen.NoCommonsTestCase; - -import org.junit.Test; - -public class FodselsnummerValidatorTest extends NoCommonsTestCase { - - @Test - public void testInvalidFodselsnummerWrongLength() { - try { - FodselsnummerValidator.validateSyntax("0123456789"); - fail(); - } catch (IllegalArgumentException e) { - assertMessageContains(e, FodselsnummerValidator.ERROR_SYNTAX); - } - } - - @Test - public void testInvalidFodselsnummerNotDigits() { - try { - FodselsnummerValidator.validateSyntax("abcdefghijk"); - fail(); - } catch (IllegalArgumentException e) { - assertMessageContains(e, FodselsnummerValidator.ERROR_SYNTAX); - } - } - - @Test - public void testInvalidIndividnummer() { - try { - FodselsnummerValidator.validateIndividnummer("01015780000"); - fail(); - } catch (IllegalArgumentException e) { - assertMessageContains(e, FodselsnummerValidator.ERROR_INVALID_INDIVIDNUMMER); - } - } - - @Test - public void testInvalidDateMonthMax() { - try { - FodselsnummerValidator.validateDate("01130400000"); - fail(); - } catch (IllegalArgumentException e) { - assertMessageContains(e, FodselsnummerValidator.ERROR_INVALID_DATE); - } - } - - @Test - public void testInvalidDateMonthMin() { - try { - FodselsnummerValidator.validateDate("01000400000"); - fail(); - } catch (IllegalArgumentException e) { - assertMessageContains(e, FodselsnummerValidator.ERROR_INVALID_DATE); - } - } - - @Test - public void testInvalidDateDayMin() { - try { - FodselsnummerValidator.validateDate("00120467800"); - fail(); - } catch (IllegalArgumentException e) { - assertMessageContains(e, FodselsnummerValidator.ERROR_INVALID_DATE); - } - } - - @Test - public void testInvalidDateDayMax() { - try { - FodselsnummerValidator.validateDate("32120400000"); - fail(); - } catch (IllegalArgumentException e) { - assertMessageContains(e, FodselsnummerValidator.ERROR_INVALID_DATE); - } - } - - @Test - public void testInvalidDateLeapDay() { - try { - FodselsnummerValidator.validateDate("29020700000"); - fail(); - } catch (IllegalArgumentException e) { - assertMessageContains(e, FodselsnummerValidator.ERROR_INVALID_DATE); - } - } - - @Test - public void testInvalidFodselsnummerChecksum() { - try { - FodselsnummerValidator.validateChecksums("01010101010"); - fail(); - } catch (IllegalArgumentException e) { - assertMessageContains(e, ERROR_INVALID_CHECKSUM); - } - } - - @Test - public void testValidLeapDay() { - assertTrue(FodselsnummerValidator.isValid("29029633310")); - } - - @Test - public void testIsValid() { - assertTrue(FodselsnummerValidator.isValid("01010101006")); - } - - @Test - public void testDNumberIsValid() { - assertTrue(FodselsnummerValidator.isValid("47086303651")); - } - - @Test - public void testGetDNumber() { - FodselsnummerValidator.getFodselsnummer("47086303651"); - } - - @Test - public void testCalculateSecondChecksumDigit() { - Fodselsnummer testcase = new Fodselsnummer("1234567891"); - int correctChecksum = 1; - int calculatedChecksum = FodselsnummerValidator.calculateSecondChecksumDigit(testcase); - assertEquals(calculatedChecksum, correctChecksum); - } -} +package no.bekk.bekkopen.person; + +import no.bekk.bekkopen.NoCommonsTestCase; +import org.junit.Test; + +import static no.bekk.bekkopen.common.Checksums.ERROR_INVALID_CHECKSUM; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class FodselsnummerValidatorTest extends NoCommonsTestCase { + + @Test + public void testInvalidFodselsnummerWrongLength() { + try { + FodselsnummerValidator.validateSyntax("0123456789"); + fail(); + } catch (IllegalArgumentException e) { + assertMessageContains(e, FodselsnummerValidator.ERROR_SYNTAX); + } + } + + @Test + public void testInvalidFodselsnummerNotDigits() { + try { + FodselsnummerValidator.validateSyntax("abcdefghijk"); + fail(); + } catch (IllegalArgumentException e) { + assertMessageContains(e, FodselsnummerValidator.ERROR_SYNTAX); + } + } + + @Test + public void testInvalidIndividnummer() { + try { + FodselsnummerValidator.validateIndividnummer("01015780000"); + fail(); + } catch (IllegalArgumentException e) { + assertMessageContains(e, FodselsnummerValidator.ERROR_INVALID_INDIVIDNUMMER); + } + } + + @Test + public void testInvalidDateMonthMax() { + try { + FodselsnummerValidator.validateDate("01130400000"); + fail(); + } catch (IllegalArgumentException e) { + assertMessageContains(e, FodselsnummerValidator.ERROR_INVALID_DATE); + } + } + + @Test + public void testInvalidDateMonthMin() { + try { + FodselsnummerValidator.validateDate("01000400000"); + fail(); + } catch (IllegalArgumentException e) { + assertMessageContains(e, FodselsnummerValidator.ERROR_INVALID_DATE); + } + } + + @Test + public void testInvalidDateDayMin() { + try { + FodselsnummerValidator.validateDate("00120467800"); + fail(); + } catch (IllegalArgumentException e) { + assertMessageContains(e, FodselsnummerValidator.ERROR_INVALID_DATE); + } + } + + @Test + public void testInvalidDateDayMax() { + try { + FodselsnummerValidator.validateDate("32120400000"); + fail(); + } catch (IllegalArgumentException e) { + assertMessageContains(e, FodselsnummerValidator.ERROR_INVALID_DATE); + } + } + + @Test + public void testInvalidDateLeapDay() { + try { + FodselsnummerValidator.validateDate("29020700000"); + fail(); + } catch (IllegalArgumentException e) { + assertMessageContains(e, FodselsnummerValidator.ERROR_INVALID_DATE); + } + } + + @Test + public void testInvalidFodselsnummerChecksum() { + try { + FodselsnummerValidator.validateChecksums("01010101010"); + fail(); + } catch (IllegalArgumentException e) { + assertMessageContains(e, ERROR_INVALID_CHECKSUM); + } + } + + @Test + public void testValidLeapDay() { + assertTrue(FodselsnummerValidator.isValid("29029633310")); + } + + @Test + public void testIsValid() { + assertTrue(FodselsnummerValidator.isValid("01010101006")); + } + + @Test + public void testDNumberIsValid() { + assertTrue(FodselsnummerValidator.isValid("47086303651")); + } + + @Test + public void testGetDNumber() { + FodselsnummerValidator.getFodselsnummer("47086303651"); + } + + @Test + public void testCalculateSecondChecksumDigit() { + Fodselsnummer testcase = new Fodselsnummer("1234567891"); + int correctChecksum = 1; + int calculatedChecksum = FodselsnummerValidator.calculateSecondChecksumDigit(testcase); + assertEquals(calculatedChecksum, correctChecksum); + } +} diff --git a/src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerCalculatorTest.java b/src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerCalculatorTest.java new file mode 100644 index 0000000..4ff3fd3 --- /dev/null +++ b/src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerCalculatorTest.java @@ -0,0 +1,50 @@ +package no.bekk.bekkopen.person; + +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +import static org.junit.Assert.assertTrue; + +public class SyntheticFodselsnummerCalculatorTest { + + private Date date = null; + + @BeforeClass + public static void setup() { + FodselsnummerValidator.FAIL_ON_SYNTHETIC_NUMBER = false; + } + + @AfterClass + public static void taredown() { + FodselsnummerValidator.FAIL_ON_SYNTHETIC_NUMBER = true; + } + + @Before + public void setUpDate() throws Exception { + DateFormat df = new SimpleDateFormat("ddMMyyyy"); + date = df.parse("09062006"); + } + + @Test + public void testThatAllGeneratedSyntheticDNumbersAreValid() { + for (Fodselsnummer fnr : FodselsnummerCalculator.getManySynteticDNumberFodselsnummerForDate(date)) { + assertTrue("Ugyldig fødselsnummer: " + fnr, FodselsnummerValidator.isValid(fnr.toString())); + } + } + + @Test + public void testThatAtLeastOneSynteticNumberIsGenerated() { + assertTrue(FodselsnummerCalculator.getManySynteticFodselsnummerForDate(date).size() >= 1); + } + + @Test + public void testThatAtLeastOneSynteticDNumberIsGenerated() { + assertTrue(FodselsnummerCalculator.getManySynteticDNumberFodselsnummerForDate(date).size() >= 1); + } +} diff --git a/src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerValidatorTest.java b/src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerValidatorTest.java new file mode 100644 index 0000000..6eb8fe6 --- /dev/null +++ b/src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerValidatorTest.java @@ -0,0 +1,27 @@ +package no.bekk.bekkopen.person; + +import no.bekk.bekkopen.NoCommonsTestCase; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class SyntheticFodselsnummerValidatorTest extends NoCommonsTestCase { + + @BeforeClass + public static void setup() { + FodselsnummerValidator.FAIL_ON_SYNTHETIC_NUMBER = false; + } + + @AfterClass + public static void taredown() { + FodselsnummerValidator.FAIL_ON_SYNTHETIC_NUMBER = true; + } + + @Test + public void testSynteticDNumberIsValid() { + assertTrue(FodselsnummerValidator.isValid("49860699787")); + } + +} From 577021093e697f6ca44968ed9a6dd7181f81302f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eivind=20Bergst=C3=B8l?= Date: Thu, 29 Oct 2020 09:44:50 +0100 Subject: [PATCH 2/3] Flipper flagget for syntetiske brukere. --- .../no/bekk/bekkopen/person/FodselsnummerValidator.java | 8 ++++---- .../person/SyntheticFodselsnummerCalculatorTest.java | 4 ++-- .../person/SyntheticFodselsnummerValidatorTest.java | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/no/bekk/bekkopen/person/FodselsnummerValidator.java b/src/main/java/no/bekk/bekkopen/person/FodselsnummerValidator.java index 9e331fd..182e079 100644 --- a/src/main/java/no/bekk/bekkopen/person/FodselsnummerValidator.java +++ b/src/main/java/no/bekk/bekkopen/person/FodselsnummerValidator.java @@ -37,11 +37,11 @@ public class FodselsnummerValidator extends StringNumberValidator implements Con protected static final String ERROR_INVALID_INDIVIDNUMMER = "Invalid individnummer in fødselsnummer : "; /** - * Truthy validation of synthetic fnr/dnr is false. You are adviced to - * set til value to false in test environments where you expect + * Truthy validation of synthetic fnr/dnr is true. You are adviced to + * set til value to true in test environments where you expect * syntetic fnr/dnr to appair. */ - public static boolean FAIL_ON_SYNTHETIC_NUMBER = true; + public static boolean ALLOW_SYNTHETIC_NUMBERS = false; /** * Returns an object that represents a Fodselsnummer. @@ -62,7 +62,7 @@ public static no.bekk.bekkopen.person.Fodselsnummer getFodselsnummer(String fods } private static void validateSynthetic(String fodselsnummer) { - if (Fodselsnummer.isSynthetic(fodselsnummer) && FAIL_ON_SYNTHETIC_NUMBER) { + if (Fodselsnummer.isSynthetic(fodselsnummer) && !ALLOW_SYNTHETIC_NUMBERS) { throw new IllegalArgumentException("Syntetic fødselsnummer is not allowd" + fodselsnummer); } } diff --git a/src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerCalculatorTest.java b/src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerCalculatorTest.java index 4ff3fd3..249c4fb 100644 --- a/src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerCalculatorTest.java +++ b/src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerCalculatorTest.java @@ -17,12 +17,12 @@ public class SyntheticFodselsnummerCalculatorTest { @BeforeClass public static void setup() { - FodselsnummerValidator.FAIL_ON_SYNTHETIC_NUMBER = false; + FodselsnummerValidator.ALLOW_SYNTHETIC_NUMBERS = true; } @AfterClass public static void taredown() { - FodselsnummerValidator.FAIL_ON_SYNTHETIC_NUMBER = true; + FodselsnummerValidator.ALLOW_SYNTHETIC_NUMBERS = false; } @Before diff --git a/src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerValidatorTest.java b/src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerValidatorTest.java index 6eb8fe6..c9d48b1 100644 --- a/src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerValidatorTest.java +++ b/src/test/java/no/bekk/bekkopen/person/SyntheticFodselsnummerValidatorTest.java @@ -11,12 +11,12 @@ public class SyntheticFodselsnummerValidatorTest extends NoCommonsTestCase { @BeforeClass public static void setup() { - FodselsnummerValidator.FAIL_ON_SYNTHETIC_NUMBER = false; + FodselsnummerValidator.ALLOW_SYNTHETIC_NUMBERS = true; } @AfterClass public static void taredown() { - FodselsnummerValidator.FAIL_ON_SYNTHETIC_NUMBER = true; + FodselsnummerValidator.ALLOW_SYNTHETIC_NUMBERS = false; } @Test From 86f020b4cdde357eff946d709c581a98817a2863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eivind=20Bergst=C3=B8l?= Date: Thu, 29 Oct 2020 10:19:03 +0100 Subject: [PATCH 3/3] =?UTF-8?q?Legg=20til=20st=C3=B8tte=20for=20=C3=A5=20h?= =?UTF-8?q?a=20et=20classpath-flagg=20for=20=C3=A5=20aktivere=20syntetiske?= =?UTF-8?q?=20brukere.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Flagget kan f.eks legges i /src/test/resources --- .../no/bekk/bekkopen/person/FodselsnummerValidator.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/no/bekk/bekkopen/person/FodselsnummerValidator.java b/src/main/java/no/bekk/bekkopen/person/FodselsnummerValidator.java index 182e079..4e716c6 100644 --- a/src/main/java/no/bekk/bekkopen/person/FodselsnummerValidator.java +++ b/src/main/java/no/bekk/bekkopen/person/FodselsnummerValidator.java @@ -4,6 +4,7 @@ import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; +import java.net.URL; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -43,6 +44,13 @@ public class FodselsnummerValidator extends StringNumberValidator implements Con */ public static boolean ALLOW_SYNTHETIC_NUMBERS = false; + static { + final URL flag = FodselsnummerValidator.class.getResource("/no.bekk.bekkopen.person.allow_synthetic_numbers.flag"); + if (flag != null) { + ALLOW_SYNTHETIC_NUMBERS = true; + } + } + /** * Returns an object that represents a Fodselsnummer. *