Enhanced test to test all rules such as just one

This commit is contained in:
Luca Frosini 2019-11-15 14:40:11 +01:00
parent 264d22b3a0
commit 2cf3b76a68
4 changed files with 69 additions and 57 deletions

View File

@ -1,55 +0,0 @@
package org.gcube.accounting.datamodel.validations.validators;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
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 TestRegexReplaceRules {
private static final Logger logger = LoggerFactory.getLogger(TestRegexReplaceRules.class);
@Test
public void testAuthorizationServiceGenerate() throws Exception {
String[] aggregationTests = new String[]{
"AuthorizationService-generate", "AuthorizationService-retrieve", "AuthorizationService-scopes",
"TagMe-tag", "GeoNetwork-login", "CkanConnector-connect", "CkanConnector-disconnect",
"FullTextIndexNode-insert", "FullTextIndexNode-delete"
};
for(String aggregationTest : aggregationTests) {
ObjectMapper mapper = DSMapper.getObjectMapper();
InputStream regexInputStream = TestRegexReplaceRules.class.getClassLoader().getResourceAsStream(aggregationTest + "-rule.json");
MatcherReplace matcherReplace = mapper.readValue(regexInputStream, MatcherReplace.class);
Replacer replacer = matcherReplace.getReplacer();
URL url = TestRegexReplaceRules.class.getClassLoader().getResource(aggregationTest + "-values.csv");
File elaborationFile = new File(url.toURI());
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(line + " does not match "+ matcherReplace.getMultiMatcher().toString() + ". This MUST not occur.");
}
}
} catch(Exception e) {
throw e;
}
}
}
}

View File

@ -0,0 +1,66 @@
package org.gcube.accounting.datamodel.validations.validators;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
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 {
String[] aggregationTests = new String[]{
"AuthorizationService-generate", "AuthorizationService-retrieve", "AuthorizationService-scopes",
"TagMe-tag", "GeoNetwork-login", "CkanConnector-connect", "CkanConnector-disconnect",
"FullTextIndexNode-insert", "FullTextIndexNode-delete"
};
for(String aggregationTest : aggregationTests) {
testRule(aggregationTest);
}
}
@Test
public void testRule(String rulePrefix) throws Exception {
ObjectMapper mapper = DSMapper.getObjectMapper();
InputStream regexInputStream = TestRules.class.getClassLoader().getResourceAsStream(rulePrefix + "-rule.json");
MatcherReplace matcherReplace = mapper.readValue(regexInputStream, MatcherReplace.class);
Replacer replacer = matcherReplace.getReplacer();
URL url = TestRules.class.getClassLoader().getResource(rulePrefix + "-values.csv");
File elaborationFile = new File(url.toURI());
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(line + " does not match "+ matcherReplace.getMultiMatcher().toString() + ". This MUST not occur.");
}
}
} catch(Exception e) {
throw e;
}
}
@Test
public void testSingleRule() throws Exception {
testRule("TagMe-tag");
}
}

View File

@ -1,6 +1,6 @@
{
"match": {
"serviceClassRegex": "Application",
"serviceClassRegex": "Application|DataAnalysis",
"serviceNameRegex": "TagMe",
"calledMethodRegex": "^((e\/)|\/){0,1}tag"
},

View File

@ -1,3 +1,4 @@
Application,TagMe,tag
Application,TagMe,e/tag
Application,TagMe,/tag
Application,TagMe,/tag
DataAnalysis,TagMe,tag
1 Application TagMe tag
2 Application TagMe e/tag
3 Application TagMe /tag
4 DataAnalysis TagMe tag