org.apache.camel.model.ChoiceDefinition Java Examples

The following examples show how to use org.apache.camel.model.ChoiceDefinition. 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: ChoiceStepParser.java    From camel-k-runtime with Apache License 2.0 5 votes vote down vote up
@Override
public ProcessorDefinition<?> toProcessor(Context context) {
    final Definition definition = context.node(Definition.class);
    final ChoiceDefinition choice = new ChoiceDefinition();

    StepParserSupport.notNull(definition.when, "when");

    for (Definition.When whenDefinition : definition.when) {
        StepParserSupport.notNull(whenDefinition.getExpression(), "when.expression");
        StepParserSupport.notNull(whenDefinition.steps, "when.steps");

        StepParserSupport.convertSteps(
            context,
            choice.when().expression(whenDefinition.getExpression()),
            whenDefinition.steps
        );
    }

    if (definition.otherwise != null) {
        StepParserSupport.notNull(definition.otherwise.steps, "otherwise.steps");

        StepParserSupport.convertSteps(
            context,
            choice.otherwise(),
            definition.otherwise.steps);
    }

    return choice;
}
 
Example #2
Source File: ChoiceStepHandler.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"PMD.AvoidReassigningParameters", "PMD.AvoidDeeplyNestedIfStmts"})
@Override
public Optional<ProcessorDefinition<?>> handle(Step step, ProcessorDefinition<?> route, IntegrationRouteBuilder builder, String flowIndex, String stepIndex) {
    ObjectHelper.notNull(route, "route");

    String routingScheme = step.getConfiguredProperties().getOrDefault("routingScheme", "direct");
    String defaultFlow = step.getConfiguredProperties().get("default");
    List<FlowOption> flows = extractFlows(step.getConfiguredProperties().get("flows"));

    if (!flows.isEmpty()) {
        ChoiceDefinition choice = route.choice();

        for (FlowOption flowOption : flows) {
            choice.when(getPredicate(flowOption.getConditionExpression(), builder.getContext()))
                    .description(flowOption.getConditionExpression())
                    .process(new EnrichActivityIdHeader())
                    .to(getEndpointUri(routingScheme, flowOption.getFlow()))
                    .end();
        }

        if (ObjectHelper.isNotEmpty(defaultFlow)) {
            choice.otherwise()
                    .process(new EnrichActivityIdHeader())
                    .to(getEndpointUri(routingScheme, defaultFlow))
                    .end();
        }

        route = choice.end();
    }

    return Optional.of(route);
}
 
Example #3
Source File: IntegrationRouteBuilderTest.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultiFlowIntegrationRouteBuilder() throws Exception {
    String configurationLocation = "classpath:syndesis/integration/multi-flow-integration.json";

    IntegrationRouteBuilder routeBuilder = new IntegrationRouteBuilder(configurationLocation, Resources.loadServices(IntegrationStepHandler.class), policyFactories);

    // initialize routes
    routeBuilder.configure();

    // Dump routes as XML for troubleshooting
    dumpRoutes(new DefaultCamelContext(), routeBuilder.getRouteCollection());

    RoutesDefinition routes = routeBuilder.getRouteCollection();

    assertThat(routes.getRoutes()).hasSize(3);

    RouteDefinition primaryRoute = routes.getRoutes().get(0);

    assertThat(primaryRoute.getRoutePolicies()).hasSize(1);
    assertThat(primaryRoute.getRoutePolicies().get(0)).isInstanceOf(IntegrationActivityTrackingPolicy.class);

    assertThat(primaryRoute.getInput()).isNotNull();
    assertThat(primaryRoute.getInput()).hasFieldOrPropertyWithValue("uri", "direct:expression");
    assertThat(primaryRoute.getOutputs()).hasSize(2);
    assertThat(getOutput(primaryRoute, 0)).isInstanceOf(PipelineDefinition.class);
    assertThat(getOutput(primaryRoute, 0).getOutputs()).hasSize(2);
    assertThat(getOutput(primaryRoute, 0).getOutputs().get(0)).isInstanceOf(SetHeaderDefinition.class);
    assertThat(getOutput(primaryRoute, 0).getOutputs().get(1)).isInstanceOf(SetHeaderDefinition.class);
    assertThat(getOutput(primaryRoute, 1)).isInstanceOf(SplitDefinition.class);
    assertThat(getOutput(primaryRoute, 1).getOutputs()).hasSize(5);
    assertThat(getOutput(primaryRoute, 1, 0)).isInstanceOf(SetHeaderDefinition.class);
    assertThat(getOutput(primaryRoute, 1, 1)).isInstanceOf(ProcessDefinition.class);
    assertThat(getOutput(primaryRoute, 1, 2)).isInstanceOf(PipelineDefinition.class);
    assertThat(getOutput(primaryRoute, 1, 2).getOutputs()).hasSize(2);
    assertThat(getOutput(primaryRoute, 1, 2, 0)).isInstanceOf(SetHeaderDefinition.class);
    assertThat(getOutput(primaryRoute, 1, 2, 1)).isInstanceOf(LogDefinition.class);
    assertThat(getOutput(primaryRoute, 1, 2, 1)).hasFieldOrPropertyWithValue("message", "Body: [${bean:bodyLogger}] Before");
    assertThat(getOutput(primaryRoute, 1, 3)).isInstanceOf(PipelineDefinition.class);
    assertThat(getOutput(primaryRoute, 1, 3).getOutputs()).hasSize(3);
    assertThat(getOutput(primaryRoute, 1, 3, 0)).isInstanceOf(SetHeaderDefinition.class);
    assertThat(getOutput(primaryRoute, 1, 3, 1)).isInstanceOf(ChoiceDefinition.class);
    assertThat(getOutput(primaryRoute, 1, 3, 2)).isInstanceOf(ProcessDefinition.class);
    assertThat(getOutput(primaryRoute, 1, 4)).isInstanceOf(PipelineDefinition.class);
    assertThat(getOutput(primaryRoute, 1, 4).getOutputs()).hasSize(2);
    assertThat(getOutput(primaryRoute, 1, 4, 0)).isInstanceOf(SetHeaderDefinition.class);
    assertThat(getOutput(primaryRoute, 1, 4, 1)).isInstanceOf(LogDefinition.class);
    assertThat(getOutput(primaryRoute, 1, 4, 1)).hasFieldOrPropertyWithValue("message", "Body: [${bean:bodyLogger}] Finished");

    RouteDefinition conditionalRoute = routes.getRoutes().get(1);

    assertThat(conditionalRoute.getRoutePolicies()).hasSize(1);
    assertThat(conditionalRoute.getRoutePolicies().get(0)).isInstanceOf(FlowActivityTrackingPolicy.class);

    assertThat(conditionalRoute.getInput()).isNotNull();
    assertThat(conditionalRoute.getInput()).hasFieldOrPropertyWithValue("uri", "direct");
    assertThat(conditionalRoute.getOutputs()).hasSize(5);
    assertThat(getOutput(conditionalRoute, 0)).isInstanceOf(PipelineDefinition.class);
    assertThat(getOutput(conditionalRoute, 0).getOutputs()).hasSize(2);
    assertThat(getOutput(conditionalRoute, 0).getOutputs().get(0)).isInstanceOf(SetHeaderDefinition.class);
    assertThat(getOutput(conditionalRoute, 0).getOutputs().get(1)).isInstanceOf(SetHeaderDefinition.class);
    assertThat(getOutput(conditionalRoute, 1)).isInstanceOf(SetHeaderDefinition.class);
    assertThat(getOutput(conditionalRoute, 2)).isInstanceOf(ProcessDefinition.class);
    assertThat(getOutput(conditionalRoute, 3)).isInstanceOf(PipelineDefinition.class);
    assertThat(getOutput(conditionalRoute, 3).getOutputs()).hasSize(2);
    assertThat(getOutput(conditionalRoute, 3, 0)).isInstanceOf(SetHeaderDefinition.class);
    assertThat(getOutput(conditionalRoute, 3, 1)).isInstanceOf(LogDefinition.class);
    assertThat(getOutput(conditionalRoute, 3, 1)).hasFieldOrPropertyWithValue("message", "Body: [${bean:bodyLogger}] Found <Play>");
    assertThat(getOutput(conditionalRoute, 4)).isInstanceOf(PipelineDefinition.class);
    assertThat(getOutput(conditionalRoute, 4).getOutputs()).hasSize(3);
    assertThat(getOutput(conditionalRoute, 4, 0)).isInstanceOf(SetHeaderDefinition.class);
    assertThat(getOutput(conditionalRoute, 4, 1)).isInstanceOf(ToDefinition.class);
    assertThat(getOutput(conditionalRoute, 4, 1)).hasFieldOrPropertyWithValue("uri", "bean:io.syndesis.connector.flow.NoOpBean?method=process");
    assertThat(getOutput(conditionalRoute, 4, 2)).isInstanceOf(ProcessDefinition.class);
}