Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Oct 29, 2020
2 parents 952e90a + 86f020b commit bfcfa2a
Show file tree
Hide file tree
Showing 9 changed files with 464 additions and 316 deletions.
8 changes: 1 addition & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>com.jcabi</groupId>
<artifactId>parent</artifactId>
<version>0.49.2</version>
<version>0.50.5</version>
</parent>

<scm>
Expand Down Expand Up @@ -141,12 +141,6 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
38 changes: 31 additions & 7 deletions src/main/java/no/bekk/bekkopen/person/Fodselsnummer.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand Down
47 changes: 43 additions & 4 deletions src/main/java/no/bekk/bekkopen/person/FodselsnummerCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,49 @@ public static List<Fodselsnummer> 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<Fodselsnummer> 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<Fodselsnummer> 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);
}

Expand Down
36 changes: 33 additions & 3 deletions src/main/java/no/bekk/bekkopen/person/FodselsnummerValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,6 +15,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<no.bekk.bekkopen.person.annotation.Fodselsnummer, String> {

Expand All @@ -28,9 +37,23 @@ 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 true. You are adviced to
* set til value to true in test environments where you expect
* syntetic fnr/dnr to appair.
*/
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.
*
*
* @param fodselsnummer
* A String containing a Fodselsnummer
* @return A Fodselsnummer instance
Expand All @@ -42,12 +65,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) && !ALLOW_SYNTHETIC_NUMBERS) {
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
Expand Down
16 changes: 5 additions & 11 deletions src/test/java/no/bekk/bekkopen/person/FodselsnummerCalculatorTest.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -24,12 +24,6 @@ public void setUpDate() throws Exception {
date = df.parse("09062006");
}

@Test
public void testGetFodselsnummerForDateAndGender() throws ParseException {
List<Fodselsnummer> options = FodselsnummerCalculator.getFodselsnummerForDateAndGender(date, KJONN.KVINNE);
assertTrue("Forventet minst 10 fødselsnumre, men fikk " + options.size(), options.size() > 10);
}

@Test
public void testGetFodselsnummerForDate() {
List<Fodselsnummer> options = FodselsnummerCalculator.getManyFodselsnummerForDate(date);
Expand All @@ -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()));
}
}

Expand All @@ -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()));
}
}

Expand Down
Loading

0 comments on commit bfcfa2a

Please sign in to comment.