2019-11-15 14:40:11 +01:00
|
|
|
package org.gcube.accounting.datamodel.validations.validators;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileReader;
|
2019-11-25 17:33:44 +01:00
|
|
|
import java.io.FilenameFilter;
|
2019-11-15 14:40:11 +01:00
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
import org.gcube.documentstore.records.DSMapper;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
|
|
public class TestRules {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(TestRules.class);
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testAllRules() throws Exception {
|
2019-11-25 17:33:44 +01:00
|
|
|
|
|
|
|
File rulesDirectory = getRulesDirectory();
|
|
|
|
FilenameFilter filenameFilter = new FilenameFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(File dir, String name) {
|
|
|
|
return name.endsWith(".json");
|
|
|
|
}
|
2019-11-15 14:40:11 +01:00
|
|
|
};
|
2019-11-25 17:33:44 +01:00
|
|
|
File[] rulesFiles = rulesDirectory.listFiles(filenameFilter);
|
2019-11-15 14:40:11 +01:00
|
|
|
|
2019-11-25 17:33:44 +01:00
|
|
|
for(File rulesFile : rulesFiles) {
|
|
|
|
testRule(rulesFile);
|
2019-11-15 14:40:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 17:00:14 +01:00
|
|
|
public File getRulesDirectory() throws Exception {
|
|
|
|
URL logbackFileURL = TestRules.class.getClassLoader().getResource("logback-test.xml");
|
|
|
|
File logbackFile = new File(logbackFileURL.toURI());
|
|
|
|
File resourcesDirectory = logbackFile.getParentFile();
|
|
|
|
return new File(resourcesDirectory, "rules");
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:33:44 +01:00
|
|
|
public void testRule(File rulesFile) throws Exception {
|
|
|
|
logger.info("-----------------------------------------------------------------------------------------------------");
|
|
|
|
logger.info("Analisyng rule from file {}\n", rulesFile.getAbsolutePath());
|
|
|
|
File rulesDirectory = rulesFile.getParentFile();
|
2019-11-15 17:00:14 +01:00
|
|
|
|
2019-11-15 14:40:11 +01:00
|
|
|
ObjectMapper mapper = DSMapper.getObjectMapper();
|
2019-11-15 17:00:14 +01:00
|
|
|
MatcherReplace matcherReplace = mapper.readValue(rulesFile, MatcherReplace.class);
|
2019-11-15 14:40:11 +01:00
|
|
|
Replacer replacer = matcherReplace.getReplacer();
|
|
|
|
|
2019-11-25 17:33:44 +01:00
|
|
|
final String requiredMatchesFileName = rulesFile.getName().replaceAll(".json", ".csv");
|
|
|
|
File elaborationFile = new File(rulesDirectory,requiredMatchesFileName);
|
2019-11-15 14:40:11 +01:00
|
|
|
try(BufferedReader br = new BufferedReader(new FileReader(elaborationFile))) {
|
|
|
|
for(String line; (line = br.readLine()) != null;) {
|
|
|
|
String[] splittedLine = line.split(",");
|
|
|
|
boolean matched = matcherReplace.check(splittedLine[0],splittedLine[1],splittedLine[2]);
|
|
|
|
if(matched) {
|
2019-11-15 16:42:11 +01:00
|
|
|
logger.info("{} --> {},{},{}", line, replacer.getServiceClass(), replacer.getServiceName(), replacer.getCalledMethod());
|
2019-11-15 14:40:11 +01:00
|
|
|
} else {
|
|
|
|
logger.error("{} does not match {}. This MUST not occur.", line, matcherReplace.getMultiMatcher().toString());
|
2019-11-15 16:42:11 +01:00
|
|
|
throw new Exception();
|
2019-11-15 14:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw e;
|
|
|
|
}
|
2019-11-25 17:33:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
FilenameFilter filenameFilter = new FilenameFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(File dir, String name) {
|
|
|
|
boolean accept = name.endsWith(".csv");
|
|
|
|
return name.compareTo(requiredMatchesFileName)!=0 && accept;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
File[] elaborationFilesNoMatch = rulesDirectory.listFiles(filenameFilter);
|
|
|
|
|
|
|
|
for(File elaborationFileNoMatch : elaborationFilesNoMatch) {
|
|
|
|
logger.trace("\n\nComparing examples which must not match from file {}", elaborationFileNoMatch.getAbsolutePath());
|
|
|
|
try(BufferedReader br = new BufferedReader(new FileReader(elaborationFileNoMatch))) {
|
|
|
|
for(String line; (line = br.readLine()) != null;) {
|
|
|
|
String[] splittedLine = line.split(",");
|
|
|
|
boolean matched = matcherReplace.check(splittedLine[0],splittedLine[1],splittedLine[2]);
|
|
|
|
if(matched) {
|
|
|
|
logger.error("{} match {} but it should NOT. This MUST not occur.", line, matcherReplace.getMultiMatcher().toString());
|
|
|
|
throw new Exception();
|
|
|
|
} else {
|
|
|
|
logger.info("{} does NOT match as requested", line, replacer.getServiceClass(), replacer.getServiceName(), replacer.getCalledMethod());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.info("-----------------------------------------------------------------------------------------------------\n\n\n");
|
2019-11-15 14:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testSingleRule() throws Exception {
|
2019-11-25 17:33:44 +01:00
|
|
|
File rulesDirectory = getRulesDirectory();
|
|
|
|
File rulesFile = new File(rulesDirectory, "SmartExecutor-UNKNOWN.json");
|
|
|
|
testRule(rulesFile);
|
2019-11-15 14:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|