Author: boday Date: Wed Aug 8 02:49:21 2012 New Revision: 1370646 URL: http://svn.apache.org/viewvc?rev=1370646&view=rev Log: CAMEL-5481 added DELETE support to camel-elasticsearch, some unit test cleanup
Removed: camel/trunk/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchIndexNameAndTypeInHeaderComponentTest.java Modified: camel/trunk/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConfiguration.java camel/trunk/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchProducer.java camel/trunk/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchComponentTest.java Modified: camel/trunk/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConfiguration.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConfiguration.java?rev=1370646&r1=1370645&r2=1370646&view=diff ============================================================================== --- camel/trunk/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConfiguration.java (original) +++ camel/trunk/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConfiguration.java Wed Aug 8 02:49:21 2012 @@ -32,6 +32,7 @@ public class ElasticsearchConfiguration public static final String PARAM_OPERATION = "operation"; public static final String OPERATION_INDEX = "INDEX"; public static final String OPERATION_GET_BY_ID = "GET_BY_ID"; + public static final String OPERATION_DELETE = "DELETE"; public static final String PARAM_INDEX_ID = "indexId"; public static final String PARAM_DATA = "data"; public static final String PARAM_INDEX_NAME = "indexName"; Modified: camel/trunk/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchProducer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchProducer.java?rev=1370646&r1=1370645&r2=1370646&view=diff ============================================================================== --- camel/trunk/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchProducer.java (original) +++ camel/trunk/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchProducer.java Wed Aug 8 02:49:21 2012 @@ -23,6 +23,7 @@ import org.apache.camel.ExpectedBodyType import org.apache.camel.Message; import org.apache.camel.impl.DefaultProducer; import org.elasticsearch.action.ListenableActionFuture; +import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.index.IndexResponse; @@ -59,6 +60,8 @@ public class ElasticsearchProducer exten addToIndex(client, exchange); } else if (operation.equalsIgnoreCase(ElasticsearchConfiguration.OPERATION_GET_BY_ID)) { getById(client, exchange); + } else if (operation.equalsIgnoreCase(ElasticsearchConfiguration.OPERATION_DELETE)) { + deleteById(client, exchange); } else { throw new IllegalArgumentException(ElasticsearchConfiguration.PARAM_OPERATION + " value '" + operation + "' is not supported"); } @@ -82,6 +85,24 @@ public class ElasticsearchProducer exten exchange.getIn().setBody(response); } + public void deleteById(Client client, Exchange exchange) { + + String indexName = exchange.getIn().getHeader(ElasticsearchConfiguration.PARAM_INDEX_NAME, String.class); + if (indexName == null) { + indexName = endpoint.getConfig().getIndexName(); + } + + String indexType = exchange.getIn().getHeader(ElasticsearchConfiguration.PARAM_INDEX_TYPE, String.class); + if (indexType == null) { + indexType = endpoint.getConfig().getIndexType(); + } + + String indexId = exchange.getIn().getBody(String.class); + + DeleteResponse response = client.prepareDelete(indexName, indexType, indexId).execute().actionGet(); + exchange.getIn().setBody(response); + } + public void addToIndex(Client client, Exchange exchange) { String indexName = exchange.getIn().getHeader(ElasticsearchConfiguration.PARAM_INDEX_NAME, String.class); Modified: camel/trunk/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchComponentTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchComponentTest.java?rev=1370646&r1=1370645&r2=1370646&view=diff ============================================================================== --- camel/trunk/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchComponentTest.java (original) +++ camel/trunk/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchComponentTest.java Wed Aug 8 02:49:21 2012 @@ -22,6 +22,7 @@ import java.util.Map; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.junit4.CamelTestSupport; +import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetResponse; import org.junit.Test; @@ -37,20 +38,45 @@ public class ElasticsearchComponentTest @Test public void testGet() throws Exception { - //first, index a document + //first, INDEX a value HashMap<String, String> map = new HashMap<String, String>(); map.put("content", "test"); sendBody("direct:index", map); String indexId = (String) template.requestBody("direct:index", map); assertNotNull("indexId should be set", indexId); - //now, verify get succeeded + //now, verify GET succeeded GetResponse response = (GetResponse) template.requestBody("direct:get", indexId); assertNotNull("response should not be null", response); assertNotNull("response source should not be null", response.getSource()); } @Test + public void testDelete() throws Exception { + + //first, INDEX a value + HashMap<String, String> map = new HashMap<String, String>(); + map.put("content", "test"); + sendBody("direct:index", map); + String indexId = (String) template.requestBody("direct:index", map); + assertNotNull("indexId should be set", indexId); + + //now, verify GET succeeded + GetResponse response = (GetResponse) template.requestBody("direct:get", indexId); + assertNotNull("response should not be null", response); + assertNotNull("response source should not be null", response.getSource()); + + //now, perform DELETE + DeleteResponse deleteResponse = (DeleteResponse) template.requestBody("direct:delete", indexId); + assertNotNull("response should not be null", deleteResponse); + + //now, verify GET fails to find the indexed value + response = (GetResponse) template.requestBody("direct:get", indexId); + assertNotNull("response should not be null", response); + assertNull("response source should be null", response.getSource()); + } + + @Test public void testIndexWithHeaders() throws Exception { HashMap<String, String> map = new HashMap<String, String>(); @@ -68,7 +94,7 @@ public class ElasticsearchComponentTest @Test public void testGetWithHeaders() throws Exception { - //first, INDEX a document + //first, INDEX a value HashMap<String, String> map = new HashMap<String, String>(); map.put("content", "test"); @@ -86,6 +112,38 @@ public class ElasticsearchComponentTest assertNotNull("response source should not be null", response.getSource()); } + @Test + public void testDeleteWithHeaders() throws Exception { + + //first, INDEX a value + HashMap<String, String> map = new HashMap<String, String>(); + map.put("content", "test"); + + Map<String, Object> headers = new HashMap<String, Object>(); + headers.put(ElasticsearchConfiguration.PARAM_OPERATION, ElasticsearchConfiguration.OPERATION_INDEX); + headers.put(ElasticsearchConfiguration.PARAM_INDEX_NAME, "twitter"); + headers.put(ElasticsearchConfiguration.PARAM_INDEX_TYPE, "tweet"); + + String indexId = (String) template.requestBodyAndHeaders("direct:start", map, headers); + + //now, verify GET + headers.put(ElasticsearchConfiguration.PARAM_OPERATION, ElasticsearchConfiguration.OPERATION_GET_BY_ID); + GetResponse response = (GetResponse) template.requestBodyAndHeaders("direct:start", indexId, headers); + assertNotNull("response should not be null", response); + assertNotNull("response source should not be null", response.getSource()); + + //now, perform DELETE + headers.put(ElasticsearchConfiguration.PARAM_OPERATION, ElasticsearchConfiguration.OPERATION_DELETE); + DeleteResponse deleteResponse = (DeleteResponse) template.requestBodyAndHeaders("direct:start", indexId, headers); + assertNotNull("response should not be null", deleteResponse); + + //now, verify GET fails to find the indexed value + headers.put(ElasticsearchConfiguration.PARAM_OPERATION, ElasticsearchConfiguration.OPERATION_GET_BY_ID); + response = (GetResponse) template.requestBodyAndHeaders("direct:start", indexId, headers); + assertNotNull("response should not be null", response); + assertNull("response source should be null", response.getSource()); + } + @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @@ -93,6 +151,7 @@ public class ElasticsearchComponentTest from("direct:start").to("elasticsearch://local"); from("direct:index").to("elasticsearch://local?operation=INDEX&indexName=twitter&indexType=tweet"); from("direct:get").to("elasticsearch://local?operation=GET_BY_ID&indexName=twitter&indexType=tweet"); + from("direct:delete").to("elasticsearch://local?operation=DELETE&indexName=twitter&indexType=tweet"); } }; }