com.github.dreamhead.bot.ObjectBot Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bot-core Show documentation
Show all versions of bot-core Show documentation
Object bot is a library for setting up Java objects as test data.
The newest version!
package com.github.dreamhead.bot;
import com.github.dreamhead.bot.util.FieldEntry;
import com.rits.cloning.Cloner;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import static com.github.dreamhead.bot.reflection.ReflectionSupport.getDeclaredField;
import static com.github.dreamhead.bot.reflection.ReflectionSupport.setFieldValue;
public class ObjectBot {
private Map container = new HashMap<>();
public final ObjectBot define(final String name,
final Object object) {
container.put(name, object);
return this;
}
@SuppressWarnings("unchecked")
public final T of(final String name, final Class clazz, final FieldEntry>... entries) {
Object object = container.get(name);
if (Objects.isNull(object)) {
throw new IllegalArgumentException("No Bot [" + name + "] found");
}
if (!clazz.isAssignableFrom(object.getClass())) {
throw new IllegalArgumentException("Mismatch class [" + clazz.getName() + "] found for [" + name + "]");
}
return override((T) object, entries);
}
private static final Cloner CLONER = new Cloner();
public static FieldEntry.FieldEntryBuilder field(final String name) {
return FieldEntry.name(name);
}
public static T override(final T object, final FieldEntry>... entries) {
validateEntries(entries);
T newObj = CLONER.deepClone(object);
Class> clazz = object.getClass();
for (FieldEntry> entry : entries) {
String fieldName = entry.name();
Optional field = getDeclaredField(clazz, fieldName);
if (field.isPresent()) {
setFieldValue(newObj, field.get(), entry.value());
} else {
throw new IllegalArgumentException("No field [" + fieldName + "] found");
}
}
return newObj;
}
private static void validateEntries(final FieldEntry>[] entries) {
long size = Arrays.stream(entries)
.map(FieldEntry::name)
.distinct()
.count();
if (size < entries.length) {
throw new IllegalArgumentException("Duplicated name for entries is not allowed");
}
}
}