content: /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.camel.component.mina;
  
  import java.nio.charset.Charset;
  
  import org.apache.camel.Endpoint;
  import org.apache.camel.Exchange;
  import org.apache.camel.ExchangePattern;
  import org.apache.camel.Message;
  import org.apache.camel.Producer;
  import org.apache.camel.builder.RouteBuilder;
  import org.junit.jupiter.api.Test;
  
  import static org.junit.jupiter.api.Assertions.assertEquals;
  import static org.junit.jupiter.api.Assertions.assertNotNull;
  import static org.junit.jupiter.api.Assertions.assertNull;
  import static org.junit.jupiter.api.Assertions.assertTrue;
  
  /**
   * Unit test for the <tt>transferExchange=true</tt> option.
   */
  public class MinaVMTransferExchangeOptionTest extends BaseMinaTest {
  
      @Test
      public void testMinaTransferExchangeOptionWithoutException() throws Exception {
          Exchange exchange = sendExchange(false);
          assertExchange(exchange, false);
      }
  
      @Test
      public void testMinaTransferExchangeOptionWithException() throws Exception {
          Exchange exchange = sendExchange(true);
          assertExchange(exchange, true);
      }
  
      private Exchange sendExchange(boolean setException) throws Exception {
          Endpoint endpoint = context.getEndpoint(
                  String.format("mina:vm://localhost:%1$s?sync=true&encoding=UTF-8&transferExchange=true&objectCodecPattern=*",
                          getPort()));
          Exchange exchange = endpoint.createExchange();
  
          Message message = exchange.getIn();
          message.setBody("Hello!");
          message.setHeader("cheese", "feta");
          exchange.setProperty("ham", "old");
          exchange.setProperty("setException", setException);
  
          Producer producer = endpoint.createProducer();
          producer.start();
          producer.process(exchange);
  
          return exchange;
      }
  
      private void assertExchange(Exchange exchange, boolean hasException) {
          if (!hasException) {
              Message out = exchange.getMessage();
              assertNotNull(out);
              assertEquals("Goodbye!", out.getBody());
              assertEquals("cheddar", out.getHeader("cheese"));
          } else {
              Message fault = exchange.getMessage();
              assertNotNull(fault);
              assertNotNull(fault.getBody());
              assertTrue(fault.getBody() instanceof InterruptedException, "Should get the InterruptedException exception");
              assertEquals("nihao", fault.getHeader("hello"));
          }
  
          // in should stay the same
          Message in = exchange.getIn();
          assertNotNull(in);
          assertEquals("Hello!", in.getBody());
          assertEquals("feta", in.getHeader("cheese"));
          // however the shared properties have changed
          assertEquals("fresh", exchange.getProperty("salami"));
          assertNull(exchange.getProperty("Charset"));
      }
  
      @Override
      protected RouteBuilder createRouteBuilder() {
          return new RouteBuilder() {
  
              public void configure() {
                  fromF("mina:vm://localhost:%1$s?sync=true&encoding=UTF-8&transferExchange=true&objectCodecPattern=*", getPort())
                          .process(e -> {
                              assertNotNull(e.getIn().getBody());
                              assertNotNull(e.getIn().getHeaders());
                              assertNotNull(e.getProperties());
                              assertEquals("Hello!", e.getIn().getBody());
                              assertEquals("feta", e.getIn().getHeader("cheese"));
                              assertEquals("old", e.getProperty("ham"));
                              assertEquals(ExchangePattern.InOut, e.getPattern());
                              Boolean setException = (Boolean) e.getProperty("setException");
  
                              if (setException) {
                                  e.getOut().setBody(new InterruptedException());
                                  e.getOut().setHeader("hello", "nihao");
                              } else {
                                  e.getOut().setBody("Goodbye!");
                                  e.getOut().setHeader("cheese", "cheddar");
                              }
                              e.setProperty("salami", "fresh");
                              e.setProperty("Charset", Charset.defaultCharset());
                          });
              }
          };
      }
  }
  