package org.gcube.accounting.datamodel.validations.validators; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FilenameFilter; 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 { File rulesDirectory = getRulesDirectory(); FilenameFilter filenameFilter = new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".json"); } }; File[] rulesFiles = rulesDirectory.listFiles(filenameFilter); for(File rulesFile : rulesFiles) { testRule(rulesFile); } } 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"); } public void testRule(File rulesFile) throws Exception { logger.info("-----------------------------------------------------------------------------------------------------"); logger.info("Analisyng rule from file {}\n", rulesFile.getAbsolutePath()); File rulesDirectory = rulesFile.getParentFile(); ObjectMapper mapper = DSMapper.getObjectMapper(); MatcherReplace matcherReplace = mapper.readValue(rulesFile, MatcherReplace.class); Replacer replacer = matcherReplace.getReplacer(); final String requiredMatchesFileName = rulesFile.getName().replaceAll(".json", ".csv"); File elaborationFile = new File(rulesDirectory,requiredMatchesFileName); 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) { logger.info("{} --> {},{},{}", line, replacer.getServiceClass(), replacer.getServiceName(), replacer.getCalledMethod()); } else { logger.error("{} does not match {}. This MUST not occur.", line, matcherReplace.getMultiMatcher().toString()); throw new Exception(); } } } catch(Exception e) { throw e; } 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"); } @Test public void testSingleRule() throws Exception { File rulesDirectory = getRulesDirectory(); File rulesFile = new File(rulesDirectory, "SmartExecutor-UNKNOWN.json"); testRule(rulesFile); } }