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

The following examples show how to use org.apache.camel.CamelContext#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: XSLTIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testXSLTSaxonTransform() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").to("xslt-saxon:transform.xsl");
        }
    });

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        String customer = producer.requestBody("direct:start", readCustomerXml("/customer.xml"), String.class);
        Assert.assertEquals("John Doe", customer);
    } finally {
        camelctx.close();
    }
}
 
Example 2
Source File: SimpleProcessTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
@Deployment(resources = { "process/example.bpmn20.xml" })
public void testRunProcessByKey() throws Exception {
    CamelContext ctx = applicationContext.getBean(CamelContext.class);
    ProducerTemplate tpl = ctx.createProducerTemplate();
    MockEndpoint me = (MockEndpoint) ctx.getEndpoint("mock:service1");
    me.expectedBodiesReceived("ala");

    tpl.sendBodyAndProperty("direct:start", Collections.singletonMap("var1", "ala"), FlowableProducer.PROCESS_KEY_PROPERTY, "key1");

    String instanceId = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey("key1").singleResult().getProcessInstanceId();
    tpl.sendBodyAndProperty("direct:receive", null, FlowableProducer.PROCESS_KEY_PROPERTY, "key1");

    assertProcessEnded(instanceId);

    me.assertIsSatisfied();
}
 
Example 3
Source File: SOAPIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testSoapV1_2Marshal() throws Exception {

    final SoapJaxbDataFormat format = new SoapJaxbDataFormat();
    format.setContextPath("org.wildfly.camel.test.jaxb.model");

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .marshal(format);
        }
    });

    camelctx.start();
    try (InputStream input = getClass().getResourceAsStream("/envelope-1.2-marshal.xml")) {
        String expected = camelctx.getTypeConverter().mandatoryConvertTo(String.class, input);
        ProducerTemplate producer = camelctx.createProducerTemplate();
        Customer customer = new Customer("John", "Doe");
        String customerXML = producer.requestBody("direct:start", customer, String.class);
        Assert.assertEquals(expected, XMLUtils.compactXML(customerXML));
    } finally {
        camelctx.close();
    }
}
 
Example 4
Source File: DirectVMSpringIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testSpringDirectVMComponent() throws Exception {
    CamelContext camelctxA = contextRegistry.getCamelContext("direct-vm-context-a");
    Assert.assertEquals(ServiceStatus.Started, camelctxA.getStatus());

    CamelContext camelctxB = contextRegistry.getCamelContext("direct-vm-context-b");
    Assert.assertEquals(ServiceStatus.Started, camelctxB.getStatus());

    MockEndpoint mockEndpoint = camelctxB.getEndpoint("mock:result", MockEndpoint.class);
    mockEndpoint.expectedBodiesReceived("Hello Kermit");

    ProducerTemplate template = camelctxA.createProducerTemplate();
    template.sendBody("direct:start", "Kermit");

    mockEndpoint.assertIsSatisfied();
}
 
Example 5
Source File: EmptyProcessTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
@Deployment(resources = { "process/empty.bpmn20.xml" })
public void testObjectAsStringVariable() throws Exception {
    CamelContext ctx = applicationContext.getBean(CamelContext.class);
    ProducerTemplate tpl = ctx.createProducerTemplate();
    Object expectedObj = 99L;

    Exchange exchange = ctx.getEndpoint("direct:startEmptyBodyAsString").createExchange();
    exchange.getIn().setBody(expectedObj);
    tpl.send("direct:startEmptyBodyAsString", exchange);

    String instanceId = (String) exchange.getProperty("PROCESS_ID_PROPERTY");

    assertProcessEnded(instanceId);
    HistoricVariableInstance var = processEngine.getHistoryService().createHistoricVariableInstanceQuery().variableName("camelBody").singleResult();
    assertThat(var).isNotNull();
    assertThat(var.getValue()).hasToString(expectedObj.toString());
}
 
Example 6
Source File: XSLTIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testXSLTTransform() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").to("xslt:transform.xsl");
        }
    });

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        String customer = producer.requestBody("direct:start", readCustomerXml("/customer.xml"), String.class);
        Assert.assertEquals("John Doe", customer);
    } finally {
        camelctx.close();
    }
}
 
Example 7
Source File: SipIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testPresenceAgentBasedPubSub() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(createRouteBuilder());

    MockEndpoint mockNeverland = camelctx.getEndpoint("mock:neverland", MockEndpoint.class);
    mockNeverland.expectedMessageCount(0);

    MockEndpoint mockNotification = camelctx.getEndpoint("mock:notification", MockEndpoint.class);
    mockNotification.expectedMinimumMessageCount(1);

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader(
                "sip://agent@localhost:" + port1 + "?stackName=client&eventHeaderName=evtHdrName&eventId=evtid&fromUser=user2&fromHost=localhost&fromPort=" + port3,
                "EVENT_A",
                "REQUEST_METHOD", Request.PUBLISH);

        mockNeverland.assertIsSatisfied();
        mockNotification.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 8
Source File: CSVIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testMarshalMap() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").marshal().csv();
        }
    });

    Map<String, String> map = new LinkedHashMap<>();
    map.put("firstName", "John");
    map.put("lastName", "Doe");

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        String result = producer.requestBody("direct:start", map, String.class);
        Assert.assertEquals("John,Doe", result.trim());
    } finally {
        camelctx.close();
    }
}
 
Example 9
Source File: DropboxIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testDropboxProducer() throws Exception {
    Assume.assumeNotNull("DROPBOX_ACCESS_TOKEN is null", DROPBOX_ACCESS_TOKEN);

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .toF("dropbox://get?accessToken=%s&clientIdentifier=CamelTesting&remotePath=/hello.txt", DROPBOX_ACCESS_TOKEN);
        }
    });

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        byte[] result = template.requestBody("direct:start", null, byte[].class);
        Assert.assertEquals("Hello Kermit\n", new String(result));
    } finally {
        camelctx.close();
    }
}
 
Example 10
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testContainsKey() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        Mockito.when(map.containsKey("testOk")).thenReturn(true);
        Mockito.when(map.containsKey("testKo")).thenReturn(false);

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

        ConsumerTemplate consumer = camelctx.createConsumerTemplate();
        Boolean body = consumer.receiveBody("seda:out", 5000, Boolean.class);
        Mockito.verify(map).containsKey("testOk");
        Assert.assertEquals(true, body);
        template.sendBodyAndHeader("direct:containsKey", null, HazelcastConstants.OBJECT_ID, "testKo");
        body = consumer.receiveBody("seda:out", 5000, Boolean.class);
        Mockito.verify(map).containsKey("testKo");
        Assert.assertEquals(false, body);
    } finally {
        camelctx.close();
    }
}
 
Example 11
Source File: SimpleTransformTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleTransform() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").transform(body().prepend("Hello "));
        }
    });

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        String result = producer.requestBody("direct:start", "Kermit", String.class);
        Assert.assertEquals("Hello Kermit", result);
    } finally {
        camelctx.close();
    }
}
 
Example 12
Source File: FhirJsonIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testFhirJsonMarshal() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .marshal().fhirJson("DSTU3");
        }
    });

    camelctx.start();
    try {
        Patient patient = createPatient();
        ProducerTemplate template = camelctx.createProducerTemplate();
        InputStream inputStream = template.requestBody("direct:start", patient, InputStream.class);
        IBaseResource result = FhirContext.forDstu3().newJsonParser().parseResource(new InputStreamReader(inputStream));
        Assert.assertTrue("Expected marshaled patient to be equal", patient.equalsDeep((Base)result));
    } finally {
        camelctx.close();
    }
}
 
Example 13
Source File: MicrometerCounterIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testOverrideUsingConstantValue() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:out", MockEndpoint.class);
        mockEndpoint.expectedMessageCount(1);

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("direct:in-3", null, HEADER_COUNTER_DECREMENT, 7.0D);

        Assert.assertEquals(417.0D, metricsRegistry.find("C").counter().count(), 0.01D);
        mockEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 14
Source File: SimpleProcessTest.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Deployment(resources = { "process/example.bpmn20.xml" })
public void testRunProcess() throws Exception {
  CamelContext ctx = applicationContext.getBean(CamelContext.class);
  ProducerTemplate tpl = ctx.createProducerTemplate();
  service1.expectedBodiesReceived("ala");

  Exchange exchange = ctx.getEndpoint("direct:start").createExchange();
  exchange.getIn().setBody(Collections.singletonMap("var1", "ala"));
  tpl.send("direct:start", exchange);
  String instanceId = (String) exchange.getProperty("PROCESS_ID_PROPERTY");

  tpl.sendBodyAndProperty("direct:receive", null, ActivitiProducer.PROCESS_ID_PROPERTY, instanceId);

  assertProcessEnded(instanceId);

  service1.assertIsSatisfied();
  Map<?, ?> m = service2.getExchanges().get(0).getIn().getBody(Map.class);
  assertEquals("ala", m.get("var1"));
  assertEquals("var2", m.get("var2"));
}
 
Example 15
Source File: EmptyProcessTest.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = { "process/empty.bpmn20.xml" })
public void testObjectAsVariable() throws Exception {
    CamelContext ctx = applicationContext.getBean(CamelContext.class);
    ProducerTemplate tpl = ctx.createProducerTemplate();
    Object expectedObj = Long.valueOf(99);
    Exchange exchange = ctx.getEndpoint("direct:startEmpty").createExchange();
    exchange.getIn().setBody(expectedObj);
    tpl.send("direct:startEmpty", exchange);
    String instanceId = (String) exchange.getProperty("PROCESS_ID_PROPERTY");
    assertProcessEnded(instanceId);
    HistoricVariableInstance var = processEngine.getHistoryService().createHistoricVariableInstanceQuery().variableName("camelBody").singleResult();
    assertNotNull(var);
    assertEquals(expectedObj, var.getValue());
}
 
Example 16
Source File: CXFRSServerTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testCxfJaxRsServer() throws Exception {
    CamelContext camelctx = contextRegistry.getCamelContext("cxfrs-server-context");
    Assert.assertNotNull("Expected cxfrs-server-context to not be null", camelctx);
    Assert.assertEquals(ServiceStatus.Started, camelctx.getStatus());

    ProducerTemplate template = camelctx.createProducerTemplate();
    String result = template.requestBody("direct:start", null, String.class);
    Assert.assertEquals("Hello Kermit", result);
}
 
Example 17
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test(expected = CamelExecutionException.class)
public void testWithInvalidOperation() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBody("direct:putInvalid", "my-foo");
    } finally {
        camelctx.close();
    }
}
 
Example 18
Source File: ElasticsearchIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testGet() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:index")
                .to("elasticsearch-rest://elasticsearch?operation=Index&indexName=twitter&hostAddresses=" + getElasticsearchHost());

            from("direct:get")
                .to("elasticsearch-rest://elasticsearch?operation=GetById&indexName=twitter&hostAddresses=" + getElasticsearchHost());
        }
    });

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

        //first, Index a value
        Map<String, String> map = createIndexedData("testGet");
        template.sendBody("direct:index", map);
        String indexId = template.requestBody("direct:index", map, String.class);
        Assert.assertNotNull("indexId should be set", indexId);

        //now, verify GET succeeded
        GetResponse response = template.requestBody("direct:get", indexId, GetResponse.class);
        Assert.assertNotNull("response should not be null", response);
        Assert.assertNotNull("response source should not be null", response.getSource());
    } finally {
        camelctx.close();
    }
}
 
Example 19
Source File: DynamoDBUtils.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
public static Map<?, ?> getItem(CamelContext camelctx) {
    HashMap<String, AttributeValue> key = new HashMap<>();
    key.put("Id", new AttributeValue().withN("103"));

    Exchange exchange = new ExchangeBuilder(camelctx)
            .withHeader(DdbConstants.OPERATION, DdbOperations.GetItem)
            .withHeader(DdbConstants.KEY, key).build();

    ProducerTemplate producer = camelctx.createProducerTemplate();
    producer.send("direct:start", exchange);
    Assert.assertNull(exchange.getException());

    return exchange.getIn().getHeader(DdbConstants.ATTRIBUTES, Map.class);
}
 
Example 20
Source File: ServiceNowIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testSearchIncidents() throws Exception {
    Map<String, Object> serviceNowOptions = createServiceNowOptions();

    Assume.assumeTrue("[#1674] Enable ServiceNow testing in Jenkins",
            serviceNowOptions.size() == ServiceNowIntegrationTest.ServiceNowOption.values().length);

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .to("servicenow:{{env:SERVICENOW_INSTANCE}}"
                + "?userName={{env:SERVICENOW_USERNAME}}"
                + "&password={{env:SERVICENOW_PASSWORD}}"
                + "&oauthClientId={{env:SERVICENOW_CLIENT_ID}}"
                + "&oauthClientSecret={{env:SERVICENOW_CLIENT_SECRET}}"
                + "&release={{env:SERVICENOW_RELEASE}}"
                + "&model.incident=org.wildfly.camel.test.servicenow.subA.Incident");
        }
    });

    camelctx.start();
    try {
        Map<String, Object> headers = new HashMap<>();
        headers.put(ServiceNowConstants.RESOURCE, "table");
        headers.put(ServiceNowConstants.ACTION, ServiceNowConstants.ACTION_RETRIEVE);
        headers.put(ServiceNowParams.PARAM_TABLE_NAME.getHeader(), "incident");

        ProducerTemplate producer = camelctx.createProducerTemplate();
        List<Incident> result = producer.requestBodyAndHeaders("direct:start", null, headers, List.class);

        Assert.assertNotNull(result);
        Assert.assertTrue(result.size() > 0);
    } finally {
        camelctx.close();
    }
}