Search in sources :

Example 1 with InterceptSendToMockEndpointStrategy

use of org.apache.camel.impl.InterceptSendToMockEndpointStrategy in project camel by apache.

the class CamelTestSupport method doSetUp.

private void doSetUp() throws Exception {
    log.debug("setUp test");
    if (!useJmx()) {
        disableJMX();
    } else {
        enableJMX();
    }
    CamelContext c2 = createCamelContext();
    if (c2 instanceof ModelCamelContext) {
        context = (ModelCamelContext) c2;
    } else {
        throw new Exception("Context must be a ModelCamelContext");
    }
    threadCamelContext.set(context);
    assertNotNull(context, "No context found!");
    // reduce default shutdown timeout to avoid waiting for 300 seconds
    context.getShutdownStrategy().setTimeout(getShutdownTimeout());
    // set debugger if enabled
    if (isUseDebugger()) {
        context.setDebugger(new DefaultDebugger());
        context.getDebugger().addBreakpoint(breakpoint);
    // note: when stopping CamelContext it will automatic remove the breakpoint
    }
    template = context.createProducerTemplate();
    template.start();
    consumer = context.createConsumerTemplate();
    consumer.start();
    threadTemplate.set(template);
    threadConsumer.set(consumer);
    // enable auto mocking if enabled
    String pattern = isMockEndpoints();
    if (pattern != null) {
        context.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(pattern));
    }
    pattern = isMockEndpointsAndSkip();
    if (pattern != null) {
        context.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(pattern, true));
    }
    // configure properties component (mandatory for testing)
    context.getComponent("properties", PropertiesComponent.class);
    postProcessTest();
    if (isUseRouteBuilder()) {
        RoutesBuilder[] builders = createRouteBuilders();
        for (RoutesBuilder builder : builders) {
            log.debug("Using created route builder: " + builder);
            context.addRoutes(builder);
        }
        boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
        if (skip) {
            log.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true.");
        } else if (isUseAdviceWith()) {
            log.info("Skipping starting CamelContext as isUseAdviceWith is set to true.");
        } else {
            startCamelContext();
        }
    } else {
        log.debug("Using route builder from the created context: " + context);
    }
    log.debug("Routing Rules are: " + context.getRoutes());
    assertValidContext(context);
    INIT.set(true);
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) ModelCamelContext(org.apache.camel.model.ModelCamelContext) DefaultDebugger(org.apache.camel.impl.DefaultDebugger) InterceptSendToMockEndpointStrategy(org.apache.camel.impl.InterceptSendToMockEndpointStrategy) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) RoutesBuilder(org.apache.camel.RoutesBuilder) ModelCamelContext(org.apache.camel.model.ModelCamelContext)

Example 2 with InterceptSendToMockEndpointStrategy

use of org.apache.camel.impl.InterceptSendToMockEndpointStrategy in project camel by apache.

the class CamelSpringTestContextLoader method handleMockEndpointsAndSkip.

/**
     * Handles auto-intercepting of endpoints with mocks based on {@link MockEndpointsAndSkip} and skipping the
     * original endpoint.
     *
     * @param context the initialized Spring context
     * @param testClass the test class being executed
     */
protected void handleMockEndpointsAndSkip(GenericApplicationContext context, Class<?> testClass) throws Exception {
    if (testClass.isAnnotationPresent(MockEndpointsAndSkip.class)) {
        final String mockEndpoints = testClass.getAnnotation(MockEndpointsAndSkip.class).value();
        CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {

            @Override
            public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
                // resovle the property place holders of the mockEndpoints 
                String mockEndpointsValue = camelContext.resolvePropertyPlaceholders(mockEndpoints);
                LOG.info("Enabling auto mocking and skipping of endpoints matching pattern [{}] on CamelContext with name [{}].", mockEndpointsValue, contextName);
                camelContext.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(mockEndpointsValue, true));
            }
        });
    }
}
Also used : SpringCamelContext(org.apache.camel.spring.SpringCamelContext) DoToSpringCamelContextsStrategy(org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy) InterceptSendToMockEndpointStrategy(org.apache.camel.impl.InterceptSendToMockEndpointStrategy)

Example 3 with InterceptSendToMockEndpointStrategy

use of org.apache.camel.impl.InterceptSendToMockEndpointStrategy in project camel by apache.

the class CamelSpringTestContextLoader method handleMockEndpoints.

/**
     * Handles auto-intercepting of endpoints with mocks based on {@link MockEndpoints}.
     *
     * @param context the initialized Spring context
     * @param testClass the test class being executed
     */
protected void handleMockEndpoints(GenericApplicationContext context, Class<?> testClass) throws Exception {
    if (testClass.isAnnotationPresent(MockEndpoints.class)) {
        final String mockEndpoints = testClass.getAnnotation(MockEndpoints.class).value();
        CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {

            @Override
            public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
                LOG.info("Enabling auto mocking of endpoints matching pattern [{}] on CamelContext with name [{}].", mockEndpoints, contextName);
                camelContext.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(mockEndpoints));
            }
        });
    }
}
Also used : SpringCamelContext(org.apache.camel.spring.SpringCamelContext) DoToSpringCamelContextsStrategy(org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy) InterceptSendToMockEndpointStrategy(org.apache.camel.impl.InterceptSendToMockEndpointStrategy)

Example 4 with InterceptSendToMockEndpointStrategy

use of org.apache.camel.impl.InterceptSendToMockEndpointStrategy in project camel by apache.

the class CamelTestSupport method doSetUp.

private void doSetUp() throws Exception {
    log.debug("setUp test");
    // jmx is enabled if we have configured to use it, or if dump route coverage is enabled (it requires JMX)
    boolean jmx = useJmx() || isDumpRouteCoverage();
    if (jmx) {
        enableJMX();
    } else {
        disableJMX();
    }
    context = (ModelCamelContext) createCamelContext();
    threadCamelContext.set(context);
    assertNotNull("No context found!", context);
    // reduce default shutdown timeout to avoid waiting for 300 seconds
    context.getShutdownStrategy().setTimeout(getShutdownTimeout());
    // set debugger if enabled
    if (isUseDebugger()) {
        if (context.getStatus().equals(ServiceStatus.Started)) {
            log.info("Cannot setting the Debugger to the starting CamelContext, stop the CamelContext now.");
            // we need to stop the context first to setup the debugger
            context.stop();
        }
        context.setDebugger(new DefaultDebugger());
        context.getDebugger().addBreakpoint(breakpoint);
    // note: when stopping CamelContext it will automatic remove the breakpoint
    }
    template = context.createProducerTemplate();
    template.start();
    fluentTemplate = context.createFluentProducerTemplate();
    fluentTemplate.start();
    consumer = context.createConsumerTemplate();
    consumer.start();
    threadTemplate.set(template);
    threadFluentTemplate.set(fluentTemplate);
    threadConsumer.set(consumer);
    // enable auto mocking if enabled
    String pattern = isMockEndpoints();
    if (pattern != null) {
        context.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(pattern));
    }
    pattern = isMockEndpointsAndSkip();
    if (pattern != null) {
        context.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(pattern, true));
    }
    // configure properties component (mandatory for testing)
    PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
    Properties extra = useOverridePropertiesWithPropertiesComponent();
    if (extra != null && !extra.isEmpty()) {
        pc.setOverrideProperties(extra);
    }
    Boolean ignore = ignoreMissingLocationWithPropertiesComponent();
    if (ignore != null) {
        pc.setIgnoreMissingLocation(ignore);
    }
    postProcessTest();
    if (isUseRouteBuilder()) {
        RoutesBuilder[] builders = createRouteBuilders();
        for (RoutesBuilder builder : builders) {
            log.debug("Using created route builder: " + builder);
            context.addRoutes(builder);
        }
        replaceFromEndpoints();
        boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
        if (skip) {
            log.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true.");
        } else if (isUseAdviceWith()) {
            log.info("Skipping starting CamelContext as isUseAdviceWith is set to true.");
        } else {
            startCamelContext();
        }
    } else {
        replaceFromEndpoints();
        log.debug("Using route builder from the created context: " + context);
    }
    log.debug("Routing Rules are: " + context.getRoutes());
    assertValidContext(context);
    INIT.set(true);
}
Also used : DefaultDebugger(org.apache.camel.impl.DefaultDebugger) InterceptSendToMockEndpointStrategy(org.apache.camel.impl.InterceptSendToMockEndpointStrategy) Properties(java.util.Properties) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) RoutesBuilder(org.apache.camel.RoutesBuilder)

Aggregations

InterceptSendToMockEndpointStrategy (org.apache.camel.impl.InterceptSendToMockEndpointStrategy)4 RoutesBuilder (org.apache.camel.RoutesBuilder)2 DefaultDebugger (org.apache.camel.impl.DefaultDebugger)2 SpringCamelContext (org.apache.camel.spring.SpringCamelContext)2 DoToSpringCamelContextsStrategy (org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy)2 Properties (java.util.Properties)1 CamelContext (org.apache.camel.CamelContext)1 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)1 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 ModelCamelContext (org.apache.camel.model.ModelCamelContext)1