qa.justtestlah.testdata.TestDataMap Maven / Gradle / Ivy
Show all versions of justtestlah-core Show documentation
package qa.justtestlah.testdata;
import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfo;
import io.github.classgraph.ScanResult;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;
/**
* Container to hold test data.
*
* Test data is loaded from YAML files on the classpath which match the specified ant pattern.
*/
@Component
public class TestDataMap {
private static final Logger LOG = LoggerFactory.getLogger(TestDataMap.class);
private static final String TEST_DATA_YAML_PATTERN = "**/__filter__/**/*.y*ml";
@Value("${testdata.filter:testdata}")
private String filter;
@Value("${model.package}")
private String modelPackage;
@Value("${testdata.enabled:false}")
private boolean testDataEnabled;
@Autowired private TestDataParser parser;
@Autowired private TestDataObjectRegistry registry;
/**
* the key is the type (class), the value is another map which holds the corresponding test data
* objects for each entity
*/
private Map, Map> testData = new HashMap<>();
/**
* Initialize the map.
*
* @throws IOException {@link IOException} if a test data resource cannot be processed
*/
@PostConstruct
public void initializeTestDataMap() throws IOException {
if (testDataEnabled) {
LOG.info("Initialising test data map");
initializeTestDataObjectRegistry();
String pattern;
if (filter != null && !filter.isEmpty() && !filter.startsWith("$")) {
pattern = TEST_DATA_YAML_PATTERN.replace("__filter__", filter);
} else {
return;
}
LOG.info("Scanning for test data files using the pattern {}", pattern);
for (Resource resource : new PathMatchingResourcePatternResolver().getResources(pattern)) {
if (resource.getFile().getPath().contains("testdata")) {
Pair