Skip to content

Commit

Permalink
feat: add more method to generate rnd data (#9)
Browse files Browse the repository at this point in the history
- Bump version of javafaker to latest
- Add methods to generate different type of rnd data to helper class
- Increase lib version
  • Loading branch information
Luciano Fiandesio authored Mar 25, 2020
1 parent 6fe9652 commit 582976f
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ hs_err_pid*

#InteliJ
.idea/*
target/*
target/*
*.iml
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.hisp.dhis</groupId>
<artifactId>api-test-utils</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down Expand Up @@ -53,7 +53,7 @@
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.18</version>
<version>1.0.2</version>
</dependency>
</dependencies>

Expand Down
187 changes: 180 additions & 7 deletions src/main/java/org/hisp/dhis/utils/DataRandomizer.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
package org.hisp.dhis.utils;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import org.apache.commons.lang3.RandomStringUtils;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.apache.commons.lang3.RandomStringUtils;

import com.github.javafaker.Faker;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

/**
* Utility methods for random data generation
*
* @author Gintare Vilkelyte <vilkelyte.gintare@gmail.com>
* @author Luciano Fiandesio <luciano@dhis2.org>
*/
public class DataRandomizer
{
/**
* Generates a {@see Point} based on randomized coordinates
*
* @return a {@see Point}
*/
public static Point randomPoint()
{
double latitude = (Math.random() * 180.0) - 90.0;
Expand All @@ -27,22 +43,43 @@ public static Point randomPoint()

/**
* Returns random string containing 6 alphabetical characters.
* @return
*
* @return a String
*/
public static String randomString()
{
return RandomStringUtils.randomAlphabetic( 6 );
}

/**
* Returns random entity name containing static string joined with 6 random alphabetical characters
* Returns random string of alphabetical characters.
*
* @param size the size of the string
* @return a String
*/
public static String randomString( int size )
{
return RandomStringUtils.randomAlphabetic( size );
}

/**
* Returns random entity name containing static string joined with 6 random
* alphabetical characters
*
* @return
*/
public static String randomEntityName()
{
return "AutoTest entity " + randomString();
}

/**
* Generates a random sequence of integers
*
* @param collectionSize max size of collection
* @param max max value of each integer
* @return a List of Integer
*/
public static List<Integer> randomSequence( int collectionSize, int max )
{
List<Integer> indexes = new ArrayList<>();
Expand All @@ -66,4 +103,140 @@ public static List<Integer> randomSequence( int collectionSize, int max )
return indexes;
}

/**
* Generates a random integer
*
* @param min the minimum boundary for the generated int
* @param max the maximum boundary for the generated int
* @return a random int
*/
public static int randomIntInRange( int min, int max )
{
return Faker.instance().number().numberBetween( min, max );
}

/**
* Generates a random integer
*
* @param decimals maximum number of places
* @param min minimum value
* @param max maximum value
*/
public static double randomDoubleInRange( int min, int max, int decimals )
{
return Faker.instance().number().randomDouble( decimals, min, max );
}

/**
* Generates a random integer value
*
* @return a int
*/
public static int randomInt()
{
return Math.toIntExact( Faker.instance().number().randomNumber() );
}

/**
* Extracts a random element from a list
*
* @param list a List
*
* @return a random element from the list
*/
public static <T> T randomElementFromList( List<T> list )
{
Random rand = new Random();
return list.get( rand.nextInt( list.size() ) );
}

/**
* Generates random boolean value
*
* @return a boolean
*/
public static boolean randomBoolean()
{
return Faker.instance().bool().bool();
}

/**
* Generates a random date in the future. The date is maximum 5 years from now
*
* @return a Date
*/
public static Date randomFutureDate()
{
return Faker.instance().date().future( 1825, TimeUnit.DAYS );
}

/**
* Generates a random date in the future. The date is maximum 5 years from now
*
* @param formatter a {@see DateTimeFormatter} for formatting the date
* @return a Date formatted according to the specified {@see DateTimeFormatter}
*/
public static String randomFutureDate( DateTimeFormatter formatter )
{
return formatDate( toLocalDate( randomFutureDate() ), formatter );
}

/**
* Generates a random date in the past. The date is maximum 5 years in the past
* from now
*
* @return a Date
*/
public static Date randomPastDate()
{
return Faker.instance().date().past( 1825, TimeUnit.DAYS );
}

/**
* Generates a random date in the past. The date is maximum 5 years in the past
* from now
*
* @param formatter a {@see DateTimeFormatter} for formatting the date
* @return a Date formatted according to the specified {@see DateTimeFormatter}
*/
public static String randomPastDate( DateTimeFormatter formatter )
{
return formatDate( toLocalDate( randomPastDate() ), formatter );
}

/**
* Generates a random date The date is maximum 5 years in the past from now or 5
* years in the future from now
*
* @return a Date
*/
public static Date getDate()
{
List<Date> dates = new ArrayList<>();
dates.add( randomPastDate() );
dates.add( randomFutureDate() );
return randomElementFromList( dates );
}

/**
* Generates a random date The date is maximum 5 years in the past from now or 5
* years in the future from now
*
* @param formatter a DateTimeFormatter
* @return a Date formatted according to the specified DateTimeFormatter
*/
public static String randomDate( DateTimeFormatter formatter )
{
return formatDate( toLocalDate( getDate() ), formatter );
}

private static String formatDate( LocalDate localDate, DateTimeFormatter format )
{
return localDate.format( format );
}

private static LocalDate toLocalDate( Date dateToConvert )
{
return dateToConvert.toInstant().atZone( ZoneId.systemDefault() ).toLocalDate();
}
}

0 comments on commit 582976f

Please sign in to comment.