Java Code Examples for org.apache.camel.CamelContext#addRoutes()
The following examples show how to use
org.apache.camel.CamelContext#addRoutes() .
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: PurchaseOrderUnmarshalBindyTest.java From camelinaction2 with Apache License 2.0 | 6 votes |
@Test public void testUnmarshalBindy() throws Exception { CamelContext context = new DefaultCamelContext(); context.addRoutes(createRoute()); context.start(); MockEndpoint mock = context.getEndpoint("mock:result", MockEndpoint.class); mock.expectedMessageCount(1); ProducerTemplate template = context.createProducerTemplate(); template.sendBody("direct:toObject", "Camel in Action,39.95,1"); mock.assertIsSatisfied(); // bindy returns the order directly (not in a list) if there is only one element PurchaseOrder order = mock.getReceivedExchanges().get(0).getIn().getBody(PurchaseOrder.class); assertNotNull(order); // assert the order contains the expected data assertEquals("Camel in Action", order.getName()); assertEquals("39.95", order.getPrice().toString()); assertEquals(1, order.getAmount()); }
Example 2
Source File: XmlSecurityIntegrationTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testXmlVerifySigning() throws Exception { CamelContext camelctx = new DefaultCamelContext(new JndiBeanRepository()); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .to("xmlsecurity-sign://enveloping?keyAccessor=#accessor&schemaResourceUri=") .to("xmlsecurity-verify://enveloping?keySelector=#selector"); } }); try { camelctx.start(); ProducerTemplate producer = camelctx.createProducerTemplate(); String verifiedXml = producer.requestBody("direct:start", XML_PAYLOAD, String.class); // Make sure the XML was unsigned Assert.assertEquals(XML_PAYLOAD, verifiedXml); } finally { camelctx.close(); } }
Example 3
Source File: BeanTransformTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testSimpleTransformFromModule() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").bean(HelloBean.class); } }); 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 4
Source File: SJMSIntegrationTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testMessageConsumerRoute() throws Exception { CamelContext camelctx = createCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("sjms:queue:" + QUEUE_NAME + "?connectionFactory=#cfactory"). transform(body().prepend("Hello ")).to("seda:end"); } }); camelctx.start(); PollingConsumer consumer = camelctx.getEndpoint("seda:end").createPollingConsumer(); consumer.start(); try { // Send a message to the queue ConnectionFactory cfactory = lookupConnectionFactory(); Connection connection = cfactory.createConnection(); try { sendMessage(connection, QUEUE_JNDI_NAME, "Kermit"); String result = consumer.receive(3000).getIn().getBody(String.class); Assert.assertEquals("Hello Kermit", result); } finally { connection.close(); } } finally { camelctx.close(); } }
Example 5
Source File: MvelTransformTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testSimpleTransformFromModule() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").to("mvel:template.mvel"); } }); 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 6
Source File: PurchaseOrderBindyTest.java From camelinaction with Apache License 2.0 | 6 votes |
@Test public void testBindy() throws Exception { final Locale locale = Locale.getDefault(); try { Locale.setDefault(Locale.US); CamelContext context = new DefaultCamelContext(); context.addRoutes(createRoute()); context.start(); MockEndpoint mock = context.getEndpoint("mock:result", MockEndpoint.class); mock.expectedBodiesReceived("Camel in Action,39.95,1\n"); PurchaseOrder order = new PurchaseOrder(); order.setAmount(1); order.setPrice(new BigDecimal("39.95")); order.setName("Camel in Action"); ProducerTemplate template = context.createProducerTemplate(); template.sendBody("direct:toCsv", order); mock.assertIsSatisfied(); } finally { Locale.setDefault(locale); } }
Example 7
Source File: JSONDataFormatTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testMarshalJohnzon() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .marshal().json(JsonLibrary.Johnzon); } }); String expected = "{'firstName':'John','lastName':'Doe'}"; camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); String result = producer.requestBody("direct:start", new Customer("John", "Doe"), String.class); Assert.assertEquals(expected.replace('\'', '"'), result); } finally { camelctx.close(); } }
Example 8
Source File: WordpressIntegrationTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testPostSingleRequest() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { configureComponent(getContext()); from("wordpress:post?id=114913").to("mock:resultSingle"); } }); camelctx.start(); try { MockEndpoint mock = camelctx.getEndpoint("mock:resultSingle", MockEndpoint.class); mock.expectedMinimumMessageCount(1); mock.allMessages().body().isInstanceOf(Post.class); mock.assertIsSatisfied(); } finally { camelctx.close(); } }
Example 9
Source File: UndertowIntegrationTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testUndertowProducer() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("undertow:http://localhost/myapp/serviceB") .setBody(simple("Hello ${header.name}")); } }); camelctx.start(); try { ProducerTemplate template = camelctx.createProducerTemplate(); String result = template.requestBody("undertow:http://localhost:8080/myapp/serviceB?name=Kermit", null, String.class); Assert.assertEquals("Hello Kermit", result); } finally { camelctx.close(); } }
Example 10
Source File: TikaIntegrationTest.java From wildfly-camel with Apache License 2.0 | 5 votes |
@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 11
Source File: FacebookIntegrationTest.java From wildfly-camel with Apache License 2.0 | 5 votes |
@Test public void testFacebookComponent() throws Exception { FacebookComponent component = new FacebookComponent(); FacebookConfiguration configuration = component.getConfiguration(); String baseURL = "http://localhost:8080/camel-facebook-tests/fake-facebook-api"; configuration.setClientURL(baseURL); configuration.setOAuthAccessTokenURL(baseURL + "/oauth-token"); configuration.setRestBaseURL(baseURL + "/rest"); CamelContext camelctx = new DefaultCamelContext(); camelctx.addComponent("facebook", component); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .toF("facebook://getTestUsers?oAuthAppId=%s&oAuthAppSecret=%s&appId=%s", FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, FACEBOOK_APP_ID); } }); camelctx.start(); try { ProducerTemplate template = camelctx.createProducerTemplate(); PagableList testUserList = template.requestBody("direct:start", null, PagableList.class); Assert.assertNotNull("Facebook app test user list was null", testUserList); } finally { camelctx.close(); } }
Example 12
Source File: TikaIntegrationTest.java From wildfly-camel with Apache License 2.0 | 5 votes |
@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 13
Source File: JsonValidatorIntegrationTest.java From wildfly-camel with Apache License 2.0 | 5 votes |
@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 14
Source File: AbstractRestDslIntegrationTest.java From wildfly-camel with Apache License 2.0 | 5 votes |
@Test public void testRestDslCorsDisabled() throws Exception { RestConfiguration restConfiguration = createRestConfiguration(); restConfiguration.setEnableCORS(false); CamelContext camelctx = new DefaultCamelContext(); camelctx.setRestConfiguration(createRestConfiguration()); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { rest() .get("/test") .route() .setBody(constant("GET: /test")) .endRest(); } }); camelctx.start(); try { Map<String, String> headers = client.getResponse("test", "OPTIONS").getHeaders(); for (String header : CORS_HEADERS) { Assert.assertFalse(headers.containsKey(header)); } } finally { camelctx.close(); } }
Example 15
Source File: SlackIntegrationTest.java From wildfly-camel with Apache License 2.0 | 5 votes |
@Test public void testSlackMessage() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:test").to("slack:#general?iconEmoji=:camel:&username=CamelTest").to("mock:result"); from("undertow:http://localhost/slack/webhook").setBody(constant("{\"ok\": true}")); } }); SlackComponent comp = camelctx.getComponent("slack", SlackComponent.class); comp.setWebhookUrl("http://localhost:8080/slack/webhook"); MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class); mockEndpoint.expectedMessageCount(1); camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); producer.sendBody("direct:test", "Hello from Camel!"); mockEndpoint.assertIsSatisfied(); } finally { camelctx.close(); } }
Example 16
Source File: CamelToCamelRequestReply.java From hazelcastmq with Apache License 2.0 | 4 votes |
@Override public void start() throws Exception { // Create a Hazelcast instance. Config config = new Config(); config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(config); try { // Create the HazelcaseMQ instance. HazelcastMQConfig mqConfig = new HazelcastMQConfig(); mqConfig.setHazelcastInstance(hazelcast); HazelcastMQInstance mqInstance = HazelcastMQ .newHazelcastMQInstance(mqConfig); // Create the camel component. HazelcastMQCamelConfig mqCamelConfig = new HazelcastMQCamelConfig(); mqCamelConfig.setHazelcastMQInstance(mqInstance); HazelcastMQCamelComponent mqCamelComponent = new HazelcastMQCamelComponent(); mqCamelComponent.setConfiguration(mqCamelConfig); // Create the Camel context. This could be done via a Spring XML file. CamelContext camelContext = new DefaultCamelContext(); camelContext.addComponent("hazelcastmq", mqCamelComponent); camelContext.addRoutes(new RouteBuilder() { @Override public void configure() { from("direct:primo.test") .to(ExchangePattern.InOut, "hazelcastmq:queue:secondo.test"); from("hazelcastmq:queue:secondo.test").delay(1500) .setBody(constant("Goodbye World!")); } }); camelContext.start(); // Create a Camel producer. ProducerTemplate camelProducer = camelContext.createProducerTemplate(); camelProducer.start(); // Send a message to the direct endpoint. String reply = (String) camelProducer.sendBody("direct:primo.test", ExchangePattern.InOut, "Hello World!"); if (reply == null) { log.warn("Did not get expected message!"); } else { log.info("Got reply message: " + reply); } camelContext.stop(); } finally { // Shutdown Hazelcast. hazelcast.getLifecycleService().shutdown(); } }
Example 17
Source File: SecureServletIntegrationTest.java From wildfly-camel with Apache License 2.0 | 4 votes |
@Test public void testSecureServletRoute() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { restConfiguration() .component("servlet"); rest() .get("/insecure") .route() .setBody(constant("GET: /insecure")) .endRest() .get("/secure") .route() .setBody(constant("GET: /secure")) .endRest(); } }); camelctx.start(); try { // Insecure should work without authentication HttpResponse response = HttpRequest.get("http://localhost:8080/camel-secure-servlet/services/insecure") .throwExceptionOnFailure(false) .getResponse(); Assert.assertEquals(200, response.getStatusCode()); Assert.assertEquals("GET: /insecure", response.getBody()); // No credentials provided so expect HTTP 401 int statusCode = HttpRequest.get("http://localhost:8080/camel-secure-servlet/services/secure") .throwExceptionOnFailure(false) .getResponse() .getStatusCode(); Assert.assertEquals(401, statusCode); // Invalid credentials provided so expect HTTP 401 statusCode = HttpRequest.get("http://localhost:8080/camel-secure-servlet/services/secure") .throwExceptionOnFailure(false) .header("Authorization", "Basic " + printBase64Binary("bad-user:bad-password".getBytes(StandardCharsets.UTF_8))) .getResponse() .getStatusCode(); Assert.assertEquals(401, statusCode); // Pass valid credentials and expect HTTP 200 response = HttpRequest.get("http://localhost:8080/camel-secure-servlet/services/secure") .throwExceptionOnFailure(false) .header("Authorization", "Basic " + printBase64Binary("camel-servlet-user:camel-servlet-password".getBytes(StandardCharsets.UTF_8))) .getResponse(); Assert.assertEquals(200, response.getStatusCode()); Assert.assertEquals("GET: /secure", response.getBody()); } finally { camelctx.close(); } }
Example 18
Source File: RxJava2IntegrationTest.java From wildfly-camel with Apache License 2.0 | 4 votes |
@Test public void testSingleConsumer() throws Exception { CamelContext camelctx = createCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("reactive-streams:singleConsumer") .process() .message(m -> m.setHeader("thread", Thread.currentThread().getId())) .to("mock:singleBucket"); } }); CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx); camelctx.start(); try { Flowable.range(0, 1000).subscribe( crs.streamSubscriber("singleConsumer", Number.class) ); MockEndpoint endpoint = camelctx.getEndpoint("mock:singleBucket", MockEndpoint.class); endpoint.expectedMessageCount(1000); endpoint.assertIsSatisfied(); Assert.assertEquals( 1, endpoint.getExchanges().stream() .map(x -> x.getIn().getHeader("thread", String.class)) .distinct() .count() ); // Ensure order is preserved when using a single consumer AtomicLong num = new AtomicLong(0); endpoint.getExchanges().stream() .map(x -> x.getIn().getBody(Long.class)) .forEach(n -> Assert.assertEquals(num.getAndIncrement(), n.longValue())); } finally { camelctx.close(); } }
Example 19
Source File: ElasticsearchIntegrationTest.java From wildfly-camel with Apache License 2.0 | 4 votes |
@Test public void testSearchWithMapQuery() 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()); from("direct:search") .to("elasticsearch-rest://elasticsearch?operation=Search&indexName=twitter&hostAddresses=" + getElasticsearchHost()); } }); camelctx.start(); try { ProducerTemplate template = camelctx.createProducerTemplate(); //first, Index a value Map<String, String> map = createIndexedData("testSearchWithMapQuery"); String indexId = template.requestBody("direct:index", map, String.class); Assert.assertNotNull("indexId should be set", indexId); //now, verify GET succeeded GetResponse getResponse = template.requestBody("direct:get", indexId, GetResponse.class); Assert.assertNotNull("response should not be null", getResponse); Assert.assertNotNull("response source should not be null", getResponse.getSource()); //now, verify GET succeeded Map<String, Object> actualQuery = new HashMap<>(); actualQuery.put("testsearchwithmapquery-key", "testsearchwithmapquery-value"); Map<String, Object> match = new HashMap<>(); match.put("match", actualQuery); Map<String, Object> query = new HashMap<>(); query.put("query", match); SearchHits response = template.requestBody("direct:search", match, SearchHits.class); Assert.assertNotNull("response should not be null", response); Assert.assertEquals("response hits should be == 0", 0, response.getTotalHits().value); } finally { camelctx.close(); } }
Example 20
Source File: CryptoCmsIntegrationTest.java From wildfly-camel with Apache License 2.0 | 4 votes |
@Test public void testCryptoCmsSignEncryptDecryptVerify() throws Exception { Assume.assumeFalse("[#2241] CryptoCmsIntegrationTest fails on IBM JDK", EnvironmentUtils.isIbmJDK() || EnvironmentUtils.isOpenJDK()); CamelContext camelctx = new DefaultCamelContext(new JndiBeanRepository()); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .to("crypto-cms:sign://testsign?signer=#signer1,signer=#signer2&includeContent=true") .to("crypto-cms:encrypt://testencrpyt?toBase64=true&recipient=#recipient1&contentEncryptionAlgorithm=DESede/CBC/PKCS5Padding&secretKeyLength=128") .to("crypto-cms:decrypt://testdecrypt?fromBase64=true&keyStoreParameters=#keyStoreParameters") .to("crypto-cms:verify://testverify?keyStoreParameters=#keyStoreParameters") .to("mock:result"); } }); KeyStoreParameters keyStoreParameters = new KeyStoreParameters(); keyStoreParameters.setType("JCEKS"); keyStoreParameters.setResource("/crypto.keystore"); keyStoreParameters.setPassword("Abcd1234"); DefaultKeyTransRecipientInfo recipient = new DefaultKeyTransRecipientInfo(); recipient.setCertificateAlias("rsa"); recipient.setKeyStoreParameters(keyStoreParameters); DefaultSignerInfo signerInfo = new DefaultSignerInfo(); signerInfo.setIncludeCertificates(true); signerInfo.setSignatureAlgorithm("SHA256withRSA"); signerInfo.setPrivateKeyAlias("rsa"); signerInfo.setKeyStoreParameters(keyStoreParameters); DefaultSignerInfo signerInfo2 = new DefaultSignerInfo(); signerInfo2.setSignatureAlgorithm("SHA256withDSA"); signerInfo2.setPrivateKeyAlias("dsa"); signerInfo2.setKeyStoreParameters(keyStoreParameters); MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class); mockEndpoint.expectedBodiesReceived("Testmessage"); context.bind("keyStoreParameters", keyStoreParameters); context.bind("signer1", signerInfo); context.bind("signer2", signerInfo2); context.bind("recipient1", recipient); camelctx.start(); try { ProducerTemplate template = camelctx.createProducerTemplate(); template.sendBody("direct:start", "Testmessage"); mockEndpoint.assertIsSatisfied(); } finally { camelctx.close(); context.unbind("keyStoreParameters"); context.unbind("signer1"); context.unbind("signer2"); context.unbind("recipient1"); } }