简体   繁体   中英

Validate xml response/string against list of xsds using RestAssured

I am trying to validate an XML response retrieved from RestAssured with a list of XSDs which are referenced within.

I tried

 given().param(...).when().get(URL).then().assertThat().body(matchesXsd(xsd))

This works if I have one XSDs file which contains all schema definitions but how can I have multiple XSDs? These XSDs are also referenced within themselves. Also, all XSDs are local to the project.

I tried Rest-Assured XSD References Other XSDs and Validate an XML File Against Multiple Schema Definitions and few more but they were either irrelevant to my goal or didn't work for me.

I solved it by:

given().param(...).when().get(URL).then().assertThat().body(matchesXsd(getSystemResourceAsStream("parent.xsd")).using(new ClasspathResourceResolver()));

ClasspathResourceResolver resolves all references from parent XSD.

and

`

public class ClasspathResourceResolver implements LSResourceResolver {

    @Override
    public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
        InputStream resource = getSystemResourceAsStream(systemId);
        return new DOMInputImpl(publicId, systemId, baseURI, resource, null);
    }
}

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM