/** {@inheritDoc} */ @Override public StreamableRecordBindingData decompose( Exchange exchange, StreamableRecordBindingData target) throws Exception { Message sourceMessage = exchange.getMessage(); getContextMapper().mapTo(exchange.getContext(), target); final InputStream content = sourceMessage.getContent(InputStream.class); target.getRecord().read(content); return target; }
/** {@inheritDoc} */ @Override public Message compose(StreamableRecordBindingData source, Exchange exchange, boolean create) throws Exception { final org.switchyard.Message message = create ? exchange.createMessage() : exchange.getMessage(); getContextMapper().mapFrom(source, exchange.getContext(message)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); source.getRecord().write(baos); message.setContent(new ByteArrayInputStream(baos.toByteArray())); return message; }
@Test public void testEsquemaFamiliarCasoOk() throws IOException, SAXException { // Creo los mocks testKit.replaceService( "TipoGrupoFamiliar", new ExchangeHandler() { @Override public void handleMessage(Exchange exchange) throws HandlerException { String request = exchange.getMessage().getContent(String.class); LOG.info("getEsquemaFamiliar provider:" + request); try { // Verifico que el request que entra al mock sea el esperado XMLAssert.assertXMLEqual( CotizadorPlanesUnitTest.loadFile( "unitTest/providerMockExpectedRequest/getExquemaFamiliarProviderExpectedRequest.xml"), request); // Genero la respuesta mockeada String mockResponse = CotizadorPlanesUnitTest.loadFile( "unitTest/providerMockResponse/getExquemaFamiliarProviderResponse.xml"); LOG.info("mockResponse:" + mockResponse); // Envio la respuesta mockeada exchange.send(exchange.createMessage().setContent(mockResponse)); } catch (AssertionError a) { LOG.error("El request al provider no valida con el esperado", a); throw new HandlerException(a); } catch (Exception e) { LOG.error("Sucedió un error al obtener el archivo", e); throw new HandlerException(e); } } @Override public void handleFault(Exchange exchange) { throw new RuntimeException("Sucedio error inesperado"); } }); // Ejecuto el servicio Message response = getEsquemaFamiliar.sendInOut( CotizadorPlanesUnitTest.loadFile( "unitTest/serviceRequest/getEsquemaFamiliarServiceRequest.xml")); // Verifico los resultados XMLAssert.assertXMLEqual( CotizadorPlanesUnitTest.loadFile( "unitTest/serviceExpectedResponse/getExquemaFamiliarServiceExpectedResponse.xml"), response.getContent(String.class)); }
private org.switchyard.Exchange createMockExchangeWithBody(final Integer payload) { final org.switchyard.Exchange switchYardExchange = mock(org.switchyard.Exchange.class); final org.switchyard.Context switchYardContext = mock(org.switchyard.Context.class); final ExchangeContract exchangeContract = mock(ExchangeContract.class); final InvocationContract invocationContract = mock(InvocationContract.class); final Message message = mock(Message.class); when(message.getContent(Integer.class)).thenReturn(payload); when(switchYardExchange.getContext()).thenReturn(switchYardContext); when(switchYardExchange.getMessage()).thenReturn(message); when(invocationContract.getOutputType()).thenReturn(new QName("java:java.lang.String")); when(exchangeContract.getInvokerInvocationMetaData()).thenReturn(invocationContract); when(switchYardExchange.getContract()).thenReturn(exchangeContract); return switchYardExchange; }
/** {@inheritDoc} */ @Override public Message compose(RESTEasyBindingData source, Exchange exchange) throws Exception { final Message message = super.compose(source, exchange); if (message.getContent() instanceof BaseClientResponse) { BaseClientResponse<?> clientResponse = (BaseClientResponse<?>) message.getContent(); if (clientResponse.getResponseStatus() == Response.Status.NOT_FOUND) { throw new ItemNotFoundException("Item not found"); } } else if (source.getOperationName().equals("addItem") && (source.getParameters().length == 2)) { // Wrap the parameters Item item = new Item((Integer) source.getParameters()[0], (String) source.getParameters()[1]); message.setContent(item); } return message; }
@Test public void testRelatesToSetOnReply() { ServiceReference service = _domain.createInOutService(new QName("ReplyTest"), new MockHandler().forwardInToOut()); MockHandler replyHandler = new MockHandler(); Exchange exchange = service.createExchange(replyHandler); Message message = exchange.createMessage(); exchange.send(message); String requestId = message.getContext().getPropertyValue(Exchange.MESSAGE_ID); String replyId = exchange.getMessage().getContext().getPropertyValue(Exchange.MESSAGE_ID); String replyRelatesTo = exchange.getMessage().getContext().getPropertyValue(Exchange.RELATES_TO); Assert.assertEquals(requestId, replyRelatesTo); Assert.assertFalse(requestId.equals(replyId)); }
@Override public void handleMessage(Exchange exchange) throws HandlerException { if (exchange.getPattern().equals(ExchangePattern.IN_OUT)) { Message message; Element request = exchange.getMessage().getContent(Element.class); Element name = XMLHelper.getFirstChildElementByName(request, "arg0"); String toWhom = ""; if (name != null) { toWhom = name.getTextContent(); } String response = null; if (toWhom.length() == 0) { message = MessageBuilder.newInstance(FaultMessage.class).buildMessage(); response = "<soap:fault xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + " <faultcode>soap:Server.AppError</faultcode>" + " <faultstring>Invalid name</faultstring>" + " <detail>" + " <message>Looks like you did not specify a name!</message>" + " <errorcode>1000</errorcode>" + " </detail>" + "</soap:fault>"; } else { message = MessageBuilder.newInstance().buildMessage(); response = "<test:sayHelloResponse xmlns:test=\"http://test.ws/\">" + " <return>Hello " + toWhom + "</return>" + "</test:sayHelloResponse>"; } try { Document responseDom = SOAPUtil.parseAsDom(response); message.setContent(responseDom.getDocumentElement()); } catch (Exception e) { // Generate fault } exchange.send(message); } }
/** Make sure that the current message is set correctly when an exchange is sent. */ @Test public void testGetMessage() throws Exception { final QName serviceName = new QName("bleh"); final String inMsgContent = "in message"; final String outMsgContent = "out message"; // create a handler to test that the in and out content match // expected result from getMessage() ExchangeHandler provider = new BaseHandler() { public void handleMessage(Exchange exchange) { Assert.assertEquals(exchange.getMessage().getContent(), inMsgContent); Message outMsg = exchange.createMessage(); outMsg.setContent(outMsgContent); try { exchange.send(outMsg); } catch (Exception ex) { Assert.fail(ex.toString()); } } }; ExchangeHandler consumer = new BaseHandler() { public void handleMessage(Exchange exchange) { Assert.assertEquals(exchange.getMessage().getContent(), outMsgContent); } }; ServiceReference service = _domain.createInOutService(serviceName, provider); Exchange exchange = service.createExchange(consumer); Message inMsg = exchange.createMessage(); inMsg.setContent(inMsgContent); exchange.send(inMsg); }
/** * Create a Message from the given SOAP message. * * @param soapMessage the SOAP message to be converted * @param exchange The message Exchange. * @return a Message * @throws SOAPException If the SOAP message is not correct. */ public Message compose(final SOAPMessage soapMessage, final Exchange exchange) throws SOAPException { Message message = exchange.createMessage(); final SOAPBody soapBody = soapMessage.getSOAPBody(); if (soapBody == null) { throw new SOAPException("Missing SOAP body from request"); } final Iterator children = soapBody.getChildElements(); boolean found = false; try { while (children.hasNext()) { final Node node = (Node) children.next(); if (node instanceof SOAPElement) { if (found) { throw new SOAPException("Found multiple SOAPElements in SOAPBody"); } node.detachNode(); message.setContent(node); found = true; } } } catch (Exception ex) { if (ex instanceof SOAPException) { throw (SOAPException) ex; } throw new SOAPException(ex); } if (!found) { throw new SOAPException("Could not find SOAPElement in SOAPBody"); } return message; }
@Test public void httpGatewayReferenceTest() throws Exception { Message responseMsg = _consumerService.operation("sayHello").sendInOut("magesh"); Assert.assertEquals("magesh", responseMsg.getContent(String.class)); }
/** {@inheritDoc} */ @Override public void handleMessage(Exchange exchange) throws HandlerException { if (!ExchangePhase.IN.equals(exchange.getPhase())) { return; } Context context = exchange.getContext(); ServiceOperation serviceOperation = exchange.getContract().getServiceOperation(); ProcessActionModel processActionModel = _actionModels.get(serviceOperation.getName()); ProcessActionType processActionType = getProcessActionType(context, processActionModel); Message messageIn = exchange.getMessage(); Long processInstanceId = null; ProcessInstance processInstance = null; switch (processActionType) { case START_PROCESS: if (_processId != null) { Object messageContentIn = messageIn.getContent(); if (messageContentIn != null) { Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put(_messageContentInName, messageContentIn); processInstance = _ksession.startProcess(_processId, parameters); } else { processInstance = _ksession.startProcess(_processId); } processInstanceId = Long.valueOf(processInstance.getId()); } else { throwNullParameterException(processActionType, PROCESS_ID_VAR); } break; case SIGNAL_EVENT: String processEventType = getProcessEventType(context, processActionModel); Object processEvent = getProcessEvent(context, messageIn); processInstanceId = getProcessInstanceId(context); if (processInstanceId != null) { _ksession.signalEvent(processEventType, processEvent, processInstanceId.longValue()); } else { throwNullParameterException(processActionType, PROCESS_INSTANCE_ID_VAR); } break; case ABORT_PROCESS_INSTANCE: processInstanceId = getProcessInstanceId(context); if (processInstanceId != null) { _ksession.abortProcessInstance(processInstanceId.longValue()); } else { throwNullParameterException(processActionType, PROCESS_INSTANCE_ID_VAR); } break; } if (processInstanceId != null) { context.setProperty(PROCESS_INSTANCE_ID_VAR, processInstanceId, Scope.EXCHANGE); ExchangePattern exchangePattern = serviceOperation.getExchangePattern(); if (ExchangePattern.IN_OUT.equals(exchangePattern)) { if (processInstance == null) { processInstance = _ksession.getProcessInstance(processInstanceId.longValue()); } Message messageOut = exchange.createMessage(); Object messageContentOut = null; if (processInstance != null) { messageContentOut = ((WorkflowProcessInstance) processInstance).getVariable(_messageContentOutName); } if (messageContentOut != null) { messageOut.setContent(messageContentOut); } exchange.send(messageOut); } } }