Search in sources :

Example 1 with XMLValidAgainstXSDAssertionVerificationResult

use of io.irontest.models.assertion.XMLValidAgainstXSDAssertionVerificationResult in project irontest by zheng-wang.

the class XMLValidAgainstXSDAssertionVerifier method verify.

/**
 * @param inputs  contains only one argument: the XML string that the assertion is verifying against the XSD
 * @return
 * @throws Exception
 */
@Override
public AssertionVerificationResult verify(Object... inputs) throws Exception {
    String xmlString = (String) inputs[0];
    XMLValidAgainstXSDAssertionProperties assertionProperties = (XMLValidAgainstXSDAssertionProperties) getAssertion().getOtherProperties();
    String fileName = StringUtils.trimToEmpty(assertionProperties.getFileName());
    // validate arguments
    if (xmlString == null) {
        throw new IllegalArgumentException("XML is null.");
    } else if ("".equals(fileName)) {
        throw new IllegalArgumentException("XSD file not uploaded.");
    } else if (!(fileName.toLowerCase().endsWith(".xsd") || fileName.toLowerCase().endsWith(".zip"))) {
        throw new IllegalArgumentException("Unrecognized XSD file format.");
    }
    XMLValidAgainstXSDAssertionVerificationResult result = new XMLValidAgainstXSDAssertionVerificationResult();
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema;
    Path tempDir = null;
    if (fileName.toLowerCase().endsWith(".xsd")) {
        schema = factory.newSchema(new StreamSource(new ByteArrayInputStream(assertionProperties.getFileBytes())));
    } else {
        // the XSD(s) are in a zip file
        // extract the zip file in a temp directory
        tempDir = Files.createTempDirectory("irontest");
        File zipFile = new File(tempDir.toFile(), assertionProperties.getFileName());
        FileUtils.writeByteArrayToFile(zipFile, assertionProperties.getFileBytes());
        new ZipFile(zipFile).extractAll(tempDir.toString());
        // create schema object out of the extracted XSD files
        Collection<File> xsdFiles = FileUtils.listFiles(tempDir.toFile(), new String[] { "xsd", "XSD" }, true);
        List<Source> sourceList = new ArrayList<>();
        for (File xsdFile : xsdFiles) {
            sourceList.add(new StreamSource(xsdFile));
        }
        schema = factory.newSchema(sourceList.toArray(new Source[sourceList.size()]));
    }
    Validator validator = schema.newValidator();
    try {
        validator.validate(new StreamSource(new StringReader(xmlString)));
        result.setResult(TestResult.PASSED);
    } catch (SAXParseException e) {
        result.setResult(TestResult.FAILED);
        result.setFailureDetails(e.toString());
    } finally {
        if (tempDir != null) {
            FileUtils.deleteDirectory(tempDir.toFile());
        }
    }
    return result;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Path(java.nio.file.Path) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) ArrayList(java.util.ArrayList) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) XMLValidAgainstXSDAssertionVerificationResult(io.irontest.models.assertion.XMLValidAgainstXSDAssertionVerificationResult) XMLValidAgainstXSDAssertionProperties(io.irontest.models.assertion.XMLValidAgainstXSDAssertionProperties) ZipFile(net.lingala.zip4j.ZipFile) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) File(java.io.File) ZipFile(net.lingala.zip4j.ZipFile) Validator(javax.xml.validation.Validator)

Aggregations

XMLValidAgainstXSDAssertionProperties (io.irontest.models.assertion.XMLValidAgainstXSDAssertionProperties)1 XMLValidAgainstXSDAssertionVerificationResult (io.irontest.models.assertion.XMLValidAgainstXSDAssertionVerificationResult)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 StringReader (java.io.StringReader)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Source (javax.xml.transform.Source)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Schema (javax.xml.validation.Schema)1 SchemaFactory (javax.xml.validation.SchemaFactory)1 Validator (javax.xml.validation.Validator)1 ZipFile (net.lingala.zip4j.ZipFile)1 SAXParseException (org.xml.sax.SAXParseException)1