http://git-wip-us.apache.org/repos/asf/camel/blob/2a0a9e66/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftConsumerSyncTest.java ---------------------------------------------------------------------- diff --git a/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftConsumerSyncTest.java b/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftConsumerSyncTest.java new file mode 100644 index 0000000..7405aa3 --- /dev/null +++ b/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftConsumerSyncTest.java @@ -0,0 +1,118 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.thrift; + +import java.io.IOException; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.thrift.generated.Calculator; +import org.apache.camel.component.thrift.generated.Operation; +import org.apache.camel.component.thrift.generated.Work; +import org.apache.camel.test.AvailablePortFinder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.transport.TFramedTransport; +import org.apache.thrift.transport.TSocket; +import org.apache.thrift.transport.TTransport; +import org.apache.thrift.transport.TTransportException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ThriftConsumerSyncTest extends CamelTestSupport { + private static final Logger LOG = LoggerFactory.getLogger(ThriftConsumerSyncTest.class); + private static final int THRIFT_TEST_PORT = AvailablePortFinder.getNextAvailable(); + private static final int THRIFT_TEST_NUM1 = 12; + private static final int THRIFT_TEST_NUM2 = 13; + private static Calculator.Client thriftClient; + + private TProtocol protocol; + private TTransport transport; + + @Before + public void startThriftClient() throws IOException, TTransportException { + if (transport == null) { + LOG.info("Connecting to the Thrift server on port: {}", THRIFT_TEST_PORT); + transport = new TSocket("localhost", THRIFT_TEST_PORT); + transport.open(); + protocol = new TBinaryProtocol(new TFramedTransport(transport)); + thriftClient = (new Calculator.Client.Factory()).getClient(protocol); + } + } + + @After + public void stopThriftClient() throws Exception { + if (transport != null) { + transport.close(); + transport = null; + LOG.info("Connection to the Thrift server closed"); + } + } + + @Test + public void testCalculateMethodInvocation() throws Exception { + Work work = new Work(THRIFT_TEST_NUM1, THRIFT_TEST_NUM2, Operation.MULTIPLY); + + int calculateResult = thriftClient.calculate(1, work); + + MockEndpoint mockEndpoint = getMockEndpoint("mock:thrift-service"); + mockEndpoint.expectedMessageCount(1); + mockEndpoint.expectedHeaderValuesReceivedInAnyOrder(ThriftConstants.THRIFT_METHOD_NAME_HEADER, "calculate"); + mockEndpoint.assertIsSatisfied(); + + assertEquals(THRIFT_TEST_NUM1 * THRIFT_TEST_NUM2, calculateResult); + } + + @Test + public void testEchoMethodInvocation() throws Exception { + Work echoResult = thriftClient.echo(new Work(THRIFT_TEST_NUM1, THRIFT_TEST_NUM2, Operation.MULTIPLY)); + + MockEndpoint mockEndpoint = getMockEndpoint("mock:thrift-service"); + mockEndpoint.expectedMessageCount(1); + mockEndpoint.expectedHeaderValuesReceivedInAnyOrder(ThriftConstants.THRIFT_METHOD_NAME_HEADER, "echo"); + mockEndpoint.assertIsSatisfied(); + + assertNotNull(echoResult); + assertTrue(echoResult instanceof Work); + assertEquals(THRIFT_TEST_NUM1, ((Work)echoResult).num1); + assertEquals(Operation.MULTIPLY, ((Work)echoResult).op); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() { + + from("thrift://localhost:" + THRIFT_TEST_PORT + "/org.apache.camel.component.thrift.generated.Calculator?synchronous=true") + .to("mock:thrift-service").choice() + .when(header(ThriftConstants.THRIFT_METHOD_NAME_HEADER).isEqualTo("calculate")).setBody(simple(new Integer(THRIFT_TEST_NUM1 * THRIFT_TEST_NUM2).toString())) + .when(header(ThriftConstants.THRIFT_METHOD_NAME_HEADER).isEqualTo("echo")).setBody(simple("${body[0]}")).bean(new CalculatorMessageBuilder(), "echo"); + } + }; + } + + public class CalculatorMessageBuilder { + public Work echo(Work work) { + return work.deepCopy(); + } + } +}
http://git-wip-us.apache.org/repos/asf/camel/blob/2a0a9e66/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftProducerAsyncTest.java ---------------------------------------------------------------------- diff --git a/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftProducerAsyncTest.java b/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftProducerAsyncTest.java new file mode 100644 index 0000000..9b0953f --- /dev/null +++ b/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftProducerAsyncTest.java @@ -0,0 +1,288 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.thrift; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.thrift.generated.InvalidOperation; +import org.apache.camel.component.thrift.generated.Operation; +import org.apache.camel.component.thrift.generated.Work; +import org.apache.camel.support.SynchronizationAdapter; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ThriftProducerAsyncTest extends ThriftProducerBaseTest { + private static final Logger LOG = LoggerFactory.getLogger(ThriftProducerAsyncTest.class); + + private Object responseBody; + + @Test + @SuppressWarnings({"rawtypes", "unchecked"}) + public void testCalculateMethodInvocation() throws Exception { + LOG.info("Thrift calculate method async test start"); + + List requestBody = new ArrayList(); + final CountDownLatch latch = new CountDownLatch(1); + + requestBody.add((int)1); + requestBody.add(new Work(THRIFT_TEST_NUM1, THRIFT_TEST_NUM2, Operation.MULTIPLY)); + + template.asyncCallbackSendBody("direct:thrift-calculate", requestBody, new SynchronizationAdapter() { + + @Override + public void onComplete(Exchange exchange) { + responseBody = exchange.getOut().getBody(); + latch.countDown(); + } + + @Override + public void onFailure(Exchange exchange) { + responseBody = exchange.getException(); + latch.countDown(); + } + }); + latch.await(5, TimeUnit.SECONDS); + + assertNotNull(responseBody); + assertTrue(responseBody instanceof Integer); + assertEquals(THRIFT_TEST_NUM1 * THRIFT_TEST_NUM2, responseBody); + } + + @Test + @SuppressWarnings({"rawtypes", "unchecked"}) + public void testAddMethodInvocation() throws Exception { + LOG.info("Thrift add method (primitive parameters only) async test start"); + + final CountDownLatch latch = new CountDownLatch(1); + List requestBody = new ArrayList(); + responseBody = null; + + requestBody.add((int)THRIFT_TEST_NUM1); + requestBody.add((int)THRIFT_TEST_NUM2); + + template.asyncCallbackSendBody("direct:thrift-add", requestBody, new SynchronizationAdapter() { + + @Override + public void onComplete(Exchange exchange) { + responseBody = exchange.getOut().getBody(); + latch.countDown(); + } + + @Override + public void onFailure(Exchange exchange) { + responseBody = exchange.getException(); + latch.countDown(); + } + }); + latch.await(5, TimeUnit.SECONDS); + + assertNotNull(responseBody); + assertTrue(responseBody instanceof Integer); + assertEquals(THRIFT_TEST_NUM1 + THRIFT_TEST_NUM2, responseBody); + } + + @Test + @SuppressWarnings({"rawtypes", "unchecked"}) + public void testCalculateWithException() throws Exception { + LOG.info("Thrift calculate method with business exception async test start"); + + final CountDownLatch latch = new CountDownLatch(1); + List requestBody = new ArrayList(); + + requestBody.add((int)1); + requestBody.add(new Work(THRIFT_TEST_NUM1, 0, Operation.DIVIDE)); + + template.asyncCallbackSendBody("direct:thrift-calculate", requestBody, new SynchronizationAdapter() { + + @Override + public void onComplete(Exchange exchange) { + latch.countDown(); + } + + @Override + public void onFailure(Exchange exchange) { + responseBody = exchange.getException(); + latch.countDown(); + } + }); + latch.await(5, TimeUnit.SECONDS); + + assertTrue("Get an InvalidOperation exception", responseBody instanceof InvalidOperation); + + } + + @Test + public void testVoidMethodInvocation() throws Exception { + LOG.info("Thrift method with empty parameters and void output async test start"); + + final CountDownLatch latch = new CountDownLatch(1); + final Object requestBody = null; + + responseBody = new Object(); + template.asyncCallbackSendBody("direct:thrift-ping", requestBody, new SynchronizationAdapter() { + + @Override + public void onComplete(Exchange exchange) { + responseBody = exchange.getOut().getBody(); + latch.countDown(); + } + + @Override + public void onFailure(Exchange exchange) { + responseBody = exchange.getException(); + latch.countDown(); + } + }); + latch.await(5, TimeUnit.SECONDS); + + assertNull(responseBody); + } + + @Test + public void testOneWayMethodInvocation() throws Exception { + LOG.info("Thrift one-way method async test start"); + + final CountDownLatch latch = new CountDownLatch(1); + final Object requestBody = null; + + responseBody = new Object(); + template.asyncCallbackSendBody("direct:thrift-zip", requestBody, new SynchronizationAdapter() { + + @Override + public void onComplete(Exchange exchange) { + responseBody = exchange.getOut().getBody(); + latch.countDown(); + } + + @Override + public void onFailure(Exchange exchange) { + responseBody = exchange.getException(); + latch.countDown(); + } + }); + latch.await(5, TimeUnit.SECONDS); + + assertNull(responseBody); + } + + @Test + @SuppressWarnings({"rawtypes", "unchecked"}) + public void testAllTypesMethodInvocation() throws Exception { + LOG.info("Thrift method with all possile types async test start"); + + final CountDownLatch latch = new CountDownLatch(1); + List requestBody = new ArrayList(); + + requestBody.add((boolean)true); + requestBody.add((byte)THRIFT_TEST_NUM1); + requestBody.add((short)THRIFT_TEST_NUM1); + requestBody.add((int)THRIFT_TEST_NUM1); + requestBody.add((long)THRIFT_TEST_NUM1); + requestBody.add((double)THRIFT_TEST_NUM1); + requestBody.add("empty"); + requestBody.add(ByteBuffer.allocate(10)); + requestBody.add(new Work(THRIFT_TEST_NUM1, THRIFT_TEST_NUM2, Operation.MULTIPLY)); + requestBody.add(new ArrayList<Integer>()); + requestBody.add(new HashSet<String>()); + requestBody.add(new HashMap<String, Long>()); + + responseBody = new Object(); + template.asyncCallbackSendBody("direct:thrift-alltypes", requestBody, new SynchronizationAdapter() { + + @Override + public void onComplete(Exchange exchange) { + responseBody = exchange.getOut().getBody(); + latch.countDown(); + } + + @Override + public void onFailure(Exchange exchange) { + responseBody = exchange.getException(); + latch.countDown(); + } + }); + latch.await(5, TimeUnit.SECONDS); + + assertNotNull(responseBody); + assertTrue(responseBody instanceof Integer); + assertEquals(1, responseBody); + } + + @Test + @SuppressWarnings({"rawtypes", "unchecked"}) + public void testEchoMethodInvocation() throws Exception { + LOG.info("Thrift echo method (return output as pass input parameter) async test start"); + + final CountDownLatch latch = new CountDownLatch(1); + List requestBody = new ArrayList(); + + requestBody.add(new Work(THRIFT_TEST_NUM1, THRIFT_TEST_NUM2, Operation.MULTIPLY)); + + responseBody = new Object(); + template.asyncCallbackSendBody("direct:thrift-echo", requestBody, new SynchronizationAdapter() { + + @Override + public void onComplete(Exchange exchange) { + responseBody = exchange.getOut().getBody(); + latch.countDown(); + } + + @Override + public void onFailure(Exchange exchange) { + responseBody = exchange.getException(); + latch.countDown(); + } + }); + latch.await(5, TimeUnit.SECONDS); + + assertNotNull(responseBody); + assertTrue(responseBody instanceof Work); + assertEquals(THRIFT_TEST_NUM1, ((Work)responseBody).num1); + assertEquals(Operation.MULTIPLY, ((Work)responseBody).op); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:thrift-calculate") + .to("thrift://localhost:" + THRIFT_TEST_PORT + "/org.apache.camel.component.thrift.generated.Calculator?method=calculate"); + from("direct:thrift-add") + .to("thrift://localhost:" + THRIFT_TEST_PORT + "/org.apache.camel.component.thrift.generated.Calculator?method=add"); + from("direct:thrift-ping") + .to("thrift://localhost:" + THRIFT_TEST_PORT + "/org.apache.camel.component.thrift.generated.Calculator?method=ping"); + from("direct:thrift-zip") + .to("thrift://localhost:" + THRIFT_TEST_PORT + "/org.apache.camel.component.thrift.generated.Calculator?method=zip"); + from("direct:thrift-alltypes") + .to("thrift://localhost:" + THRIFT_TEST_PORT + "/org.apache.camel.component.thrift.generated.Calculator?method=alltypes"); + from("direct:thrift-echo") + .to("thrift://localhost:" + THRIFT_TEST_PORT + "/org.apache.camel.component.thrift.generated.Calculator?method=echo"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/2a0a9e66/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftProducerBaseTest.java ---------------------------------------------------------------------- diff --git a/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftProducerBaseTest.java b/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftProducerBaseTest.java new file mode 100644 index 0000000..b0e3228 --- /dev/null +++ b/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftProducerBaseTest.java @@ -0,0 +1,204 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.thrift; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.camel.component.thrift.generated.Calculator; +import org.apache.camel.component.thrift.generated.InvalidOperation; +import org.apache.camel.component.thrift.generated.Work; +import org.apache.camel.test.AvailablePortFinder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.apache.thrift.TException; +import org.apache.thrift.async.AsyncMethodCallback; +import org.apache.thrift.server.THsHaServer; +import org.apache.thrift.server.THsHaServer.Args; +import org.apache.thrift.server.TServer; +import org.apache.thrift.transport.TNonblockingServerSocket; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ThriftProducerBaseTest extends CamelTestSupport { + protected static final int THRIFT_TEST_PORT = AvailablePortFinder.getNextAvailable(); + protected static final int THRIFT_TEST_NUM1 = 12; + protected static final int THRIFT_TEST_NUM2 = 13; + + private static final Logger LOG = LoggerFactory.getLogger(ThriftProducerBaseTest.class); + + private static TNonblockingServerSocket serverTransport; + private static TServer server; + @SuppressWarnings({"rawtypes"}) + private static Calculator.Processor processor; + + @BeforeClass + @SuppressWarnings({"unchecked", "rawtypes"}) + public static void startThriftServer() throws Exception { + processor = new Calculator.Processor(new CalculatorSyncServerImpl()); + serverTransport = new TNonblockingServerSocket(THRIFT_TEST_PORT); + server = new THsHaServer(new Args(serverTransport).processor(processor)); + Runnable simple = new Runnable() { + public void run() { + LOG.info("Thrift server started on port: {}", THRIFT_TEST_PORT); + server.serve(); + } + }; + new Thread(simple).start(); + } + + @AfterClass + public static void stopThriftServer() throws IOException { + if (server != null) { + server.stop(); + serverTransport.close(); + LOG.info("Thrift server stoped"); + } + } + + /** + * Test Thrift Calculator blocking server implementation + */ + public static class CalculatorSyncServerImpl implements Calculator.Iface { + + @Override + public void ping() throws TException { + } + + @Override + public int add(int num1, int num2) throws TException { + return num1 + num2; + } + + @Override + public int calculate(int logId, Work work) throws InvalidOperation, TException { + int val = 0; + switch (work.op) { + case ADD: + val = work.num1 + work.num2; + break; + case SUBTRACT: + val = work.num1 - work.num2; + break; + case MULTIPLY: + val = work.num1 * work.num2; + break; + case DIVIDE: + if (work.num2 == 0) { + InvalidOperation io = new InvalidOperation(); + io.whatOp = work.op.getValue(); + io.why = "Cannot divide by 0"; + throw io; + } + val = work.num1 / work.num2; + break; + default: + InvalidOperation io = new InvalidOperation(); + io.whatOp = work.op.getValue(); + io.why = "Unknown operation"; + throw io; + } + + return val; + } + + @Override + public void zip() throws TException { + } + + @Override + public Work echo(Work w) throws TException { + return w.deepCopy(); + } + + @Override + public int alltypes(boolean v1, byte v2, short v3, int v4, long v5, double v6, String v7, ByteBuffer v8, Work v9, List<Integer> v10, Set<String> v11, Map<String, Long> v12) + throws TException { + return 1; + } + } + + /** + * Test Thrift Calculator nonblocking server implementation + */ + public static class CalculatorAsyncServerImpl implements Calculator.AsyncIface { + + @Override + @SuppressWarnings({"rawtypes", "unchecked"}) + public void ping(AsyncMethodCallback resultHandler) throws TException { + resultHandler.onComplete(new Object()); + } + + @Override + public void add(int num1, int num2, AsyncMethodCallback<Integer> resultHandler) throws TException { + resultHandler.onComplete(new Integer(num1 + num2)); + } + + @Override + public void calculate(int logid, Work work, AsyncMethodCallback<Integer> resultHandler) throws TException { + int val = 0; + switch (work.op) { + case ADD: + val = work.num1 + work.num2; + break; + case SUBTRACT: + val = work.num1 - work.num2; + break; + case MULTIPLY: + val = work.num1 * work.num2; + break; + case DIVIDE: + if (work.num2 == 0) { + InvalidOperation io = new InvalidOperation(); + io.whatOp = work.op.getValue(); + io.why = "Cannot divide by 0"; + resultHandler.onError(io); + } + val = work.num1 / work.num2; + break; + default: + InvalidOperation io = new InvalidOperation(); + io.whatOp = work.op.getValue(); + io.why = "Unknown operation"; + resultHandler.onError(io); + } + resultHandler.onComplete(val); + } + + @Override + @SuppressWarnings({"rawtypes", "unchecked"}) + public void zip(AsyncMethodCallback resultHandler) throws TException { + resultHandler.onComplete(new Object()); + } + + @Override + public void echo(Work w, AsyncMethodCallback<Work> resultHandler) throws TException { + resultHandler.onComplete(w.deepCopy()); + } + + @Override + public void alltypes(boolean v1, byte v2, short v3, int v4, long v5, double v6, String v7, ByteBuffer v8, Work v9, List<Integer> v10, Set<String> v11, + Map<String, Long> v12, AsyncMethodCallback<Integer> resultHandler) + throws TException { + resultHandler.onComplete(new Integer(1)); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/2a0a9e66/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftProducerSyncTest.java ---------------------------------------------------------------------- diff --git a/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftProducerSyncTest.java b/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftProducerSyncTest.java new file mode 100644 index 0000000..536f45d --- /dev/null +++ b/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftProducerSyncTest.java @@ -0,0 +1,173 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.thrift; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; + +import org.apache.camel.CamelExecutionException; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.thrift.generated.InvalidOperation; +import org.apache.camel.component.thrift.generated.Operation; +import org.apache.camel.component.thrift.generated.Work; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ThriftProducerSyncTest extends ThriftProducerBaseTest { + private static final Logger LOG = LoggerFactory.getLogger(ThriftProducerSyncTest.class); + + @Test + @SuppressWarnings({"rawtypes", "unchecked"}) + public void testCalculateMethodInvocation() throws Exception { + LOG.info("Thrift calculate method sync test start"); + + List requestBody = new ArrayList(); + + requestBody.add((int)1); + requestBody.add(new Work(THRIFT_TEST_NUM1, THRIFT_TEST_NUM2, Operation.MULTIPLY)); + + Object responseBody = template.requestBody("direct:thrift-calculate", requestBody); + + assertNotNull(responseBody); + assertTrue(responseBody instanceof Integer); + assertEquals(THRIFT_TEST_NUM1 * THRIFT_TEST_NUM2, responseBody); + } + + @Test + @SuppressWarnings({"rawtypes", "unchecked"}) + public void testAddMethodInvocation() throws Exception { + LOG.info("Thrift add method (primitive parameters only) sync test start"); + + List requestBody = new ArrayList(); + + requestBody.add((int)THRIFT_TEST_NUM1); + requestBody.add((int)THRIFT_TEST_NUM2); + + Object responseBody = template.requestBody("direct:thrift-add", requestBody); + + assertNotNull(responseBody); + assertTrue(responseBody instanceof Integer); + assertEquals(THRIFT_TEST_NUM1 + THRIFT_TEST_NUM2, responseBody); + } + + @Test + @SuppressWarnings({"rawtypes", "unchecked"}) + public void testCalculateWithException() throws Exception { + LOG.info("Thrift calculate method with business exception sync test start"); + + List requestBody = new ArrayList(); + + requestBody.add((int)1); + requestBody.add(new Work(THRIFT_TEST_NUM1, 0, Operation.DIVIDE)); + + try { + template.requestBody("direct:thrift-calculate", requestBody); + fail("Expect the exception here"); + } catch (Exception ex) { + assertTrue("Expect CamelExecutionException", ex instanceof CamelExecutionException); + assertTrue("Get an InvalidOperation exception", ex.getCause() instanceof InvalidOperation); + } + } + + @Test + public void testVoidMethodInvocation() throws Exception { + LOG.info("Thrift method with empty parameters and void output sync test start"); + + Object requestBody = null; + Object responseBody = template.requestBody("direct:thrift-ping", requestBody); + assertNull(responseBody); + } + + @Test + public void testOneWayMethodInvocation() throws Exception { + LOG.info("Thrift one-way method sync test start"); + + Object requestBody = null; + Object responseBody = template.requestBody("direct:thrift-zip", requestBody); + assertNull(responseBody); + } + + @Test + @SuppressWarnings({"rawtypes", "unchecked"}) + public void testAllTypesMethodInvocation() throws Exception { + LOG.info("Thrift method with all possile types sync test start"); + + List requestBody = new ArrayList(); + + requestBody.add((boolean)true); + requestBody.add((byte)THRIFT_TEST_NUM1); + requestBody.add((short)THRIFT_TEST_NUM1); + requestBody.add((int)THRIFT_TEST_NUM1); + requestBody.add((long)THRIFT_TEST_NUM1); + requestBody.add((double)THRIFT_TEST_NUM1); + requestBody.add("empty"); + requestBody.add(ByteBuffer.allocate(10)); + requestBody.add(new Work(THRIFT_TEST_NUM1, THRIFT_TEST_NUM2, Operation.MULTIPLY)); + requestBody.add(new ArrayList<Integer>()); + requestBody.add(new HashSet<String>()); + requestBody.add(new HashMap<String, Long>()); + + Object responseBody = template.requestBody("direct:thrift-alltypes", requestBody); + + assertNotNull(responseBody); + assertTrue(responseBody instanceof Integer); + assertEquals(1, responseBody); + } + + @Test + @SuppressWarnings({"rawtypes", "unchecked"}) + public void testEchoMethodInvocation() throws Exception { + LOG.info("Thrift echo method (return output as pass input parameter) sync test start"); + + List requestBody = new ArrayList(); + + requestBody.add(new Work(THRIFT_TEST_NUM1, THRIFT_TEST_NUM2, Operation.MULTIPLY)); + + Object responseBody = template.requestBody("direct:thrift-echo", requestBody); + + assertNotNull(responseBody); + assertTrue(responseBody instanceof Work); + assertEquals(THRIFT_TEST_NUM1, ((Work)responseBody).num1); + assertEquals(Operation.MULTIPLY, ((Work)responseBody).op); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:thrift-calculate") + .to("thrift://localhost:" + THRIFT_TEST_PORT + "/org.apache.camel.component.thrift.generated.Calculator?method=calculate&synchronous=true"); + from("direct:thrift-add") + .to("thrift://localhost:" + THRIFT_TEST_PORT + "/org.apache.camel.component.thrift.generated.Calculator?method=add&synchronous=true"); + from("direct:thrift-ping") + .to("thrift://localhost:" + THRIFT_TEST_PORT + "/org.apache.camel.component.thrift.generated.Calculator?method=ping&synchronous=true"); + from("direct:thrift-zip") + .to("thrift://localhost:" + THRIFT_TEST_PORT + "/org.apache.camel.component.thrift.generated.Calculator?method=zip&synchronous=true"); + from("direct:thrift-alltypes") + .to("thrift://localhost:" + THRIFT_TEST_PORT + "/org.apache.camel.component.thrift.generated.Calculator?method=alltypes&synchronous=true"); + from("direct:thrift-echo") + .to("thrift://localhost:" + THRIFT_TEST_PORT + "/org.apache.camel.component.thrift.generated.Calculator?method=echo&synchronous=true"); + } + }; + } +}