/** * Emulate the Spring WS MessageDispatcherServlet by calling the gateway * with a DOMSource object representing the payload of the original SOAP * 'echoRequest' message. Expect an 'echoResponse' DOMSource object * to be returned in synchronous fashion, which the MessageDispatcherServlet * would in turn wrap in a SOAP envelope and return to the client. */ @Test public void testSendAndReceive() throws Exception { String xml = "<echoRequest xmlns=\"http://www.springframework.org/spring-ws/samples/echo\">hello</echoRequest>"; DomPoxMessageFactory messageFactory = new DomPoxMessageFactory(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(new InputSource(new StringReader(xml))); Transformer transformer = TransformerFactory.newInstance().newTransformer(); DomPoxMessage request = new DomPoxMessage(document, transformer, "text/xml"); MessageContext messageContext = new DefaultMessageContext(request, messageFactory); gateway.invoke(messageContext); Object reply = messageContext.getResponse().getPayloadSource(); assertThat(reply, is(instanceOf(DOMSource.class))); DOMSource replySource = (DOMSource) reply; Element element = (Element) replySource.getNode().getFirstChild(); assertThat(element.getTagName(), equalTo("echoResponse")); } }
@Override public void setResponse(WebServiceMessage response) { checkForResponse(); this.response = response; }
/** * Calls all interceptors and {@link RequestProcessor}s. Returns first generated response or first thrown exception. */ public WebServiceMessage receive(WebServiceMessageFactory messageFactory) throws IOException { DefaultMessageContext messageContext = new DefaultMessageContext(request, messageFactory); boolean callRequestProcessors = handleRequest(messageContext); if (callRequestProcessors) { WebServiceMessage response = generateResponse(messageFactory); messageContext.setResponse(response); } handleResponse(messageContext); return messageContext.getResponse(); }
/** * Creates validator based on the response. * @param response * @return */ public static ResponseActions validate(Object response) { DefaultMessageContext messageContext = new DefaultMessageContext(messageFactory); messageContext.setResponse(serialize(response)); return validate(messageContext); } /**
/** * Creates validator based on the response and request. Use this method if your {@link ResponseMatcher}s need to read data from request. * @param response * @param request * @return */ public static ResponseActions validate(Object response, Object request) { DefaultMessageContext messageContext = new DefaultMessageContext(serialize(request), messageFactory); messageContext.setResponse(serialize(response)); return validate(messageContext); }
/** * Creates {@link MessageContext}. * @param message * @return */ protected DefaultMessageContext createMessageContext(WebServiceMessage message) { return new DefaultMessageContext(message, messageFactory); }
@Override public void setResponse(WebServiceMessage response) { checkForResponse(); this.response = response; }
/** * Creates {@link MessageContext}. Can be overriden. * @param message * @return */ protected DefaultMessageContext createMessageContext(WebServiceMessage message) { return new DefaultMessageContext(message, messageFactory ); }
public void setResponse(WebServiceMessage response) { checkForResponse(); this.response = response; }
@Test public void testMessageHistoryWithMarshallingGateway() throws Exception { MessageContext context = new DefaultMessageContext(new StubMessageFactory()); Unmarshaller unmarshaller = mock(Unmarshaller.class); when(unmarshaller.unmarshal(Mockito.any())).thenReturn("hello"); marshallingGateway.setUnmarshaller(unmarshaller); marshallingGateway.invoke(context); Message<?> message = requestsMarshalling.receive(100); MessageHistory history = MessageHistory.read(message); assertNotNull(history); Properties componentHistoryRecord = TestUtils.locateComponentInHistory(history, "marshalling", 0); assertNotNull(componentHistoryRecord); assertEquals("ws:inbound-gateway", componentHistoryRecord.get("type")); }
@Override public void setResponse(WebServiceMessage response) { checkForResponse(); this.response = response; }
@Test public void testMessageHistoryWithSimpleGateway() throws Exception { MessageContext context = new DefaultMessageContext(new StubMessageFactory()); payloadExtractingGateway.invoke(context); Message<?> message = requestsSimple.receive(100); MessageHistory history = MessageHistory.read(message); assertNotNull(history); Properties componentHistoryRecord = TestUtils.locateComponentInHistory(history, "extractsPayload", 0); assertNotNull(componentHistoryRecord); assertEquals("ws:inbound-gateway", componentHistoryRecord.get("type")); }
@Override public void readResponse(InputStream inputStream) throws IOException { checkForResponse(); response = messageFactory.createWebServiceMessage(inputStream); }
/** * Sends a request message by using the given {@link RequestCreator}. Typically called by using the default request * creators provided by {@link RequestCreators}. * * @param requestCreator the request creator * @return the response actions * @see RequestCreators */ public ResponseActions sendRequest(RequestCreator requestCreator) { Assert.notNull(requestCreator, "'requestCreator' must not be null"); try { WebServiceMessage request = requestCreator.createRequest(messageFactory); MessageContext messageContext = new DefaultMessageContext(request, messageFactory); messageReceiver.receive(messageContext); return new MockWebServiceClientResponseActions(messageContext); } catch (Exception ex) { logger.error("Could not send request", ex); fail(ex.getMessage()); return null; } }
@Override public void readResponse(InputStream inputStream) throws IOException { checkForResponse(); response = messageFactory.createWebServiceMessage(inputStream); }
/** * Sends a request message by using the given {@link RequestCreator}. Typically called by using the default request * creators provided by {@link RequestCreators}. * * @param requestCreator the request creator * @return the response actions * @see RequestCreators */ public ResponseActions sendRequest(RequestCreator requestCreator) { Assert.notNull(requestCreator, "'requestCreator' must not be null"); try { WebServiceMessage request = requestCreator.createRequest(messageFactory); MessageContext messageContext = new DefaultMessageContext(request, messageFactory); messageReceiver.receive(messageContext); return new MockWebServiceClientResponseActions(messageContext); } catch (Exception ex) { logger.error("Could not send request", ex); fail(ex.getMessage()); return null; } }
@Override public void readResponse(InputStream inputStream) throws IOException { checkForResponse(); response = messageFactory.createWebServiceMessage(inputStream); }
@SuppressWarnings("unchecked") public <T> T deserialize(WebServiceMessage message, Class<T> targetClass) throws Exception { Assert.notNull(message, "message can not be null"); Assert.notNull(targetClass, "targetClass can not be null"); MethodParameter parameter = new SmockMethodParameter(targetClass,0); MessageContext messageContext = new DefaultMessageContext(message, messageFactory); for (MethodArgumentResolver resolver: adapter.getMethodArgumentResolvers()) { if (resolver.supportsParameter(parameter)){ Object result = resolver.resolveArgument(messageContext, parameter); if (result != null) { return (T) result; } } } return null; }
public void readResponse(InputStream inputStream) throws IOException { checkForResponse(); response = messageFactory.createWebServiceMessage(inputStream); }
/** * Sends a request message by using the given {@link RequestCreator}. Typically called by using the default request * creators provided by {@link RequestCreators}. * * @param requestCreator the request creator * @return the response actions * @see RequestCreators */ public ResponseActions sendRequest(RequestCreator requestCreator) { Assert.notNull(requestCreator, "'requestCreator' must not be null"); try { WebServiceMessage request = requestCreator.createRequest(messageFactory); MessageContext messageContext = new DefaultMessageContext(request, messageFactory); messageReceiver.receive(messageContext); return new MockWebServiceClientResponseActions(messageContext); } catch (Exception ex) { logger.error("Could not send request", ex); fail(ex.getMessage()); return null; } }