Added code to test usage record aggregation with new MatcherReplacer
refs #18087
This commit is contained in:
parent
d72858196c
commit
eeb8f9b8d7
|
@ -6,7 +6,14 @@ import java.io.FileReader;
|
||||||
import java.io.FilenameFilter;
|
import java.io.FilenameFilter;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
|
import org.gcube.accounting.aggregator.RegexRulesAggregator;
|
||||||
|
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
|
||||||
|
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
|
||||||
|
import org.gcube.documentstore.exception.NotAggregatableRecordsExceptions;
|
||||||
import org.gcube.documentstore.records.DSMapper;
|
import org.gcube.documentstore.records.DSMapper;
|
||||||
|
import org.gcube.testutility.ContextTest;
|
||||||
|
import org.gcube.testutility.TestUsageRecord;
|
||||||
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -28,6 +35,13 @@ public class TestRules {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
File[] rulesFiles = rulesDirectory.listFiles(filenameFilter);
|
File[] rulesFiles = rulesDirectory.listFiles(filenameFilter);
|
||||||
|
RegexRulesAggregator regexRulesAggregator = RegexRulesAggregator.getInstance();
|
||||||
|
|
||||||
|
for(File rulesFile : rulesFiles) {
|
||||||
|
ObjectMapper mapper = DSMapper.getObjectMapper();
|
||||||
|
MatcherReplace matcherReplace = mapper.readValue(rulesFile, MatcherReplace.class);
|
||||||
|
regexRulesAggregator.addMatcherReplace(matcherReplace);
|
||||||
|
}
|
||||||
|
|
||||||
for(File rulesFile : rulesFiles) {
|
for(File rulesFile : rulesFiles) {
|
||||||
testRule(rulesFile);
|
testRule(rulesFile);
|
||||||
|
@ -41,6 +55,30 @@ public class TestRules {
|
||||||
return new File(resourcesDirectory, "rules");
|
return new File(resourcesDirectory, "rules");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected long durationWeightedAverage(int numberA, long durationA, int numberB, long durationB){
|
||||||
|
return ((numberA * durationA) + (numberB * durationB)) / (numberA + numberB);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void initAllRules() throws Exception {
|
||||||
|
RegexRulesAggregator regexRulesAggregator = RegexRulesAggregator.getInstance();
|
||||||
|
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) {
|
||||||
|
ObjectMapper mapper = DSMapper.getObjectMapper();
|
||||||
|
MatcherReplace matcherReplace = mapper.readValue(rulesFile, MatcherReplace.class);
|
||||||
|
regexRulesAggregator.addMatcherReplace(matcherReplace);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testRule(File rulesFile) throws Exception {
|
public void testRule(File rulesFile) throws Exception {
|
||||||
logger.info("-----------------------------------------------------------------------------------------------------");
|
logger.info("-----------------------------------------------------------------------------------------------------");
|
||||||
logger.info("Analisyng rule from file {}\n", rulesFile.getAbsolutePath());
|
logger.info("Analisyng rule from file {}\n", rulesFile.getAbsolutePath());
|
||||||
|
@ -50,19 +88,65 @@ public class TestRules {
|
||||||
MatcherReplace matcherReplace = mapper.readValue(rulesFile, MatcherReplace.class);
|
MatcherReplace matcherReplace = mapper.readValue(rulesFile, MatcherReplace.class);
|
||||||
Replacer replacer = matcherReplace.getReplacer();
|
Replacer replacer = matcherReplace.getReplacer();
|
||||||
|
|
||||||
|
ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecord();
|
||||||
|
serviceUsageRecord.setServiceClass(replacer.getServiceClass());
|
||||||
|
serviceUsageRecord.setServiceName(replacer.getServiceName());
|
||||||
|
serviceUsageRecord.setCalledMethod(replacer.getCalledMethod());
|
||||||
|
serviceUsageRecord.validate();
|
||||||
|
|
||||||
|
AggregatedServiceUsageRecord aggregated = new AggregatedServiceUsageRecord(serviceUsageRecord);
|
||||||
|
|
||||||
final String requiredMatchesFileName = rulesFile.getName().replaceAll(".json", ".csv");
|
final String requiredMatchesFileName = rulesFile.getName().replaceAll(".json", ".csv");
|
||||||
File elaborationFile = new File(rulesDirectory,requiredMatchesFileName);
|
File elaborationFile = new File(rulesDirectory,requiredMatchesFileName);
|
||||||
|
int count = 1;
|
||||||
try(BufferedReader br = new BufferedReader(new FileReader(elaborationFile))) {
|
try(BufferedReader br = new BufferedReader(new FileReader(elaborationFile))) {
|
||||||
for(String line; (line = br.readLine()) != null;) {
|
for(String line; (line = br.readLine()) != null;) {
|
||||||
String[] splittedLine = line.split(",");
|
String[] splittedLine = line.split(",");
|
||||||
boolean matched = matcherReplace.check(splittedLine[0],splittedLine[1],splittedLine[2]);
|
++count;
|
||||||
|
|
||||||
|
String serviceClass = splittedLine[0];
|
||||||
|
String serviceName = splittedLine[1];
|
||||||
|
String calledMethod = splittedLine[2];
|
||||||
|
|
||||||
|
boolean matched = matcherReplace.check(serviceClass,serviceName,calledMethod);
|
||||||
if(matched) {
|
if(matched) {
|
||||||
logger.info("{} --> {},{},{}", line, replacer.getServiceClass(), replacer.getServiceName(), replacer.getCalledMethod());
|
//logger.info("{} --> {},{},{}", line, replacer.getServiceClass(), replacer.getServiceName(), replacer.getCalledMethod());
|
||||||
} else {
|
} else {
|
||||||
logger.error("{} does not match {}. This MUST not occur.", line, matcherReplace.getMultiMatcher().toString());
|
logger.error("{} does not match {}. This MUST not occur.", line, matcherReplace.getMultiMatcher().toString());
|
||||||
throw new Exception();
|
throw new Exception();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ServiceUsageRecord sur = TestUsageRecord.createTestServiceUsageRecord();
|
||||||
|
sur.setServiceClass(serviceClass);
|
||||||
|
sur.setServiceName(serviceName);
|
||||||
|
sur.setCalledMethod(calledMethod);
|
||||||
|
sur.validate();
|
||||||
|
// logger.trace("To Be aggregated ServiceUsageRecord {}", sur);
|
||||||
|
|
||||||
|
long minInvocationTime = aggregated.getMinInvocationTime();
|
||||||
|
long maxInvocationTime = aggregated.getMaxInvocationTime();
|
||||||
|
long oldDuration = aggregated.getDuration();
|
||||||
|
long surDuration = sur.getDuration();
|
||||||
|
|
||||||
|
aggregated.aggregate(sur);
|
||||||
|
//logger.debug("Resulting Aggregated ServiceUsageRecord: {}", aggregated);
|
||||||
|
|
||||||
|
long avgDuration = durationWeightedAverage(count-1, oldDuration, 1, surDuration);
|
||||||
|
Assert.assertTrue(aggregated.getDuration() == (avgDuration));
|
||||||
|
Assert.assertTrue(aggregated.getOperationCount() == count);
|
||||||
|
|
||||||
|
if(minInvocationTime >= surDuration){
|
||||||
|
Assert.assertTrue(aggregated.getMinInvocationTime() == surDuration);
|
||||||
|
}else{
|
||||||
|
Assert.assertTrue(aggregated.getMinInvocationTime() == minInvocationTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(maxInvocationTime >= surDuration){
|
||||||
|
Assert.assertTrue(aggregated.getMaxInvocationTime() == maxInvocationTime);
|
||||||
|
}else{
|
||||||
|
Assert.assertTrue(aggregated.getMaxInvocationTime() == surDuration);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
throw e;
|
throw e;
|
||||||
|
@ -83,14 +167,31 @@ public class TestRules {
|
||||||
try(BufferedReader br = new BufferedReader(new FileReader(elaborationFileNoMatch))) {
|
try(BufferedReader br = new BufferedReader(new FileReader(elaborationFileNoMatch))) {
|
||||||
for(String line; (line = br.readLine()) != null;) {
|
for(String line; (line = br.readLine()) != null;) {
|
||||||
String[] splittedLine = line.split(",");
|
String[] splittedLine = line.split(",");
|
||||||
boolean matched = matcherReplace.check(splittedLine[0],splittedLine[1],splittedLine[2]);
|
String serviceClass = splittedLine[0];
|
||||||
|
String serviceName = splittedLine[1];
|
||||||
|
String calledMethod = splittedLine[2];
|
||||||
|
boolean matched = matcherReplace.check(serviceClass,serviceName,calledMethod);
|
||||||
if(matched) {
|
if(matched) {
|
||||||
logger.error("{} match {} but it should NOT. This MUST not occur.", line, matcherReplace.getMultiMatcher().toString());
|
logger.error("{} match {} but it should NOT. This MUST not occur.", line, matcherReplace.getMultiMatcher().toString());
|
||||||
throw new Exception();
|
throw new Exception();
|
||||||
} else {
|
} else {
|
||||||
logger.info("{} does NOT match as requested", line, replacer.getServiceClass(), replacer.getServiceName(), replacer.getCalledMethod());
|
//logger.info("{} does NOT match as requested", line, replacer.getServiceClass(), replacer.getServiceName(), replacer.getCalledMethod());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ServiceUsageRecord sur = TestUsageRecord.createTestServiceUsageRecord();
|
||||||
|
sur.setServiceClass(serviceClass);
|
||||||
|
sur.setServiceName(serviceName);
|
||||||
|
sur.setCalledMethod(calledMethod);
|
||||||
|
sur.validate();
|
||||||
|
//logger.trace("Should not be aggregated ServiceUsageRecord {}", sur);
|
||||||
|
|
||||||
|
try {
|
||||||
|
aggregated.aggregate(sur);
|
||||||
|
throw new Exception("The record has been aggregated and it should NOT");
|
||||||
|
}catch (NotAggregatableRecordsExceptions e) {
|
||||||
|
//logger.trace("{} is not aggragable as expected", sur);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
throw e;
|
throw e;
|
||||||
|
@ -101,6 +202,8 @@ public class TestRules {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSingleRule() throws Exception {
|
public void testSingleRule() throws Exception {
|
||||||
|
ContextTest.setContextByName(ContextTest.DEFAULT_TEST_SCOPE);
|
||||||
|
initAllRules();
|
||||||
File rulesDirectory = getRulesDirectory();
|
File rulesDirectory = getRulesDirectory();
|
||||||
File rulesFile = new File(rulesDirectory, "SmartExecutor-UNKNOWN.json");
|
File rulesFile = new File(rulesDirectory, "SmartExecutor-UNKNOWN.json");
|
||||||
testRule(rulesFile);
|
testRule(rulesFile);
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
/token.properties
|
/token.properties
|
||||||
|
/*.gcubekey
|
Loading…
Reference in New Issue