Spring Integration WS: mock Endpoint to return response - spring

I have setup a Spring Integration flow configuration to send messages to an external web service and then unmarshalling the response and then doing some post processing based on the response object type.
I have the following outbound-gateway configuration:
<int:channel id="sendRequestChannel"/>
<ws:outbound-gateway request-channel="sendRequestChannel" uri="${send.ws.uri}" reply-channel="responseTransformer" >
<ws:request-handler-advice-chain>
<ref bean="retryAdviceUserUpdateWs" />
</ws:request-handler-advice-chain>
</ws:outbound-gateway>
Now, I want to test the flow and check that the correct post processing is triggered based on the response object.
Is there anyway in my integration test to mock the Endpoint response based on the message I'm sending?

Actually you should understand from which part of your flow it would be better to mock and return the desired response.
You can inject ChannelInterceptor to the sendRequestChannel with preSend which returns null, to prevent the further process and send a message with desired response to the responseTransformer.
Another powerful option is to add one more Advice to the <ws:request-handler-advice-chain> and implement it as extension of AbstractRequestHandlerAdvice.
And the last option which I see via Java code is a mock for WebServiceTemplate.sendAndReceive and inject it to the <ws:outbound-gateway>.
From other side I know that SoapUI has a tool to mock target service, so, you even don't need to do anything in Java, unless tests.
So, it up to you to choose the proper way to test you flow.

Related

How to map path elements to method parameter in spring cloud function web

I'm working on a Microservice that receives webhooks, the idea is to use path variables for some processing down stream,
/webhooks/{foo}/{bar}/{baz}
for example
/webhooks/sony/pony/tony would populate foo=sony; bar=pony; baz=tony.
Can't figure how to make it work in spring cloud function web.
This was never the purpose of spring-cloud-function to replace spring-mvc etc.
Also, Function has only one argument, so what you can do is have Function<Message, ...> and we translate HTTP request into Message where body will turn into payload and HTTP headers will turn into Message headers.

How to mock REST service response in actual code flow for lower environments

I have a service which calls the REST Api and that REST API call is chargeable, so i want when we deploy the code in Dev & QA environment, mock response should be returned while actual functionality testing.
How can i achieve that ?
Mock Rest API response in actual code flow
The endpoint for your api shouldn't be hard-coded in the code, instead you can set the endpoint in a properties file.
ie. In a file like .../myapp/src/resources/application.properties
some.service.api.endpoint=someservice.com/api
Then in your code you can use Spring's #Value annotation to get the value.
#Service
public class SomeServiceApi {
#Value("some.service.api.endpoint")
private String endpoint;
// ...
While in development, the property can point to some mock endpoint that you've set up yourself, or if the api supports it, a sandbox endpoint for the api.

Extending Spring Integration while maintaining previous functionality?

so I'm new to Spring Integration, and mostly to Spring as well, so I might not be up on all of the terminology, but I'm running across the following scenario:
I have a small Spring Integration application with three SI flows... each flow has its own Gateway, and each Gateway has its own request channel and reply channel. These flows receive a null invocation (for all intensive purposes... basically just a 'GO' signal / empty message) and reply with a status message, depending upon the (trivial) business logic results.
I would now like to wire each of these flows together to run in one 'master flow', given one request, without taking away their ability to run separately, and I'd like to wire it up completely through annotation / XML (IE. given a controller that invokes the main gateway's service interface, no additional code needs to be written outside of annotation / XML configuration.)
Is this feasible, what Integration components should I be using to do so, and/or should I just be adjusting the expected channels for each of these gateways to be meeting each other end-to-end (and if so, how would that strategy compensate to allow each of the flows to be called on a case-by-case basis)?
In addition, if this is not feasible, would it be appropriate to use a service activator to invoke each of the child flows? I wanted to avoid coding more, but if that is the only option, I guess that it'll have to do.
Thanks!
Probably the simplest way to do this is use Spring Profiles (a Spring 3.1 feature). When deployed in stand-alone mode, the final element can be a "bridge to nowhere"...
<int:bridge input-channel="app1Final" />
... when the final element in a flow has no output channel, the message is returned to the gateway's reply channel. If you prefer to explicitly configure the bridge to point to the gateway's reply-channel, that's ok too; it just not needed.
In the "linked" profile, you configure the bridge thus...
<int:bridge input-channel="app1Final" output-channel="app2Inbound"/>
...where app2Inbound is the same as that app's gateway's request-channel.
<beans profile="default">
<int:bridge input-channel="app1Final" />
</beans>
<beans profile="linked">
<int:bridge input-channel="app1Final" output-channel="app2Inbound"/>
</beans>
To run with the linked profile, set system property 'spring.profiles.active' to 'linked'

HttpservletRequest and response in Spring JMS listener

Can someone tell whether is there a way to get HttpServletRequest and HttpServletResponse in Spring JMS listener class? My JMS listener is defined in springContext.xml file.
First of all you don't have access to HTTP servlet request and response within JMS listeners. These are completely independent modules that can even reside on different physical servers.
You can use MockHttpServletRequest and MockHttpServletResponse from spring-test.jar - but they are meant to be used within unit/integration tests, not in production code.
I would really like to see your code that requires MockHttpServletRequest and response. My guess is that it can be refactored or redesigned to use only relevant fields from the above, like request URL or user name.
I ended up using JAXDispatcher to invoke my service, from my JMS listener.
jaxbDispatcher.doGET(null, url, null, "application/xml", true);

Best way to mock complex soap responses

I have a Java method I want to Unit test, but it requires a mocked SOAP response which contains multiple lists and layers of nodes. I am doing this with a handwritten mock i.e. just manually creating the objects and setting the values, but as the response is quite complex its a pain building up the response. I have a sample XML response is there an easy way of creating the mock using the XML?
Also I looked at Mockito and it looks fine for simple Objects, but it doesnt seem that good for complex responses (I may not be using it to its full potential).
The app stack is Java 1.6, Spring 3 and using JAX-WS.
I do something like this
#WebService
public class MyWebService {
#Autowired
private ServiceBean serviceBean;
public SomeReturedData getData(SomeInputData inputData) {
return serviceBean.getData(inputData);
}
}
For my UnitTest, I have a mock instanciation of "ServiceBean" which I inject in to #MyWebService, and "MyWebService" is deployed using the "in-vm" transport as described here
By Using the in-vm transport, All the XML marshalling/unmarshalling is still done by the web-service framework ,and you only have to deal with Java part.
Now someone might ask, why not test the "ServiceBean" directly, why the need to deply a WS using in-vm transport ? Well 2 things, Using in-vm transport you get to test that the JAXB XML marshalling/unmarshalling is working correctly, and it also allows you to test any intercepting handlers that you might have defined for your webservice.

Resources