@Override
  public MuleEvent process(MuleEvent event) throws MuleException {
    try {
      String address2Use = expressionManager.parse(addressExpression, event.getMessage());

      ClientMessage msg = writeBodyToMessage(event.getMessage().getPayload(), clientSession);

      if (StringUtils.isNotBlank(event.getMessage().getCorrelationId())) {
        msg.putStringProperty(JMS_CORRELATION_ID, event.getMessage().getCorrelationId());
      }

      for (Map.Entry<String, String> e : headerExpressions.entrySet()) {
        Object value2Use = expressionManager.evaluate(e.getValue(), event.getMessage());
        msg.putObjectProperty(e.getKey(), value2Use);
      }

      for (String prop : event.getMessage().getOutboundPropertyNames()) {
        msg.putObjectProperty(prop, event.getMessage().getOutboundProperty(prop));
      }

      producer.send(address2Use, msg);

      Map<String, Object> props = new HashMap<String, Object>();
      props.put("hornetq.address", address2Use);

      event.getMessage().addProperties(props, PropertyScope.INBOUND);

      return event;

    } catch (Exception e) {
      throw new MessagingException(event, e);
    }
  }
  @Test(expected = QueryResolutionException.class)
  public void throwsErrorOnExpressionEvaluationError() throws Exception {
    ExpressionManager expressionManager = mock(ExpressionManager.class);
    when(expressionManager.parse(DYNAMIC_SQL_TEXT, muleEvent))
        .thenThrow(new ExpressionRuntimeException(CoreMessages.createStaticMessage("Error")));

    DynamicQueryResolver queryResolver = new DynamicQueryResolver(query, null, expressionManager);

    queryResolver.resolve(null, muleEvent);
  }
  @Test(expected = QueryResolutionException.class)
  public void throwsErrorOnParsingError() throws Exception {
    ExpressionManager expressionManager = mock(ExpressionManager.class);
    String staticSqlText = STATIC_SQL_TEXT;
    when(expressionManager.parse(DYNAMIC_SQL_TEXT, muleEvent)).thenReturn(staticSqlText);

    QueryTemplateParser queryTemplateParser = mock(QueryTemplateParser.class);
    when(queryTemplateParser.parse(staticSqlText))
        .thenThrow(new QueryTemplateParsingException("Parse error"));

    DynamicQueryResolver queryResolver =
        new DynamicQueryResolver(query, queryTemplateParser, expressionManager);

    queryResolver.resolve(null, muleEvent);
  }
  @Test
  public void resolvesDynamicQuery() throws Exception {
    ExpressionManager expressionManager = mock(ExpressionManager.class);
    String staticSqlText = STATIC_SQL_TEXT;
    when(expressionManager.parse(DYNAMIC_SQL_TEXT, muleEvent)).thenReturn(staticSqlText);

    QueryTemplate expectedQueryTemplate = createQueryTemplate(staticSqlText);
    QueryTemplateParser queryTemplateParser = mock(QueryTemplateParser.class);
    when(queryTemplateParser.parse(staticSqlText)).thenReturn(expectedQueryTemplate);

    DynamicQueryResolver queryResolver =
        new DynamicQueryResolver(query, queryTemplateParser, expressionManager);

    Query resolvedQuery = queryResolver.resolve(null, muleEvent);

    assertThat(expectedQueryTemplate, sameInstance(resolvedQuery.getQueryTemplate()));
  }