Java Code Examples for org.apache.camel.ProducerTemplate#sendBodyAndHeader()

The following examples show how to use org.apache.camel.ProducerTemplate#sendBodyAndHeader() . 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: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAllEmptySet() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        Set<Object> l = new HashSet<Object>();
        Map t = new HashMap();
        t.put("key1", "value1");
        t.put("key2", "value2");
        t.put("key3", "value3");
        Mockito.when(map.getAll(Mockito.anySet())).thenReturn(t);

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("direct:getAll", null, HazelcastConstants.OBJECT_ID, l);

        ConsumerTemplate consumer = camelctx.createConsumerTemplate();
        String body = consumer.receiveBody("seda:out", 5000, String.class);
        Mockito.verify(map).getAll(l);
        Assert.assertTrue(body.contains("key1=value1"));
        Assert.assertTrue(body.contains("key2=value2"));
        Assert.assertTrue(body.contains("key3=value3"));
    } finally {
        camelctx.close();
    }
}
 
Example 2
Source File: NagiosIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendToNagiosWarnAsText() throws Exception {

    CamelContext camelctx = createCamelContext();

    MessagePayload expectedPayload1 = new MessagePayload("localhost", Level.WARNING, camelctx.getName(),  "Hello Nagios");

    MockEndpoint mock = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    mock.expectedMessageCount(1);
    mock.expectedBodiesReceived("Hello Nagios");

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("direct:start", "Hello Nagios", NagiosConstants.LEVEL, "WARNING");

        mock.assertIsSatisfied();

        Mockito.verify(nagiosPassiveCheckSender).send(expectedPayload1);
    } finally {
        camelctx.close();
    }
}
 
Example 3
Source File: MicrometerCounterIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testUsingScriptEvaluation() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:out", MockEndpoint.class);
        mockEndpoint.expectedMessageCount(1);

        String message = "Hello from Camel Metrics!";
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("direct:in-4", message, HEADER_COUNTER_DECREMENT, 7.0D);

        Counter counter = metricsRegistry.find("D").counter();
        Assert.assertEquals(message.length(), counter.count(), 0.01D);
        Assert.assertEquals(Integer.toString(message.length()), counter.getId().getTag("a"));
        mockEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 4
Source File: TenantProcessor.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
/**
 * Send a message to the landing zone queue for the given file.
 *
 * @param filePathname
 *            the file to be ingested
 *
 * @return true if the message was successfully sent to the landing zone queue
 *
 * @throws IOException
 */
private void sendMessageToLzQueue(String filePathname) {
    // Create a new process to invoke the ruby script to send the message.
    try {
        /*
         * The logic to send this message is also present in following ruby script. Any changes
         * here should also be made to the script.
         * sli/opstools/ingestion_trigger/publish_file_uploaded.rb
         */
        ProducerTemplate template = new DefaultProducerTemplate(camelContext);
        template.start();
        template.sendBodyAndHeader(landingZoneQueueUri, "Sample lzfile message", "filePath", filePathname);
        template.stop();
    } catch (Exception e) {
        LOG.error("Error publishing sample file " + filePathname + " for ingestion", e);
    }
}
 
Example 5
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuery() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        String sql = "bar > 1000";
        Mockito.when(map.values(Mockito.any(SqlPredicate.class))).thenReturn(Arrays.<Object>asList(new Dummy("beta", 2000), new Dummy("gamma", 3000)));

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("direct:queue", null, HazelcastConstants.QUERY, sql);
        Mockito.verify(map).values(Mockito.any(SqlPredicate.class));

        ConsumerTemplate consumer = camelctx.createConsumerTemplate();
        Collection<?> b1 = consumer.receiveBody("seda:out", 5000, Collection.class);

        Assert.assertNotNull(b1);
        Assert.assertEquals(2, b1.size());
    } finally {
        camelctx.close();
    }
}
 
Example 6
Source File: JCloudsComputeIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testListImages() throws Exception {
    CamelContext camelctx = getCamelContext();
    ProducerTemplate template = camelctx.createProducerTemplate();

    MockEndpoint result = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    result.expectedMessageCount(1);
    template.sendBodyAndHeader("direct:start", null, JcloudsConstants.OPERATION, JcloudsConstants.LIST_IMAGES);
    result.assertIsSatisfied();

    List<Exchange> exchanges = result.getExchanges();
    if (exchanges != null && !exchanges.isEmpty()) {
        for (Exchange exchange : exchanges) {
            Set<?> images = exchange.getIn().getBody(Set.class);
            Assert.assertTrue(images.size() > 0);
            for (Object obj : images) {
                Assert.assertTrue(obj instanceof Image);
            }
        }
    }
}
 
Example 7
Source File: DecanterTraceEventHandlerTest.java    From karaf-decanter with Apache License 2.0 6 votes vote down vote up
@Test
public void testTracerWithExtender() throws Exception {
    MockEventAdmin eventAdmin = new MockEventAdmin();
    TestExtender extender = new TestExtender();
    DecanterTraceEventHandler handler = new DecanterTraceEventHandler();
    handler.setExtender(extender);
    handler.setEventAdmin(eventAdmin);
    DefaultCamelContext camelContext = createCamelContext(handler);

    ProducerTemplate template = camelContext.createProducerTemplate();
    template.sendBodyAndHeader("direct:start", "TEST", "header", "test");

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

    Assert.assertEquals("test", eventAdmin.getPostEvents().get(0).getProperty("extender-test"));
    Assert.assertEquals("test", eventAdmin.getPostEvents().get(1).getProperty("extender-test"));
}
 
Example 8
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 9
Source File: KarafIT.java    From fcrepo-camel-toolbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testService() throws Exception {
    final String baseUrl = "http://localhost:" + System.getProperty("fcrepo.port") + "/fcrepo/rest";
    final CamelContext ctx = getOsgiService(CamelContext.class,
            "(camel.context.name=FcrepoService)", 10000);

    assertNotNull(ctx);

    final MockEndpoint resultEndpoint = (MockEndpoint) ctx.getEndpoint("mock:result");

    final String url1 = post(baseUrl).replace(baseUrl, "");
    final String url2 = post(baseUrl).replace(baseUrl, "");

    final ProducerTemplate template = ctx.createProducerTemplate();
    final Map<String, Object> headers = new HashMap<>();
    headers.put(FCREPO_BASE_URL, baseUrl);
    headers.put(FCREPO_IDENTIFIER, url1);
    template.sendBodyAndHeaders(ctx.getEndpoint("direct:start"), null, headers);

    template.sendBodyAndHeader(ctx.getEndpoint("direct:start"), null, FCREPO_URI, baseUrl + url2);

    resultEndpoint.expectedMinimumMessageCount(2);
    assertIsSatisfied(resultEndpoint);
}
 
Example 10
Source File: DecanterTraceEventHandlerTest.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
@Test
public void testTracer() throws Exception {
    JsonMarshaller marshaller = new JsonMarshaller();
    MockEventAdmin eventAdmin = new MockEventAdmin();
    DecanterTraceEventHandler handler = new DecanterTraceEventHandler();
    handler.setEventAdmin(eventAdmin);
    DefaultCamelContext camelContext = createCamelContext(handler);

    ProducerTemplate template = camelContext.createProducerTemplate();
    template.sendBodyAndHeader("direct:start", "TEST", "header", "test");

    for (Event event : eventAdmin.getPostEvents()) {
        String jsonString = marshaller.marshal(event);
        JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
        JsonObject rootObject = jsonReader.readObject();
        System.out.println(jsonString);
        Assert.assertEquals("InOnly", rootObject.getString("exchangePattern"));
        Assert.assertEquals("camelTracer", rootObject.getString("type"));
        Assert.assertEquals("test-route", rootObject.getString("routeId"));
        Assert.assertEquals("test-context", rootObject.getString("camelContextName"));
        JsonObject headersObject = rootObject.getJsonObject("inHeaders");
        Assert.assertEquals("test", headersObject.getString("header"));
        Assert.assertEquals("direct://start", rootObject.getString("fromEndpointUri"));
        Assert.assertEquals("TEST", rootObject.getString("inBody"));
        JsonObject propertiesObject = rootObject.getJsonObject("properties");
        Assert.assertEquals("log://foo", propertiesObject.getString("CamelToEndpoint"));
        Assert.assertEquals("String", rootObject.getString("inBodyType"));
        Assert.assertEquals("decanter/collect/camel/tracer", rootObject.getString("event_topics"));
        System.out.println(marshaller.marshal(event));
    }
}
 
Example 11
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutWithOperationNumber() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("direct:putWithOperationNumber", "my-foo", HazelcastConstants.OBJECT_ID, "4711");
        Mockito.verify(map).put("4711", "my-foo");
    } finally {
        camelctx.close();
    }
}
 
Example 12
Source File: CamelSourceTaskTest.java    From camel-kafka-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testSourcePollingWithKey() {
    Map<String, String> props = new HashMap<>();
    props.put(CamelSourceConnectorConfig.TOPIC_CONF, TOPIC_NAME);
    props.put(CamelSourceConnectorConfig.CAMEL_SOURCE_URL_CONF, DIRECT_URI);
    props.put(CamelSourceConnectorConfig.CAMEL_SOURCE_MESSAGE_HEADER_KEY_CONF, "CamelSpecialTestKey");

    CamelSourceTask sourceTask = new CamelSourceTask();
    sourceTask.start(props);
    final ProducerTemplate template = sourceTask.getCms().createProducerTemplate();

    // key in the message with body
    template.sendBodyAndHeader(DIRECT_URI, "test", "CamelSpecialTestKey", 1234);

    List<SourceRecord> poll1 = sourceTask.poll();
    assertEquals(1, poll1.size());
    assertEquals(1234, poll1.get(0).key());
    assertEquals(Schema.Type.INT32, poll1.get(0).keySchema().type());

    // no key under the header
    template.sendBodyAndHeader(DIRECT_URI, "test", "WrongHeader", 1234);

    List<SourceRecord> poll2 = sourceTask.poll();
    assertEquals(1, poll2.size());
    assertNull(poll2.get(0).key());
    assertNull(poll2.get(0).keySchema());

    // header with null value
    template.sendBodyAndHeader(DIRECT_URI, "test", "CamelSpecialTestKey", null);

    List<SourceRecord> poll3 = sourceTask.poll();
    assertEquals(1, poll3.size());
    assertNull(poll3.get(0).key());
    assertNull(poll3.get(0).keySchema());

    sourceTask.stop();
}
 
Example 13
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 14
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutWithOperationName() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("direct:putWithOperationName", "my-foo", HazelcastConstants.OBJECT_ID, "4711");
        Mockito.verify(map).put("4711", "my-foo");
    } finally {
        camelctx.close();
    }
}
 
Example 15
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testDelete() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("direct:delete", null, HazelcastConstants.OBJECT_ID, 4711);
        Mockito.verify(map).remove(4711);
    } finally {
        camelctx.close();
    }
}
 
Example 16
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 17
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 18
Source File: FileIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileIdempotentConsumer() throws Exception {
    final String datadir = System.getProperty("jboss.server.data.dir");
    Assert.assertNotNull("Directory name not nul", datadir);

    final String fileEndpointUri = "file://" + datadir + "/camel-file-tests";

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from(fileEndpointUri + "?noop=true&idempotent=true&delay=10")
            .to("mock:result");
        }
    });

    camelctx.start();
    try {
        MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
        mockEndpoint.expectedMessageCount(1);

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader(fileEndpointUri, "Testing idempotency", Exchange.FILE_NAME, "idempotent-test.txt");

        // Give the file consumer a chance to run multiple times
        Thread.sleep(300);

        mockEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 19
Source File: JsonValidatorIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidMessage() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        public void configure() throws Exception {
            from("file:target/validator?noop=true")
                .doTry()
                    .to("json-validator:jsonvalidator/schema.json")
                .doCatch(ValidationException.class)
                    .to("mock:invalid")
                .end();
        }
    });

    MockEndpoint mockInvalid = camelctx.getEndpoint("mock:invalid", MockEndpoint.class);
    mockInvalid.expectedMessageCount(1);

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("file:target/validator",
                "{ \"name\": \"Joe Doe\", \"id\": \"AA1\", \"price\": 12.5 }",
                Exchange.FILE_NAME, "invalid.json");

        mockInvalid.assertIsSatisfied();


        Assert.assertTrue("Can delete the file", FileUtil.deleteFile(new File("target/validator/invalid.json")));

    } finally {
      camelctx.close();
    }
}
 
Example 20
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"));
}