use of org.springframework.xml.transform.StringSource in project spring-integration by spring-projects.
the class SimpleWebServiceOutboundGatewayTests method testAttachments.
@Test
public void testAttachments() throws Exception {
String uri = "http://www.example.org";
SimpleWebServiceOutboundGateway gateway = new SimpleWebServiceOutboundGateway(uri);
gateway.setBeanFactory(mock(BeanFactory.class));
final SettableListenableFuture<WebServiceMessage> requestFuture = new SettableListenableFuture<>();
ClientInterceptorAdapter interceptorAdapter = new ClientInterceptorAdapter() {
@Override
public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
requestFuture.set(messageContext.getRequest());
return super.handleRequest(messageContext);
}
};
gateway.setInterceptors(interceptorAdapter);
gateway.afterPropertiesSet();
WebServiceMessageFactory messageFactory = new SaajSoapMessageFactory(MessageFactory.newInstance());
MimeMessage webServiceMessage = (MimeMessage) messageFactory.createWebServiceMessage();
String request = "<test>foo</test>";
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new StringSource(request), webServiceMessage.getPayloadResult());
webServiceMessage.addAttachment("myAttachment", new ByteArrayResource("my_data".getBytes()), "text/plain");
try {
gateway.handleMessage(new GenericMessage<>(webServiceMessage));
fail("Expected MessageHandlingException");
} catch (MessageHandlingException e) {
// expected
}
WebServiceMessage requestMessage = requestFuture.get(10, TimeUnit.SECONDS);
assertNotNull(requestMessage);
assertThat(requestMessage, instanceOf(MimeMessage.class));
transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringResult stringResult = new StringResult();
transformer.transform(requestMessage.getPayloadSource(), stringResult);
assertEquals(request, stringResult.toString());
Attachment myAttachment = ((MimeMessage) requestMessage).getAttachment("myAttachment");
assertNotNull(myAttachment);
assertEquals("text/plain", myAttachment.getContentType());
assertEquals("my_data", StreamUtils.copyToString(myAttachment.getInputStream(), Charset.forName("UTF-8")));
}
use of org.springframework.xml.transform.StringSource in project spring-integration by spring-projects.
the class WebServiceInboundGatewayJavaConfigTests method testWebServiceInboundGatewayJavaConfig.
@Test
public void testWebServiceInboundGatewayJavaConfig() throws Exception {
MessageContext context = mock(MessageContext.class);
SoapMessage request = mock(SoapMessage.class);
SoapMessage response = mock(SoapMessage.class);
SoapBody soapBody = mock(SoapBody.class);
String input = "<hello/>";
Source payloadSource = new StringSource(input);
StringWriter output = new StringWriter();
Result payloadResult = new StreamResult(output);
when(context.getResponse()).thenReturn(response);
when(response.getPayloadResult()).thenReturn(payloadResult);
when(response.getSoapBody()).thenReturn(soapBody);
when(context.getRequest()).thenReturn(request);
when(request.getPayloadSource()).thenReturn(payloadSource);
this.messageReceiver.receive(context);
verify(soapBody).addServerOrReceiverFault(eq("503 Service Unavailable"), any(Locale.class));
this.wsGateway.start();
this.messageReceiver.receive(context);
assertTrue(output.toString().endsWith(input));
context = mock(MessageContext.class);
request = mock(SoapMessage.class);
payloadSource = new StringSource("<order/>");
when(context.getRequest()).thenReturn(request);
when(request.getPayloadSource()).thenReturn(payloadSource);
this.messageReceiver.receive(context);
Message<?> receive = this.webserviceRequestsQueue.receive(10000);
assertNotNull(receive);
assertThat(receive.getPayload(), instanceOf(Element.class));
Element order = (Element) receive.getPayload();
assertEquals("order", order.getLocalName());
}
use of org.springframework.xml.transform.StringSource in project spring-integration by spring-projects.
the class JaxbMarshallingIntegrationTests method testUnmarshalling.
@SuppressWarnings("unchecked")
@Test
public void testUnmarshalling() throws Exception {
StringSource source = new StringSource("<person><firstname>bob</firstname></person>");
unmarshallIn.send(new GenericMessage<Source>(source));
GenericMessage<Object> res = (GenericMessage<Object>) unmarshallOut.receive(2000);
assertNotNull("No response", res);
assertTrue("Not a Person ", res.getPayload() instanceof JaxbAnnotatedPerson);
JaxbAnnotatedPerson person = (JaxbAnnotatedPerson) res.getPayload();
assertEquals("Worng firstname", "bob", person.getFirstName());
}
use of org.springframework.xml.transform.StringSource in project spring-integration by spring-projects.
the class StringSourceTests method testWithDocument.
@Test
public void testWithDocument() throws Exception {
String docString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item>one</item>";
Document doc = XmlTestUtil.getDocumentForString(docString);
StringSource source = (StringSource) sourceFactory.createSource(doc);
BufferedReader reader = new BufferedReader(source.getReader());
String docAsString = reader.readLine();
assertXMLEqual("Wrong content in StringSource", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item>one</item>", docAsString);
}
use of org.springframework.xml.transform.StringSource in project spring-integration by spring-projects.
the class UnmarshallingTransformerTests method testStringSourceToString.
@Test
public void testStringSourceToString() {
Unmarshaller unmarshaller = new TestUnmarshaller(false);
UnmarshallingTransformer transformer = new UnmarshallingTransformer(unmarshaller);
Object transformed = transformer.transformPayload(new StringSource("world"));
assertEquals(String.class, transformed.getClass());
assertEquals("hello world", transformed.toString());
}
Aggregations