Skip to content

Commit

Permalink
refactored test classes:
Browse files Browse the repository at this point in the history
  (i) removed assertNotNull(jestResult) lines because result object cannot be null
  (ii) now using error message from json response as the error message when asserting result.isSucceeded
  • Loading branch information
Cihat Keser committed Jan 18, 2015
1 parent 8eb1fe2 commit ab5605d
Show file tree
Hide file tree
Showing 40 changed files with 106 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,14 @@ public void getSuccessIndexResult() {
"}\n";
Index index = new Index.Builder("{\"abc\":\"dce\"}").index("test").build();
JestResult result = index.createNewElasticSearchResult(jsonString, 200, null, new Gson());
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());
}

@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());
assertNotNull(result);
assertFalse(result.isSucceeded());
assertEquals("Invalid index", result.getErrorMessage());
}
Expand All @@ -215,8 +213,7 @@ public void getSuccessDeleteResult() {
"}\n";
Delete delete = new Delete.Builder("1").index("twitter").type("tweet").build();
JestResult result = delete.createNewElasticSearchResult(jsonString, 200, null, new Gson());
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());
}

//TODO: This cannot be derived fron the result anymore
Expand All @@ -230,7 +227,6 @@ public void getFailedDeleteResult() {
"}\n";
Delete delete = new Delete.Builder("1").index("test").type("tweet").build();
JestResult result = delete.createNewElasticSearchResult(jsonString, 404, null, new Gson());
assertNotNull(result);
assertFalse(result.isSucceeded());
}

Expand All @@ -244,8 +240,7 @@ public void getSuccessGetResult() {
"}";
Get get = new Get.Builder("test", "1").build();
JestResult result = get.createNewElasticSearchResult(jsonString, 200, null, new Gson());
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());
}

class Source {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void testConnectionThroughDefaultProxy() throws IOException, ExecutionExc
assertNotNull(jestClient);

JestResult result = jestClient.execute(new Status.Builder().build());
assertNotNull(result);
assertTrue(result.getErrorMessage(), result.isSucceeded());
assertEquals(1, numProxyRequests.intValue());

factory.setHttpClientConfig(new HttpClientConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ public class HealthIntegrationTest extends AbstractIntegrationTest {
@Test
public void health() throws Exception {
JestResult result = client.execute(new Health.Builder().build());
assertNotNull(result);
assertTrue(result.getErrorMessage(), result.isSucceeded());
assertThat(
result.getJsonObject().get("status").getAsString(),
anyOf(equalTo("green"), equalTo("yellow"), equalTo("red"))
);
assertTrue(result.isSucceeded());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public class NodesHotThreadsIntegrationTest extends AbstractIntegrationTest {
@Test
public void allNodesHotThreads() throws IOException {
JestResult result = client.execute(new NodesHotThreads.Builder().build());
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());

assertTrue(result.getJsonString().contains("out of 500ms"));
assertTrue("Result should contain info for first node", result.getJsonString().contains("::: [node_0]["));
Expand All @@ -27,8 +26,7 @@ public void allNodesHotThreads() throws IOException {
@Test
public void singleNodeHotThreads() throws IOException {
JestResult result = client.execute(new NodesHotThreads.Builder().addNode("node_0").build());
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());

assertTrue(result.getJsonString().contains("::: [node_0]["));
assertFalse(result.getJsonString().contains("::: [node_1]["));
Expand All @@ -40,8 +38,7 @@ public void singleNodeHotThreadsWithCustomInterval() throws IOException {
.addNode("node_0")
.interval("100ms")
.build());
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());

assertTrue(result.getJsonString().contains("out of 100ms"));
assertTrue(result.getJsonString().contains("::: [node_0]["));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,35 @@ public class NodesInfoIntegrationTest extends AbstractIntegrationTest {
@Test
public void nodesInfoWithoutNodeAndInfo() throws IOException {
JestResult result = client.execute(new NodesInfo.Builder().build());
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());
}

@Test
public void nodesInfoWithNodeWithoutInfo() throws IOException {
NodesInfo nodesInfo = new NodesInfo.Builder().addNode("node1").build();
JestResult result = client.execute(nodesInfo);
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());
}

@Test
public void nodesInfoWithoutNodeWithInfo() throws IOException {
NodesInfo nodesInfo = new NodesInfo.Builder().os(true).build();
JestResult result = client.execute(nodesInfo);
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());
}

@Test
public void nodesInfoWithNodeAndWithInfo() throws IOException {
NodesInfo nodesInfo = new NodesInfo.Builder().addNode("node1").os(true).build();
JestResult result = client.execute(nodesInfo);
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());
}

@Test
public void nodesInfoWithMultipleNodeAndWithoutInfo() throws IOException {
NodesInfo nodesInfo = new NodesInfo.Builder().addNode("node1").addNode("node2").build();
JestResult result = client.execute(nodesInfo);
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());
}

@Test
Expand All @@ -61,7 +56,6 @@ public void nodesInfoWithMultipleNodeAndMultipleInfo() throws IOException {
.os(true)
.build();
JestResult result = client.execute(nodesInfo);
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public void nodesStatsAllWithClear() throws IOException {
JestResult result = client.execute(new NodesStats.Builder()
.clear(true)
.build());
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());

JsonObject nodes = result.getJsonObject().getAsJsonObject("nodes");
assertNotNull(nodes);
Expand Down Expand Up @@ -59,8 +58,7 @@ public void nodesStatsWithClear() throws IOException {
.addNode("node_0")
.clear(true)
.build());
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());

JsonObject nodes = result.getJsonObject().getAsJsonObject("nodes");
assertNotNull(nodes);
Expand Down Expand Up @@ -96,8 +94,7 @@ public void nodesStatsWithClearAndIndices() throws IOException {
.clear(true)
.indices(true)
.build());
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());

JsonObject nodes = result.getJsonObject().getAsJsonObject("nodes");
assertNotNull(nodes);
Expand Down Expand Up @@ -135,8 +132,7 @@ public void nodesStatsWithClearAndIndicesAndJvm() throws IOException {
.indices(true)
.jvm(true)
.build());
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());

JsonObject nodes = result.getJsonObject().getAsJsonObject("nodes");
assertNotNull(nodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public void setup() {
@Test
public void clusterState() throws IOException {
JestResult result = client.execute(new State.Builder().build());
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());

JsonObject resultJson = result.getJsonObject();
assertNotNull(resultJson);
Expand All @@ -39,8 +38,7 @@ public void clusterState() throws IOException {
@Test
public void clusterStateWithMetadata() throws IOException {
JestResult result = client.execute(new State.Builder().filterMetadata(true).build());
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());

JsonObject resultJson = result.getJsonObject();
assertNotNull(resultJson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public String getRestMethodName() {
return "GET";
}
});

assertNotNull(result);
assertTrue(result.getErrorMessage(), result.isSucceeded());

JsonObject jsonObject = result.getJsonObject();

Expand Down
10 changes: 3 additions & 7 deletions jest/src/test/java/io/searchbox/core/BulkIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@ public void bulkOperationWithCustomGson() throws Exception {
.build();

JestResult result = client.execute(bulk);
assertNotNull(result);
assertTrue(result.getErrorMessage(), result.isSucceeded());
((List) result.getValue("items")).get(0);
if (((Map) ((List) result.getValue("items")).get(0)).get("index") != null) {
assertEquals("twitter", ((Map) ((Map) ((List) result.getValue("items")).get(0)).get("index")).get("_index"));
}
if (((Map) ((List) result.getValue("items")).get(0)).get("delete") != null) {
assertTrue((Boolean) ((Map) ((Map) ((List) result.getValue("items")).get(0)).get("delete")).get("ok"));
}
assertTrue(result.isSucceeded());

GetResponse getResponse = client().get(new GetRequest("twitter", "tweet", "1")).actionGet(5000);
assertNotNull(getResponse);
Expand Down Expand Up @@ -139,7 +138,6 @@ public void bulkOperationWithIndexWithSourceIncludingLineBreak() {
.build();

JestResult result = client.execute(bulk);
assertNotNull(result);
assertFalse(result.isSucceeded());
} catch (IOException e) {
fail("Failed during the bulk operation Exception:" + e.getMessage());
Expand All @@ -162,8 +160,7 @@ public void bulkOperationWithIndexWithParam() {

// should fail because version 6 does not exist yet
JestResult result = client.execute(bulk);
assertNotNull(result);
assertTrue(result.getJsonString().contains("VersionConflictEngineException"));
assertTrue(result.getErrorMessage(), result.isSucceeded());
} catch (IOException e) {
fail("Failed during the bulk operation Exception:" + e.getMessage());
}
Expand Down Expand Up @@ -309,15 +306,14 @@ public void bulkOperationWithSourceList() {

private JestResult executeTestCase(Action action) throws RuntimeException, IOException {
JestResult result = client.execute(action);
assertNotNull(result);
assertTrue(result.getErrorMessage(), result.isSucceeded());
((List) result.getValue("items")).get(0);
if ((Map) ((Map) ((Map) ((Map) ((List) result.getValue("items")).get(0)))).get("index") != null) {
assertEquals("twitter",((Map) ((Map) ((Map) ((Map) ((List) result.getValue("items")).get(0)))).get("index")).get("_index"));
}
if ((Map) ((Map) ((Map) ((Map) ((List) result.getValue("items")).get(0)))).get("delete") != null) {
assertEquals("twitter",((Map) ((Map) ((Map) ((Map) ((List) result.getValue("items")).get(0)))).get("delete")).get("_index"));
}
assertTrue(result.isSucceeded());
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public void delete() throws IOException {
.build();

JestResult result = client.execute(deleteByQuery);
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());;

assertEquals(
0,
Expand Down
17 changes: 3 additions & 14 deletions jest/src/test/java/io/searchbox/core/DeleteIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
/**
* @author Dogukan Sonmez
*/


@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE, numDataNodes = 1)
public class DeleteIntegrationTest extends AbstractIntegrationTest {

Expand All @@ -28,8 +26,7 @@ public void deleteDocument() throws IOException {
.index("twitter")
.type("tweet")
.build());
executeTestCase(result);
log.info("Successfully finished document delete operation");
assertFalse(result.isSucceeded());
}

@Ignore // async execution disturbs flow of the test suite
Expand All @@ -41,15 +38,14 @@ public void deleteDocumentAsynchronously() throws InterruptedException, Executio
.build(), new JestResultHandler<JestResult>() {
@Override
public void completed(JestResult result) {
executeTestCase(result);
assertFalse(result.isSucceeded());
}

@Override
public void failed(Exception ex) {
fail("failed during the asynchronous calling");
}
});
log.info("Successfully finished document delete operation");
}

@Test
Expand All @@ -61,14 +57,7 @@ public void deleteRealDocument() throws IOException {
.type("candidate")
.build());

assertNotNull(result);
assertTrue(result.isSucceeded());
log.info("Successfully finished document delete operation");
}

private void executeTestCase(JestResult result) {
assertNotNull(result);
assertFalse(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());
}

}
13 changes: 3 additions & 10 deletions jest/src/test/java/io/searchbox/core/ExplainIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.searchbox.core;

import io.searchbox.action.Action;
import io.searchbox.client.JestResult;
import io.searchbox.common.AbstractIntegrationTest;
import org.elasticsearch.action.index.IndexRequest;
Expand Down Expand Up @@ -35,14 +34,8 @@ public void explain() throws IOException {
" }\n" +
"}";

executeTestCase(new Explain.Builder("twitter", "tweet", "1", query).build());
log.info("Successfully finished explain operation");
Explain explain = new Explain.Builder("twitter", "tweet", "1", query).build();
JestResult result = client.execute(explain);
assertTrue(result.getErrorMessage(), result.isSucceeded());
}

private void executeTestCase(Action action) throws RuntimeException, IOException {
JestResult result = client.execute(action);
assertNotNull(result);
assertTrue(result.isSucceeded());
}

}
6 changes: 2 additions & 4 deletions jest/src/test/java/io/searchbox/core/GetIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public void getWithSpecialCharacterInDocId() throws IOException {
.type("tweet")
.build()
);
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());
}

@Test
Expand All @@ -60,8 +59,7 @@ public void getAsynchronously() {
client.executeAsync(new Get.Builder("twitter", "1").type("tweet").build(), new JestResultHandler<JestResult>() {
@Override
public void completed(JestResult result) {
assertNotNull(result);
assertTrue(result.isSucceeded());
assertTrue(result.getErrorMessage(), result.isSucceeded());
}

@Override
Expand Down
Loading

0 comments on commit ab5605d

Please sign in to comment.