Java Code Examples for org.apache.camel.component.mock.MockEndpoint#expectedMessagesMatches()

The following examples show how to use org.apache.camel.component.mock.MockEndpoint#expectedMessagesMatches() . 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: TikaIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void detectDoc() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").to("tika:detect").to("mock:result");
        }
    });
    MockEndpoint resultEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);

    try {
        camelctx.start();

        ProducerTemplate template = camelctx.createProducerTemplate();
        File document = new File("src/test/resources/tika/test.doc");
        template.sendBody("direct:start", document);

        resultEndpoint.setExpectedMessageCount(1);

        resultEndpoint.expectedMessagesMatches(new Predicate() {
            @Override
            public boolean matches(Exchange exchange) {
                Object body = exchange.getIn().getBody(String.class);
                Assert.assertThat(body, instanceOf(String.class));
                Assert.assertThat((String) body, containsString("application/x-tika-msoffice"));
                return true;
            }
        });
        resultEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 2
Source File: MongoDBConnectorChangeStreamConsumerTest.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Test
public void singleInsertTest() throws Exception {
    // When
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(2);
    mock.expectedMessagesMatches(
        exchange -> validateDocument(exchange, "junit"),
        exchange -> validateDocument(exchange, "junit2")
    );
    // Given
    Document doc = new Document();
    doc.append("someKey", "someValue");
    doc.append("test", "junit");
    collection.insertOne(doc);
    Document doc2 = new Document();
    doc2.append("someKey", "someValue2");
    doc2.append("test", "junit2");
    collection.insertOne(doc2);
    // Then
    mock.assertIsSatisfied();
}
 
Example 3
Source File: MongoDBConnectorCappedCollectionConsumerTest.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Test
public void mongoTest() throws Exception {
    collection = database.getCollection(COLLECTION);

    String unique1 = UUID.randomUUID().toString();
    int id1 = 1;
    String unique2 = UUID.randomUUID().toString();
    int id2 = 2;
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(2);
    mock.expectedMessagesMatches(
        exchange -> validateDocument(exchange, unique1, id1),
        exchange -> validateDocument(exchange, unique2, id2)
    );

    Document doc1 = new Document();
    doc1.append("id", id1);
    doc1.append("unique", unique1);
    collection.insertOne(doc1);
    Document doc2 = new Document();
    doc2.append("id", id2);
    doc2.append("unique", unique2);
    collection.insertOne(doc2);

    mock.assertIsSatisfied();
}
 
Example 4
Source File: TikaIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void parseGif() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").to("tika:parse").to("mock:result");
        }
    });
    MockEndpoint resultEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);

    try {
        camelctx.start();
        ProducerTemplate template = camelctx.createProducerTemplate();

        File document = new File("src/test/resources/tika/testGIF.gif");
        template.sendBody("direct:start", document);

        resultEndpoint.setExpectedMessageCount(1);

        resultEndpoint.expectedMessagesMatches(new Predicate() {
            @Override
            public boolean matches(Exchange exchange) {
                Object body = exchange.getIn().getBody(String.class);
                Map<String, Object> headerMap = exchange.getIn().getHeaders();
                Assert.assertThat(body, instanceOf(String.class));
                Assert.assertThat((String) body, containsString("<body"));
                Assert.assertThat(headerMap.get(Exchange.CONTENT_TYPE), equalTo("image/gif"));
                return true;
            }
        });
        resultEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 5
Source File: TikaIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void parseDocWithRegistryConfig() throws Exception {
    CamelContext camelctx = new DefaultCamelContext(createRegistryWithEmptyConfig());
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").to("tika:parse?tikaConfig=#testConfig").to("mock:result");
        }
    });
    MockEndpoint resultEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);

    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        camelctx.start();
        File document = new File("src/test/resources/tika/test.doc");
        template.sendBody("direct:start", document);

        resultEndpoint.setExpectedMessageCount(1);

        resultEndpoint.expectedMessagesMatches(new Predicate() {
            @Override
            public boolean matches(Exchange exchange) {
                Object body = exchange.getIn().getBody(String.class);
                Map<String, Object> headerMap = exchange.getIn().getHeaders();
                Assert.assertThat(body, instanceOf(String.class));
                Assert.assertThat((String) body, containsString("<body"));
                Assert.assertThat(headerMap.get(Exchange.CONTENT_TYPE), equalTo("application/msword"));
                return true;
            }
        });
        resultEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 6
Source File: TikaIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void parseDocWithEmptyRegistryConfig() throws Exception {
    CamelContext camelctx = new DefaultCamelContext(createRegistryWithEmptyConfig());
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").to("tika:parse?tikaConfig=#testConfig").to("mock:result");
        }
    });
    MockEndpoint resultEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);

    try {
        camelctx.start();
        ProducerTemplate template = camelctx.createProducerTemplate();

        File document = new File("src/test/resources/tika/test.doc");
        template.sendBody("direct:start", document);

        resultEndpoint.setExpectedMessageCount(1);

        resultEndpoint.expectedMessagesMatches(new Predicate() {
            @Override
            public boolean matches(Exchange exchange) {
                Object body = exchange.getIn().getBody(String.class);
                Map<String, Object> headerMap = exchange.getIn().getHeaders();
                Assert.assertThat(body, instanceOf(String.class));
                Assert.assertThat((String) body, containsString("<body"));
                Assert.assertThat(headerMap.get(Exchange.CONTENT_TYPE), equalTo("application/msword"));
                return true;
            }
        });
        resultEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 7
Source File: TikaIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void detectGif() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").to("tika:detect").to("mock:result");
        }
    });
    MockEndpoint resultEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);

    try {
        camelctx.start();

        ProducerTemplate template = camelctx.createProducerTemplate();
        File document = new File("src/test/resources/tika/testGIF.gif");
        template.sendBody("direct:start", document);

        resultEndpoint.setExpectedMessageCount(1);

        resultEndpoint.expectedMessagesMatches(new Predicate() {
            @Override
            public boolean matches(Exchange exchange) {
                Object body = exchange.getIn().getBody(String.class);
                Assert.assertThat(body, instanceOf(String.class));
                Assert.assertThat((String) body, containsString("image/gif"));
                return true;
            }
        });
        resultEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 8
Source File: MongoDBConnectorCappedCollectionConsumerAllOptionsTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Test
public void mongoTest() throws Exception {
    // When
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(3);
    mock.expectedMessagesMatches(
        exchange -> validateDocument(exchange, 10),
        exchange -> validateDocument(exchange, 20),
        exchange -> validateDocument(exchange, 25)
    );
    // Given
    Document doc = new Document();
    doc.append("someKey", "someValue");
    doc.append(COLLECTION_TRACKING_FIELD, 10);
    collection.insertOne(doc);
    Document doc2 = new Document();
    doc2.append("someKey", "someNewValue");
    doc2.append(COLLECTION_TRACKING_FIELD, 20);
    collection.insertOne(doc2);
    Document doc3 = new Document();
    doc3.append("someKey", "someNewValue");
    doc3.append(COLLECTION_TRACKING_FIELD, 25);
    collection.insertOne(doc3);

    // Then
    mock.assertIsSatisfied();
}
 
Example 9
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 10
Source File: TikaIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void parseDoc() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").to("tika:parse").to("mock:result");
        }
    });
    MockEndpoint resultEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);

    try {
        camelctx.start();

        ProducerTemplate template = camelctx.createProducerTemplate();

        File document = new File("src/test/resources/tika/test.doc");
        template.sendBody("direct:start", document);

        resultEndpoint.setExpectedMessageCount(1);
        resultEndpoint.expectedMessagesMatches(new Predicate() {
            @Override
            public boolean matches(Exchange exchange) {
                Object body = exchange.getIn().getBody(String.class);
                Map<String, Object> headerMap = exchange.getIn().getHeaders();
                Assert.assertThat(body, instanceOf(String.class));

                Charset detectedCharset = null;
                try {
                    InputStream bodyIs = new ByteArrayInputStream(((String) body).getBytes());
                    UniversalEncodingDetector encodingDetector = new UniversalEncodingDetector();
                    detectedCharset = encodingDetector.detect(bodyIs, new Metadata());
                } catch (IOException e1) {
                    throw new RuntimeException(e1);
                }

                Assert.assertThat((String) body, containsString("test"));
                Assert.assertThat(detectedCharset.name(), startsWith(Charset.defaultCharset().name()));

                Assert.assertThat(headerMap.get(Exchange.CONTENT_TYPE), equalTo("application/msword"));
                return true;
            }
        });
        resultEndpoint.assertIsSatisfied();

    } finally {
        camelctx.close();
    }
}
 
Example 11
Source File: KnativeHttpTest.java    From camel-k-runtime with Apache License 2.0 4 votes vote down vote up
void doTestKnativeSource(CloudEvent ce, String basePath, String path) throws Exception {
    KnativeComponent component = configureKnativeComponent(
        context,
        ce,
        sourceEndpoint(
            "myEndpoint",
            mapOf(
                Knative.SERVICE_META_PATH, path,
                Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
                Knative.CONTENT_TYPE, "text/plain"
            ))
    );

    if (ObjectHelper.isNotEmpty(basePath)) {
        component.getConfiguration().addTransportOptions("basePath", basePath);
    }

    RouteBuilder.addRoutes(context, b -> {
        b.from("knative:endpoint/myEndpoint")
            .to("mock:ce");
    });

    context.start();

    MockEndpoint mock = context.getEndpoint("mock:ce", MockEndpoint.class);
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_VERSION, ce.version());
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_TYPE, "org.apache.camel.event");
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE, "/somewhere");
    mock.expectedHeaderReceived(Exchange.CONTENT_TYPE, "text/plain");
    mock.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(CloudEvent.CAMEL_CLOUD_EVENT_TIME));
    mock.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(CloudEvent.CAMEL_CLOUD_EVENT_ID));
    mock.expectedBodiesReceived("test");
    mock.expectedMessageCount(1);

    String targetPath = ObjectHelper.supplyIfEmpty(path, () -> "/");
    if (ObjectHelper.isNotEmpty(basePath)) {
        targetPath = basePath + targetPath;
    }

    given()
        .body("test")
        .header(Exchange.CONTENT_TYPE, "text/plain")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_VERSION).http(), ce.version())
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TYPE).http(), "org.apache.camel.event")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_ID).http(), "myEventID")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TIME).http(), DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()))
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE).http(), "/somewhere")
    .when()
        .post(targetPath)
    .then()
        .statusCode(200);

    mock.assertIsSatisfied();
}
 
Example 12
Source File: KnativeHttpTest.java    From camel-k-runtime with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@EnumSource(CloudEvents.class)
void testConsumeContentWithTypeAndVersion(CloudEvent ce) throws Exception {
    configureKnativeComponent(
        context,
        ce,
        sourceEndpoint(
            "myEndpoint",
            mapOf(
                Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
                Knative.CONTENT_TYPE, "text/plain",
                Knative.KNATIVE_KIND, "MyObject",
                Knative.KNATIVE_API_VERSION, "v1"
            )),
        sourceEndpoint(
            "myEndpoint",
            mapOf(
                Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
                Knative.CONTENT_TYPE, "text/plain",
                Knative.KNATIVE_KIND, "MyObject",
                Knative.KNATIVE_API_VERSION, "v2"
            ))
    );

    RouteBuilder.addRoutes(context, b -> {
        b.from("knative:endpoint/myEndpoint?kind=MyObject&apiVersion=v2")
            .to("mock:ce");
    });

    context.start();

    MockEndpoint mock = context.getEndpoint("mock:ce", MockEndpoint.class);
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_VERSION, ce.version());
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_TYPE, "org.apache.camel.event");
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_ID, "myEventID");
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE, "/somewhere");
    mock.expectedHeaderReceived(Exchange.CONTENT_TYPE, "text/plain");
    mock.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(CloudEvent.CAMEL_CLOUD_EVENT_TIME));
    mock.expectedBodiesReceived("test");
    mock.expectedMessageCount(1);

    given()
        .body("test")
        .header(Exchange.CONTENT_TYPE, "text/plain")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_VERSION).http(), ce.version())
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TYPE).http(), "org.apache.camel.event")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_ID).http(), "myEventID")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TIME).http(), DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()))
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE).http(), "/somewhere")
    .when()
        .post()
    .then()
        .statusCode(200);

    mock.assertIsSatisfied();
}
 
Example 13
Source File: KnativeHttpTest.java    From camel-k-runtime with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@EnumSource(CloudEvents.class)
void testEventsWithTypeAndVersion(CloudEvent ce) throws Exception {
    configureKnativeComponent(
        context,
        ce,
        event(
            Knative.EndpointKind.sink,
            "default",
            "localhost",
            platformHttpPort,
            mapOf(
                Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
                Knative.CONTENT_TYPE, "text/plain",
                Knative.KNATIVE_KIND, "MyObject",
                Knative.KNATIVE_API_VERSION, "v1"
            )),
        sourceEvent(
            "default",
            mapOf(
                Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
                Knative.CONTENT_TYPE, "text/plain",
                Knative.KNATIVE_KIND, "MyOtherObject",
                Knative.KNATIVE_API_VERSION, "v2"
            ))
    );

    RouteBuilder.addRoutes(context, b -> {
        b.from("direct:source")
            .to("knative:event/myEvent?kind=MyObject&apiVersion=v1");
        b.from("knative:event/myEvent?kind=MyOtherObject&apiVersion=v2")
            .to("mock:ce");
    });

    context.start();

    MockEndpoint mock = context.getEndpoint("mock:ce", MockEndpoint.class);
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_VERSION, ce.version());
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_TYPE, "myEvent");
    mock.expectedHeaderReceived(Exchange.CONTENT_TYPE, "text/plain");
    mock.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(CloudEvent.CAMEL_CLOUD_EVENT_TIME));
    mock.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(CloudEvent.CAMEL_CLOUD_EVENT_ID));
    mock.expectedBodiesReceived("test");
    mock.expectedMessageCount(1);

    template.sendBody("direct:source", "test");

    mock.assertIsSatisfied();
}
 
Example 14
Source File: KnativeHttpTest.java    From camel-k-runtime with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@EnumSource(CloudEvents.class)
void testEvents(CloudEvent ce) throws Exception {
    configureKnativeComponent(
        context,
        ce,
        event(
            Knative.EndpointKind.sink,
            "default",
            "localhost",
            platformHttpPort,
            mapOf(
                Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
                Knative.CONTENT_TYPE, "text/plain"
            )),
        sourceEvent(
            "default",
            mapOf(
                Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
                Knative.CONTENT_TYPE, "text/plain"
            ))
    );

    RouteBuilder.addRoutes(context, b -> {
        b.from("direct:source")
            .to("knative:event/myEvent");
        b.from("knative:event/myEvent")
            .to("mock:ce");
    });

    context.start();

    MockEndpoint mock = context.getEndpoint("mock:ce", MockEndpoint.class);
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_VERSION, ce.version());
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_TYPE, "myEvent");
    mock.expectedHeaderReceived(Exchange.CONTENT_TYPE, "text/plain");
    mock.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(CloudEvent.CAMEL_CLOUD_EVENT_TIME));
    mock.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(CloudEvent.CAMEL_CLOUD_EVENT_ID));
    mock.expectedBodiesReceived("test");
    mock.expectedMessageCount(1);

    template.sendBody("direct:source", "test");

    mock.assertIsSatisfied();
}
 
Example 15
Source File: TikaIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void parseOdtWithEncoding() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").to("tika:parse?tikaParseOutputEncoding=" + StandardCharsets.UTF_16.name())
                    .to("mock:result");
        }
    });
    MockEndpoint resultEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);

    try {
        camelctx.start();
        ProducerTemplate template = camelctx.createProducerTemplate();

        File document = new File("src/test/resources/tika/testOpenOffice2.odt");
        template.sendBody("direct:start", document);

        resultEndpoint.setExpectedMessageCount(1);

        resultEndpoint.expectedMessagesMatches(new Predicate() {
            @Override
            public boolean matches(Exchange exchange) {
                Object body = exchange.getIn().getBody(String.class);
                Map<String, Object> headerMap = exchange.getIn().getHeaders();
                Assert.assertThat(body, instanceOf(String.class));

                Charset detectedCharset = null;
                try {
                    InputStream bodyIs = new ByteArrayInputStream(
                            ((String) body).getBytes(StandardCharsets.UTF_16));
                    UniversalEncodingDetector encodingDetector = new UniversalEncodingDetector();
                    detectedCharset = encodingDetector.detect(bodyIs, new Metadata());
                } catch (IOException e1) {
                    throw new RuntimeException(e1);
                }

                Assert.assertThat(detectedCharset.name(), startsWith(StandardCharsets.UTF_16.name()));
                Assert.assertThat(headerMap.get(Exchange.CONTENT_TYPE),
                        equalTo("application/vnd.oasis.opendocument.text"));
                return true;
            }
        });
        resultEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 16
Source File: KnativeHttpTest.java    From camel-k-runtime with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@EnumSource(CloudEvents.class)
void testConsumeContentWithRegExFilter(CloudEvent ce) throws Exception {
    configureKnativeComponent(
        context,
        ce,
        sourceEndpoint(
            "ep1",
            mapOf(
                Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
                Knative.CONTENT_TYPE, "text/plain",
                Knative.KNATIVE_FILTER_PREFIX + ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE).http(), "CE[01234]"
            )),
        sourceEndpoint(
            "ep2",
            mapOf(
                Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
                Knative.CONTENT_TYPE, "text/plain",
                Knative.KNATIVE_FILTER_PREFIX + ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE).http(), "CE[56789]"
            ))
    );

    RouteBuilder.addRoutes(context, b -> {
        b.from("knative:endpoint/ep1")
            .convertBodyTo(String.class)
            .to("log:ce1?showAll=true&multiline=true")
            .to("mock:ce1");
        b.from("knative:endpoint/ep2")
            .convertBodyTo(String.class)
            .to("log:ce2?showAll=true&multiline=true")
            .to("mock:ce2");
    });

    context.start();

    MockEndpoint mock1 = context.getEndpoint("mock:ce1", MockEndpoint.class);
    mock1.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(CloudEvent.CAMEL_CLOUD_EVENT_TIME));
    mock1.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_VERSION, ce.version());
    mock1.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_TYPE, "org.apache.camel.event");
    mock1.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_ID, "myEventID1");
    mock1.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE, "CE0");
    mock1.expectedBodiesReceived("test");
    mock1.expectedMessageCount(1);

    MockEndpoint mock2 = context.getEndpoint("mock:ce2", MockEndpoint.class);
    mock2.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(CloudEvent.CAMEL_CLOUD_EVENT_TIME));
    mock2.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_VERSION, ce.version());
    mock2.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_TYPE, "org.apache.camel.event");
    mock2.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_ID, "myEventID2");
    mock2.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE, "CE5");
    mock2.expectedBodiesReceived("test");
    mock2.expectedMessageCount(1);

    given()
        .body("test")
        .header(Exchange.CONTENT_TYPE, "text/plain")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_VERSION).http(), ce.version())
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TYPE).http(), "org.apache.camel.event")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_ID).http(), "myEventID1")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TIME).http(), DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()))
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE).http(), "CE0")
    .when()
        .post()
    .then()
        .statusCode(200);

    given()
        .body("test")
        .header(Exchange.CONTENT_TYPE, "text/plain")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_VERSION).http(), ce.version())
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TYPE).http(), "org.apache.camel.event")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_ID).http(), "myEventID2")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TIME).http(), DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()))
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE).http(), "CE5")
    .when()
        .post()
    .then()
        .statusCode(200);

    mock1.assertIsSatisfied();
    mock2.assertIsSatisfied();
}
 
Example 17
Source File: KnativeHttpTest.java    From camel-k-runtime with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@EnumSource(CloudEvents.class)
void testConsumeContentWithFilter(CloudEvent ce) throws Exception {
    configureKnativeComponent(
        context,
        ce,
        sourceEndpoint(
            "ep1",
            mapOf(
                Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
                Knative.CONTENT_TYPE, "text/plain",
                Knative.KNATIVE_FILTER_PREFIX + ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE).http(), "CE1"
            )),
        sourceEndpoint(
            "ep2",
            mapOf(
                Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
                Knative.CONTENT_TYPE, "text/plain",
                Knative.KNATIVE_FILTER_PREFIX + ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE).http(), "CE2"
            ))
    );

    RouteBuilder.addRoutes(context, b -> {
        b.from("knative:endpoint/ep1")
            .convertBodyTo(String.class)
            .to("log:ce1?showAll=true&multiline=true")
            .to("mock:ce1");
        b.from("knative:endpoint/ep2")
            .convertBodyTo(String.class)
            .to("log:ce2?showAll=true&multiline=true")
            .to("mock:ce2");
    });

    context.start();

    MockEndpoint mock1 = context.getEndpoint("mock:ce1", MockEndpoint.class);
    mock1.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(CloudEvent.CAMEL_CLOUD_EVENT_TIME));
    mock1.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_VERSION, ce.version());
    mock1.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_TYPE, "org.apache.camel.event");
    mock1.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_ID, "myEventID1");
    mock1.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE, "CE1");
    mock1.expectedBodiesReceived("test");
    mock1.expectedMessageCount(1);

    MockEndpoint mock2 = context.getEndpoint("mock:ce2", MockEndpoint.class);
    mock2.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(CloudEvent.CAMEL_CLOUD_EVENT_TIME));
    mock2.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_VERSION, ce.version());
    mock2.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_TYPE, "org.apache.camel.event");
    mock2.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_ID, "myEventID2");
    mock2.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE, "CE2");
    mock2.expectedBodiesReceived("test");
    mock2.expectedMessageCount(1);

    given()
        .body("test")
        .header(Exchange.CONTENT_TYPE, "text/plain")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_VERSION).http(), ce.version())
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TYPE).http(), "org.apache.camel.event")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_ID).http(), "myEventID1")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TIME).http(), DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()))
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE).http(), "CE1")
        .when()
        .post()
        .then()
        .statusCode(200);

    given()
        .body("test")
        .header(Exchange.CONTENT_TYPE, "text/plain")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_VERSION).http(), ce.version())
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TYPE).http(), "org.apache.camel.event")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_ID).http(), "myEventID2")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TIME).http(), DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()))
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE).http(), "CE2")
    .when()
        .post()
    .then()
        .statusCode(200);

    mock1.assertIsSatisfied();
    mock2.assertIsSatisfied();
}
 
Example 18
Source File: KnativeHttpTest.java    From camel-k-runtime with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@EnumSource(CloudEvents.class)
void testConsumeContent(CloudEvent ce) throws Exception {
    configureKnativeComponent(
        context,
        ce,
        sourceEndpoint(
            "myEndpoint",
            mapOf(
                Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
                Knative.CONTENT_TYPE, "text/plain"
            ))
    );

    RouteBuilder.addRoutes(context, b -> {
        b.from("knative:endpoint/myEndpoint")
            .to("mock:ce");
    });

    context.start();

    MockEndpoint mock = context.getEndpoint("mock:ce", MockEndpoint.class);
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_VERSION, ce.version());
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_TYPE, "org.apache.camel.event");
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_ID, "myEventID");
    mock.expectedHeaderReceived(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE, "/somewhere");
    mock.expectedHeaderReceived(Exchange.CONTENT_TYPE, "text/plain");
    mock.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(CloudEvent.CAMEL_CLOUD_EVENT_TIME));
    mock.expectedBodiesReceived("test");
    mock.expectedMessageCount(1);

    given()
        .body("test")
        .header(Exchange.CONTENT_TYPE, "text/plain")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_VERSION).http(), ce.version())
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TYPE).http(), "org.apache.camel.event")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_ID).http(), "myEventID")
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TIME).http(), DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()))
        .header(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE).http(), "/somewhere")
    .when()
        .post()
    .then()
        .statusCode(200);

    mock.assertIsSatisfied();
}
 
Example 19
Source File: KnativeHttpTest.java    From camel-k-runtime with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@EnumSource(CloudEvents.class)
void testInvokeEndpointByUrl(CloudEvent ce) throws Exception {
    configureKnativeComponent(
        context,
        ce,
        endpoint(
            Knative.EndpointKind.sink,
            "myEndpoint",
            "none",
            -1,
            mapOf(
                Knative.SERVICE_META_PATH, "/does/not/exist",
                Knative.SERVICE_META_URL, String.format("http://localhost:%d/a/path", platformHttpPort),
                Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
                Knative.CONTENT_TYPE, "text/plain"
            ))
    );

    RouteBuilder.addRoutes(context, b -> {
        b.from("direct:source")
            .to("knative:endpoint/myEndpoint");
        b.from("platform-http:/a/path")
            .to("mock:ce");
    });

    context.start();

    MockEndpoint mock = context.getEndpoint("mock:ce", MockEndpoint.class);
    mock.expectedHeaderReceived(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_VERSION).http(), ce.version());
    mock.expectedHeaderReceived(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TYPE).http(), "org.apache.camel.event");
    mock.expectedHeaderReceived(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE).http(), "knative://endpoint/myEndpoint");
    mock.expectedHeaderReceived(Exchange.CONTENT_TYPE, "text/plain");
    mock.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TIME).http()));
    mock.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_ID).http()));
    mock.expectedBodiesReceived("test");
    mock.expectedMessageCount(1);

    template.sendBody("direct:source", "test");

    mock.assertIsSatisfied();
}
 
Example 20
Source File: KnativeHttpTest.java    From camel-k-runtime with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@EnumSource(CloudEvents.class)
void testInvokeEndpoint(CloudEvent ce) throws Exception {
    configureKnativeComponent(
        context,
        ce,
        endpoint(
            Knative.EndpointKind.sink,
            "myEndpoint",
            "localhost",
            platformHttpPort,
            mapOf(
                Knative.SERVICE_META_PATH, "/a/path",
                Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
                Knative.CONTENT_TYPE, "text/plain"
            ))
    );

    RouteBuilder.addRoutes(context, b -> {
        b.from("direct:source")
            .to("knative:endpoint/myEndpoint");
        b.from("platform-http:/a/path")
            .to("mock:ce");
    });

    context.start();

    MockEndpoint mock = context.getEndpoint("mock:ce", MockEndpoint.class);
    mock.expectedHeaderReceived(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_VERSION).http(), ce.version());
    mock.expectedHeaderReceived(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TYPE).http(), "org.apache.camel.event");
    mock.expectedHeaderReceived(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE).http(), "knative://endpoint/myEndpoint");
    mock.expectedHeaderReceived(Exchange.CONTENT_TYPE, "text/plain");
    mock.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TIME).http()));
    mock.expectedMessagesMatches(e -> e.getMessage().getHeaders().containsKey(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_ID).http()));
    mock.expectedBodiesReceived("test");
    mock.expectedMessageCount(1);

    template.sendBody("direct:source", "test");

    mock.assertIsSatisfied();
}