-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from personnummer/issue/unit-tests-and-regex
Issue/unit tests and regex
- Loading branch information
Showing
8 changed files
with
179 additions
and
55 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Release | ||
on: | ||
release: | ||
types: [published] | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Set up JDK 1.8 | ||
uses: actions/setup-java@v1 | ||
|
||
- name: Build with Gradle | ||
run: gradle build | ||
|
||
- name: Publish to GitHub Packages | ||
run: gradle publish | ||
env: | ||
USERNAME: ${{ github.actor }} | ||
TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: Test | ||
on: | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-18.04 | ||
strategy: | ||
matrix: | ||
java: [ 8, 10, 12, 13 ] | ||
name: Java ${{ matrix.java }} test. | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Setup java | ||
uses: actions/setup-java@v1 | ||
with: | ||
java-version: ${{ matrix.java }} | ||
- run: gradle test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,4 @@ build/ | |
.classpath | ||
.project | ||
.settings/ | ||
temp.json |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,136 @@ | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.json.*; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class PersonnummerTest { | ||
private static Boolean fileLoaded = false; | ||
|
||
private static List<Long> validSsnInt = new ArrayList<>(); | ||
private static List<String> validSsnString = new ArrayList<>(); | ||
private static List<Long> invalidSsnInt = new ArrayList<>(); | ||
private static List<String> invalidSsnString = new ArrayList<>(); | ||
private static List<Long> validConInt = new ArrayList<>(); | ||
private static List<String> validConString = new ArrayList<>(); | ||
private static List<Long> invalidConInt = new ArrayList<>(); | ||
private static List<String> invalidConString = new ArrayList<>(); | ||
|
||
@AfterClass | ||
public static void deleteTestData() throws IOException { | ||
Files.delete(Paths.get("temp.json")); | ||
} | ||
|
||
@BeforeClass | ||
public static void loadTestData() throws IOException { | ||
if (fileLoaded) { | ||
return; | ||
} | ||
|
||
if (!Files.exists(Paths.get("temp.json"))) { | ||
InputStream in = new URL("https://raw.githubusercontent.com/personnummer/meta/master/testdata/structured.json").openStream(); | ||
Files.copy(in, Paths.get("temp.json"), StandardCopyOption.REPLACE_EXISTING); | ||
fileLoaded = true; | ||
} | ||
|
||
String jsonString = new String(Files.readAllBytes(Paths.get("temp.json"))); | ||
JSONObject json = new JSONObject(jsonString); | ||
|
||
JSONObject ssn = json.getJSONObject("ssn"); | ||
JSONObject con = json.getJSONObject("con"); | ||
|
||
validSsnInt = getIntList(ssn, "integer", "valid"); | ||
invalidSsnInt = getIntList(ssn, "integer", "invalid"); | ||
validSsnString = getStringList(ssn, "string", "valid"); | ||
invalidSsnString = getStringList(ssn, "string", "invalid"); | ||
|
||
validConInt = getIntList(con, "integer", "valid"); | ||
invalidConInt = getIntList(con, "integer", "invalid"); | ||
validConString = getStringList(con, "string", "valid"); | ||
invalidConString = getStringList(con, "string", "invalid"); | ||
} | ||
|
||
private static ArrayList<String> getStringList(JSONObject root, String dataType, String valid) { | ||
JSONArray arr = root.getJSONObject(dataType).getJSONArray(valid); | ||
ArrayList<String> result = new ArrayList<>(); | ||
for (int i=0; i<arr.length(); i++) { | ||
result.add(arr.getString(i)); | ||
} | ||
return result; | ||
} | ||
|
||
private static ArrayList<Long> getIntList(JSONObject root, String dataType, String valid) { | ||
JSONArray arr = root.getJSONObject(dataType).getJSONArray(valid); | ||
ArrayList<Long> result = new ArrayList<>(); | ||
for (int i=0; i<arr.length(); i++) { | ||
result.add(arr.getLong(i)); | ||
} | ||
return result; | ||
} | ||
|
||
@Test | ||
public void testPersonnNummerWithInvalidIntegerValues() { | ||
for (Long ssn: invalidSsnInt) { | ||
assertFalse(Personnummer.valid(ssn)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCoordinationNummerWithInvalidIntegerValues() { | ||
for (Long ssn: invalidConInt) { | ||
assertFalse(Personnummer.valid(ssn)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testPersonnNummerWithInvalidStringValues() { | ||
for (String ssn: invalidSsnString) { | ||
assertFalse(Personnummer.valid(ssn)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testWithControlDigit() { | ||
assertTrue(Personnummer.valid("6403273813")); | ||
assertTrue(Personnummer.valid("510818-9167")); | ||
assertTrue(Personnummer.valid("19900101-0017")); | ||
assertTrue(Personnummer.valid("19130401+2931")); | ||
assertTrue(Personnummer.valid("196408233234")); | ||
assertTrue(Personnummer.valid("0001010107")); | ||
assertTrue(Personnummer.valid("000101-0107")); | ||
assertTrue(Personnummer.valid("1010101010")); | ||
assertTrue(Personnummer.valid(6403273813L)); | ||
assertTrue(Personnummer.valid(5108189167L)); | ||
assertTrue(Personnummer.valid(199001010017L)); | ||
assertTrue(Personnummer.valid(191304012931L)); | ||
assertTrue(Personnummer.valid(196408233234L)); | ||
public void testCoordinationNummerWithInvalidStringValues() { | ||
for (String ssn: invalidConString) { | ||
assertFalse(Personnummer.valid(ssn)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testWithoutControlDigit() { | ||
assertFalse(Personnummer.valid(640327381L)); | ||
assertFalse(Personnummer.valid(510818916L)); | ||
assertFalse(Personnummer.valid(19900101001L)); | ||
assertFalse(Personnummer.valid(100101001L)); | ||
assertFalse(Personnummer.valid("640327-381")); | ||
assertFalse(Personnummer.valid("510818-916")); | ||
assertFalse(Personnummer.valid("19900101-001")); | ||
assertFalse(Personnummer.valid("100101+001")); | ||
public void testPersonnNummerWithValidIntegerValues() { | ||
for (Long ssn: validSsnInt) { | ||
assertTrue(Personnummer.valid(ssn)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testWithInvalidValues() { | ||
assertFalse(Personnummer.valid("A string")); | ||
assertFalse(Personnummer.valid("Two")); | ||
assertFalse(Personnummer.valid("222")); | ||
assertFalse(Personnummer.valid(null)); | ||
assertFalse(Personnummer.valid("9701063-2391")); | ||
public void testCoordinationNummerVnvalidIntegerValues() { | ||
for (Long ssn: validConInt) { | ||
assertTrue(Personnummer.valid(ssn)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCoordinationNumbers() { | ||
assertTrue(Personnummer.valid("701063-2391")); | ||
assertTrue(Personnummer.valid("640883-3231")); | ||
assertTrue(Personnummer.valid(7010632391L)); | ||
assertTrue(Personnummer.valid(6408833231L)); | ||
public void testPersonnNummerWithValidStringValues() { | ||
for (String ssn: validSsnString) { | ||
assertTrue(Personnummer.valid(ssn)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testWithBadCoordinationNumbers() { | ||
assertFalse(Personnummer.valid(9001610017L)); | ||
assertFalse(Personnummer.valid(6408933231L)); | ||
assertFalse(Personnummer.valid("900161-0017")); | ||
assertFalse(Personnummer.valid("640893-3231")); | ||
public void testCoordinationNummerWithValidStringValues() { | ||
for (String ssn: validConString) { | ||
assertTrue(Personnummer.valid(ssn)); | ||
} | ||
} | ||
|
||
} |