camel-mybatis: add outputHeader parameter (CAMEL-8192)
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2c4c06c1 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2c4c06c1 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2c4c06c1 Branch: refs/heads/master Commit: 2c4c06c1e3712e2cd4a4b7e10c0b649db5a94886 Parents: c88effa Author: Askannon <askan...@flexarc.com> Authored: Wed Dec 31 13:55:38 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Jan 2 08:30:44 2015 +0100 ---------------------------------------------------------------------- .../component/mybatis/MyBatisProducer.java | 10 +++- .../MyBatisInsertWithOutputHeaderTest.java | 63 ++++++++++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/2c4c06c1/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java b/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java index f37c6d3..602ae09 100644 --- a/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java +++ b/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java @@ -226,7 +226,8 @@ public class MyBatisProducer extends DefaultProducer { } private void doProcessResult(Exchange exchange, Object result, SqlSession session) { - if (endpoint.getStatementType() == StatementType.SelectList || endpoint.getStatementType() == StatementType.SelectOne) { + final String outputHeader = getEndpoint().getOutputHeader(); + if (endpoint.getStatementType() == StatementType.SelectList || endpoint.getStatementType() == StatementType.SelectOne) { Message answer = exchange.getIn(); if (ExchangeHelper.isOutCapable(exchange)) { answer = exchange.getOut(); @@ -236,7 +237,6 @@ public class MyBatisProducer extends DefaultProducer { // we should not set the body if its a stored procedure as the result is already in its OUT parameter MappedStatement ms = session.getConfiguration().getMappedStatement(statement); - final String outputHeader = getEndpoint().getOutputHeader(); if (ms != null && ms.getStatementType() == org.apache.ibatis.mapping.StatementType.CALLABLE) { if (result == null) { LOG.trace("Setting result as existing body as MyBatis statement type is Callable, and there was no result."); @@ -268,7 +268,11 @@ public class MyBatisProducer extends DefaultProducer { answer.setHeader(MyBatisConstants.MYBATIS_STATEMENT_NAME, statement); } else { Message msg = exchange.getIn(); - msg.setHeader(MyBatisConstants.MYBATIS_RESULT, result); + if(outputHeader != null) { + msg.setHeader(outputHeader, result); + } else { + msg.setHeader(MyBatisConstants.MYBATIS_RESULT, result); + } msg.setHeader(MyBatisConstants.MYBATIS_STATEMENT_NAME, statement); } } http://git-wip-us.apache.org/repos/asf/camel/blob/2c4c06c1/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertWithOutputHeaderTest.java ---------------------------------------------------------------------- diff --git a/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertWithOutputHeaderTest.java b/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertWithOutputHeaderTest.java new file mode 100644 index 0000000..8feeded --- /dev/null +++ b/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertWithOutputHeaderTest.java @@ -0,0 +1,63 @@ +/** + * 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.mybatis; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Test; + +public class MyBatisInsertWithOutputHeaderTest extends MyBatisTestSupport { + + private static final String TEST_CASE_HEADER_NAME = "testCaseHeader"; + private static final int TEST_ACCOUNT_ID = 444; + + @Test + public void testInsert() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(Account.class); + mock.message(0).header(TEST_CASE_HEADER_NAME).isEqualTo(1); + mock.message(0).header(MyBatisConstants.MYBATIS_RESULT).isNull(); + + Account account = new Account(); + account.setId(TEST_ACCOUNT_ID); + account.setFirstName("Willem"); + account.setLastName("Jiang"); + account.setEmailAddress("fara...@gmail.com"); + + template.sendBody("direct:start", account); + + assertMockEndpointsSatisfied(); + + // there should be 3 rows now + Integer rows = template.requestBody("mybatis:count?statementType=SelectOne", null, Integer.class); + assertEquals("There should be 3 rows", 3, rows.intValue()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .to("mybatis:insertAccount?statementType=Insert&outputHeader=" + TEST_CASE_HEADER_NAME) + .to("mock:result"); + } + }; + } + +}