Search in sources :

Example 1 with UnwrapStreamProcessor

use of org.apache.camel.component.reactive.streams.util.UnwrapStreamProcessor in project camel by apache.

the class BeanCallTest method beanCallTest.

@Test
public void beanCallTest() throws Exception {
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            onException(Throwable.class).to("direct:handle").handled(true);
            from("direct:num").bean(BeanCallTest.this, "processBody").process(// Can be removed?
            new UnwrapStreamProcessor()).to("mock:endpoint");
            from("direct:handle").setBody().constant("ERR").to("mock:endpoint");
        }
    }.addRoutesToCamelContext(context);
    MockEndpoint mock = getMockEndpoint("mock:endpoint");
    mock.expectedMessageCount(1);
    context.start();
    template.sendBody("direct:num", 1);
    mock.assertIsSatisfied();
    Exchange exchange = mock.getExchanges().get(0);
    assertEquals("HelloBody 1", exchange.getIn().getBody());
}
Also used : Exchange(org.apache.camel.Exchange) UnwrapStreamProcessor(org.apache.camel.component.reactive.streams.util.UnwrapStreamProcessor) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 2 with UnwrapStreamProcessor

use of org.apache.camel.component.reactive.streams.util.UnwrapStreamProcessor in project camel by apache.

the class BeanCallTest method beanCallWithErrorTest.

@Test
public void beanCallWithErrorTest() throws Exception {
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            onException(Throwable.class).to("direct:handle").handled(true);
            from("direct:num").bean(BeanCallTest.this, "processBodyWrongType").process(// Can be removed?
            new UnwrapStreamProcessor()).to("mock:endpoint");
            from("direct:handle").setBody().constant("ERR").to("mock:endpoint");
        }
    }.addRoutesToCamelContext(context);
    MockEndpoint mock = getMockEndpoint("mock:endpoint");
    mock.expectedMessageCount(1);
    context.start();
    template.sendBody("direct:num", 1);
    mock.assertIsSatisfied();
    Exchange exchange = mock.getExchanges().get(0);
    assertEquals("ERR", exchange.getIn().getBody());
}
Also used : Exchange(org.apache.camel.Exchange) UnwrapStreamProcessor(org.apache.camel.component.reactive.streams.util.UnwrapStreamProcessor) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 3 with UnwrapStreamProcessor

use of org.apache.camel.component.reactive.streams.util.UnwrapStreamProcessor in project camel by apache.

the class BeanCallTest method beanCallEmptyPublisherTest.

@Test
public void beanCallEmptyPublisherTest() throws Exception {
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            onException(Throwable.class).to("direct:handle").handled(true);
            from("direct:num").bean(BeanCallTest.this, "processBodyEmpty").process(// Can be removed?
            new UnwrapStreamProcessor()).to("mock:endpoint");
            from("direct:handle").setBody().constant("ERR").to("mock:endpoint");
        }
    }.addRoutesToCamelContext(context);
    MockEndpoint mock = getMockEndpoint("mock:endpoint");
    mock.expectedMessageCount(1);
    context.start();
    template.sendBody("direct:num", 1);
    mock.assertIsSatisfied();
    Exchange exchange = mock.getExchanges().get(0);
    Object body = exchange.getIn().getBody();
    // unchanged
    assertEquals(new Integer(1), body);
}
Also used : Exchange(org.apache.camel.Exchange) UnwrapStreamProcessor(org.apache.camel.component.reactive.streams.util.UnwrapStreamProcessor) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 4 with UnwrapStreamProcessor

use of org.apache.camel.component.reactive.streams.util.UnwrapStreamProcessor in project camel by apache.

the class BeanCallTest method beanCallHeaderMappingTest.

@Test
public void beanCallHeaderMappingTest() throws Exception {
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            onException(Throwable.class).to("direct:handle").handled(true);
            from("direct:num").bean(BeanCallTest.this, "processHeader").process(// Can be removed?
            new UnwrapStreamProcessor()).to("mock:endpoint");
            from("direct:handle").setBody().constant("ERR").to("mock:endpoint");
        }
    }.addRoutesToCamelContext(context);
    MockEndpoint mock = getMockEndpoint("mock:endpoint");
    mock.expectedMessageCount(1);
    context.start();
    template.sendBodyAndHeader("direct:num", 1, "myheader", 2);
    mock.assertIsSatisfied();
    Exchange exchange = mock.getExchanges().get(0);
    assertEquals("HelloHeader 2", exchange.getIn().getBody());
}
Also used : Exchange(org.apache.camel.Exchange) UnwrapStreamProcessor(org.apache.camel.component.reactive.streams.util.UnwrapStreamProcessor) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 5 with UnwrapStreamProcessor

use of org.apache.camel.component.reactive.streams.util.UnwrapStreamProcessor in project camel by apache.

the class CamelReactiveStreamsServiceImpl method process.

@Override
public void process(String uri, Function<? super Publisher<Exchange>, ?> processor) {
    try {
        new RouteBuilder() {

            @Override
            public void configure() throws Exception {
                from(uri).process(exchange -> {
                    Exchange copy = exchange.copy();
                    Object result = processor.apply(new MonoPublisher<>(copy));
                    exchange.getIn().setBody(result);
                }).process(new UnwrapStreamProcessor());
            }
        }.addRoutesToCamelContext(context);
    } catch (Exception e) {
        throw new IllegalStateException("Unable to add reactive stream processor to the direct URI: " + uri, e);
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) UnwrapStreamProcessor(org.apache.camel.component.reactive.streams.util.UnwrapStreamProcessor) RouteBuilder(org.apache.camel.builder.RouteBuilder)

Aggregations

Exchange (org.apache.camel.Exchange)7 RouteBuilder (org.apache.camel.builder.RouteBuilder)7 UnwrapStreamProcessor (org.apache.camel.component.reactive.streams.util.UnwrapStreamProcessor)7 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)6 Test (org.junit.Test)6 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 DefaultExchange (org.apache.camel.impl.DefaultExchange)1