hi,
here
import static com.google.common.collect.MapDifference.ValueDifference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import com.google.common.collect.MapDifference
import com.google.common.collect.Maps
import com.google.common.collect.MapDifference.*;
import com.kms.katalon.core.util.KeywordUtil
KeywordUtil logger = new KeywordUtil()
String pdfFilePath = System.getProperty("user.dir")+"\\pdfFiles\\pdfcontent.pdf";
String text = ""
PDDocument document = PDDocument.load(new File(pdfFilePath));
if (!document.isEncrypted()) {
PDFTextStripper stripper = new PDFTextStripper();
text = stripper.getText(document);
}
document.close();
def lines = text.split("(\r\n|\r|\n)", -1);
//regex pattern to find out Rule and Outcome
String pattern = '([CR\\d]+) * Description ([\\S\\s]) Person ([\\S\\s])([\\S\\s]+)';
String rule = ""
String outcome = ""
Map<String, String> rulesOutcomes = new HashMap<>();
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
for(String line:lines){
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(1) ); //rule
rule = m.group(1).replaceAll("\\s","")
System.out.println("Found value: " + m1.group(4) ); //outcome
outcome = m.group(4).replaceAll("\\s","")
rulesOutcomes.put(rule, outcome)
}else {
System.out.println("NO MATCH");
}
}
//define map where are expected key and value pair
Map<String, String> expectedValues = new HashMap<>();
expectedValues.put("CR001","GREEN")
expectedValues.put("CR002","GREEN")
//compare maps
assertFalse("Maps should be unequal", MapDiffUtil.validateEqual(
rulesOutcomes, expectedValues, "map1", "map2"));
/**
* Map comparison with detailed log messages
*/
public class MapDiffUtil {
private static KeywordUtil logg = new KeywordUtil()
private static final Logger log =
LoggerFactory.getLogger(MapDiffUtil.class);
public static <K, V> boolean validateEqual(
Map<K, V> map1, Map<K, V> map2,
String map1Name, String map2Name) {
final MapDifference<K, V> diff = Maps.difference(map1, map2);
if (diff.areEqual()) {
def error = "Maps "+map1Name+" and "+map2Name+" contain exactly the same name/value pairs"
logg.markWarning(error)
//log.info("Maps '{}' and '{}' contain exactly the same name/value pairs", map1Name, map2Name);
return true;
} else {
logKeys(diff.entriesOnlyOnLeft(), map1Name, map2Name);
logKeys(diff.entriesOnlyOnRight(), map2Name, map1Name);
logEntries(diff.entriesDiffering(), map1Name, map2Name);
return false;
}
}
private static <K, V> void logKeys(
Map<K, V> mapSubset, String n1, String n2) {
if (not(mapSubset.isEmpty())) {
logg.markWarning("Keys found in "+n1+" but not in "+n2+" : "+mapSubset.keySet());
//log.error("Keys found in {} but not in {}: {}",n1, n2, mapSubset.keySet());
}
}
private static <K, V> void logEntries(
Map<K, ValueDifference<V>> differing,
String n1, String n2) {
if (not(differing.isEmpty())) {
logg.markWarning("Differing values found {key="+n1+"-value,"+n2+"-value}: "+differing);
//log.error("Differing values found {key={}-value,{}-value}: {}", n1, n2, differing);
}
}
private static boolean not(boolean b) {
return !b;
}
}
2020-03-08 11:57:42.914 WARN com.kms.katalon.core.util.KeywordUtil - Differing values found {key=map1-value,map2-value}: [CR002:(YELLOW, GREEN)]