Skip to content

Commit

Permalink
Save #575 and #577
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhaskarla authored and Bhaskarla committed Jan 3, 2023
1 parent 598ee88 commit d990f23
Show file tree
Hide file tree
Showing 10 changed files with 499 additions and 204 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,55 @@
package gov.nist.toolkit.testengine.engine;

import gov.nist.toolkit.testengine.transactions.BasicTransaction;
import gov.nist.toolkit.commondatatypes.client.MetadataTypes;
import gov.nist.toolkit.testkitutilities.TestkitWalker;
import gov.nist.toolkit.utilities.xml.Util;
import gov.nist.toolkit.utilities.xml.XmlUtil;
import gov.nist.toolkit.valregmsg.message.SchemaValidation;
import gov.nist.toolkit.xdsexception.client.XdsInternalException;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.xpath.AXIOMXPath;

import java.util.List;
import java.util.logging.Logger;
import org.jaxen.JaxenException;

import javax.xml.parsers.FactoryConfigurationError;
import java.io.File;
import java.util.regex.Matcher;

/**
* Scan testkit for problems.
* @author bill
*
*/
public class ErrorScan extends TestkitWalker {
static int pnrSchemaPassed;
static int pnrSchemaFailed;
static int missingMetadataFileErrors;
static int metadataParserExceptions;
static boolean debug = false;
static boolean doEvalTestPlanEnabled;
static int skipTestPlanCount;
static String schemaLocation ="C:\\Users\\skb1\\myprojects\\iheos-toolkit2\\xdstools2\\src\\test\\resources\\war\\toolkitx\\schema";
private final static Logger logger = Logger.getLogger(ErrorScan.class.getName());


void evalTestPlan(File testplan, String expectedTestLabel) throws XdsInternalException, FactoryConfigurationError, JaxenException {
testPlanCount++;
if (! doEvalTestPlanEnabled) {
skipTestPlanCount++;
return;
}
OMElement tplan = Util.parse_xml(testplan);
String reportTestPlan = testplan.toString().replace(testkitPathName, "");
AXIOMXPath xpathExpression = new AXIOMXPath ("//TestPlan/Test");
String test = xpathExpression.stringValueOf(tplan);
if (test == null || test.equals("")) {
logger.info(testplan.toString() + ": No Test element");
logger.info(reportTestPlan + ": No Test element");
errors++;
}
else if (!test.equals(expectedTestLabel)) {
logger.info(testplan.toString() + ": expected " + expectedTestLabel + " found " + test);
logger.info(reportTestPlan + ": expected " + expectedTestLabel + " found " + test);
errors++;
}
}
Expand All @@ -41,6 +59,53 @@ public void doStep(String step) {

}

@Override
protected void doTransaction(File testPlanFile, OMElement tranElement, String testStepElementName, String stepElementName) throws JaxenException {
logger.fine("doTransaction " + testStepElementName + "/" + stepElementName);

String metadataFileElementName = "MetadataFile";
List<OMElement> steps = XmlUtil.childrenWithLocalName(tranElement, metadataFileElementName);

if (steps.size()>0) {
OMElement mEle = steps.get(0); // pick the first one only, should be only one metadatafile element per step

if (mEle == null) {
logger.fine(() ->" No MetadataFile element declared.");
} else {
String mFileString = mEle.getText();
File metadataFile = new File( testPlanFile.getParentFile(), mFileString);
if (metadataFile.exists()) {
logger.finer(" Found MetadataFile element!");
try {
OMElement metadataElement = Util.parse_xml(metadataFile);
switch (stepElementName) {
case "ProvideAndRegisterTransaction":
String errors = SchemaValidation.validate(schemaLocation, metadataElement, MetadataTypes.METADATA_TYPE_PR);
if ("".equals(errors))
pnrSchemaPassed++;
else {
pnrSchemaFailed++;
System.out.println(
String.format("Metadatafile %s Failed %s XML Validation result is %s",
metadataFile,
"Input",
errors));
}
break;

}
} catch (Exception ex) {
metadataParserExceptions++;
}
} else {
missingMetadataFileErrors++;
}
}


}
}

@Override
public void endPart(File part) {

Expand Down Expand Up @@ -79,12 +144,12 @@ public void startTestPlan(File testplan) throws XdsInternalException, JaxenExcep
// given the testplan filename and the testkit base name, calculate the
// string that should be in the <Test/> element in the testplan file
String nameFilter(File testplan) {
String[] filenameElements = testplan.toString().split("\\/");
String[] filenameElements = testplan.toString().split(Matcher.quoteReplacement(File.separator));

return join(filenameElements,
testkitPathElementsToIgnore,
filenameElements.length-2, // -2 so testplan.xml is removed
"/");
"/" /* always use forward slash inside test plan */);
}

@Override
Expand All @@ -104,9 +169,11 @@ public static void main(String[] args) {
System.exit(-1);
}
String testkit = args[0];
boolean completed = false;
try {
tst = new ErrorScan();
tst.walkTree(new File(testkit));
completed = true;
} catch (XdsInternalException e) {
e.printStackTrace();
} catch (JaxenException e) {
Expand All @@ -115,13 +182,24 @@ public static void main(String[] args) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (tst != null) {
if (doEvalTestPlanEnabled) {
System.out.println("Evaluated " + tst.testPlanCount + " testplans" + (!completed ? ", skipping other tests." : "."));
System.out.println(String.format("Found %d invalid test plan Id errors.", errors));
} else {
System.out.println("Testplan evaluation is not enabled.");
System.out.println("Skipped " + skipTestPlanCount + " testplans.");
}
}
System.out.println(String.format("Found %d missing MetadataFile errors.", missingMetadataFileErrors));
System.out.println(String.format("Found %d MetadataFile parser exceptions.", metadataParserExceptions));
System.out.println(String.format("Passed %d MetadataFile PnR transactions.", pnrSchemaPassed));
System.out.println(String.format("Failed %d MetadataFile PnR transactions.", pnrSchemaFailed));
}

if (tst != null) {
System.err.println("Evaluated " + tst.testPlanCount + " testplans");
}

System.exit(errors);

int returnCode = (errors > 0 ? 1 : 0);
System.exit(returnCode);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ public void end() throws Exception {
System.out.println("Wrote " + outfile);
}

@Override
protected void doTransaction(File testPlanFile, OMElement stepEle, String testStepElementName, String stepElementName) throws JaxenException {

}

public static void main(String[] args) {
if (args.length != 3) {
System.err.println("Usage: testkitstructure <toolkit_location> <testkit_location> <output_dir>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;

import javax.xml.parsers.FactoryConfigurationError;

import gov.nist.toolkit.utilities.xml.XmlUtil;
import org.apache.axiom.om.OMElement;
import org.jaxen.JaxenException;

abstract public class TestkitWalker {
protected String testkitPathName;
Expand Down Expand Up @@ -60,7 +62,7 @@ protected void setAreas(String[] areas) {

public void walkTree(File testkit) throws FactoryConfigurationError, Exception {
testkitPathName = testkit.toString();
testkitPathNameSize = testkitPathName.split("\\/").length;
testkitPathNameSize = testkitPathName.split(Matcher.quoteReplacement(File.separator)).length;
testkitPathElementsToIgnore = testkitPathNameSize + 1;


Expand All @@ -78,6 +80,11 @@ public void walkTree(File testkit) throws FactoryConfigurationError, Exception {
System.out.println("Scanning " + area);

File areaDir = new File(testkit.toString() + File.separator + area);
if (!areaDir.isDirectory() || !areaDir.exists()) {
System.out.println("Scan area does not exist. Skipping.");
continue;
}

if (debug)
System.out.println("Area: " + areaDir);

Expand Down Expand Up @@ -150,14 +157,30 @@ public void walkTree(File testkit) throws FactoryConfigurationError, Exception {
void walkTestPlan(File testPlanFile) throws FactoryConfigurationError, Exception {
OMElement testplanEle = Util.parse_xml(testPlanFile);

List<OMElement> steps = XmlUtil.childrenWithLocalName(testplanEle, "TestStep");
String testStepElementName = "TestStep";
List<OMElement> steps = XmlUtil.childrenWithLocalName(testplanEle, testStepElementName);

for(int i=0; i<steps.size(); i++) {
OMElement stepEle = steps.get(i);
doStep(stepEle.getLocalName());
String stepElementName = stepEle.getLocalName();
doStep(stepElementName);

List<OMElement> testStepChildrenElements = XmlUtil.children(stepEle);
for(int j=0; j<testStepChildrenElements.size(); j++) {
OMElement testStepChildEle = testStepChildrenElements.get(j);
String testStepChildEleLocalName = testStepChildEle.getLocalName();

if (testStepChildEleLocalName.endsWith("Transaction")) {
doTransaction(testPlanFile, testStepChildEle, testStepElementName, testStepChildEleLocalName);
}
}

}

}

protected abstract void doTransaction(File testPlanFile, OMElement stepEle, String testStepElementName, String stepElementName) throws JaxenException;

protected String join(String[] parts, int first, int last, String separator) {
StringBuffer buf = new StringBuffer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@ public class SchemaValidation extends MetadataTypes {
static public String toolkitSchemaLocation = null;

public static String validate(OMElement ele, int metadataType) throws XdsInternalException {
return validate(ele.toString(), metadataType);
return validate(null, ele.toString(), metadataType);
}
public static String validate(String schemaLocation, OMElement ele, int metadataType) throws XdsInternalException {
return validate(schemaLocation, ele.toString(), metadataType);
}

// empty string as result means no errors
static private String validate(String metadata, int metadataType) throws XdsInternalException {
String localSchema = Installation.instance().schemaFile().toString();
/**
*
* @param extSchemaLocation
* @param metadata
* @param metadataType
* @return
* empty string as result means no errors
* @throws XdsInternalException
*/
public static String validate(String extSchemaLocation, String metadata, int metadataType) throws XdsInternalException {
String localSchema = (extSchemaLocation == null ? Installation.instance().schemaFile().toString() : extSchemaLocation);
localSchema = localSchema.replaceAll(" ", "%20");
MyErrorHandler errors = null;
DOMParser p = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package gov.nist.toolkit.xdstools2.server;

import gov.nist.toolkit.commondatatypes.client.MetadataTypes;
import gov.nist.toolkit.registrymetadata.Metadata;
import gov.nist.toolkit.registrymetadata.MetadataParser;
import gov.nist.toolkit.registrymetadata.client.*;
import gov.nist.toolkit.commondatatypes.MetadataSupport;
import gov.nist.toolkit.results.MetadataToMetadataCollectionParser;
import gov.nist.toolkit.testengine.transactions.BasicTransaction;
import gov.nist.toolkit.utilities.io.Io;
import gov.nist.toolkit.utilities.xml.OMFormatter;
import gov.nist.toolkit.utilities.xml.Util;
import gov.nist.toolkit.valregmsg.message.SchemaValidation;
import gov.nist.toolkit.xdsexception.client.XdsInternalException;
import org.apache.axiom.om.OMElement;

Expand Down Expand Up @@ -274,39 +278,85 @@ static public void main(String[] args) {
Metadata m = null;

try {
m = MetadataParser.parseNonSubmission(inFile);
// m = MetadataParser.parseNonSubmission(inFile);
/*
* When parseNonSubmission is used to translate to a v3 metadata file,
* the v3 Association registry object structure is not preserved properly.
* Certain types of Association object structure are corrupted.
* Example associationType="urn:ihe:iti:2010:AssociationType:UpdateAvailabilityStatus"
* Corruption happens when some test metadata input files have mixed content: v2 (detected as any namespace non-v3) rim XML namespace with v3 Association objects.
*
* When metadata files are attempted to translate into v3 using this main method or the parse_xml (unlike the parseNonSubmission) method,
* the first translate call changes all registry object namespaces to v3, and when the v3 Association structure is translated it is kept intact because the namespace is v3 by that time.
*
*
*/


OMElement e = Util.parse_xml(inFile);
validateMetadata("Input", e, MetadataTypes.METADATA_TYPE_Rb);

m = MetadataParser.parse(e);
OMElement x = null;
if (metadataV2) {
x = m.getV2SubmitObjectsRequest();
} else {
x = m.getV3SubmitObjectsRequest();
try {
validateMetadata("Output", x, MetadataTypes.METADATA_TYPE_Rb);
Io.stringToFile(outFile, new OMFormatter(x).toString());
} catch (IOException e1) {
e1.printStackTrace();
}

}

}
catch (Exception e) {
e.printStackTrace();
System.exit(0);
}


/*
MetadataCollection mc = MetadataToMetadataCollectionParser.buildMetadataCollection(m, "test");
Metadata m2 = MetadataCollectionToMetadata.buildMetadata(mc, true);
List<OMElement> eles = null;

OMElement x = null;
try {
if (metadataV2) {
eles = m2.getV2();
x = MetadataSupport.om_factory.createOMElement("Metadata", null);
for (OMElement e : eles)
x.addChild(e);
} else
eles = m2.getV3();
// eles = m2.getV3(); // Misses the SubmitObjectsRequest wrapper if it existed
x = m2.getV3SubmitObjectsRequest(); // assume SubmitObjectsRequest was present
} catch (XdsInternalException e1) {
e1.printStackTrace();
System.exit(1);
}
OMElement x = MetadataSupport.om_factory.createOMElement("Metadata", null);

for (OMElement e : eles)
x.addChild(e);

try {
Io.stringToFile(outFile, new OMFormatter(x).toString());
} catch (IOException e1) {
e1.printStackTrace();
}
*/
}

private static void validateMetadata(String label, OMElement e, int metadata_type) throws Exception {
String schemaLocation ="C:\\Users\\skb1\\myprojects\\iheos-toolkit2\\xdstools2\\src\\test\\resources\\war\\toolkitx\\schema";
String errors = SchemaValidation.validate(schemaLocation, e, metadata_type);
System.out.println(
String.format("%s XML Validation error(s) ? %s",
label,
("".equals(errors) ? "None." : errors)));

}

}
Loading

0 comments on commit d990f23

Please sign in to comment.