AbstractAction implements Action<T> { protected T createNewElasticSearchResult(T result, String responseBody, int statusCode, String reasonPhrase, Gson gson) { JsonObject jsonMap = parseResponseBody(responseBody); result.setResponseCode(statusCode); result.setJsonString(responseBody); result.setJsonObject(jsonMap); result.setPathToResult(getPathToResult()); if (isHttpSuccessful(statusCode)) { result.setSucceeded(true); log.debug("Request and operation succeeded"); } else { result.setSucceeded(false); if (result.getErrorMessage() == null) { result.setErrorMessage(statusCode + " " + (reasonPhrase == null ? "null" : reasonPhrase)); } log.debug("Response is failed. errorMessage is " + result.getErrorMessage()); } return result; } AbstractAction(); @SuppressWarnings("unchecked")  AbstractAction(Builder builder); static String getIdFromSource(Object source); Collection<Object> getParameter(String parameter); Object getHeader(String header); @Override Map<String, Object> getHeaders(); @Override String getURI(ElasticsearchVersion elasticsearchVersion); @Override String getData(Gson gson); @Override String getPathToResult(); @Override String toString(); @Override int hashCode(); @Override boolean equals(Object obj); abstract String getRestMethodName(); static String CHARSET; }
@Test public void getSuccessIndexResult() { String jsonString = "{\n" + " \"ok\" : true,\n" + " \"_index\" : \"twitter\",\n" + " \"_type\" : \"tweet\",\n" + " \"_id\" : \"1\"\n" + "}\n"; Index index = new Index.Builder("{\"abc\":\"dce\"}").index("test").build(); JestResult result = index.createNewElasticSearchResult(jsonString, 200, null, new Gson()); assertTrue(result.getErrorMessage(), result.isSucceeded()); assertEquals(200, result.getResponseCode()); }
@Test public void getFailedIndexResult() { String jsonString = "{\"error\":\"Invalid index\",\"status\":400}"; Index index = new Index.Builder("{\"abc\":\"dce\"}").index("test").build(); JestResult result = index.createNewElasticSearchResult(jsonString, 400, null, new Gson()); assertFalse(result.isSucceeded()); assertEquals("\"Invalid index\"", result.getErrorMessage()); }
@Test public void getSuccessDeleteResult() { String jsonString = "{\n" + " \"ok\" : true,\n" + " \"_index\" : \"twitter\",\n" + " \"_type\" : \"tweet\",\n" + " \"_id\" : \"1\",\n" + " \"found\" : true\n" + "}\n"; Delete delete = new Delete.Builder("1").index("twitter").type("tweet").build(); JestResult result = delete.createNewElasticSearchResult(jsonString, 200, null, new Gson()); assertTrue(result.getErrorMessage(), result.isSucceeded()); }
@Test public void getFailedDeleteResult() { String jsonString = "{\n" + " \"_index\" : \"twitter\",\n" + " \"_type\" : \"tweet\",\n" + " \"_id\" : \"1\",\n" + " \"found\" : false\n" + "}\n"; Delete delete = new Delete.Builder("1").index("test").type("tweet").build(); JestResult result = delete.createNewElasticSearchResult(jsonString, 404, null, new Gson()); assertFalse(result.isSucceeded()); }
@Test public void getSuccessGetResult() { String jsonString = "{" + " \"_index\" : \"twitter\"," + " \"_type\" : \"tweet\"," + " \"_id\" : \"1\"," + " \"exists\" : true" + "}"; Get get = new Get.Builder("test", "1").build(); JestResult result = get.createNewElasticSearchResult(jsonString, 200, null, new Gson()); assertTrue(result.getErrorMessage(), result.isSucceeded()); }