

package org.camelcookbook.examples.testing.blueprint;

import java.util.Dictionary;

import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
import org.junit.Test;

/**
 * Test class that demonstrates overriding properties in a Blueprint environment.
 */
public class PropertyOverrideBlueprintTest extends CamelBlueprintTestSupport {

  @Produce(uri = "direct:in")
  private ProducerTemplate producerTemplate;

  @EndpointInject(uri = "mock:out")
  private MockEndpoint mockOut;

  @Override
  protected String getBlueprintDescriptor() {
    return "OSGI-INF/blueprint/simpleTransform-context.xml,"
         + "OSGI-INF/blueprint/simpleTransform-properties-context.xml";
  }

  @Override
  public boolean isUseAdviceWith() {
    return true;
  }

  @Override
  protected String useOverridePropertiesWithConfigAdmin(Dictionary props) throws Exception {
    props.put("transform.message", "Overridden");
    return "org.camelcookbook.testing";
  }

  @Test
  public void testPayloadIsTransformed() throws Exception {
    context.getRouteDefinition("blueprintPropertiesTest")
        .adviceWith(context, new AdviceWithRouteBuilder() {
          @Override
          public void configure() throws Exception {
            replaceFromWith("direct:in");

            interceptSendToEndpoint("log:*")
                .skipSendToOriginalEndpoint()
                .to("mock:out");
          }
        });
    context.start();

    mockOut.setExpectedMessageCount(1);
    mockOut.message(0).body().isEqualTo("Overridden: Cheese");

    producerTemplate.sendBody("Cheese");

    assertMockEndpointsSatisfied();
  }

  @Test
  public void testPayloadIsTransformedAgain() throws Exception {
    context.getRouteDefinition("blueprintPropertiesTest")
        .adviceWith(context, new AdviceWithRouteBuilder() {
          @Override
          public void configure() throws Exception {
            replaceFromWith("direct:in");

            interceptSendToEndpoint("log:*")
                .skipSendToOriginalEndpoint()
                .to("mock:out");
          }
        });
    context.start();

    mockOut.setExpectedMessageCount(1);
    mockOut.message(0).body().isEqualTo("Overridden: Foo");

    producerTemplate.sendBody("Foo");

    assertMockEndpointsSatisfied();
  }
}
