/** {@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;
 }
  /** {@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;
  }
  @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;
  }
Exemple #5
0
 @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);
     }
   }
 }