Skip to content

Commit

Permalink
Changed behavior so that this returns only digits, and the skip param…
Browse files Browse the repository at this point in the history
…eter tells how many digits to skip from the start.
  • Loading branch information
Rolf committed Feb 9, 2020
1 parent cd90fcd commit f23ad1c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,41 @@
* @author Bartłomiej Komendarczuk
*/

public class PartialCharacterAnonymizer implements Anonymizer {
public class PartialDigitsAnonymizer implements Anonymizer {
private static final Random random = new Random();

public static String PARAMETER = "characters";
private static final String DIGITS = "0123456789";

protected String CHARS;
public static String PARAMETER = "skip";

public int NUMBER_OF_SKIPPED_CHARACTERS;

public PartialCharacterAnonymizer() {
CHARS = getDefaultCharacterString();
NUMBER_OF_SKIPPED_CHARACTERS = getNumberOfSkippedCharacters();
}

protected String getDefaultCharacterString() {
return "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
public PartialDigitsAnonymizer() {
}

protected int getNumberOfSkippedCharacters() { return 5;}

@Override
public String getType() {
return "PARTIALRANDOMCHARACTERS";
return "PARTIALRANDOMDIGITS";
}

@Override
public Synonym anonymize(Object from, int size, boolean shortlived, Map<String, String> parameters) {
if (parameters == null || !parameters.containsKey(PARAMETER)) {
throw new UnsupportedOperationException("Please provide '" + PARAMETER + "' as configuration parameter.");
throw new UnsupportedOperationException("Please provide '" + PARAMETER + "' parameter to indicate the number of characters to skip.");
}
return anonymize(from, size, shortlived, parameters.get(PARAMETER));
return anonymize(from, size, shortlived, Integer.valueOf(parameters.get(PARAMETER)));
}

@Override
public Synonym anonymize(Object from, int size, boolean shortlived) {
return anonymize(from, size, shortlived, CHARS);
return anonymize(from, size, shortlived, 0);
}

private Synonym anonymize(Object from, int size, boolean shortlived, String characters) {
private Synonym anonymize(Object from, int size, boolean shortlived, int charactersToSkip) {
String fromString = from.toString();
Random r = new Random();

int length = fromString.length();
StringBuilder sb = new StringBuilder(fromString.substring(0, NUMBER_OF_SKIPPED_CHARACTERS));
for (int i = NUMBER_OF_SKIPPED_CHARACTERS; i < length; i++) {
sb.append(characters.charAt(r.nextInt(characters.length())));

StringBuilder sb = new StringBuilder(fromString.substring(0, charactersToSkip));
for (int i = charactersToSkip; i < length; i++) {
sb.append(sb.append(DIGITS.charAt(random.nextInt(DIGITS.length()))));
}

String to = sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,40 @@
import java.util.Map;

/**
* Tests for {@link PartialCharacterAnonymizer}.
* Tests for {@link PartialDigitsAnonymizer}.
*
* @author Bartłomiej Komendarczuk
*/

public class PartialCharacterAnonymizerTest extends TestCase {

private PartialCharacterAnonymizer partialCharacterAnonymizer = new PartialCharacterAnonymizer();
private PartialDigitsAnonymizer partialCharacterAnonymizer = new PartialDigitsAnonymizer();

@Override
protected void setUp() throws Exception {
super.setUp();
partialCharacterAnonymizer = new PartialCharacterAnonymizer();
partialCharacterAnonymizer = new PartialDigitsAnonymizer();
}

public void testShouldAnonymizeCharactersAfterSkippingCharacters(){

String from = "TEST_ANONYMIZER";
String CHARS = "ABCDEFGHIJKLMNOPRSTUWVXYZ";
partialCharacterAnonymizer.NUMBER_OF_SKIPPED_CHARACTERS = 5;
String from = "TEST_123";

Map<String, String> m = new HashMap<>();
m.put(PartialCharacterAnonymizer.PARAMETER, CHARS);
m.put(PartialDigitsAnonymizer.PARAMETER, String.valueOf(3));
Synonym synonym = partialCharacterAnonymizer.anonymize(from, 5, false, m);

String to = (String) synonym.getTo();

Assert.assertNotSame(to.substring(5) +" is not different than " + from.substring(5), to.substring(5) ,from.substring(5));
Assert.assertNotSame(to.substring(3) +" is not different than " + from.substring(3), to.substring(3) ,from.substring(3));
}

public void testShouldNotAnonymizeTheFirstCharacters(){

String from = "TEST_ANONYMIZER";
String CHARS = "ABCDEFGHIJKLMNOPRSTUWVXYZ";
partialCharacterAnonymizer.NUMBER_OF_SKIPPED_CHARACTERS = 5;

Map<String, String> m = new HashMap<>();
m.put(PartialCharacterAnonymizer.PARAMETER, CHARS);
m.put(PartialDigitsAnonymizer.PARAMETER, String.valueOf(5));
Synonym synonym = partialCharacterAnonymizer.anonymize(from, 5, false, m);

String to = (String) synonym.getTo();
Expand All @@ -56,12 +52,10 @@ public void testShouldNotAnonymizeTheFirstCharacters(){
public void testShouldAnonymizeCharactersAfterSkippingCharactersUsingDigitals(){

String from = "3551085";
String CHARS = "1234567890";
partialCharacterAnonymizer.NUMBER_OF_SKIPPED_CHARACTERS = 3;

Map<String, String> m = new HashMap<>();
m.put(PartialCharacterAnonymizer.PARAMETER, CHARS);
Synonym synonym = partialCharacterAnonymizer.anonymize(from, 5, false, m);
m.put(PartialDigitsAnonymizer.PARAMETER, String.valueOf(3));
Synonym synonym = partialCharacterAnonymizer.anonymize(from, 3, false, m);

String to = (String) synonym.getTo();

Expand All @@ -71,15 +65,13 @@ public void testShouldAnonymizeCharactersAfterSkippingCharactersUsingDigitals(){
public void testShouldNotAnonymizeTheFirstCharactersUsingDigitals(){

String from = "3551085";
String CHARS = "1234567890";
partialCharacterAnonymizer.NUMBER_OF_SKIPPED_CHARACTERS = 3;

Map<String, String> m = new HashMap<>();
m.put(PartialCharacterAnonymizer.PARAMETER, CHARS);
m.put(PartialDigitsAnonymizer.PARAMETER, String.valueOf(5));
Synonym synonym = partialCharacterAnonymizer.anonymize(from, 5, false, m);

String to = (String) synonym.getTo();

Assert.assertEquals(to.substring(0,3) + " is not same as " + from.substring(0,3),to.substring(0,3), from.substring(0,3));
Assert.assertEquals(to.substring(0,5) + " is not same as " + from.substring(0,5),to.substring(0,5), from.substring(0,5));
}
}

0 comments on commit f23ad1c

Please sign in to comment.