AbstractAction implements Action<T> { protected JsonObject parseResponseBody(String responseBody) { if (responseBody == null || responseBody.trim().isEmpty()) { return new JsonObject(); } JsonElement parsed = new JsonParser().parse(responseBody); if (parsed.isJsonObject()) { return parsed.getAsJsonObject(); } else { throw new JsonSyntaxException("Response did not contain a JSON Object"); } } 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 convertJsonStringToMapObject() { String json = "{\n" + " \"ok\" : true,\n" + " \"_index\" : \"twitter\",\n" + " \"_type\" : \"tweet\",\n" + " \"_id\" : \"1\"\n" + "}"; JsonObject jsonMap = new DummyAction.Builder().build().parseResponseBody(json); assertNotNull(jsonMap); assertEquals(4, jsonMap.entrySet().size()); assertEquals(true, jsonMap.get("ok").getAsBoolean()); assertEquals("twitter", jsonMap.get("_index").getAsString()); assertEquals("tweet", jsonMap.get("_type").getAsString()); assertEquals("1", jsonMap.get("_id").getAsString()); }
@Test public void convertEmptyJsonStringToMapObject() { JsonObject jsonMap = new DummyAction.Builder().build().parseResponseBody(""); assertNotNull(jsonMap); }
@Test public void convertNullJsonStringToMapObject() { JsonObject jsonMap = new DummyAction.Builder().build().parseResponseBody(null); assertNotNull(jsonMap); }
@Test(expected = JsonSyntaxException.class) public void propagateExceptionWhenTheResponseIsNotJson1() { new DummyAction.Builder().build().parseResponseBody("401 Unauthorized"); }
@Test(expected = JsonSyntaxException.class) public void propagateExceptionWhenTheResponseIsNotJson2() { new DummyAction.Builder().build().parseResponseBody("banana"); }