Ich bin über drei Jahre zu spät zu dieser Party, aber es dauerte eine Weile, um das gleiche Problem zu bearbeiten, so dass ich meine Lösung als eine Antwort dokumentieren würde, damit jemand anderen die Kopfschmerzen des manuellen Handelns erspart mit SOAP-Nutzdaten von Grund auf.
Ich habe ein vernünftiges über die Forschung versucht, dieses Problem für meine Integration Testsuite zu lösen. Versuchte alle möglichen Dinge, einschließlich benutzergenerierte CXF-Server, SOAP-UI, eine CGLIB-beeinflusste Bibliothek, die den realen Client in einem Testkontext ersetzt.
Ich endete mit WireMock mit custom request matchers, um alle SOAP
-Yness zu behandeln.
Der Kern davon war eine Klasse, die das Unmarshaling von SOAP-Anfragen und Marshaling von SOAP-Antworten behandelte, um Autoren, die JAXB-generierte Objekte benötigten und sich nie mit den Details von SOAP befassen mussten, einen praktischen Wrapper zu bieten.
Antwort Marshalling
/**
* Accepts a WebService response object (as defined in the WSDL) and marshals
* to a SOAP envelope String.
*/
public <T> String serializeObject(T object) {
ByteArrayOutputStream byteArrayOutputStream;
Class clazz = object.getClass();
String responseRootTag = StringUtils.uncapitalize(clazz.getSimpleName());
QName payloadName = new QName("your_namespace_URI", responseRootTag, "namespace_prefix");
try {
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Marshaller objectMarshaller = jaxbContext.createMarshaller();
JAXBElement<T> jaxbElement = new JAXBElement<>(payloadName, clazz, null, object);
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
objectMarshaller.marshal(jaxbElement, document);
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
SOAPBody body = soapMessage.getSOAPPart().getEnvelope().getBody();
body.addDocument(document);
byteArrayOutputStream = new ByteArrayOutputStream();
soapMessage.saveChanges();
soapMessage.writeTo(byteArrayOutputStream);
} catch (Exception e) {
throw new RuntimeException(String.format("Exception trying to serialize [%s] to a SOAP envelope", object), e);
}
return byteArrayOutputStream.toString();
}
Anfrage Unmarshaling
/**
* Accepts a WebService request object (as defined in the WSDL) and unmarshals
* to the supplied type.
*/
public <T> T deserializeSoapRequest(String soapRequest, Class<T> clazz) {
XMLInputFactory xif = XMLInputFactory.newFactory();
JAXBElement<T> jb;
try {
XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(soapRequest));
// Advance the tag iterator to the tag after Body, eg the start of the SOAP payload object
do {
xsr.nextTag();
} while(!xsr.getLocalName().equals("Body"));
xsr.nextTag();
JAXBContext jc = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = jc.createUnmarshaller();
jb = unmarshaller.unmarshal(xsr, clazz);
xsr.close();
} catch (Exception e) {
throw new RuntimeException(String.format("Unable to deserialize request to type: %s. Request \n %s", clazz, soapRequest), e);
}
return jb.getValue();
}
private XPath getXPathFactory() {
Map<String, String> namespaceUris = new HashMap<>();
namespaceUris.put("xml", XMLConstants.XML_NS_URI);
namespaceUris.put("soap", "http://schemas.xmlsoap.org/soap/envelope/");
// Add additional namespaces to this map
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new NamespaceContext() {
public String getNamespaceURI(String prefix) {
if (namespaceUris.containsKey(prefix)) {
return namespaceUris.get(prefix);
} else {
return XMLConstants.NULL_NS_URI;
}
}
public String getPrefix(String uri) {
throw new UnsupportedOperationException();
}
public Iterator getPrefixes(String uri) {
throw new UnsupportedOperationException();
}
});
return xpath;
}
Zusätzlich dazu war einige XPath Dienstprogramme für die in der Anfrage-Payload spähen und schauen, was Operation angefordert wurde.
Das gesamte SOAP-Handling war der fiddliest Teil, um zu arbeiten. Von dort erstellen Sie einfach Ihre eigene API, um WireMocks zu ergänzen. Zum Beispiel
public <T> void stubOperation(String operation, Class<T> clazz, Predicate<T> predicate, Object response) {
wireMock.stubFor(requestMatching(
new SoapObjectMatcher<>(context, clazz, operation, predicate))
.willReturn(aResponse()
.withHeader("Content-Type", "text/xml")
.withBody(serializeObject(response))));
}
und als Ergebnis erhalten Sie eine schöne, schlanke Tests.
SoapContext context = new SoapContext(...) // URIs, QName, Prefix, ect
context.stubOperation("createUser", CreateUser.class, (u) -> "myUser".equals(u.getUserName()), new CreateUserResponse());
soapClient.createUser("myUser");
https://github.com/skjolber/mockito-soap-cxf – ThomasRS