Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Deserialization process should not swallow exceptions #185

Merged
merged 3 commits into from
Mar 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,9 @@ protected T createNewElasticSearchResult(T result, String json, int statusCode,
return result;
}

protected static JsonObject convertJsonStringToMapObject(String jsonTxt) {
protected JsonObject convertJsonStringToMapObject(String jsonTxt) {
if (jsonTxt != null && !jsonTxt.trim().isEmpty()) {
try {
return new JsonParser().parse(jsonTxt).getAsJsonObject();
} catch (Exception e) {
log.error("An exception occurred while converting json string to map object");
}
}
return new JsonObject();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.searchbox.cluster;

import com.google.gson.JsonObject;
import io.searchbox.action.AbstractMultiINodeActionBuilder;
import io.searchbox.action.GenericResultAbstractAction;

Expand All @@ -26,6 +27,11 @@ protected String buildURI() {
return sb.toString();
}

@Override
protected JsonObject convertJsonStringToMapObject(String jsonTxt) {
return new JsonObject();
}

@Override
public String getRestMethodName() {
return "GET";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import io.searchbox.annotations.JestId;
import io.searchbox.client.JestResult;
import io.searchbox.core.Delete;
import io.searchbox.core.Get;
import io.searchbox.core.Index;
import io.searchbox.core.Update;
import io.searchbox.indices.Flush;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -158,7 +161,7 @@ public void convertJsonStringToMapObject() {
" \"_type\" : \"tweet\",\n" +
" \"_id\" : \"1\"\n" +
"}";
JsonObject jsonMap = AbstractAction.convertJsonStringToMapObject(json);
JsonObject jsonMap = new DummyAction.Builder().build().convertJsonStringToMapObject(json);
assertNotNull(jsonMap);
assertEquals(4, jsonMap.entrySet().size());
assertEquals(true, jsonMap.get("ok").getAsBoolean());
Expand All @@ -169,16 +172,21 @@ public void convertJsonStringToMapObject() {

@Test
public void convertEmptyJsonStringToMapObject() {
JsonObject jsonMap = AbstractAction.convertJsonStringToMapObject("");
JsonObject jsonMap = new DummyAction.Builder().build().convertJsonStringToMapObject("");
assertNotNull(jsonMap);
}

@Test
public void convertNullJsonStringToMapObject() {
JsonObject jsonMap = AbstractAction.convertJsonStringToMapObject(null);
JsonObject jsonMap = new DummyAction.Builder().build().convertJsonStringToMapObject(null);
assertNotNull(jsonMap);
}

@Test(expected = JsonSyntaxException.class)
public void propagateExceptionWhenTheResponseIsNotJson() {
new DummyAction.Builder().build().convertJsonStringToMapObject("401 Unauthorized");
}


@Test
public void getSuccessIndexResult() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public void searchWithValidQueryAndExplain() throws IOException {

@Test
public void searchWithQueryBuilder() throws IOException {
Index index = new Index.Builder("{\"user\":\"kimchy\"}").setParameter(Parameters.REFRESH, true).build();
Index index = new Index.Builder("{\"user\":\"kimchy\"}")
.index("twitter")
.type("tweet")
.setParameter(Parameters.REFRESH, true).build();
client.execute(index);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("user", "kimchy"));
Expand Down