Repository: searchbox-io/Jest
Pull request #249 (2015-10-23 23:54:06)

Return source as String + Cleanup
Adds method <code>JestResult#getSourceString()</code> (incl. tests in <code>JestResultTest</code>)

In addition:
- cleaned up some unused imports and variables
- make use of <code>org.junit</code> instead of <code>junit.assert</code>.
- JestHttpClient needs not to implement JestClient, as it already extends AbstractJestClient which implements it.


Patch:
diff --git a/jest-common/pom.xml b/jest-common/pom.xml
index 99bbe86f..c8891bda 100644
--- a/jest-common/pom.xml
+++ b/jest-common/pom.xml
@@ -3,10 +3,8 @@
 
     <modelVersion>4.0.0</modelVersion>
 
-    <groupId>io.searchbox</groupId>
     <artifactId>jest-common</artifactId>
     <packaging>jar</packaging>
-    <version>1.0.0-SNAPSHOT</version>
     <name>Jest Common Jar</name>
     <description>ElasticSearch Java REST client - shared library package</description>
     <url>https://github.com/searchbox-io/Jest</url>
diff --git a/jest-common/src/main/java/io/searchbox/action/AbstractAction.java b/jest-common/src/main/java/io/searchbox/action/AbstractAction.java
index 759b74a7..9444d625 100644
--- a/jest-common/src/main/java/io/searchbox/action/AbstractAction.java
+++ b/jest-common/src/main/java/io/searchbox/action/AbstractAction.java
@@ -1,6 +1,5 @@
 package io.searchbox.action;
 
-import com.google.common.collect.HashMultimap;
 import com.google.common.collect.LinkedHashMultimap;
 import com.google.common.collect.Multimap;
 import com.google.gson.Gson;
diff --git a/jest-common/src/main/java/io/searchbox/action/AbstractMultiIndexActionBuilder.java b/jest-common/src/main/java/io/searchbox/action/AbstractMultiIndexActionBuilder.java
index 7245c6c2..68f25cc1 100644
--- a/jest-common/src/main/java/io/searchbox/action/AbstractMultiIndexActionBuilder.java
+++ b/jest-common/src/main/java/io/searchbox/action/AbstractMultiIndexActionBuilder.java
@@ -1,13 +1,12 @@
 package io.searchbox.action;
 
 import io.searchbox.params.Parameters;
-import org.apache.commons.lang3.StringUtils;
-
 import java.util.Collection;
-import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.Set;
 
+import org.apache.commons.lang3.StringUtils;
+
 /**
  * @author cihat keser
  */
diff --git a/jest-common/src/main/java/io/searchbox/action/AbstractMultiTypeActionBuilder.java b/jest-common/src/main/java/io/searchbox/action/AbstractMultiTypeActionBuilder.java
index 731f99ee..f742d1e7 100644
--- a/jest-common/src/main/java/io/searchbox/action/AbstractMultiTypeActionBuilder.java
+++ b/jest-common/src/main/java/io/searchbox/action/AbstractMultiTypeActionBuilder.java
@@ -3,7 +3,6 @@ package io.searchbox.action;
 import org.apache.commons.lang3.StringUtils;
 
 import java.util.Collection;
-import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.Set;
 
diff --git a/jest-common/src/main/java/io/searchbox/client/JestResult.java b/jest-common/src/main/java/io/searchbox/client/JestResult.java
index 38feb1dd..0e97fe88 100644
--- a/jest-common/src/main/java/io/searchbox/client/JestResult.java
+++ b/jest-common/src/main/java/io/searchbox/client/JestResult.java
@@ -116,6 +116,19 @@ public class JestResult {
         String json = gson.toJson(resultMap, Map.class);
         setJsonObject(new JsonParser().parse(json).getAsJsonObject());
     }
+    
+    public String getSourceAsString() {
+    	String[] keys = getKeys();
+        if(!isSucceeded || jsonObject == null || keys == null || keys.length == 0 || !jsonObject.has(keys[0])) {
+            return null;
+        }
+
+        JsonElement obj = jsonObject.get(keys[0]);
+        for (int i = 1; i < keys.length; i++) {
+            obj = ((JsonObject) obj).get(keys[i]);
+        }
+        return obj.toString();
+    }
 
     public <T> T getSourceAsObject(Class<T> clazz) {
         T sourceAsObject = null;
@@ -271,7 +284,7 @@ public class JestResult {
     }
 
     protected String[] getKeys() {
-        return pathToResult == null ? null : (pathToResult + "").split("/");
+        return pathToResult == null ? null : pathToResult.split("/");
     }
 
 }
diff --git a/jest-common/src/main/java/io/searchbox/cluster/UpdateSettings.java b/jest-common/src/main/java/io/searchbox/cluster/UpdateSettings.java
index a1fae3fd..fdbdc5af 100644
--- a/jest-common/src/main/java/io/searchbox/cluster/UpdateSettings.java
+++ b/jest-common/src/main/java/io/searchbox/cluster/UpdateSettings.java
@@ -52,7 +52,6 @@ public class UpdateSettings extends GenericResultAbstractAction {
             return false;
         }
 
-        UpdateSettings rhs = (UpdateSettings) obj;
         return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .isEquals();
diff --git a/jest-common/src/main/java/io/searchbox/core/Bulk.java b/jest-common/src/main/java/io/searchbox/core/Bulk.java
index ae4b6a80..58b89695 100644
--- a/jest-common/src/main/java/io/searchbox/core/Bulk.java
+++ b/jest-common/src/main/java/io/searchbox/core/Bulk.java
@@ -6,7 +6,6 @@ import com.google.gson.JsonObject;
 import io.searchbox.action.AbstractAction;
 import io.searchbox.action.BulkableAction;
 import io.searchbox.action.GenericResultAbstractAction;
-import io.searchbox.client.JestResult;
 import io.searchbox.params.Parameters;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
diff --git a/jest-common/src/main/java/io/searchbox/core/BulkResult.java b/jest-common/src/main/java/io/searchbox/core/BulkResult.java
index 72f78539..8947cfc7 100644
--- a/jest-common/src/main/java/io/searchbox/core/BulkResult.java
+++ b/jest-common/src/main/java/io/searchbox/core/BulkResult.java
@@ -7,7 +7,6 @@ import io.searchbox.client.JestResult;
 import org.apache.commons.lang3.builder.EqualsBuilder;
 import org.apache.commons.lang3.builder.HashCodeBuilder;
 
-import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
diff --git a/jest-common/src/main/java/io/searchbox/core/DeleteByQuery.java b/jest-common/src/main/java/io/searchbox/core/DeleteByQuery.java
index e0c7a922..88f8a1d2 100644
--- a/jest-common/src/main/java/io/searchbox/core/DeleteByQuery.java
+++ b/jest-common/src/main/java/io/searchbox/core/DeleteByQuery.java
@@ -52,7 +52,6 @@ public class DeleteByQuery extends GenericResultAbstractAction {
             return false;
         }
 
-        DeleteByQuery rhs = (DeleteByQuery) obj;
         return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .isEquals();
diff --git a/jest-common/src/main/java/io/searchbox/core/Explain.java b/jest-common/src/main/java/io/searchbox/core/Explain.java
index a871f8c3..c350be7f 100644
--- a/jest-common/src/main/java/io/searchbox/core/Explain.java
+++ b/jest-common/src/main/java/io/searchbox/core/Explain.java
@@ -45,7 +45,6 @@ public class Explain extends SingleResultAbstractDocumentTargetedAction {
             return false;
         }
 
-        Explain rhs = (Explain) obj;
         return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .isEquals();
diff --git a/jest-common/src/main/java/io/searchbox/core/Index.java b/jest-common/src/main/java/io/searchbox/core/Index.java
index 87b4eed2..7a5c2247 100644
--- a/jest-common/src/main/java/io/searchbox/core/Index.java
+++ b/jest-common/src/main/java/io/searchbox/core/Index.java
@@ -63,7 +63,6 @@ public class Index extends SingleResultAbstractDocumentTargetedAction implements
             return false;
         }
 
-        Index rhs = (Index) obj;
         return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .isEquals();
diff --git a/jest-common/src/main/java/io/searchbox/core/MoreLikeThis.java b/jest-common/src/main/java/io/searchbox/core/MoreLikeThis.java
index f99d6733..e2629585 100644
--- a/jest-common/src/main/java/io/searchbox/core/MoreLikeThis.java
+++ b/jest-common/src/main/java/io/searchbox/core/MoreLikeThis.java
@@ -46,7 +46,6 @@ public class MoreLikeThis extends GenericResultAbstractDocumentTargetedAction {
             return false;
         }
 
-        MoreLikeThis rhs = (MoreLikeThis) obj;
         return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .isEquals();
diff --git a/jest-common/src/main/java/io/searchbox/core/MultiGet.java b/jest-common/src/main/java/io/searchbox/core/MultiGet.java
index ee779058..017c45e1 100644
--- a/jest-common/src/main/java/io/searchbox/core/MultiGet.java
+++ b/jest-common/src/main/java/io/searchbox/core/MultiGet.java
@@ -73,7 +73,6 @@ public class MultiGet extends GenericResultAbstractAction {
             return false;
         }
 
-        MultiGet rhs = (MultiGet) obj;
         return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .isEquals();
diff --git a/jest-common/src/main/java/io/searchbox/core/Percolate.java b/jest-common/src/main/java/io/searchbox/core/Percolate.java
index eaa0b559..dc868542 100644
--- a/jest-common/src/main/java/io/searchbox/core/Percolate.java
+++ b/jest-common/src/main/java/io/searchbox/core/Percolate.java
@@ -50,7 +50,6 @@ public class Percolate extends GenericResultAbstractAction {
             return false;
         }
 
-        Percolate rhs = (Percolate) obj;
         return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .isEquals();
diff --git a/jest-common/src/main/java/io/searchbox/core/SearchScroll.java b/jest-common/src/main/java/io/searchbox/core/SearchScroll.java
index d039b2d9..05a6a3b4 100644
--- a/jest-common/src/main/java/io/searchbox/core/SearchScroll.java
+++ b/jest-common/src/main/java/io/searchbox/core/SearchScroll.java
@@ -62,7 +62,6 @@ public class SearchScroll extends GenericResultAbstractAction {
             return false;
         }
 
-        SearchScroll rhs = (SearchScroll) obj;
         return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .isEquals();
diff --git a/jest-common/src/main/java/io/searchbox/core/Suggest.java b/jest-common/src/main/java/io/searchbox/core/Suggest.java
index b977cd1e..d88df80a 100644
--- a/jest-common/src/main/java/io/searchbox/core/Suggest.java
+++ b/jest-common/src/main/java/io/searchbox/core/Suggest.java
@@ -57,7 +57,6 @@ public class Suggest extends AbstractAction<SuggestResult> {
             return false;
         }
 
-        Suggest rhs = (Suggest) obj;
         return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .isEquals();
diff --git a/jest-common/src/main/java/io/searchbox/core/Validate.java b/jest-common/src/main/java/io/searchbox/core/Validate.java
index 94bcd6b5..9cec0e90 100644
--- a/jest-common/src/main/java/io/searchbox/core/Validate.java
+++ b/jest-common/src/main/java/io/searchbox/core/Validate.java
@@ -53,7 +53,6 @@ public class Validate extends GenericResultAbstractAction {
             return false;
         }
 
-        Validate rhs = (Validate) obj;
         return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .isEquals();
diff --git a/jest-common/src/main/java/io/searchbox/indices/Analyze.java b/jest-common/src/main/java/io/searchbox/indices/Analyze.java
index f135316d..394ed46e 100644
--- a/jest-common/src/main/java/io/searchbox/indices/Analyze.java
+++ b/jest-common/src/main/java/io/searchbox/indices/Analyze.java
@@ -50,7 +50,6 @@ public class Analyze extends GenericResultAbstractAction {
             return false;
         }
 
-        Analyze rhs = (Analyze) obj;
         return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .isEquals();
diff --git a/jest-common/src/main/java/io/searchbox/indices/mapping/PutMapping.java b/jest-common/src/main/java/io/searchbox/indices/mapping/PutMapping.java
index 89580bf4..8fe251f6 100644
--- a/jest-common/src/main/java/io/searchbox/indices/mapping/PutMapping.java
+++ b/jest-common/src/main/java/io/searchbox/indices/mapping/PutMapping.java
@@ -48,7 +48,6 @@ public class PutMapping extends GenericResultAbstractAction {
             return false;
         }
 
-        PutMapping rhs = (PutMapping) obj;
         return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .isEquals();
diff --git a/jest-common/src/main/java/io/searchbox/indices/script/CreateIndexedScript.java b/jest-common/src/main/java/io/searchbox/indices/script/CreateIndexedScript.java
index e7461be0..a99d8021 100644
--- a/jest-common/src/main/java/io/searchbox/indices/script/CreateIndexedScript.java
+++ b/jest-common/src/main/java/io/searchbox/indices/script/CreateIndexedScript.java
@@ -4,14 +4,10 @@ import com.google.common.io.CharStreams;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import io.searchbox.action.AbstractAction;
-import io.searchbox.action.GenericResultAbstractAction;
 
 import java.io.*;
 import java.nio.charset.Charset;
 
-import static io.searchbox.indices.script.ScriptLanguage.GROOVY;
-import static java.net.URLEncoder.encode;
-
 public class CreateIndexedScript extends AbstractIndexedScript {
 
     protected CreateIndexedScript(Builder builder) {
diff --git a/jest-common/src/main/java/io/searchbox/indices/script/DeleteIndexedScript.java b/jest-common/src/main/java/io/searchbox/indices/script/DeleteIndexedScript.java
index befe5808..0020ab72 100644
--- a/jest-common/src/main/java/io/searchbox/indices/script/DeleteIndexedScript.java
+++ b/jest-common/src/main/java/io/searchbox/indices/script/DeleteIndexedScript.java
@@ -1,13 +1,5 @@
 package io.searchbox.indices.script;
 
-import io.searchbox.action.AbstractAction;
-import io.searchbox.action.GenericResultAbstractAction;
-
-import java.io.UnsupportedEncodingException;
-
-import static io.searchbox.indices.script.ScriptLanguage.GROOVY;
-import static java.net.URLEncoder.encode;
-
 public class DeleteIndexedScript extends AbstractIndexedScript {
 
     protected DeleteIndexedScript(Builder builder) {
diff --git a/jest-common/src/main/java/io/searchbox/indices/settings/UpdateSettings.java b/jest-common/src/main/java/io/searchbox/indices/settings/UpdateSettings.java
index bb3053cf..f09080e7 100644
--- a/jest-common/src/main/java/io/searchbox/indices/settings/UpdateSettings.java
+++ b/jest-common/src/main/java/io/searchbox/indices/settings/UpdateSettings.java
@@ -41,7 +41,6 @@ public class UpdateSettings extends IndicesSettingsAbstractAction {
             return false;
         }
 
-        UpdateSettings rhs = (UpdateSettings) obj;
         return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .isEquals();
diff --git a/jest-common/src/main/java/io/searchbox/indices/template/PutTemplate.java b/jest-common/src/main/java/io/searchbox/indices/template/PutTemplate.java
index b65c7104..980bc782 100644
--- a/jest-common/src/main/java/io/searchbox/indices/template/PutTemplate.java
+++ b/jest-common/src/main/java/io/searchbox/indices/template/PutTemplate.java
@@ -40,7 +40,6 @@ public class PutTemplate extends TemplateAction {
             return false;
         }
 
-        PutTemplate rhs = (PutTemplate) obj;
         return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .isEquals();
diff --git a/jest-droid/pom.xml b/jest-droid/pom.xml
index 0211c81b..56e663e9 100644
--- a/jest-droid/pom.xml
+++ b/jest-droid/pom.xml
@@ -3,10 +3,8 @@
 
     <modelVersion>4.0.0</modelVersion>
 
-    <groupId>io.searchbox</groupId>
     <artifactId>jest-droid</artifactId>
     <packaging>jar</packaging>
-    <version>1.0.0-SNAPSHOT</version>
     <name>Jest Android Jar</name>
     <description>ElasticSearch Java REST client - Android library package</description>
     <url>https://github.com/searchbox-io/Jest</url>
diff --git a/jest/pom.xml b/jest/pom.xml
index 4e8b2f1f..e47c666a 100644
--- a/jest/pom.xml
+++ b/jest/pom.xml
@@ -3,10 +3,8 @@
 
     <modelVersion>4.0.0</modelVersion>
 
-    <groupId>io.searchbox</groupId>
     <artifactId>jest</artifactId>
     <packaging>jar</packaging>
-    <version>1.0.0-SNAPSHOT</version>
     <name>Jest Apache HC Jar</name>
     <description>ElasticSearch Java REST client - Apache HTTP Client package</description>
     <url>https://github.com/searchbox-io/Jest</url>
diff --git a/jest/src/main/java/io/searchbox/client/http/JestHttpClient.java b/jest/src/main/java/io/searchbox/client/http/JestHttpClient.java
index 56071e8e..fbe6859f 100755
--- a/jest/src/main/java/io/searchbox/client/http/JestHttpClient.java
+++ b/jest/src/main/java/io/searchbox/client/http/JestHttpClient.java
@@ -1,19 +1,21 @@
 package io.searchbox.client.http;
 
-import com.google.gson.Gson;
 import io.searchbox.action.Action;
 import io.searchbox.client.AbstractJestClient;
-import io.searchbox.client.JestClient;
 import io.searchbox.client.JestResult;
 import io.searchbox.client.JestResultHandler;
 import io.searchbox.client.http.apache.HttpDeleteWithEntity;
 import io.searchbox.client.http.apache.HttpGetWithEntity;
-import org.apache.http.HttpEntity;
+import java.io.IOException;
+import java.util.Map.Entry;
 import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
 import org.apache.http.StatusLine;
 import org.apache.http.client.entity.EntityBuilder;
-import org.apache.http.client.methods.*;
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.concurrent.FutureCallback;
 import org.apache.http.entity.ContentType;
 import org.apache.http.impl.client.CloseableHttpClient;
@@ -21,15 +23,13 @@ import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
 import org.apache.http.util.EntityUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
-import java.io.IOException;
-import java.util.Map.Entry;
+import com.google.gson.Gson;
 
 /**
  * @author Dogukan Sonmez
  * @author cihat keser
  */
-public class JestHttpClient extends AbstractJestClient implements JestClient {
+public class JestHttpClient extends AbstractJestClient {
 
     private final static Logger log = LoggerFactory.getLogger(JestHttpClient.class);
 


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/client/JestResultTest.java b/jest-common/src/test/java/io/searchbox/client/JestResultTest.java
index 19c0a0af..4ddf2c7b 100644
--- a/jest-common/src/test/java/io/searchbox/client/JestResultTest.java
+++ b/jest-common/src/test/java/io/searchbox/client/JestResultTest.java
@@ -1,9 +1,13 @@
 package io.searchbox.client;
 
+import static org.junit.Assert.*;
+
 import com.google.gson.Gson;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
+
 import io.searchbox.annotations.JestId;
+
 import org.junit.Test;
 
 import java.util.ArrayList;
@@ -11,8 +15,6 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
-import static junit.framework.Assert.*;
-
 /**
  * @author Dogukan Sonmez
  */
@@ -125,6 +127,70 @@ public class JestResultTest {
         assertEquals("trying out Elastic Search", twitter.getMessage());
         assertEquals("2009-11-15T14:12:12", twitter.getPostDate());
     }
+    
+    @Test
+    public void getGetSourceAsString() {
+        String response = "{\n" +
+                "    \"_index\" : \"twitter\",\n" +
+                "    \"_type\" : \"tweet\",\n" +
+                "    \"_id\" : \"1\", \n" +
+                "    \"_source\" : {\n" +
+                "        \"user\" : \"kimchy\",\n" +
+                "        \"postDate\" : \"2009-11-15T14:12:12\",\n" +
+                "        \"message\" : \"trying out Elastic Search\"\n" +
+                "    }\n" +
+                "}\n";
+        
+        result.setJsonMap(new Gson().fromJson(response, Map.class));
+        result.setPathToResult("_source");
+        result.setSucceeded(true);
+        
+        String onlySource = "{" +
+                "\"user\":\"kimchy\"," +
+                "\"postDate\":\"2009-11-15T14:12:12\"," +
+                "\"message\":\"trying out Elastic Search\"" +
+                "}";
+        assertEquals(onlySource, result.getSourceAsString());
+    }
+    
+    @Test
+    public void getGetSourceAsStringArray() {
+        String response = "{\n" +
+                "    \"_index\" : \"twitter\",\n" +
+                "    \"_type\" : \"tweet\",\n" +
+                "    \"_id\" : \"1\", \n" +
+                "    \"_source\" : [" +
+                "        { \"user\" : \"kimch\" }, " +
+                "        { \"user\" : \"bello\" }," +
+                "        { \"user\" : \"ionex\" }" +
+                "    ]\n" +
+                "}\n";
+        
+        result.setJsonMap(new Gson().fromJson(response, Map.class));
+        result.setPathToResult("_source");
+        result.setSucceeded(true);
+        
+        String onlySource = "[" +
+                "{\"user\":\"kimch\"}," +
+                "{\"user\":\"bello\"}," +
+                "{\"user\":\"ionex\"}" +
+                "]";
+        assertEquals(onlySource, result.getSourceAsString());
+    }
+    
+    @Test
+    public void getGetSourceAsStringNoResult() {
+        String response = "{\n" +
+                "    \"_index\" : \"twitter\",\n" +
+                "    \"_type\" : \"tweet\",\n" +
+                "    \"_id\" : \"1\" \n" +
+                "}\n";
+        
+        result.setJsonMap(new Gson().fromJson(response, Map.class));
+        result.setPathToResult("_source");
+        result.setSucceeded(true);
+        assertNull(result.getSourceAsString());
+    }
 
     @Test
     public void getUnFoundGetResultAsAnObject() {
@@ -418,7 +484,7 @@ public class JestResultTest {
         result.setJsonMap(new Gson().fromJson(response, Map.class));
         result.setPathToResult("count");
         Double actual = result.extractSource().get(0).getAsDouble();
-        assertEquals(1.0, actual);
+        assertEquals(1.0, actual, 0.01);
     }
 
     @Test
@@ -435,7 +501,7 @@ public class JestResultTest {
         result.setPathToResult("count");
         result.setSucceeded(true);
         Double count = result.getSourceAsObject(Double.class);
-        assertEquals(1.0, count);
+        assertEquals(1.0, count, 0.01);
     }
 
     @Test