@Test @Ignore public void testRuleOnBody() throws Exception { Person person = new Person(); person.setName("Young Scott"); person.setAge(18); Cheese cheese = new Cheese(); cheese.setPrice(10); cheese.setType("Stilton"); // Add cheese ruleOnBodyEndpoint.requestBody(cheese, Cheese.class); // Check If Person can Drink Person response = ruleOnBodyEndpoint.requestBody(person, Person.class); assertNotNull(response); assertFalse(person.isCanDrink()); // Test for alternative result person.setName("Scott"); person.setAge(21); response = ruleOnBodyEndpoint.requestBody(person, Person.class); assertNotNull(response); assertTrue(person.isCanDrink()); }
@Test @Ignore // TODO drools-camel component should be improved to allow to set Global value on the session public void testRuleOnCommand() throws Exception { Person person = new Person(); person.setName("Young Scott"); person.setAge(18); Cheese cheese = new Cheese(); cheese.setPrice(10); cheese.setType("Stilton"); // Add a Person ruleOnBodyEndpoint.requestBody(cheese, Cheese.class); // Add cheese ruleOnBodyEndpoint.requestBody(cheese, Cheese.class); // Remark : passing person here is not required ExecutionResultImpl response = ruleOnCommandEndpoint.requestBody(person, ExecutionResultImpl.class); assertNotNull(response); // Expecting single result value of type Person Collection<String> identifiers = response.getIdentifiers(); assertNotNull(identifiers); assertTrue(identifiers.size() >= 1); for (String identifier : identifiers) { final Object value = response.getValue(identifier); assertNotNull(value); assertIsInstanceOf(Person.class, value); assertFalse(((Person) value).isCanDrink()); System.out.println(identifier + " = " + value); } // Test for alternative result person.setName("Scott"); person.setAge(21); response = ruleOnCommandEndpoint.requestBody(person, ExecutionResultImpl.class); assertNotNull(response); // Expecting single result value of type Person identifiers = response.getIdentifiers(); assertNotNull(identifiers); assertTrue(identifiers.size() >= 1); for (String identifier : identifiers) { final Object value = response.getValue(identifier); assertNotNull(value); assertIsInstanceOf(Person.class, value); assertTrue(((Person) value).isCanDrink()); System.out.println(identifier + " = " + value); } }
@Test public void testNoAuthenticationHeader() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes( new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .policy(new DomainAuthorizationPolicy()) .transform(body().prepend("Hello ")); } }); camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); try { producer.requestBody("direct:start", "Kermit", String.class); Assert.fail("CamelExecutionException expected"); } catch (CamelExecutionException e) { // expected } } finally { camelctx.stop(); } }
@Test public void testUnmarshalJackson() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes( new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").unmarshal().json(JsonLibrary.Jackson, Customer.class); } }); String input = "{'firstName':'John','lastName':'Doe'}"; camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); Customer customer = producer.requestBody("direct:start", input.replace('\'', '"'), Customer.class); Assert.assertEquals("John", customer.getFirstName()); Assert.assertEquals("Doe", customer.getLastName()); } finally { camelctx.stop(); } }
@Test public void testOgnlExpressionFromFile() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes( new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .choice() .when() .ognl("resource:classpath:test-ognl-expression.txt") .transform(simple("Hello ${body.name}")) .otherwise() .to("mock:dlq"); } }); Person person = new Person(); person.setName("Kermit"); camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); String result = producer.requestBody("direct:start", person, String.class); Assert.assertEquals("Hello Kermit", result); } finally { camelctx.stop(); } }
@Test public void testContextScan() throws Exception { CamelContext camelctx = contextRegistry.getCamelContext("contextScanA"); Assert.assertEquals(ServiceStatus.Started, camelctx.getStatus()); ProducerTemplate producer = camelctx.createProducerTemplate(); String result = producer.requestBody("direct:start", "Kermit", String.class); Assert.assertEquals("Hello Kermit", result); }
private static void put(CamelContext camelContext) { Put put = new Put(Bytes.toBytes("row1")); put.add(Bytes.toBytes("fam1"), Bytes.toBytes("qual1"), Bytes.toBytes("val1")); put.add(Bytes.toBytes("fam2"), Bytes.toBytes("qual2"), Bytes.toBytes("val2")); put.add(Bytes.toBytes("fam3"), Bytes.toBytes("qual3"), Bytes.toBytes("val3")); ProducerTemplate producer = camelContext.createProducerTemplate(); producer.requestBody("direct:start-put", put); }
@Test public void testCallFromCamel() throws Exception { // get camel context CamelContext context = (CamelContext) applicationContext.getBean("conduit_context"); ProducerTemplate producer = context.createProducerTemplate(); // The echo parameter will be ignore, since the service has the fix response String response = producer.requestBody("direct://jbiStart", "Hello World!", String.class); assertEquals("Get a wrong response ", "echo Hello World!", response); }
private void sendRequest() throws Exception { String response = producerTemplate.requestBody( "netty:tcp://localhost:{{port}}?sync=true&ssl=true&sslContextParameters=#sslContextParameters", "Epitaph in Kohima, India marking the WWII Battle of Kohima and Imphal, Burma Campaign - Attributed to John Maxwell Edmonds", String.class); assertEquals( "When You Go Home, Tell Them Of Us And Say, For Your Tomorrow, We Gave Our Today.", response); }
public Object requestBody(String endpointUri, Object body) throws Exception { ProducerTemplate template = context.createProducerTemplate(); Object answer = null; try { answer = template.requestBody(endpointUri, body); } finally { template.stop(); } return answer; }
@Test public void shouldDLQOrginalHandler() throws Exception { ModelCamelContext context = new DefaultCamelContext(); context.setTracing(true); ProducerTemplate pt = context.createProducerTemplate(); context.addRoutes( new RouteBuilder() { @Override public void configure() throws Exception { // will use original ErrorHandlerBuilder a = deadLetterChannel("seda:dead") .maximumRedeliveries(1) .redeliveryDelay(300) .logStackTrace(false) .useOriginalMessage() .logHandled(false); from("seda:start") .errorHandler(a) .log(LoggingLevel.INFO, "myCamel", "==== ${body}") .bean(SampleErrorBean.class) .bean(ChangeBody.class) .bean(ErrorBean.class) .bean(SampleErrorBean.class) .transform(constant("ok")); from("seda:dead") .bean(FailureBean.class) .process( exchange -> { counterA.incrementAndGet(); Throwable e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class); log.info("+++ properties : {}", exchange.getProperties()); log.info("+++ body : {}", exchange.getIn().getBody()); log.info("+++ headers : {}", exchange.getIn().getHeaders()); log.info("+++ exception Processor : {}", e); log.info( "+++ counterA : {} , header : {}", counterA.get(), exchange.getProperty("CamelExceptionCaught")); }) .convertBodyTo(String.class) .bean(SampleErrorBean.class); } }); context.start(); String result = (String) pt.requestBody("seda:start", "test"); Assertions.assertThat(result).isEqualTo("test"); Thread.sleep(3000); }
@Test @Ignore public void testRuleOnBody() throws Exception { Person person = new Person(); person.setName("Young Scott"); person.setAge(18); Person response = ruleOnBodyEndpoint.requestBody(person, Person.class); assertNotNull(response); assertFalse(person.isCanDrink()); // Test for alternative result person.setName("Scott"); person.setAge(21); response = ruleOnBodyEndpoint.requestBody(person, Person.class); assertNotNull(response); assertTrue(person.isCanDrink()); }
@Test public void testCamelRoute() throws Exception { String body = "<data><stuff/></data>\n"; String response = inputEndpoint.requestBody( "mina:tcp://localhost:9001?textline=true&sync=true", body, java.lang.String.class); assertEquals( "Incorrect Response", "<data><stuff/><controller>Controller 1</controller></data>", response); }
@Test public void testUDPObjectInOutWithNettyConsumer() throws Exception { if (LOG.isDebugEnabled()) { LOG.debug("Beginning Test ---> testUDPInOutWithNettyConsumer()"); } Poetry poetry = new Poetry(); Poetry response = producerTemplate.requestBody("netty:udp://localhost:5155?sync=true", poetry, Poetry.class); assertEquals("Dr. Sarojini Naidu", response.getPoet()); if (LOG.isDebugEnabled()) { LOG.debug("Completed Test ---> testUDPInOutWithNettyConsumer()"); } }
private static void get(CamelContext camelContext) { Get get = new Get(Bytes.toBytes("row1")); ProducerTemplate producer = camelContext.createProducerTemplate(); Object object = producer.requestBody("direct:start-get", get); KeyValue[] keyValues = (KeyValue[]) object; for (KeyValue keyValue : keyValues) { logger.info( "key: " + Bytes.toString(keyValue.getKey()) + " value: " + Bytes.toString(keyValue.getValue())); } }
@Ignore @Test @DirtiesContext public void validHL7IsConvertedAndAckIsSent() throws Exception { standardHL7XMLEndpoint.expectedMessageCount(1); t5XMLEndpoint.expectedMessageCount(1); String hl7Input = "MSH|^~\\&|SendingApplication|SendingFacility|ReceivingApplication|ReceivingFacility|20120530112345||ORU^R01^ORU_R01|9879790003|P|2.6|||NE|AL|USA|ASCII|EN^English^ISO639||IHE_PCD_ORU_R01^IHE PCD^1.3.6.1.4.1.19376.1.6.1.1.1^ISO\r" + "PID|||HO2009001^^^NIST^PI||Hon^Albert^^^^^L|Adams^^^^^^L|19610101|M|||15 N Saguaro^^Tucson^AZ^85701\r" + "PV1||I|ICU^2^23\r" + "OBR|1|||44616-1^Pulse oximetry panel ^LN|||20120512031234||||||||||||||||||F|||||||||||||||||||252465000^Pulse oximetry^SCT|7087005^Intermittent^SCT\r" + "NTE|1||This comment refers to all the results in the battery\r" + "OBX|1|NM|8889-8^Heart Rate by Oximetry^LN^149530^ MDC_PULS_OXIM_PULS_RATE^MDC|1.11.2.3|55|{beats}/min^beats per minute^UCUM|35-125||99||R|||20120530112340|||AMEAS^auto-measurement^MDC|0123456789ABCDEF^Pulse_Oximeter_Vendor_X^0123456789ABCDEF^EUI-64||49521004^left ear structure^SCT\n" + "NTE|1||This is a comment about pulse"; Parser parser = new PipeParser(); ORU_R01 originalMessage = (ORU_R01) parser.parse(hl7Input); String encodedHL7 = parser.encode(originalMessage); String response = minaTemplate.requestBody((Object) encodedHL7, String.class); ACK responseMessage = (ACK) parser.parse(response); assertEquals( AcknowledgmentCode.AA.name(), responseMessage.getMSA().getAcknowledgmentCode().getValue()); standardHL7XMLEndpoint.assertIsSatisfied(); t5XMLEndpoint.assertIsSatisfied(); ORU_R01 outputStandardXMLMessage = (ORU_R01) parseXMLExchange(standardHL7XMLEndpoint.getReceivedExchanges().get(0)); assertEquals( originalMessage.getMSH().getMessageType().getName(), outputStandardXMLMessage.getMSH().getMessageType().getName()); assertEquals( originalMessage.getMSH().getMessageControlID().getValue(), outputStandardXMLMessage.getMSH().getMessageControlID().getValue()); }
@Test public void shouldDLQHandler() throws Exception { ModelCamelContext context = new DefaultCamelContext(); ProducerTemplate pt = context.createProducerTemplate(); context.addRoutes( new RouteBuilder() { @Override public void configure() throws Exception { ErrorHandlerBuilder b = deadLetterChannel("direct:dead") .maximumRedeliveries(3) .redeliveryDelay(300) .logStackTrace(true) .logHandled(true); from("seda:start") .errorHandler(b) .log(LoggingLevel.INFO, "myCamel", "==== ${body}") .bean(SampleErrorBean.class) .bean(ChangeBody.class) .bean(ErrorBean.class) .bean(SampleErrorBean.class) .transform(constant("ok")); from("direct:dead") .bean(FailureBean.class) .process(new ErrorLogProcessor()) .convertBodyTo(String.class) .bean(SampleErrorBean.class); } }); context.start(); String result = (String) pt.requestBody("seda:start", "test"); Assertions.assertThat(result).isEqualTo("change :: test"); Thread.sleep(3000); }
@Test public void testMarshalJackson() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes( new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").marshal().json(JsonLibrary.Jackson); } }); 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.stop(); } }
@Test @Ignore @DirtiesContext public void invalidMessageReutrnsApplicationReject() throws Exception { standardHL7XMLEndpoint.expectedMessageCount(1); t5XMLEndpoint.expectedMessageCount(1); String invalidHL7Input = "MSH|^~\\&|SendingApplication|SendingFacility|ReceivingApplication|ReceivingFacility|20120530112345||ORU^R01^ORU_R01|9879790003|P|2.6|||NE|AL|USA|ASCII|EN^English^ISO639||IHE_PCD_ORU_R01^IHE PCD^1.3.6.1.4.1.19376.1.6.1.1.1^ISO\n" + "PID|||HO2009001^^^NIST^PI||Hon^Albert^^^^^L|Adams^^^^^^L|19610101|M|||15 N Saguaro^^Tucson^AZ^85701\n" + "PV1||I|ICU^2^23\n" + "OBR|1|||44616-1^Pulse oximetry panel ^LN|||20120512031234||||||||||||||||||F|||||||||||||||||||252465000^Pulse oximetry^SCT|7087005^Intermittent^SCT\n" + "NTE|1||This comment refers to all the results in the battery\n" + "OBX||||||35-125||99||R|||20120530112340|||AMEAS^auto-measurement^MDC|0123456789ABCDEF^Pulse_Oximeter_Vendor_X^0123456789ABCDEF^EUI-64||49521004^left ear structure^SCT\n" + "NTE|1||This is a comment about pulse"; String response = minaTemplate.requestBody((Object) invalidHL7Input, String.class); Parser parser = new PipeParser(); ACK responseMessage = (ACK) parser.parse(response); assertEquals( AcknowledgmentCode.AR.name(), responseMessage.getMSA().getAcknowledgmentCode().getValue()); }
@Test public void testRequestReply() { assertEquals("Hello, Camel!", producerTemplate.requestBody("Camel")); }
/** * {@inheritDoc} * * <p>This method forwards the current {@code item} to the configured Camel endpoint for * processing, and also retrieves the processing result. * * @param item the current item to be processed * @return processing result * @throws Exception */ @Override public Object processItem(final Object item) throws Exception { return producerTemplate.requestBody(endpoint, item); }
public String lookup(int num) { return producerTemplate.requestBody(endpoint, num, String.class); }