I have exposed userservice in Spring Rest service using spring boot and in
that calling userDataWebService using camel cxf component. in junit
skipping cxf endpoing and invoke process to return response then it is
giving null in "mock:result" but if I throw exception then it comes to
"mock:wsError" and works fine.
Cod as below
<cxf:cxfEndpoint id="WebServiceUserData"
address="http://${tos.hostname}/crmtos/USERDATAWS"
wsdlURL="src/main/resources/wsdl/USERDATAWS.wsdl"
serviceClass="com. UserData.USERDATAWS"
endpointName="ws:USERDATAWSPort"
serviceName="ws:USERDATAWSService"
xmlns:ws="http://UserData.wsif /">
<cxf:properties>
<entry key="dataFormat" value="POJO" />
</cxf:properties>
</cxf:cxfEndpoint>
<route id="wsUserDataClient" handleFault="true">
<from uri="direct:UserDataWsRoute" />
<onException>
<exception>java.net.ConnectException</exception>
<redeliveryPolicy maximumRedeliveries="2"
retryAttemptedLogLevel="INFO"
redeliveryDelay="2000"/>
<handled>
<constant>true</constant>
</handled>
<to uri="mock:error" />
</onException>
<to
uri="cxf:bean:WebServiceUserData?defaultOperationName=UserDataCommand"
id="TosCallSer1"/>
<to uri="mock:result" id="mockr2" />
</route>
Calling camel by template in rest
@Produce(uri = "direct:UserDataWsRoute")
ProducerTemplate UserWsRoute;
@EndpointInject(uri = "mock:result")
protected MockEndpoint wsResultEndpoint;
@EndpointInject(uri = "mock:wsError")
protected MockEndpoint wsErrorResultEndpoint;
public UserResponseType getPostUser(UserRequestType request) {
UserResponseType response = new UserResponseType();
LOG.info("-----------------------------WS
Request-----------------");
if(wsResultEndpoint!= null && wsResultEndpoint.getExchanges()!=
null &&
!wsResultEndpoint.getExchanges().isEmpty()){
wsResultEndpoint.getExchanges().clear();
}
if(wsErrorResultEndpoint!= null &&
wsErrorResultEndpoint.getExchanges()!=
null && !wsErrorResultEndpoint.getExchanges().isEmpty()){
wsErrorResultEndpoint.getExchanges().clear();
}
UserWsRoute.sendBody(request);
// i am getting empty for this wsResultEndpoint.getExchanges()
if(!wsResultEndpoint.getExchanges().isEmpty()){
Exchange exchange =
wsResultEndpoint.getExchanges().get(0);
UserCommandResponse wsResponse =
exchange.getIn().getBody(UserCommandResponse.class);
response.getResponse().setResponseCode(wsResponse.getUserResponse().getResponseCode().value());
response.getResponse().setResponseDescription(wsResponse.getUserResponse().getResponseDescription());
}else{
// this is working when we get exception
if(!wsErrorResultEndpoint.getExchanges().isEmpty()){
Exchange exchange
=wsErrorResultEndpoint.getExchanges().get(0);
ResponseType
resType=exchange.getIn().getBody(ResponseType.class);
// LOG.info("-----------------------------WS error
-----------------"+errorMessage);
response.setResponse(resType);
}
Junit :
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebIntegrationTest(randomPort = true)
public class RestJunitWithSimulater {
private MediaType contentType = new
MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(),
Charset.forName("utf8"));
private MockMvc mockMvc;
private HttpMessageConverter mappingJackson2HttpMessageConverter;
@Autowired
private CamelContext context;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setup() throws Exception {
this.mockMvc =
webAppContextSetup(webApplicationContext).build();
AdviceWithRouteBuilder mocker = new
AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
interceptSendToEndpoint("cxf:bean:*")
.skipSendToOriginalEndpoint()
.process(new SimulateResponseProcessor()); //
coustome
response or exception
}
};
context.getRouteDefinition("wsUserDataClient").adviceWith(context,
mocker);
}
@Autowired
void setConverters(HttpMessageConverter<?>[] converters) {
this.mappingJackson2HttpMessageConverter =
Arrays.asList(converters).stream().filter(
hmc -> hmc instanceof
MappingJackson2HttpMessageConverter).findAny().get();
Assert.assertNotNull("the JSON message converter must not be
null",
this.mappingJackson2HttpMessageConverter);
}
protected String json(Object o) throws IOException {
MockHttpOutputMessage mockHttpOutputMessage = new
MockHttpOutputMessage();
this.mappingJackson2HttpMessageConverter.write(
o, MediaType.APPLICATION_JSON, mockHttpOutputMessage);
return mockHttpOutputMessage.getBodyAsString();
}
@Test
public void testSuccess() throws Exception {
User restReqObj=getResUserRequest("jhon");
String restReqJson = json(restReqObj);
mockMvc.perform(post("/getPostUser")
.contentType(contentType)
.content(restReqJson))
.andExpect(jsonPath("$.response.responseCode",
is("Success")));
}
@Test //
public void testConnError() throws Exception {
User restReqObj=getResUserRequest("mike");
String restReqJson = json(restReqObj);
mockMvc.perform(post("/getPostUser")
.contentType(contentType)
.content(restReqJson))
.andExpect(jsonPath("$.response.responseCode",
is("ConnectException")));
}
--
View this message in context:
http://camel.465427.n5.nabble.com/Junit-camel-with-spring-boot-tp5779653.html
Sent from the Camel - Users mailing list archive at Nabble.com.