Skip to content

Commit

Permalink
Replace jPopulator with Easy Random (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmbenhassine authored and brharrington committed May 6, 2019
1 parent d4e145f commit 31a8163
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
import com.google.common.reflect.ClassPath;
import com.google.common.io.Resources;

import io.github.benas.jpopulator.api.Populator;
import io.github.benas.jpopulator.impl.PopulatorBuilder;
import org.jeasy.random.EasyRandom;
import org.jeasy.random.EasyRandomParameters;

import com.amazonaws.services.route53.model.ResourceRecordSet;

import java.lang.reflect.Field;
import java.util.Set;
import java.util.function.Predicate;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -66,7 +69,12 @@ private boolean isModelClass(Class<?> c) {
public void mapRandomAwsObjects() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
AmazonObjectMapperConfigurer.configure(mapper);
final Populator p = (new PopulatorBuilder()).build();
final EasyRandomParameters parameters = new EasyRandomParameters()
.ignoreRandomizationErrors(true)
.excludeField(excludedFields())
.excludeType(excludedTypes())
.collectionSizeRange(1, 3);
final EasyRandom easyRandom = new EasyRandom(parameters);
final Set<ClassPath.ClassInfo> classes = ClassPath
.from(getClass().getClassLoader())
.getTopLevelClassesRecursive("com.amazonaws");
Expand All @@ -76,7 +84,7 @@ public void mapRandomAwsObjects() throws Exception {
&& !cinfo.getName().contains(".s3.model.")) { // TODO: problem with CORSRule
final Class<?> c = cinfo.load();
if (isModelClass(c)) {
Object obj = p.populateBean(c);
Object obj = easyRandom.nextObject(c);
String j1 = mapper.writeValueAsString(obj);
Object d1 = mapper.readValue(j1, c);
String j2 = mapper.writeValueAsString(d1);
Expand All @@ -86,12 +94,25 @@ public void mapRandomAwsObjects() throws Exception {
}
}

private Predicate<Field> excludedFields() {
return field -> field.getType().equals(com.amazonaws.ResponseMetadata.class) ||
field.getType().equals(com.amazonaws.http.SdkHttpMetadata.class);
}

private Predicate<Class<?>> excludedTypes() {
return type -> type.getSuperclass().equals(com.amazonaws.AmazonWebServiceRequest.class) ||
type.equals(com.amazonaws.services.elasticmapreduce.model.Cluster.class) ||
type.equals(com.amazonaws.services.elasticmapreduce.model.Configuration.class) ||
type.equals(com.amazonaws.services.kinesisvideo.model.AckEvent.class) ||
type.equals(com.amazonaws.services.simplesystemsmanagement.model.InventoryAggregator.class);
}

@Test
@SuppressWarnings("deprecation")
public void testDeprecatedMapper() throws Exception {
final AmazonObjectMapper mapper = new AmazonObjectMapper();
final Populator p = new PopulatorBuilder().build();
Object obj = p.populateBean(VersionInfo.class);
final EasyRandom easyRandom = new EasyRandom();
Object obj = easyRandom.nextObject(VersionInfo.class);
String j1 = mapper.writeValueAsString(obj);
Object d1 = mapper.readValue(j1, VersionInfo.class);
String j2 = mapper.writeValueAsString(d1);
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ subprojects {
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.8'
testCompile 'junit:junit:4.10'
testCompile 'com.google.guava:guava:18.0'
testCompile 'io.github.benas:jpopulator:1.0.1'
testCompile 'org.jeasy:easy-random-core:4.0.0'
}
}

Expand Down

0 comments on commit 31a8163

Please sign in to comment.