public void testMessageProcessingWithSoapAttachment() throws Exception {
WebServiceEndpoint endpoint = new WebServiceEndpoint();
Map<String, Object> requestHeaders = new HashMap<String, Object>();
requestHeaders.put(SoapMessageHeaders.SOAP_ACTION, "sayHello");
final Message requestMessage = new DefaultMessage("<?xml version=\"1.0\" encoding=\"UTF-8\"?><TestRequest><Message>Hello World!</Message></TestRequest>", requestHeaders);
final Message responseMessage = new DefaultMessage("<?xml version=\"1.0\" encoding=\"UTF-8\"?><TestResponse><Message>Hello World!</Message></TestResponse>");
endpoint.setEndpointAdapter(new StaticEndpointAdapter() {
public Message handleMessageInternal(Message message) {
Assert.assertTrue(SoapMessage.class.isInstance(message));
SoapMessage soapMessage = (SoapMessage) message;
Assert.assertEquals(soapMessage.getAttachments().size(), 1L);
Attachment attachment = soapMessage.getAttachments().get(0);
Assert.assertEquals(attachment.getContentId(), "myContentId");
Assert.assertEquals(attachment.getContentType(), "text/xml");
Assert.assertEquals(message.getHeader(SoapMessageHeaders.SOAP_ACTION), "sayHello");
Assert.assertEquals(message.getPayload(), requestMessage.getPayload());
return responseMessage;
}
});
org.springframework.ws.soap.SoapMessage soapRequest = EasyMock.createMock(org.springframework.ws.soap.SoapMessage.class);
SoapHeader soapRequestHeader = EasyMock.createMock(SoapHeader.class);
org.springframework.ws.soap.SoapMessage soapResponse = EasyMock.createMock(org.springframework.ws.soap.SoapMessage.class);
StringResult soapResponsePayload = new StringResult();
Set<Attachment> attachments = new HashSet<Attachment>();
Attachment attachment = EasyMock.createMock(Attachment.class);
attachments.add(attachment);
reset(messageContext, soapRequest, soapRequestHeader, soapResponse, attachment);
expect(messageContext.getRequest()).andReturn(soapRequest).anyTimes();
expect(soapRequest.getPayloadSource()).andReturn(new StringSource("<TestRequest><Message>Hello World!</Message></TestRequest>")).times(2);
expect(messageContext.getPropertyNames()).andReturn(new String[]{}).once();
expect(soapRequest.getSoapHeader()).andReturn(soapRequestHeader).once();
expect(soapRequestHeader.getSource()).andReturn(null).once();
Set<SoapHeaderElement> emptyHeaderSet = Collections.emptySet();
expect(soapRequestHeader.examineAllHeaderElements()).andReturn(emptyHeaderSet.iterator()).once();
expect(soapRequest.getSoapAction()).andReturn("sayHello").anyTimes();
expect(soapRequest.getAttachments()).andReturn(attachments.iterator()).once();
expect(attachment.getContentId()).andReturn("myContentId").anyTimes();
expect(attachment.getContentType()).andReturn("text/xml").anyTimes();
expect(messageContext.getResponse()).andReturn(soapResponse).once();
expect(soapResponse.getPayloadResult()).andReturn(soapResponsePayload).once();
expect(attachment.getInputStream()).andReturn(new ByteArrayInputStream("AttachmentBody".getBytes())).once();
replay(messageContext, soapRequest, soapRequestHeader, soapResponse, attachment);
endpoint.invoke(messageContext);
Assert.assertEquals(soapResponsePayload.toString(), responseMessage.getPayload());
verify(messageContext, soapRequest, soapRequestHeader, soapResponse, attachment);
}