Forum Discussion

jurven007's avatar
jurven007
Occasional Contributor
7 years ago
Solved

Run script assertion without sending

Hi All,   I copied an xml message into a request (part of a testcase using property transfer). Next I created a script assertion which will validate the content of the request. Is it possible to ru...
  • jurven007's avatar
    jurven007
    7 years ago

    Hi,

     

    Thanks for sharing the script. I replaced the 'println' with 'log.info' so I can see what's going on. The script is now:

    /**
    * this groovy script validates given xml file 
    * with the given xsd file
    **/
    import org.xml.sax.SAXException
    import javax.xml.transform.stream.StreamSource
    import java.nio.charset.StandardCharsets
    import javax.xml.XMLConstants
    import javax.xml.validation.SchemaFactory
    
    //Provide valid xml instance & and xsd file paths below
    def xml = 'C://Temp//BGTConnector//test2.xml'
    def xsd = 'C://SoapUI projecten//BGT Connector//imgeo0300//verticaal//imgeo0300_msg_verticaal.xsd'
    
    //Not required to edit beyond this point
    
    def isFileExists(String fileName) {
        def file = new File(fileName)
        def result = file.exists()
        if (result) {
            log.info "$fileName exists"
        } else {
            log.info "$fileName does not exist"
        }
        result
    }
    
    def isValid(String xsd, String xml) {
        def result = false
        if (isFileExists(xsd) && isFileExists(xml)) {
            def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
            try {
                def schema = factory.newSchema(new File(xsd))
                def validator = schema.newValidator()
                validator.validate(new StreamSource(new InputStreamReader(new FileInputStream(xml), StandardCharsets.UTF_8)))
                log.info "$xml is valid."
                result = true
            } catch (SAXException ex) {
                log.info "$xml is not valid because \n"
                log.info ex.localizedMessage 
            } catch (FileNotFoundException ex) {
                log.info ex.localizedMessage
            } catch (IOException ex) {
                log.info ex.localizedMessage
            } finally {
                return result
            }
        } else {
            log.info "Can't validate file as one of the file is not found"
        }
        result
    }
    
    //Validate xml by calling above method
    isValid(xsd, xml)

     When I run the groovy script it finds the xsd and xml but fails because it cannot resolve a name.

     

     

     

    I searched for it and it's found in another xsd. I suspect the selected xsd has references to other xsd's which aren't loaded. Is it possible to load multiple xsd's? Can I refer to a property (which contains the message) instead of the xml file?

    Thanks for helping out,

     

    Jurriaan