How to stub wsdl service endpoints? - java

I want to run some integration tests on a WSDL client application. Therefore I'd like to have the WSDL server (which is not in control of me) to response with the same xml response always.
How could I achieve this? Is there any tool out that offers a wsdl endpoint an can always return the same xml (which I then could take from my live logs)?

SOAPUI offers this functionality. I've used it before with ease.
Alternatively if you do not want to go through the hassle of converting the WSDL request to a meaningful response, take a look at services like mocky which simply echo back a payload.

Take a look at Soap-UI - it has the ability to mock web services exactly as you mentioned using just a wsdl. You can then decide which XML the mocked web service will return, and it will run as a server locally. Instead of pointing to the 'real' server you can then point to your own local endpoint in order to retreive the same response. More information available on my blog here

Related

Java log request with corresponding response

I have web application which interacts with SOAP and REST external web services for some use cases as a client, and as a server for other use cases.
By the business requirements I need to log all outcoming requests to the external systems with the corresponding response, and I also need to log all incoming requests with the corresponding response.
So i need to write the list of objects like this:
{communicationId, timestamp, OUT[IN], request, response}.
What is the best way to achieve it from several perspectives:
1. To have the code in the single place if it is possible.
2. To map request with corresponding response effectively.
The best solution is dependent on the tooling you are using.
I assume you have an ESB (such as Mule) in front of your web application. If so, you have the ESB log the necessary information on the way in/out of the app.
If you have Apache CXF available, you could use interceptors to do the logging for you.
As for logging the 'response' - does this mean the actual object? If so, you could use JAXB to convert to XML and then log.

Testing Web Service WSDL without SoapUI

I have a WSDL that has a handful of methods that I want to test. I was wondering if there is a way to write service requests and send them directly to the WSDL through Java? I could then parse the response to validate the success of the tests. I know that you can do this through soap UI but the problem I have with this is that I want multiple people who use this service to be able to run these tests and not everyone is using the same version of Soap UI. They are also going to be data driven tests so I want to be able to handle large data sets in cvs/xls files through java automatically instead of manually.
Any advice on where to begin?
Thanks
1.) Generate your webservices stub classes from your WSDL with the tool of your joice (for example wsimport).
In the simplest way this looks like this:
wsimport.exe -d <Path2StoreGeneratedStubs> -s <Path2StoreSourceFiles> -keep <Path2YourWsdl>
2.) Use those classes from within your JUnit tests to directly talk with your webservice.
So for your JUnit youre basicially on the client-side of your webservice. You want to generate stub classes to allow you to talk to your webservice but instead of writing a nice client application you are just using those Stubs for TestCases (just pseudo...) like this:
#Test
public void testPingMethod(){
MyService service = new MyServiceImpl();
MyPingResponse res = service.ping();
Assert.assertNotNull(res);
}
So MyService/ MyPingResonse in this example would be generated from the WSDL while the (also just an example) of the Assert would verify your Webservice sends back a response fo this request.
If you never had to write client code i recomend you do a 5min. Tutorial on the topic since its pretty easy to use an existing webservice :)

How to see request generated by wsdl2java code?

I using a stub generated by wsld2java:
ServicesStub.ResponseMessage resp = stub.processService(rm);
Also Rampart module is engaged which includes a security header.
How can I see the complete XML request which gets send to the server?
Or if you don't want to be tied to Eclipse use TCPMON http://ws.apache.org/commons/tcpmon/
You basically redirect traffic through TCPMON and you view exactly what HTML actions happen between your outgoing webservices call (Either REST or SOAP) and you can diagnose, very useful for when SOAP faults do not properly translate to Java with useful error messages.
Eclipse has a build in tcp/monitor tool - netbeans something similar. Also possible is a normal network monitor tool like shark or a web proxy build for debugging purpose.

Consuming Spring (SOAP) web service in a java servlet

Is there any way to consume a SOAP based Spring web service without generating stubs on the client (as suggested by umpteen threads pointing to JAX-WS)?
Here is my complete scenario:
I have 2 web applications, say APP1 & APP2, both of which have Spring support. APP2 exposes it's APIs as Spring-WS that accept POJOs (Reqeust and Response objects) via. SOAP. Now, I want to call these web services from APP1, but would like to avoid having to create stubs using the WSDL frmo APP2. Is this possible?
For more detail, here is one of my web service's operation:
#PayloadRoot(localPart = "CreateNewRequest", namespace = "myNameSpace")
public CreateNewReqResponse createNewRequest( CreateNewReqRequest requestObj ) throws Exception
{
NewCase newCase = this.localSpringService.createNewCase( requestObj.getParam1(), requestObj.getParam2() );
CreateNewReqResponse response = this.objectFactory.createCreateNewReqResponse();
CreateNewReqResponseObject responseObject = this.objectFactory
.createCreateNewReqResponseObject();
if( null != newCase )
{
responseObject.setParam1( newCase.getParam1() );
responseObject.setParam2( newCase.setParam2() );
}
responseObject.setCaseRequestedDate( caseRequestedDate );
}
response.setResponseObject( responseObject );
return response;
}
Now, as you can see, the web service method accepts CreateNewReqRequest and returns CreateNewReqResponse. What I am trying to figure out is how can I call this web service from APP1, which does not have any clue about these classes - CreateNewReqRequest and CreateNewReqResponse? Is there no other way other than creating stubs in APP1 (from the WSDL) using JAX-WS?
Both the applications in question are our own (that is we have developed them) but run on different servers, because of which APP1 can not call the web service directly - cross-domain policy. Hence, I will writing a servlet in APP1 which will consume the web service exposed by APP2.
At the end of they day SOAP is simple a protocol on top of HTTP. So if you wish to forgo the usage of JAX-WS, you could start using raw http connections and hand code the SOAP requests and manually parse the SOAP Response on your own. This would simply mean you are re - inventing the wheel which is JAX-WS Client stubs.
So If you absolutely want to avoid Stub creation, have a go it at with HTTP post and get Messages at the WSDL end point URL.
What the client Stub does is simply abstract out that implementation for you. i.e. you will not have to deal with nitty gritty of SOAP/WSDL and http connection, you would be dealing with SOAP at a higher level, i.e through Java Objects.
You could also look into other libraries such as Apache CXF or Axis, but even there you will have to generate client stubs.
Thus the question you want to ask is, Do you want to really go in and manually muck around with http connections and SOAP XMLs or let the a framework take do that grunt work for you.
Reply to Nitin's comment follows below
To answer your questions, 1. Yes you will have to re-create the stubs if the WSDL changes, if you are not using the stubs but parsing everything manually you will have to change that code. So effectively there is no difference between the two. Your program will have to change if the WSDL (i.e. the contract between client and service) changes. This would be same even for REST, i.e. if the contract published by the service changes(Maybe the parameters, or the action etc), you will have to Change your client code. There is no escaping that. Hopefully, the public Webservices would have been designed in such a way to allow future modification, and such modifications wouldn't happen overnight thus giving you enough time to modify your code. This issue has nothing to do with how the web service has been implemented i.e. Spring Web Service has nothing to do with.
You seem to be missing the point of the Client stubs that a SOAP framework like JAX-WS, Axis, CXF generate for you. The Client Stub is one way to talk to the Web service. It is not the only way. The Client stub is the preferred method, because it abstracts outout the nitty gritties of handling SOAP calls manually. So, rather then you re inventing the wheel and implementing a SOAP(XML) parsing library, you can concentrate your efforts on the actual application that you are writing. In your actual program you would only have to deal with POJOs and never have to worry about how the SOAP magic happens i.e. how to convert your data and package it up in a SOAP message, send that SOAP Message to the Service using a HTTP connection, handle the response, Parse the response SOAP Message and retrieve the data you care about. All of this you avoid by using the POJOs. You set the properties for request, make a method call to the client stub service method, and recieve an object, everything else is you don't have to worry about (ideally).
I hope this clear things up a bit.
Take a look at WebServiceTemplate class.

Compose Soap Request in Java

I have a wsdl file and a detailed document about all the elements in every request and response from a web-service provider. My job is to compose around 40 requests and parse corresponding responses.
More specifically, our platform submits the requests and gets responses from the service, so for me, as an application developer, I only need to compose soap requests and pass them as String to the platform. I also get response as String from the platform.
I tried StringBuilder, but it looks pretty primitive. It has to be a better way to do it.
Can I put all the requests in an xml document and somehow generate requests from it?
Or even better, is it possible to generate requests from the wsdl file?
Thanks,
Sarah
Have a look at the wsdl2java utilities (there are several versions, one packaged with Axis2, another from IBM, etc.). These can generate client stubs from your WSDL, and should save you a considerable amount of work.
EDIT: Just realized that this may require some additional work since you say your platform submits the requests. The generated code should be attempting to submit strings to the service if that is what's specified by your WSDL, perhaps you can modify the code to pass the strings to your platform?
JAX-WS's wsimport
Client stubs w/ XFire
Axis2's wsdl2java
IBM's wsdl2java
You can use SAAJ API for this purpose.
For more details visit these links:
http://docs.oracle.com/javaee/5/tutorial/doc/bnbhg.html
https://saaj.dev.java.net/nonav/spec-1.3/api/
http://lia.deis.unibo.it/Courses/TecnologieWeb0708/materiale/laboratorio/guide/j2ee14tutorial7/SAAJ3.html
The simplest way is soap-ws library: https://github.com/reficio/soap-ws
SoapClient client = SoapClient.builder()
.endpointUrl("http://rpc.middleearth.com")
.build();
client.post(envelope);

Categories

Resources