Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Replace jPopulator with Easy Random #50

Merged
merged 1 commit into from
May 6, 2019
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
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