BAEL-2983 java objects generation with EasyRandom (#7209)

This commit is contained in:
Denis 2019-06-30 06:51:05 +02:00 committed by Grzegorz Piwowarek
parent 883f0a525b
commit 447c8c3599
9 changed files with 294 additions and 0 deletions

23
easy-random/pom.xml Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>easy-random</artifactId>
<name>easy-random</name>
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-random-core</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,17 @@
package org.baeldung.easy.random.model;
import java.util.StringJoiner;
public class Department {
private String depName;
public Department(String depName) {
this.depName = depName;
}
@Override
public String toString() {
return new StringJoiner(", ", Department.class.getSimpleName() + "[", "]").add("depName='" + depName + "'")
.toString();
}
}

View File

@ -0,0 +1,70 @@
package org.baeldung.easy.random.model;
import java.util.*;
public class Employee {
private long id;
private String firstName;
private String lastName;
private Department department;
private Collection<Employee> coworkers;
private Map<YearQuarter, Grade> quarterGrades;
public Employee(long id, String firstName, String lastName, Department department) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.department = department;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Employee employee = (Employee) o;
return id == employee.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public long getId() {
return id;
}
public Department getDepartment() {
return department;
}
public Collection<Employee> getCoworkers() {
return Collections.unmodifiableCollection(coworkers);
}
public Map<YearQuarter, Grade> getQuarterGrades() {
return Collections.unmodifiableMap(quarterGrades);
}
@Override
public String toString() {
return new StringJoiner(", ", Employee.class.getSimpleName() + "[", "]").add("id=" + id)
.add("firstName='" + firstName + "'")
.add("lastName='" + lastName + "'")
.add("department=" + department)
.add("coworkers size=" + ((coworkers == null) ? 0 : coworkers.size()))
.add("quarterGrades=" + quarterGrades)
.toString();
}
}

View File

@ -0,0 +1,22 @@
package org.baeldung.easy.random.model;
import java.util.StringJoiner;
public class Grade {
private int grade;
public Grade(int grade) {
this.grade = grade;
}
public int getGrade() {
return grade;
}
@Override
public String toString() {
return new StringJoiner(", ", Grade.class.getSimpleName() + "[", "]").add("grade=" + grade)
.toString();
}
}

View File

@ -0,0 +1,30 @@
package org.baeldung.easy.random.model;
import java.util.StringJoiner;
public class Person {
private String firstName;
private String lastName;
private Integer age;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Integer getAge() {
return age;
}
@Override
public String toString() {
return new StringJoiner(", ", Person.class.getSimpleName() + "[", "]").add("firstName='" + firstName + "'")
.add("lastName='" + lastName + "'")
.add("age=" + age)
.toString();
}
}

View File

@ -0,0 +1,50 @@
package org.baeldung.easy.random.model;
import java.time.LocalDate;
import java.util.Objects;
import java.util.StringJoiner;
public class YearQuarter {
private LocalDate startDate;
private LocalDate endDate;
public YearQuarter(LocalDate startDate) {
this.startDate = startDate;
autoAdjustEndDate();
}
private void autoAdjustEndDate() {
endDate = startDate.plusMonths(3L);
}
public LocalDate getStartDate() {
return startDate;
}
public LocalDate getEndDate() {
return endDate;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
YearQuarter quarter = (YearQuarter) o;
return Objects.equals(startDate, quarter.startDate) && Objects.equals(endDate, quarter.endDate);
}
@Override
public int hashCode() {
return Objects.hash(startDate, endDate);
}
@Override
public String toString() {
return new StringJoiner(", ", YearQuarter.class.getSimpleName() + "[", "]").add("startDate=" + startDate)
.add("endDate=" + endDate)
.toString();
}
}

View File

@ -0,0 +1,18 @@
package org.baeldung.easy.random.randomizer;
import org.baeldung.easy.random.model.YearQuarter;
import org.jeasy.random.api.Randomizer;
import java.time.LocalDate;
import java.time.Month;
public class YearQuarterRandomizer implements Randomizer<YearQuarter> {
private LocalDate date = LocalDate.of(1990, Month.SEPTEMBER, 25);
@Override
public YearQuarter getRandomValue() {
date = date.plusMonths(3);
return new YearQuarter(date);
}
}

View File

@ -0,0 +1,63 @@
package org.baeldung.easy.random;
import org.baeldung.easy.random.model.Employee;
import org.baeldung.easy.random.model.Person;
import org.baeldung.easy.random.model.YearQuarter;
import org.baeldung.easy.random.randomizer.YearQuarterRandomizer;
import org.jeasy.random.EasyRandom;
import org.jeasy.random.EasyRandomParameters;
import org.jeasy.random.FieldPredicates;
import org.jeasy.random.TypePredicates;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
class EasyRandomUnitTest {
@Test
void givenDefaultConfiguration_thenGenerateSingleObject() {
EasyRandom generator = new EasyRandom();
Person person = generator.nextObject(Person.class);
assertNotNull(person.getAge());
assertNotNull(person.getFirstName());
assertNotNull(person.getLastName());
}
@Test
void givenDefaultConfiguration_thenGenerateObjectsList() {
EasyRandom generator = new EasyRandom();
List<Person> persons = generator.objects(Person.class, 5)
.collect(Collectors.toList());
assertEquals(5, persons.size());
}
@Test
void givenCustomConfiguration_thenGenerateSingleEmployee() {
EasyRandomParameters parameters = new EasyRandomParameters();
parameters.stringLengthRange(3, 3);
parameters.collectionSizeRange(5, 5);
parameters.excludeField(FieldPredicates.named("lastName").and(FieldPredicates.inClass(Employee.class)));
parameters.excludeType(TypePredicates.inPackage("not.existing.pkg"));
parameters.randomize(YearQuarter.class, new YearQuarterRandomizer());
EasyRandom generator = new EasyRandom(parameters);
Employee employee = generator.nextObject(Employee.class);
assertEquals(3, employee.getFirstName().length());
assertEquals(5, employee.getCoworkers().size());
assertEquals(5, employee.getQuarterGrades().size());
assertNotNull(employee.getDepartment());
assertNull(employee.getLastName());
for (YearQuarter key : employee.getQuarterGrades().keySet()) {
assertEquals(key.getStartDate(), key.getEndDate().minusMonths(3L));
}
}
}

View File

@ -422,6 +422,7 @@
<module>drools</module> <module>drools</module>
<module>dubbo</module> <module>dubbo</module>
<module>easy-random</module>
<module>ethereum</module> <module>ethereum</module>
<module>feign</module> <module>feign</module>