CAMEL-9407: Camel-Elasticsearch: Re-enable cluster tests

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b4b067e1
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b4b067e1
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b4b067e1

Branch: refs/heads/master
Commit: b4b067e1f4227e64ad2773b2c85a6a986f762048
Parents: 64008de
Author: Andrea Cosentino <anco...@gmail.com>
Authored: Wed Dec 9 13:47:24 2015 +0100
Committer: Andrea Cosentino <anco...@gmail.com>
Committed: Thu Dec 10 10:01:30 2015 +0100

----------------------------------------------------------------------
 components/camel-elasticsearch/pom.xml          |   8 ++
 .../ElasticsearchClusterBaseTest.java           | 125 +++++++++++++++++++
 .../ElasticsearchClusterIndexTest.java          | 101 +++++++++++++++
 .../elasticsearch/ElasticsearchIndexTest.java   |  58 ---------
 4 files changed, 234 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b4b067e1/components/camel-elasticsearch/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-elasticsearch/pom.xml 
b/components/camel-elasticsearch/pom.xml
index ef92eb9..5191318 100644
--- a/components/camel-elasticsearch/pom.xml
+++ b/components/camel-elasticsearch/pom.xml
@@ -52,6 +52,12 @@
       <artifactId>camel-test</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.codelibs</groupId>
+      <artifactId>elasticsearch-cluster-runner</artifactId>
+      <version>2.0.0.0</version>
+      <scope>test</scope>
+    </dependency>
 
     <!-- logging -->
     <dependency>
@@ -70,6 +76,8 @@
           <systemPropertyVariables>
             <es.path.data>target/data</es.path.data>
           </systemPropertyVariables>
+          <forkCount>1</forkCount>
+          <reuseForks>false</reuseForks>
         </configuration>
       </plugin>
     </plugins>

http://git-wip-us.apache.org/repos/asf/camel/blob/b4b067e1/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchClusterBaseTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchClusterBaseTest.java
 
b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchClusterBaseTest.java
new file mode 100644
index 0000000..83de307
--- /dev/null
+++ 
b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchClusterBaseTest.java
@@ -0,0 +1,125 @@
+/**
+ * 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.elasticsearch;
+
+import static 
org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner.newConfigs;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner;
+import org.elasticsearch.client.Client;
+import org.elasticsearch.common.settings.Settings.Builder;
+import org.elasticsearch.node.Node;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+
+public class ElasticsearchClusterBaseTest extends CamelTestSupport {
+
+    public static ElasticsearchClusterRunner runner;
+    public static String clusterName;  
+    public static Node node;
+    public static Client client;
+
+    @BeforeClass
+    public static void cleanUpOnce() throws Exception {
+        deleteDirectory("target/testcluster/");
+        clusterName = "es-cl-run-" + System.currentTimeMillis();
+        // create runner instance
+        runner = new ElasticsearchClusterRunner();
+        // create ES nodes
+        runner.onBuild(new ElasticsearchClusterRunner.Builder() {
+            @Override
+            public void build(final int number, final Builder settingsBuilder) 
{
+                settingsBuilder.put("http.cors.enabled", true);
+                settingsBuilder.put("http.cors.allow-origin", "*");
+            }
+        }).build(
+                newConfigs()
+                .clusterName("testCluster")
+                .numOfNode(3)
+                .basePath("target/testcluster/")
+                .useLogger());
+
+        // wait for green status
+        runner.ensureGreen();
+        
+        client = runner.client();
+    }
+
+    @AfterClass
+    public static void teardownOnce() throws Exception {
+        if (client != null) {
+            client.close();
+        }
+        // close runner
+        runner.close();
+        // delete all files
+        runner.clean();
+    }
+    
+    @Override
+    public boolean isCreateCamelContextPerClass() {
+        // let's speed up the tests using the same context
+        return true;
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+
+        // reuse existing client
+        ElasticsearchComponent es = context.getComponent("elasticsearch", 
ElasticsearchComponent.class);
+        es.setClient(client);
+
+        return context;
+    }
+
+    /**
+     * As we don't delete the {@code target/data} folder for <b>each</b> test
+     * below (otherwise they would run much slower), we need to make sure
+     * there's no side effect of the same used data through creating unique
+     * indexes.
+     */
+    Map<String, String> createIndexedData(String... additionalPrefixes) {
+        String prefix = createPrefix();
+
+        // take over any potential prefixes we may have been asked for
+        if (additionalPrefixes.length > 0) {
+            StringBuilder sb = new StringBuilder(prefix);
+            for (String additionalPrefix : additionalPrefixes) {
+                sb.append(additionalPrefix).append("-");
+            }
+            prefix = sb.toString();
+        }
+
+        String key = prefix + "key";
+        String value = prefix + "value";
+        log.info("Creating indexed data using the key/value pair {} => {}", 
key, value);
+
+        Map<String, String> map = new HashMap<String, String>();
+        map.put(key, value);
+        return map;
+    }
+
+    String createPrefix() {
+        // make use of the test method name to avoid collision
+        return getTestMethodName().toLowerCase() + "-";
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/b4b067e1/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchClusterIndexTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchClusterIndexTest.java
 
b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchClusterIndexTest.java
new file mode 100644
index 0000000..b9aec08
--- /dev/null
+++ 
b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchClusterIndexTest.java
@@ -0,0 +1,101 @@
+package org.apache.camel.component.elasticsearch;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class ElasticsearchClusterIndexTest extends 
ElasticsearchClusterBaseTest {
+
+    @Test
+    public void indexWithIp()  throws Exception {
+        Map<String, String> map = createIndexedData();
+        Map<String, Object> headers = new HashMap<String, Object>();
+        headers.put(ElasticsearchConstants.PARAM_OPERATION, 
ElasticsearchConstants.OPERATION_INDEX);
+        headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter");
+        headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet");
+        headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "1");
+
+        String indexId = template.requestBodyAndHeaders("direct:indexWithIp", 
map, headers, String.class);
+        assertNotNull("indexId should be set", indexId);
+        
+        headers.clear();
+        
+        headers.put(ElasticsearchConstants.PARAM_OPERATION, 
ElasticsearchConstants.OPERATION_INDEX);
+        headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter");
+        headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "status");
+        headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "2");
+
+
+        indexId = template.requestBodyAndHeaders("direct:indexWithIp", map, 
headers, String.class);
+        assertNotNull("indexId should be set", indexId);
+        
+        assertEquals("Cluster must be of three nodes", runner.getNodeSize(), 
3);
+        assertEquals("Index id 1 must exists", true, 
client.prepareGet("twitter", "tweet", "1").get().isExists());
+        assertEquals("Index id 2 must exists", true, 
client.prepareGet("twitter", "status", "2").get().isExists());
+    }
+
+    @Test
+    public void indexWithIpAndPort()  throws Exception {
+        Map<String, String> map = createIndexedData();
+        Map<String, Object> headers = new HashMap<String, Object>();
+        headers.put(ElasticsearchConstants.PARAM_OPERATION, 
ElasticsearchConstants.OPERATION_INDEX);
+        headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "instagram");
+        headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "photo");
+        headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "3");
+
+        String indexId = 
template.requestBodyAndHeaders("direct:indexWithIpAndPort", map, headers, 
String.class);
+        assertNotNull("indexId should be set", indexId);
+        
+        assertEquals("Cluster must be of three nodes", runner.getNodeSize(), 
3);
+        assertEquals("Index id 3 must exists", true, 
client.prepareGet("instagram", "photo", "3").get().isExists());
+    }
+
+    @Test
+    public void indexWithTransportAddresses()  throws Exception {
+        Map<String, String> map = createIndexedData();
+        Map<String, Object> headers = new HashMap<String, Object>();
+        headers.put(ElasticsearchConstants.PARAM_OPERATION, 
ElasticsearchConstants.OPERATION_INDEX);
+        headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "facebook");
+        headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "post");
+        headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "4");
+
+        String indexId = 
template.requestBodyAndHeaders("direct:indexWithTransportAddresses", map, 
headers, String.class);
+        assertNotNull("indexId should be set", indexId);
+        
+        assertEquals("Cluster must be of three nodes", runner.getNodeSize(), 
3);
+        assertEquals("Index id 4 must exists", true, 
client.prepareGet("facebook", "post", "4").get().isExists());
+    }
+
+    @Test
+    public void indexWithIpAndTransportAddresses()  throws Exception {
+        Map<String, String> map = createIndexedData();
+        Map<String, Object> headers = new HashMap<String, Object>();
+        headers.put(ElasticsearchConstants.PARAM_OPERATION, 
ElasticsearchConstants.OPERATION_INDEX);
+        headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "ebay");
+        headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "search");
+        headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "5");
+
+        //should ignore transport addresses configuration
+        String indexId = 
template.requestBodyAndHeaders("direct:indexWithIpAndTransportAddresses", map, 
headers, String.class);
+        assertNotNull("indexId should be set", indexId);
+        
+        assertEquals("Cluster must be of three nodes", runner.getNodeSize(), 
3);
+        assertEquals("Index id 5 must exists", true, client.prepareGet("ebay", 
"search", "5").get().isExists());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                
from("direct:indexWithIp").to("elasticsearch://elasticsearch?operation=INDEX&indexName=twitter&indexType=tweet&ip=localhost");
+                
from("direct:indexWithIpAndPort").to("elasticsearch://elasticsearch?operation=INDEX&indexName=twitter&indexType=tweet&ip=localhost&port=9300");
+                
from("direct:indexWithTransportAddresses").to("elasticsearch://elasticsearch?operation=INDEX&indexName=twitter&indexType=tweet&transportAddresses=localhost:9300,localhost:9301");
+                from("direct:indexWithIpAndTransportAddresses").
+                
to("elasticsearch://elasticsearch?operation=INDEX&indexName=twitter&indexType=tweet&ip=localhost&port=9300&transportAddresses=localhost:4444,localhost:5555");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/b4b067e1/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchIndexTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchIndexTest.java
 
b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchIndexTest.java
index ef6cda3..5563163 100644
--- 
a/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchIndexTest.java
+++ 
b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchIndexTest.java
@@ -72,59 +72,6 @@ public class ElasticsearchIndexTest extends 
ElasticsearchBaseTest {
         assertEquals("indexId should be equals to the provided id", "123", 
indexId);
     }
 
-    @Test
-    @Ignore("need to setup the cluster IP for this test")
-    public void indexWithIp()  throws Exception {
-        Map<String, String> map = createIndexedData();
-        Map<String, Object> headers = new HashMap<String, Object>();
-        headers.put(ElasticsearchConstants.PARAM_OPERATION, 
ElasticsearchConstants.OPERATION_INDEX);
-        headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter");
-        headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet");
-
-        String indexId = template.requestBodyAndHeaders("direct:indexWithIp", 
map, headers, String.class);
-        assertNotNull("indexId should be set", indexId);
-    }
-
-    @Test
-    @Ignore("need to setup the cluster IP/Port for this test")
-    public void indexWithIpAndPort()  throws Exception {
-        Map<String, String> map = createIndexedData();
-        Map<String, Object> headers = new HashMap<String, Object>();
-        headers.put(ElasticsearchConstants.PARAM_OPERATION, 
ElasticsearchConstants.OPERATION_INDEX);
-        headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter");
-        headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet");
-
-        String indexId = 
template.requestBodyAndHeaders("direct:indexWithIpAndPort", map, headers, 
String.class);
-        assertNotNull("indexId should be set", indexId);
-    }
-
-    @Test
-    @Ignore("need to setup the cluster with multiple nodes for this test")
-    public void indexWithTransportAddresses()  throws Exception {
-        Map<String, String> map = createIndexedData();
-        Map<String, Object> headers = new HashMap<String, Object>();
-        headers.put(ElasticsearchConstants.PARAM_OPERATION, 
ElasticsearchConstants.OPERATION_INDEX);
-        headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter");
-        headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet");
-
-        String indexId = 
template.requestBodyAndHeaders("direct:indexWithTransportAddresses", map, 
headers, String.class);
-        assertNotNull("indexId should be set", indexId);
-    }
-
-    @Test
-    @Ignore("need to setup the cluster with multiple nodes for this test")
-    public void indexWithIpAndTransportAddresses()  throws Exception {
-        Map<String, String> map = createIndexedData();
-        Map<String, Object> headers = new HashMap<String, Object>();
-        headers.put(ElasticsearchConstants.PARAM_OPERATION, 
ElasticsearchConstants.OPERATION_INDEX);
-        headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter");
-        headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet");
-
-        //should ignore transport addresses configuration
-        String indexId = 
template.requestBodyAndHeaders("direct:indexWithIpAndTransportAddresses", map, 
headers, String.class);
-        assertNotNull("indexId should be set", indexId);
-    }
-
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
@@ -134,11 +81,6 @@ public class ElasticsearchIndexTest extends 
ElasticsearchBaseTest {
                 
from("direct:index").to("elasticsearch://local?operation=INDEX&indexName=twitter&indexType=tweet");
                 
from("direct:indexWithReplication").to("elasticsearch://local?operation=INDEX&indexName=twitter&indexType=tweet");
                 
from("direct:indexWithWriteConsistency").to("elasticsearch://local?operation=INDEX&indexName=twitter&indexType=tweet&consistencyLevel=ONE");
-                
//from("direct:indexWithIp").to("elasticsearch://elasticsearch?operation=INDEX&indexName=twitter&indexType=tweet&ip=localhost");
-                
//from("direct:indexWithIpAndPort").to("elasticsearch://elasticsearch?operation=INDEX&indexName=twitter&indexType=tweet&ip=localhost&port=9300");
-                
//from("direct:indexWithTransportAddresses").to("elasticsearch://elasticsearch?operation=INDEX&indexName=twitter&indexType=tweet&transportAddresses=localhost:9300,localhost:9301");
-                //from("direct:indexWithIpAndTransportAddresses").
-                
//to("elasticsearch://elasticsearch?operation=INDEX&indexName=twitter&indexType=tweet&ip=localhost&port=9300&transportAddresses=localhost:4444,localhost:5555");
             }
         };
     }

Reply via email to