com.github.invictum.data.injector.DataInjector Maven / Gradle / Ivy
package com.github.invictum.data.injector;
import com.github.invictum.velocity.VelocityProcessor;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class DataInjector {
private static final Logger LOG = LoggerFactory.getLogger(DataInjector.class);
public static void injectInto(Object objectToInject) {
for (Field field : objectToInject.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(TestData.class)) {
injectIntoField(field, objectToInject);
}
}
}
private static void injectIntoField(Field field, Object target) {
LOG.debug("Trying to inject data into {} property of {} class", field.getName(),
target.getClass().getSimpleName());
if (List.class.isAssignableFrom(field.getType())) {
/** Load and inject list of dto (pojo) */
Class> genericType = (Class>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
LOG.debug("List with {} generic type detected", genericType.getSimpleName());
/** Wrap yaml documents into list og generic types */
Yaml yaml = new Yaml(new Constructor(genericType));
List