Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: Implement readable toString for PredictableRandomizer [TECH-1449] #17

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/main/java/org/hisp/dhis/utils/PredictableRandomizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ public class PredictableRandomizer implements Randomizer {

private final Faker faker;

private final long seed;

public PredictableRandomizer( long seed )
{
this.seed = seed;
this.rnd = new Random( seed );
this.faker = new Faker( rnd );
}
Expand Down Expand Up @@ -79,12 +82,6 @@ public <T> List<T> randomElementsFromList( List<T> list, int size )
return resultList;
}

@Override
public String randomString()
{
return this.faker.programmingLanguage().name();
}

public String randomString( int size )
{
return this.faker.lorem().characters( size, true, false );
Expand Down Expand Up @@ -132,11 +129,6 @@ public String randomNationalId() {
return this.faker.idNumber().valid();
}

@Override
public Date randomAdultBirthday() {
return this.faker.date().birthday( 18, 80 );
}

@Override
public Date randomBirthday(int minAge, int maxAge){
return this.faker.date().birthday( minAge, maxAge );
Expand Down Expand Up @@ -191,4 +183,9 @@ private LocalDate toLocalDate( Date dateToConvert )
{
return dateToConvert.toInstant().atZone( ZoneId.systemDefault() ).toLocalDate();
}

@Override
public String toString() {
return "PredictableRandomizer("+ this.seed +")";
}
}
10 changes: 7 additions & 3 deletions src/main/java/org/hisp/dhis/utils/Randomizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ default <T> T randomElementFromList( List<T> list )
*
* @return a String
*/
String randomString();
default String randomString(){
return randomString(randomInt(20));
}

/**
* Returns random string of alphabetical characters.
Expand Down Expand Up @@ -125,7 +127,9 @@ default <T> T randomElementFromList( List<T> list )
*
* @return a birthday date
*/
Date randomAdultBirthday();
default Date randomAdultBirthday(){
return randomBirthday(18, 80);
};

/**
* Generates random birthday date with age
Expand All @@ -141,7 +145,7 @@ default <T> T randomElementFromList( List<T> list )
* @param wordCount number of words in the text
* @return a long text
*/
String randomLongText(int wordCount );
String randomLongText( int wordCount );

/**
* Generates random phone number
Expand Down