-
Notifications
You must be signed in to change notification settings - Fork 10
Working with SOAP web services
Chirag Jayswal edited this page Feb 25, 2019
·
1 revision
For SOAP web-service, set the request method to POST
and header "Content-Type":"text/xml"
and provide XML message in body.
- Generate SOAP Message body Template(s) for each soap operations from WSDL
- You can use wizdler plugin for firefox or chrome.
- You can generate template using predic8, refer sample code.
- Create request body xml using generated template
- Create request call, refer sample calls
- Use request call in test case
- Validate response as usual
/**
*
*/
package com.qaf.ws.support;
import java.io.StringWriter;
import com.predic8.wsdl.BindingOperation;
import com.predic8.wsdl.Definitions;
import com.predic8.wsdl.Port;
import com.predic8.wsdl.Service;
import com.predic8.wsdl.WSDLParser;
import com.predic8.wstool.creator.RequestTemplateCreator;
import com.predic8.wstool.creator.SOARequestCreator;
import groovy.xml.MarkupBuilder;
/**
* @author chirag.jayswal
*
*/
public class SOAPMessageTemplateGenerator {
/**
* @param args
*/
public static void main(String[] args) {
String wsdlFile = "http://www.holidaywebservice.com//HolidayService_v2/HolidayService2.asmx?wsdl";
//"http://www.thomas-bayer.com/axis2/services/BLZService?wsdl";
// Scanner s = new Scanner(System.in);
// String wsdlFile = s.nextLine();
WSDLParser parser = new WSDLParser();
Definitions wsdl = parser.parse(wsdlFile);
StringWriter writer = new StringWriter();
// SOAPRequestCreator constructor: SOARequestCreator(Definitions, Creator,
// MarkupBuilder)
SOARequestCreator creator = new SOARequestCreator(wsdl, new RequestTemplateCreator(),
new MarkupBuilder(writer));
for (Service service : wsdl.getServices()) {
System.out.println(" Service Name: " + service.getName());
System.out.println(" Service Potrs: ");
for (Port port : service.getPorts()) {
if (port.getBinding().getProtocol().toString().indexOf("SOAP") >= 0) {
for (BindingOperation op : port.getBinding().getOperations()) {
writer.write("\n======================================================================\n");
writer.write(port.getBinding().getProtocol() + " PortType name: " + port.getName()
+ " Operation name: " + op.getName() + " Binding name:" + port.getBinding().getName());
writer.write("\n======================================================================\n");
try {
// creator.createRequest(PortType name, Operation name, Binding name);
creator.createRequest(port.getName(), op.getName(), port.getBinding().getName());
} catch (Exception e) {
System.err.println("PortType name: " + port.getName() + " Operation name: " + op.getName()
+ " Binding name:" + port.getBinding().getName() + "\n" + e.getMessage());
}
}
}
}
}
// creator.createRequest(PortType name, Operation name, Binding name);
System.out.println("\n\n===========================\n\n");
System.out.println(writer);
}
}
<requests>
<!-- template to be used by each request call -->
<soap-ws-template>
<method>POST</method>
<baseUrl>${ws.url}</baseUrl>
<headers>{"Content-Type":"text/xml"}</headers>
</soap-ws-template>
<!-- request calls -->
<!-- message body from file-->
<operation1>
<request>
<reference>soap-ws-template</reference>
<body>file:resources/data/op_req.xml</body>
</request>
</operation1>
<!-- message body inline-->
<GetHolidaysForYear>
<request>
<reference>soap-ws-template</reference>
<body>
<![CDATA[
<s12:Envelope xmlns:s12='http://www.w3.org/2003/05/soap-envelope'>
<s12:Body>
<ns1:GetHolidaysForYear xmlns:ns1='http://www.holidaywebservice.com/HolidayService_v2/'>
<ns1:countryCode>UnitedStates</ns1:countryCode>
<ns1:year>2019</ns1:year>
</ns1:GetHolidaysForYear>
</s12:Body>
</s12:Envelope>
]]>
</body>
</request>
</GetHolidaysForYear>
</requests>