Java Code Examples for org.apache.camel.impl.DefaultCamelContext#createProducerTemplate()

The following examples show how to use org.apache.camel.impl.DefaultCamelContext#createProducerTemplate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleRegistryTest.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUp() throws Exception {
    // create the registry to be the SimpleRegistry which is just a Map based implementation
    SimpleRegistry registry = new SimpleRegistry();
    // register our HelloBean under the name helloBean
    registry.put("helloBean", new HelloBean());

    // tell Camel to use our SimpleRegistry
    context = new DefaultCamelContext(registry);

    // create a producer template to use for testing
    template = context.createProducerTemplate();

    // add the route using an inlined RouteBuilder
    context.addRoutes(new RouteBuilder() {
        public void configure() throws Exception {
            from("direct:hello").bean("helloBean", "hello");
        }
    });
    // star Camel
    context.start();
}
 
Example 2
Source File: DecanterEventNotifierTest.java    From karaf-decanter with Apache License 2.0 6 votes vote down vote up
@Test
public void testExtender() throws Exception {
    MockEventAdmin eventAdmin = new MockEventAdmin();
    DecanterEventNotifier notifier = new DecanterEventNotifier();
    notifier.setIgnoreCamelContextEvents(true);
    notifier.setIgnoreRouteEvents(true);
    notifier.setEventAdmin(eventAdmin);
    notifier.setExtender(new TestExtender());

    DefaultCamelContext camelContext = createCamelContext(notifier);

    ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
    producerTemplate.sendBodyAndHeader("direct:start", "TEST", "foo", "bar");

    Assert.assertEquals(6, eventAdmin.getPostEvents().size());

    Assert.assertEquals("test", eventAdmin.getPostEvents().get(0).getProperty("extender-test"));
}
 
Example 3
Source File: DecanterEventNotifierTest.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
@Test
public void testIgnoredEvents() throws Exception {
    MockEventAdmin eventAdmin = new MockEventAdmin();
    DecanterEventNotifier notifier = new DecanterEventNotifier();
    notifier.setEventAdmin(eventAdmin);
    notifier.setIgnoreCamelContextEvents(true);

    DefaultCamelContext camelContext = createCamelContext(notifier);

    ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
    producerTemplate.sendBodyAndHeader("direct:start", "TEST", "foo", "bar");

    Assert.assertEquals(8, eventAdmin.getPostEvents().size());
}
 
Example 4
Source File: SplitStepHandlerJsonTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
/**
 * Test split to the very end of the integration - no aggregate
 * direct -> split -> mock
 */
@Test
public void testSplitToEnd() throws Exception {
    final DefaultCamelContext context = new DefaultCamelContext();

    try {
        final RouteBuilder routes = new IntegrationRouteBuilder(
                "classpath:/syndesis/integration/split-to-end.json",
                Resources.loadServices(IntegrationStepHandler.class)
        );

        // Set up the camel context
        context.addRoutes(routes);
        context.start();

        // Dump routes as XML for troubleshooting
        dumpRoutes(context);

        final ProducerTemplate template = context.createProducerTemplate();
        final MockEndpoint result = context.getEndpoint("mock:expression", MockEndpoint.class);
        final String[] expected = { "a","b","c" };
        final List<String> body = Arrays.asList(expected);

        result.expectedMessageCount(3);
        result.expectedBodiesReceived((Object[])expected);

        template.sendBody("direct:expression", body);

        result.assertIsSatisfied();
    } finally {
        context.stop();
    }
}
 
Example 5
Source File: DecanterEventNotifierTest.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
@Test
public void testRouteIdFilter() throws Exception {
    MockEventAdmin eventAdmin = new MockEventAdmin();
    DecanterEventNotifier notifier = new DecanterEventNotifier();
    notifier.setEventAdmin(eventAdmin);
    notifier.setCamelContextMatcher(".*");
    notifier.setRouteMatcher("foo");

    DefaultCamelContext camelContext = createCamelContext(notifier);

    ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
    producerTemplate.sendBodyAndHeader("direct:start", "TEST", "foo", "bar");

    Assert.assertEquals(4, eventAdmin.getPostEvents().size());
}
 
Example 6
Source File: EtcdIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testEtcdComponent() throws Exception {
    DefaultCamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            String dockerHost = TestUtils.getDockerHost();

            from("direct:start")
            .toF("etcd:keys?uris=http://%s:23379,http://%s:40001", dockerHost, dockerHost)
            .to("mock:result");
        }
    });

    String path = "/camel/" + UUID.randomUUID().toString();
    String value = UUID.randomUUID().toString();

    Map<String, Object> headers = new HashMap<>();
    headers.put(EtcdConstants.ETCD_ACTION, EtcdConstants.ETCD_KEYS_ACTION_SET);
    headers.put(EtcdConstants.ETCD_PATH, path);

    MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    mockEndpoint.expectedMinimumMessageCount(1);
    mockEndpoint.expectedHeaderReceived(EtcdConstants.ETCD_PATH, path);

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeaders("direct:start", value, headers);
        mockEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 7
Source File: QuteTestBase.java    From camel-quarkus with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void beforeEach() throws Exception {
    context = new DefaultCamelContext();
    context.getShutdownStrategy().setTimeout(10);
    template = context.createProducerTemplate();
    template.start();

    context.addRoutes(createRouteBuilder());
    context.start();
}
 
Example 8
Source File: DecanterEventNotifierTest.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
@Test
public void testCamelContextFilter() throws Exception {
    MockEventAdmin eventAdmin = new MockEventAdmin();
    DecanterEventNotifier notifier = new DecanterEventNotifier();
    notifier.setEventAdmin(eventAdmin);
    notifier.setCamelContextMatcher("foo");

    DefaultCamelContext camelContext = createCamelContext(notifier);

    ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
    producerTemplate.sendBodyAndHeader("direct:start", "TEST", "foo", "bar");

    Assert.assertEquals(0, eventAdmin.getPostEvents().size());
}
 
Example 9
Source File: SplitStepHandlerJsonTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
/**
 * Test chains of split/aggregate where a 2nd split directly follows on the 1st aggregate.
 * direct -> split -> log -> aggregate -> split -> log -> aggregate -> mock
 */
@Test
public void testSplitAggregateChain() throws Exception {
    final DefaultCamelContext context = new DefaultCamelContext();

    try {
        final RouteBuilder routes = new IntegrationRouteBuilder(
                "classpath:/syndesis/integration/split-chain.json",
                Resources.loadServices(IntegrationStepHandler.class)
        );

        // Set up the camel context
        context.addRoutes(routes);
        addBodyLogger(context);
        context.start();

        // Dump routes as XML for troubleshooting
        dumpRoutes(context);

        final ProducerTemplate template = context.createProducerTemplate();
        final MockEndpoint result = context.getEndpoint("mock:expression", MockEndpoint.class);
        final List<String> body = Arrays.asList("a,b,c", "de", "f,g");

        result.expectedBodiesReceived(body);

        template.sendBody("direct:expression", body);

        result.assertIsSatisfied();
    } finally {
        context.stop();
    }
}
 
Example 10
Source File: DecanterEventNotifierTest.java    From karaf-decanter with Apache License 2.0 4 votes vote down vote up
@Test
public void testEventNotifier() throws Exception {
    MockEventAdmin eventAdmin = new MockEventAdmin();
    DecanterEventNotifier notifier = new DecanterEventNotifier();
    notifier.setEventAdmin(eventAdmin);

    DefaultCamelContext camelContext = createCamelContext(notifier);

    ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
    producerTemplate.sendBodyAndHeader("direct:start", "TEST", "foo", "bar");

    Assert.assertEquals(10, eventAdmin.getPostEvents().size());

    Event camelContextStartingEvent = eventAdmin.getPostEvents().get(0);
    Assert.assertEquals("test-context", camelContextStartingEvent.getProperty("camelContextName"));
    Assert.assertEquals(CamelContextStartingEvent.class.getName(), camelContextStartingEvent.getProperty("eventType"));
    Assert.assertEquals("camelEvent", camelContextStartingEvent.getProperty("type"));

    Event routeAddedEvent = eventAdmin.getPostEvents().get(1);
    Assert.assertEquals("test-context", routeAddedEvent.getProperty("camelContextName"));
    Assert.assertEquals("test-route", routeAddedEvent.getProperty("routeId"));
    Assert.assertEquals(RouteAddedEvent.class.getName(), routeAddedEvent.getProperty("eventType"));
    Assert.assertEquals("camelEvent", routeAddedEvent.getProperty("type"));

    Event routeStartedEvent = eventAdmin.getPostEvents().get(2);
    Assert.assertEquals("test-context", routeStartedEvent.getProperty("camelContextName"));
    Assert.assertEquals("test-route", routeStartedEvent.getProperty("routeId"));
    Assert.assertEquals(RouteStartedEvent.class.getName(), routeStartedEvent.getProperty("eventType"));
    Assert.assertEquals("camelEvent", routeStartedEvent.getProperty("type"));

    Event camelContextStartedEvent = eventAdmin.getPostEvents().get(3);
    Assert.assertEquals("test-context", camelContextStartedEvent.getProperty("camelContextName"));
    Assert.assertEquals(CamelContextStartedEvent.class.getName(), camelContextStartedEvent.getProperty("eventType"));
    Assert.assertEquals("camelEvent", camelContextStartedEvent.getProperty("type"));

    Event exchangeSendingEvent = eventAdmin.getPostEvents().get(4);
    Assert.assertEquals("test-context", exchangeSendingEvent.getProperty("camelContextName"));
    Assert.assertEquals(ExchangeSendingEvent.class.getName(), exchangeSendingEvent.getProperty("eventType"));
    Assert.assertEquals("camelEvent", exchangeSendingEvent.getProperty("type"));
    Assert.assertEquals("InOnly", exchangeSendingEvent.getProperty("exchangePattern"));
    Assert.assertEquals("direct://start", exchangeSendingEvent.getProperty("sendingToEndpointUri"));
    Assert.assertEquals("TEST", exchangeSendingEvent.getProperty("inBody"));

    Event exchangeCreatedEvent = eventAdmin.getPostEvents().get(5);
    Assert.assertEquals("test-context", exchangeCreatedEvent.getProperty("camelContextName"));
    Assert.assertEquals(ExchangeCreatedEvent.class.getName(), exchangeCreatedEvent.getProperty("eventType"));
    Assert.assertEquals("camelEvent", exchangeCreatedEvent.getProperty("type"));
    Assert.assertEquals("InOnly", exchangeCreatedEvent.getProperty("exchangePattern"));
    Assert.assertEquals("TEST", exchangeCreatedEvent.getProperty("inBody"));
}
 
Example 11
Source File: FilterStepHandlerTest.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Test
public void testRuleFilterStepWithJsonComplexPath() throws Exception {
    final DefaultCamelContext context = new DefaultCamelContext();

    try {
        final RouteBuilder routes = newIntegrationRouteBuilder(activityTracker,
            new Step.Builder()
                .id(START_STEP)
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .descriptor(new ConnectorDescriptor.Builder()
                        .componentScheme("direct")
                        .putConfiguredProperty("name", "start")
                        .build())
                    .build())
                .build(),
            new Step.Builder()
                .id(FILTER_STEP)
                .stepKind(StepKind.ruleFilter)
                .putConfiguredProperty("type", "rule")
                .putConfiguredProperty("predicate", "OR")
                .putConfiguredProperty("rules", "[{\"path\":\"user.name\",\"op\":\"==\",\"value\":\"James\"}, {\"path\":\"user.name\",\"op\":\"==\",\"value\":\"Roland\"}]")
                .build(),
            new Step.Builder()
                .id(MOCK_STEP)
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .descriptor(new ConnectorDescriptor.Builder()
                        .componentScheme("mock")
                        .putConfiguredProperty("name", "result")
                        .build())
                    .build())
                .build()
        );

        // Set up the camel context
        context.setUuidGenerator(KeyGenerator::createKey);
        context.addLogListener(new IntegrationLoggingListener(activityTracker));
        context.addInterceptStrategy(new ActivityTrackingInterceptStrategy(activityTracker));
        context.addRoutes(routes);
        context.start();

        // Dump routes as XML for troubleshooting
        dumpRoutes(context);

        final List<String> matchingMessages = Arrays.asList(buildUserJson("James"), buildUserJson("Roland"));
        final List<String> notMatchingMessages = Collections.singletonList(buildUserJson("Jimmi"));
        final ProducerTemplate template = context.createProducerTemplate();
        final MockEndpoint result = context.getEndpoint("mock:result", MockEndpoint.class);

        List<String> allMessages = new ArrayList<>();
        allMessages.addAll(matchingMessages);
        allMessages.addAll(notMatchingMessages);

        result.expectedBodiesReceived(matchingMessages);

        for (Object body : allMessages) {
            template.sendBody("direct:start", body);
        }

        result.assertIsSatisfied();

        verify(activityTracker, times(allMessages.size())).startTracking(any(Exchange.class));
        verifyActivityStepTracking(MOCK_STEP, matchingMessages.size());
        verifyActivityStepTracking(FILTER_STEP, allMessages.size());
        verify(activityTracker, times(allMessages.size())).finishTracking(any(Exchange.class));
    } finally {
        context.stop();
    }
}
 
Example 12
Source File: ChoiceStepHandlerTest.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Test
public void testChoiceStep() throws Exception {
    final DefaultCamelContext context = new DefaultCamelContext();

    try {
        final RouteBuilder integrationRoute = newIntegrationRouteBuilder(activityTracker,
            new Step.Builder()
                .id(START_STEP)
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .descriptor(new ConnectorDescriptor.Builder()
                        .componentScheme("direct")
                        .putConfiguredProperty("name", "flow")
                        .build())
                    .build())
                .build(),
            new Step.Builder()
                .id(CHOICE_STEP)
                .stepKind(StepKind.choice)
                .putConfiguredProperty("flows", "[" +
                                    "{\"condition\": \"${body} contains 'Hello'\", \"flow\": \"hello-flow\"}," +
                                    "{\"condition\": \"${body} contains 'Bye'\", \"flow\": \"bye-flow\"}" +
                                "]")
                .build(),
            new Step.Builder()
                .id(MOCK_STEP)
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .descriptor(new ConnectorDescriptor.Builder()
                        .componentScheme("mock")
                        .putConfiguredProperty("name", "result")
                        .build())
                    .build())
                .build()
        );

        final RouteBuilder helloRoute = new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:hello-flow")
                        .to("mock:hello");
            }
        };

        final RouteBuilder byeRoute = new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:bye-flow")
                        .to("mock:bye");
            }
        };

        // Set up the camel context
        context.setUuidGenerator(KeyGenerator::createKey);
        context.addLogListener(new IntegrationLoggingListener(activityTracker));
        context.addInterceptStrategy(new ActivityTrackingInterceptStrategy(activityTracker));
        context.addRoutes(helloRoute);
        context.addRoutes(byeRoute);
        context.addRoutes(integrationRoute);

        SimpleRegistry beanRegistry = new SimpleRegistry();
        beanRegistry.bind("bodyLogger", new BodyLogger.Default());
        context.setRegistry(beanRegistry);
        context.start();

        // Dump routes as XML for troubleshooting
        dumpRoutes(context);

        final ProducerTemplate template = context.createProducerTemplate();
        final MockEndpoint result = context.getEndpoint("mock:result", MockEndpoint.class);
        final MockEndpoint helloResult = context.getEndpoint("mock:hello", MockEndpoint.class);
        final MockEndpoint byeResult = context.getEndpoint("mock:bye", MockEndpoint.class);

        final List<String> messages = Arrays.asList("Hello Camel!", "Bye Camel!", "And Now for Something Completely Different");

        result.expectedBodiesReceived(messages);
        helloResult.expectedBodiesReceived("Hello Camel!");
        byeResult.expectedBodiesReceived("Bye Camel!");

        for (String message : messages) {
            template.sendBody("direct:flow", message);
        }

        result.assertIsSatisfied();
        helloResult.assertIsSatisfied();
        byeResult.assertIsSatisfied();

        verify(activityTracker, times(3)).startTracking(any(Exchange.class));
        verifyActivityStepTracking(CHOICE_STEP, 3);
        verifyActivityStepTracking(MOCK_STEP, 3);
        verify(activityTracker, times(3)).finishTracking(any(Exchange.class));
    } finally {
        context.stop();
    }
}
 
Example 13
Source File: FilterStepHandlerTest.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Test
public void testExpressionFilterStep() throws Exception {
    final DefaultCamelContext context = new DefaultCamelContext();

    try {
        final RouteBuilder routes = newIntegrationRouteBuilder(activityTracker,
                new Step.Builder()
                        .id(START_STEP)
                        .stepKind(StepKind.endpoint)
                        .action(new ConnectorAction.Builder()
                                .descriptor(new ConnectorDescriptor.Builder()
                                        .componentScheme("direct")
                                        .putConfiguredProperty("name", "start")
                                        .build())
                                .build())
                        .build(),
                new Step.Builder()
                        .id(FILTER_STEP)
                        .stepKind(StepKind.expressionFilter)
                        .putConfiguredProperty("filter", "${body.name} == 'James'")
                        .build(),
                new Step.Builder()
                        .id(MOCK_STEP)
                        .stepKind(StepKind.endpoint)
                        .action(new ConnectorAction.Builder()
                                .descriptor(new ConnectorDescriptor.Builder()
                                        .componentScheme("mock")
                                        .putConfiguredProperty("name", "result")
                                        .build())
                                .build())
                        .build()
        );

        // Set up the camel context
        context.setUuidGenerator(KeyGenerator::createKey);
        context.addLogListener(new IntegrationLoggingListener(activityTracker));
        context.addInterceptStrategy(new ActivityTrackingInterceptStrategy(activityTracker));
        context.addRoutes(routes);
        context.start();

        // Dump routes as XML for troubleshooting
        dumpRoutes(context);

        final List<String> matchingMessages = Collections.singletonList(buildPersonJson("James"));
        final List<String> notMatchingMessages = Collections.singletonList(buildPersonJson("Jimmi"));
        final ProducerTemplate template = context.createProducerTemplate();
        final MockEndpoint result = context.getEndpoint("mock:result", MockEndpoint.class);

        List<String> allMessages = new ArrayList<>();
        allMessages.addAll(matchingMessages);
        allMessages.addAll(notMatchingMessages);

        result.expectedBodiesReceived(matchingMessages);

        for (Object body : allMessages) {
            template.sendBody("direct:start", body);
        }

        result.assertIsSatisfied();

        verify(activityTracker, times(allMessages.size())).startTracking(any(Exchange.class));
        verifyActivityStepTracking(MOCK_STEP, matchingMessages.size());
        verifyActivityStepTracking(FILTER_STEP, allMessages.size());
        verify(activityTracker, times(allMessages.size())).finishTracking(any(Exchange.class));
    } finally {
        context.stop();
    }
}
 
Example 14
Source File: SplitStepHandlerJsonTest.java    From syndesis with Apache License 2.0 4 votes vote down vote up
/**
 * Test subsequent split/aggregate where a 1st split operates on a initial collection of elements and a
 * 2nd split operates on a completely new collection provided by some mock endpoint.
 * direct -> split -> log -> aggregate -> bean:myMock -> split -> log -> aggregate -> mock
 */
@Test
public void testSubsequentSplitAggregate() throws Exception {
    final DefaultCamelContext context = new DefaultCamelContext();

    try {
        final RouteBuilder routes = new IntegrationRouteBuilder(
                "classpath:/syndesis/integration/subsequent-split.json",
                Resources.loadServices(IntegrationStepHandler.class)
        );

        // Set up the camel context
        context.addRoutes(routes);

        SimpleRegistry beanRegistry = new SimpleRegistry();
        beanRegistry.bind("bodyLogger", new BodyLogger.Default());
        beanRegistry.bind("myMock", (Processor) exchange -> exchange.getIn().setBody(Arrays.asList("d", "e", "f")));
        context.setRegistry(beanRegistry);

        context.start();

        // Dump routes as XML for troubleshooting
        dumpRoutes(context);

        final ProducerTemplate template = context.createProducerTemplate();
        final MockEndpoint result = context.getEndpoint("mock:expression", MockEndpoint.class);
        final List<String> body = Arrays.asList("a", "b", "c");

        result.expectedMessageCount(1);

        template.sendBody("direct:expression", body);

        result.assertIsSatisfied();
        List<?> bodyReceived = result.getExchanges().get(0).getIn().getBody(List.class);
        assertThat(bodyReceived).hasSize(3);
        assertThat(bodyReceived.get(0)).isEqualTo("d");
        assertThat(bodyReceived.get(1)).isEqualTo("e");
        assertThat(bodyReceived.get(2)).isEqualTo("f");
    } finally {
        context.stop();
    }
}
 
Example 15
Source File: SplitStepHandlerTest.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Test
public void testSplitBody() throws Exception {
    final DefaultCamelContext context = new DefaultCamelContext();

    try {
        final RouteBuilder routes = newIntegrationRouteBuilder(activityTracker,
            new Step.Builder()
                .id(START_STEP)
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .descriptor(new ConnectorDescriptor.Builder()
                        .componentScheme("direct")
                        .putConfiguredProperty("name", "expression")
                        .build())
                    .build())
                .build(),
            new Step.Builder()
                .id(SPLIT_STEP)
                .stepKind(StepKind.split)
                .build(),
            new Step.Builder()
                .id(MOCK_STEP)
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .descriptor(new ConnectorDescriptor.Builder()
                        .componentScheme("mock")
                        .putConfiguredProperty("name", "expression")
                        .build())
                    .build())
                .build()
        );

        // Set up the camel context
        context.setUuidGenerator(KeyGenerator::createKey);
        context.addLogListener(new IntegrationLoggingListener(activityTracker));
        context.addInterceptStrategy(new ActivityTrackingInterceptStrategy(activityTracker));
        context.addRoutes(routes);
        context.start();

        // Dump routes as XML for troubleshooting
        dumpRoutes(context);

        final ProducerTemplate template = context.createProducerTemplate();
        final MockEndpoint result = context.getEndpoint("mock:expression", MockEndpoint.class);
        final List<String> body = Arrays.asList("a","b","c");

        result.expectedMessageCount(3);
        result.expectedBodiesReceived(body);

        template.sendBody("direct:expression", body);

        result.assertIsSatisfied();

        verify(activityTracker).startTracking(any(Exchange.class));
        verifyActivityStepTracking(SPLIT_STEP, 0);
        verifyActivityStepTracking(MOCK_STEP, 3);
        verify(activityTracker).finishTracking(any(Exchange.class));
    } finally {
        context.stop();
    }
}
 
Example 16
Source File: SplitStepHandlerTest.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Test
public void testTokenizeSplitStep() throws Exception {
    final DefaultCamelContext context = new DefaultCamelContext();

    try {
        final RouteBuilder routes = newIntegrationRouteBuilder(activityTracker,
            new Step.Builder()
                .id(START_STEP)
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .descriptor(new ConnectorDescriptor.Builder()
                        .componentScheme("direct")
                        .putConfiguredProperty("name", "expression")
                        .build())
                    .build())
                .build(),
            new Step.Builder()
                .id(SPLIT_STEP)
                .stepKind(StepKind.split)
                .putConfiguredProperty("language", "tokenize")
                .putConfiguredProperty("expression", "|")
                .build(),
            new Step.Builder()
                .id(MOCK_STEP)
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .descriptor(new ConnectorDescriptor.Builder()
                        .componentScheme("mock")
                        .putConfiguredProperty("name", "expression")
                        .build())
                    .build())
                .build()
        );

        // Set up the camel context
        context.setUuidGenerator(KeyGenerator::createKey);
        context.addLogListener(new IntegrationLoggingListener(activityTracker));
        context.addInterceptStrategy(new ActivityTrackingInterceptStrategy(activityTracker));
        context.addRoutes(routes);
        context.start();

        // Dump routes as XML for troubleshooting
        dumpRoutes(context);

        final ProducerTemplate template = context.createProducerTemplate();
        final MockEndpoint result = context.getEndpoint("mock:expression", MockEndpoint.class);
        final String body = "a|b|c";

        result.expectedMessageCount(3);
        result.expectedBodiesReceived((Object[])body.split("|"));

        template.sendBody("direct:expression", body);

        result.assertIsSatisfied();

        verify(activityTracker).startTracking(any(Exchange.class));
        verifyActivityStepTracking(SPLIT_STEP, 0);
        verifyActivityStepTracking(MOCK_STEP, 5);
        verify(activityTracker).finishTracking(any(Exchange.class));
    } finally {
        context.stop();
    }
}
 
Example 17
Source File: SimpleEndpointStepHandlerTest.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Test
public void testSimpleEndpointStepWithSplitAggregate() throws Exception {
    final DefaultCamelContext context = getDefaultCamelContextWithMyBeanInRegistry();

    try {
        final RouteBuilder routes = newIntegrationRouteBuilder(activityTracker,
            new Step.Builder()
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .descriptor(new ConnectorDescriptor.Builder()
                        .componentScheme("direct")
                        .putConfiguredProperty("name", "start")
                        .build())
                    .build())
                .build(),
            new Step.Builder()
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .descriptor(new ConnectorDescriptor.Builder()
                        .componentScheme("bean")
                        .putConfiguredProperty("beanName", "myBean")
                        .putConfiguredProperty("method", "myProcessor")
                        .build())
                    .build())
                .build(),
            new Step.Builder()
                .stepKind(StepKind.split)
                .putConfiguredProperty("language", "tokenize")
                .putConfiguredProperty("expression", "|")
                .build(),
            new Step.Builder()
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .descriptor(new ConnectorDescriptor.Builder()
                        .componentScheme("mock")
                        .putConfiguredProperty("name", "result")
                        .build())
                    .build())
                .build(),
            new Step.Builder()
                .stepKind(StepKind.aggregate)
                .build()
        );

        // Set up the camel context
        context.setUuidGenerator(KeyGenerator::createKey);
        context.addLogListener(new IntegrationLoggingListener(activityTracker));
        context.addInterceptStrategy(new ActivityTrackingInterceptStrategy(activityTracker));
        context.addRoutes(routes);
        context.start();

        // Dump routes as XML for troubleshooting
        dumpRoutes(context);

        final ProducerTemplate template = context.createProducerTemplate();
        final MockEndpoint result = context.getEndpoint("mock:result", MockEndpoint.class);
        final String body = "a|b|c";
        final String[] expected = body.toUpperCase().split("|");

        result.expectedMessageCount(3);
        result.expectedBodiesReceived((Object[])expected);
        template.sendBody("direct:start", body);

        result.assertIsSatisfied();

        verify(activityTracker).startTracking(any(Exchange.class));
        verify(activityTracker, times(7)).track(eq("exchange"), anyString(), eq("step"), anyString(), eq("id"), anyString(), eq("duration"), anyLong(), eq("failure"), isNull());
        verify(activityTracker).finishTracking(any(Exchange.class));
    } finally {
        context.stop();
    }
}
 
Example 18
Source File: HeadersStepHandlerTest.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Test
public void testRemoveHeadersStepHandler() throws Exception {
    final DefaultCamelContext context = new DefaultCamelContext();

    try {
        final RouteBuilder routes = newIntegrationRouteBuilder(
            new Step.Builder()
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .id(KeyGenerator.createKey())
                    .descriptor(new ConnectorDescriptor.Builder()
                        .connectorId("new")
                        .componentScheme("direct")
                        .putConfiguredProperty("name", "start")
                        .build())
                    .build())
                .build(),
            new Step.Builder()
                .stepKind(StepKind.headers)
                .putConfiguredProperty("action", "set")
                .putConfiguredProperty("MyHeader1", "1")
                .putConfiguredProperty("MyHeader2", "2")
                .build(),
            new Step.Builder()
                .stepKind(StepKind.headers)
                .putConfiguredProperty("action", "remove")
                .putConfiguredProperty("MyHeader1", "")
                .build(),
            new Step.Builder()
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .descriptor(new ConnectorDescriptor.Builder()
                        .componentScheme("mock")
                        .putConfiguredProperty("name", "result")
                        .build())
                    .build())
                .build()
        );

        // Set up the camel context
        context.addRoutes(routes);
        context.start();

        // Dump routes as XML for troubleshooting
        dumpRoutes(context);

        final ProducerTemplate template = context.createProducerTemplate();
        final MockEndpoint result = context.getEndpoint("mock:result", MockEndpoint.class);

        result.expectedMessageCount(1);
        result.expectedMessagesMatches(e -> !e.getIn().getHeaders().containsKey("Myheader1"));
        result.expectedMessagesMatches(e -> e.getIn().getHeaders().containsKey("Myheader2"));

        template.sendBody("direct:start", "");

        result.assertIsSatisfied();
    } finally {
        context.stop();
    }
}
 
Example 19
Source File: CamelCollectorTest.java    From karaf-decanter with Apache License 2.0 4 votes vote down vote up
@Test
public void testTracer() throws Exception {
    // install decanter
    System.out.println(executeCommand("feature:repo-add decanter " + System.getProperty("decanter.version")));
    System.out.println(executeCommand("feature:install decanter-collector-camel", new RolePrincipal("admin")));

    // add a event handler
    List<Event> received = new ArrayList();
    EventHandler eventHandler = new EventHandler() {
        @Override
        public void handleEvent(Event event) {
            received.add(event);
        }
    };
    Hashtable serviceProperties = new Hashtable();
    serviceProperties.put(EventConstants.EVENT_TOPIC, "decanter/collect/*");
    bundleContext.registerService(EventHandler.class, eventHandler, serviceProperties);

    // create route with tracer
    EventAdmin eventAdmin = getOsgiService(EventAdmin.class);
    DecanterTraceEventHandler handler = new DecanterTraceEventHandler();
    handler.setEventAdmin(eventAdmin);

    RouteBuilder routeBuilder = new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:test").routeId("route-test").to("log:foo");
        }
    };
    DefaultCamelContext camelContext = new DefaultCamelContext();
    camelContext.setName("context-test");
    camelContext.addRoutes(routeBuilder);
    Tracer tracer = new Tracer();
    tracer.setEnabled(true);
    tracer.setTraceOutExchanges(true);
    tracer.setLogLevel(LoggingLevel.OFF);
    tracer.addTraceHandler(handler);
    camelContext.setTracing(true);
    camelContext.setDefaultTracer(tracer);
    camelContext.start();

    // send a exchange into the route
    ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
    producerTemplate.sendBodyAndHeader("direct:test", "This is a test", "testHeader", "testValue");

    // TODO two events ?
    Assert.assertEquals(2, received.size());

    Assert.assertEquals("decanter/collect/camel/tracer", received.get(0).getTopic());
    Assert.assertEquals("context-test", received.get(0).getProperty("camelContextName"));
    Assert.assertEquals("InOnly", received.get(0).getProperty("exchangePattern"));
    Assert.assertEquals("camelTracer", received.get(0).getProperty("type"));
    Assert.assertEquals("log://foo", received.get(0).getProperty("toNode"));
    Assert.assertEquals("route-test", received.get(0).getProperty("routeId"));
    Assert.assertEquals("direct://test", received.get(0).getProperty("fromEndpointUri"));
    Assert.assertEquals("root", received.get(0).getProperty("karafName"));
    Assert.assertEquals("This is a test", received.get(0).getProperty("inBody"));
    Assert.assertEquals("String", received.get(0).getProperty("inBodyType"));
}
 
Example 20
Source File: LogsAndErrorsTest.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Test
public void testRoute() throws Exception {
    final DefaultCamelContext context = new DefaultCamelContext();

    try {
        final RouteBuilder routes = newIntegrationRouteBuilder(
            new io.syndesis.common.model.integration.Step.Builder()
                .id("s1")
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .descriptor(new ConnectorDescriptor.Builder()
                        .componentScheme("direct")
                        .putConfiguredProperty("name", "expression")
                        .build())
                    .build())
                .build(),
            new io.syndesis.common.model.integration.Step.Builder()
                .id("s2")
                .stepKind(StepKind.extension)
                .action(new StepAction.Builder()
                    .descriptor(new StepDescriptor.Builder()
                        .kind(StepAction.Kind.STEP)
                        .entrypoint(LogExtension.class.getName())
                        .build())
                    .build())
                .build(),
            new io.syndesis.common.model.integration.Step.Builder()
                .id("s3")
                .stepKind(StepKind.extension)
                .action(new StepAction.Builder()
                    .descriptor(new StepDescriptor.Builder()
                        .kind(StepAction.Kind.STEP)
                        .entrypoint(ErrorExtension.class.getName())
                        .build())
                    .build())
                .build(),
            new io.syndesis.common.model.integration.Step.Builder()
                .id("s4")
                .stepKind(StepKind.endpoint)
                .action(new ConnectorAction.Builder()
                    .descriptor(new ConnectorDescriptor.Builder()
                        .componentScheme("mock")
                        .putConfiguredProperty("name", "expression")
                        .build())
                    .build())
                .build()
        );

        // Set up the camel context
        context.addRoutes(routes);
        context.start();

        // Dump routes as XML for troubleshooting
        dumpRoutes(context);

        final ProducerTemplate template = context.createProducerTemplate();
        final MockEndpoint result = context.getEndpoint("mock:expression", MockEndpoint.class);

        result.expectedBodiesReceived("1","3");

        Exchange e1 = template.request("direct:expression", e -> e.getIn().setBody("1"));
        assertThat(e1.isFailed()).isFalse();

        Exchange e2 = template.request("direct:expression", e -> e.getIn().setBody("2"));
        assertThat(e2.isFailed()).isTrue();

        Exchange e3 = template.request("direct:expression", e -> e.getIn().setBody("3"));
        assertThat(e3.isFailed()).isFalse();

        result.assertIsSatisfied();

    } finally {
        context.stop();
    }
}