Skip to content

Commit

Permalink
Prevent duplicated random BSNs in one test run
Browse files Browse the repository at this point in the history
Fixes #61
  • Loading branch information
fhoeben committed Dec 18, 2024
1 parent 9cfd379 commit 446e4f7
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 31 deletions.
90 changes: 61 additions & 29 deletions src/main/java/nl/hsac/fitnesse/util/BsnUtil.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,77 @@
package nl.hsac.fitnesse.util;

import java.util.HashSet;
import java.util.Set;

/**
* Helpers for BSNs.
*/
public class BsnUtil {
private RandomUtil randomUtil = new RandomUtil();
private final Set<String> bsnsToExclude = new HashSet<>();

/**
* Generates random number that could be a BSN.
* Based on: http://www.testnummers.nl/bsn.js
* @return random BSN.
*/
public String generateBsn() {
String result;
int attempts = 0;
do {
if (attempts > 1000) {
throw new RuntimeException("Unable to generate new random BSN");
}
attempts++;
result = generateNextBsn();
} while (!bsnsToExclude.add(result));
return result;
}

/**
* Checks whether BSN is valid.
* Based on: https://mxforum.mendix.com/questions/2162/
* @param bsn BSN to check.
* @return true if it is structurally sound.
*/
public boolean testBsn(String bsn) {
try {
Double.parseDouble(bsn);
} catch (Exception e) {
return false;
}
if (bsn.length() != 9) {
return false;
} else {
int checksum = 0;
for (int i = 0; i < 8; i++) {
checksum += (Integer.parseInt(Character.toString(bsn.charAt(i))) * (9 - i));
}
checksum -= Integer.parseInt(Character.toString(bsn.charAt(8)));
if (checksum % 11 != 0) {
return false;
}
}
return true;

}

public void addBsnToExclude(String bsn) {
bsnsToExclude.add(bsn);
}

public Set<String> getBsnsToExclude() {
return bsnsToExclude;
}

public void resetExcludedBsns() {
bsnsToExclude.clear();
}

/**
* Based on: http://www.testnummers.nl/bsn.js
* @return String that passes BSN test.
*/
private String generateNextBsn() {
String Result1 = "";
int Nr9 = randomUtil.random(3);
int Nr8 = randomUtil.random(10);
Expand Down Expand Up @@ -49,34 +109,6 @@ public String generateBsn() {
return Result1;
}

/**
* Checks whether BSN is valid.
* Based on: https://mxforum.mendix.com/questions/2162/
* @param bsn BSN to check.
* @return true if it is structurally sound.
*/
public boolean testBsn(String bsn) {
try {
Double.parseDouble(bsn);
} catch (Exception e) {
return false;
}
if (bsn.length() != 9) {
return false;
} else {
int checksum = 0;
for (int i = 0; i < 8; i++) {
checksum += (Integer.parseInt(Character.toString(bsn.charAt(i))) * (9 - i));
}
checksum -= Integer.parseInt(Character.toString(bsn.charAt(8)));
if (checksum % 11 != 0) {
return false;
}
}
return true;

}

private int floor(double d) {
return (int) d;
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/nl/hsac/fitnesse/symbols/RandomBsnTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package nl.hsac.fitnesse.symbols;

import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

import static org.junit.Assert.assertFalse;

public class RandomBsnTest {
@Test
public void ensureNoDuplicates() {
Set<String> generatedBsns = new HashSet<>();
for (int i = 0; i < 10000; i++) {
String bsn = new RandomBsn().toTarget(null, null);
assertFalse("Duplicated value at loop: " + i, generatedBsns.contains(bsn));
generatedBsns.add(bsn);
}
}
}
43 changes: 41 additions & 2 deletions src/test/java/nl/hsac/fitnesse/util/BsnUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
package nl.hsac.fitnesse.util;

import org.junit.After;
import org.junit.Test;

import static org.junit.Assert.*;
import java.util.HashSet;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Tests BsnUtil.
*/
public class BsnUtilTest {
private final BsnUtil generator = new BsnUtil();

@After
public void resetGenerator() {
generator.resetExcludedBsns();
}

/**
* Tests basic generation.
*/
@Test
public void testGenerate() {
for (int i = 0; i < 100; i++) {
for (int i = 0; i < 1000; i++) {
String result = generator.generateBsn();
assertEquals("Got: " + result, 9, result.length());
assertTrue("Got: " + result, generator.testBsn(result));
}
}

@Test
public void ensureNoDuplicates() {
Set<String> generatedBsns = new HashSet<>();
for (int i = 0; i < 100_000; i++) {
String result = generator.generateBsn();
assertFalse("Duplicated value at loop: " + i, generatedBsns.contains(result));
generatedBsns.add(result);
}

assertEquals(generatedBsns, generator.getBsnsToExclude());
}

@Test
public void resetClears() {
generator.generateBsn();
assertFalse(generator.getBsnsToExclude().isEmpty());

generator.resetExcludedBsns();
assertTrue(generator.getBsnsToExclude().isEmpty());
}

@Test
public void testAddBsnToExclude() {
assertFalse(generator.getBsnsToExclude().contains("abc"));
generator.addBsnToExclude("abc");
assertTrue(generator.getBsnsToExclude().contains("abc"));
}
}

0 comments on commit 446e4f7

Please sign in to comment.