Skip to content

Commit

Permalink
Use jsonassert and query ordering to make the tests more stable
Browse files Browse the repository at this point in the history
In passing, remove the imports of deprecated junit.framework classes
  • Loading branch information
gsmet committed Nov 24, 2016
1 parent 9825e04 commit 9fa8327
Show file tree
Hide file tree
Showing 34 changed files with 220 additions and 171 deletions.
5 changes: 5 additions & 0 deletions jest-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<artifactId>hamcrest-all</artifactId>
</dependency>

<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* @author Dogukan Sonmez
Expand Down
44 changes: 24 additions & 20 deletions jest-common/src/test/java/io/searchbox/client/JestResultTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package io.searchbox.client;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;

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;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @author Dogukan Sonmez
*/
Expand Down Expand Up @@ -127,9 +131,9 @@ public void getGetSourceAsObject() {
assertEquals("trying out Elastic Search", twitter.getMessage());
assertEquals("2009-11-15T14:12:12", twitter.getPostDate());
}

@Test
public void getGetSourceAsString() {
public void getGetSourceAsString() throws JSONException {
String response = "{\n" +
" \"_index\" : \"twitter\",\n" +
" \"_type\" : \"tweet\",\n" +
Expand All @@ -140,21 +144,21 @@ public void getGetSourceAsString() {
" \"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());
JSONAssert.assertEquals(onlySource, result.getSourceAsString(), false);
}

@Test
public void getGetSourceAsStringArray() {
public void getGetSourceAsStringArray() throws JSONException {
String response = "{\n" +
" \"_index\" : \"twitter\",\n" +
" \"_type\" : \"tweet\",\n" +
Expand All @@ -165,27 +169,27 @@ public void getGetSourceAsStringArray() {
" { \"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());
JSONAssert.assertEquals(onlySource, result.getSourceAsString(), false);
}

@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);
Expand Down
29 changes: 16 additions & 13 deletions jest-common/src/test/java/io/searchbox/core/BulkTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.searchbox.core;

import com.google.gson.Gson;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

import java.util.HashMap;
import java.util.Map;

import static junit.framework.Assert.assertEquals;
import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import com.google.gson.Gson;

/**
* @author Dogukan Sonmez
Expand All @@ -19,7 +22,7 @@ public void emptyBulkOperation() {
}

@Test
public void bulkOperationWithIndex() {
public void bulkOperationWithIndex() throws JSONException {
Map source = new HashMap();
source.put("field", "value");

Expand All @@ -29,22 +32,22 @@ public void bulkOperationWithIndex() {
executeAsserts(bulk);
String expectedData = "{\"index\":{\"_id\":\"1\",\"_index\":\"twitter\",\"_type\":\"tweet\"}}\n" +
"{\"field\":\"value\"}";
assertEquals(expectedData.trim(), bulk.getData(new Gson()).toString().trim());
JSONAssert.assertEquals(expectedData, bulk.getData(new Gson()).toString(), false);
}

@Test
public void bulkOperationWithSingleDelete() {
public void bulkOperationWithSingleDelete() throws JSONException {
Bulk bulk = new Bulk.Builder()
.addAction(new Delete.Builder("1").index("twitter").type("tweet").build())
.build();

executeAsserts(bulk);
String expectedData = "{\"delete\":{\"_id\":\"1\",\"_index\":\"twitter\",\"_type\":\"tweet\"}}\n";
assertEquals(expectedData.trim(), bulk.getData(new Gson()).toString().trim());
JSONAssert.assertEquals(expectedData, bulk.getData(new Gson()).toString(), false);
}

@Test
public void bulkOperationWithMultipleIndex() {
public void bulkOperationWithMultipleIndex() throws JSONException {
Map source = new HashMap();
source.put("field", "value");

Expand All @@ -58,11 +61,11 @@ public void bulkOperationWithMultipleIndex() {
"{\"field\":\"value\"}\n" +
"{\"index\":{\"_id\":\"2\",\"_index\":\"elasticsearch\",\"_type\":\"jest\"}}\n" +
"{\"field\":\"value\"}";
assertEquals(expectedData.trim(), bulk.getData(new Gson()).toString().trim());
JSONAssert.assertEquals(expectedData, bulk.getData(new Gson()).toString(), false);
}

@Test
public void bulkOperationWithMultipleDelete() {
public void bulkOperationWithMultipleDelete() throws JSONException {
Bulk bulk = new Bulk.Builder()
.addAction(new Delete.Builder("1").index("twitter").type("tweet").build())
.addAction(new Delete.Builder("2").index("twitter").type("tweet").build())
Expand All @@ -71,11 +74,11 @@ public void bulkOperationWithMultipleDelete() {
executeAsserts(bulk);
String expectedData = "{\"delete\":{\"_id\":\"1\",\"_index\":\"twitter\",\"_type\":\"tweet\"}}\n" +
"{\"delete\":{\"_id\":\"2\",\"_index\":\"twitter\",\"_type\":\"tweet\"}}";
assertEquals(expectedData.trim(), bulk.getData(new Gson()).toString().trim());
JSONAssert.assertEquals(expectedData, bulk.getData(new Gson()).toString(), false);
}

@Test
public void bulkOperationWithMultipleIndexAndDelete() {
public void bulkOperationWithMultipleIndexAndDelete() throws JSONException {
Map source = new HashMap();
source.put("field", "value");

Expand All @@ -93,7 +96,7 @@ public void bulkOperationWithMultipleIndexAndDelete() {
"{\"field\":\"value\"}\n" +
"{\"delete\":{\"_id\":\"1\",\"_index\":\"twitter\",\"_type\":\"tweet\"}}\n" +
"{\"delete\":{\"_id\":\"2\",\"_index\":\"twitter\",\"_type\":\"tweet\"}}";
assertEquals(expectedData.trim(), bulk.getData(new Gson()).trim());
JSONAssert.assertEquals(expectedData, bulk.getData(new Gson()), false);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.Test;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.Test;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

/**
Expand Down
21 changes: 12 additions & 9 deletions jest-common/src/test/java/io/searchbox/core/MultiGetTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.searchbox.core;

import com.google.gson.Gson;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import com.google.gson.Gson;

/**
* @author Dogukan Sonmez
Expand All @@ -18,16 +21,16 @@ public class MultiGetTest {
Doc doc3 = new Doc("twitter", "tweet", "3");

@Test
public void getMultipleDocs() {
public void getMultipleDocs() throws JSONException {
MultiGet get = new MultiGet.Builder.ByDoc(Arrays.asList(doc1, doc2, doc3)).build();

assertEquals("GET", get.getRestMethodName());
assertEquals("/_mget", get.getURI());
assertEquals("{\"docs\":[" +
JSONAssert.assertEquals("{\"docs\":[" +
"{\"_index\":\"twitter\",\"_type\":\"tweet\",\"_id\":\"1\"}," +
"{\"_index\":\"twitter\",\"_type\":\"tweet\",\"_id\":\"2\"}," +
"{\"_index\":\"twitter\",\"_type\":\"tweet\",\"_id\":\"3\"}]}",
get.getData(new Gson()));
get.getData(new Gson()), false);
}

@Test
Expand All @@ -47,12 +50,12 @@ public void equalsReturnsFalseForDiffererntDocs() {
}

@Test
public void getDocumentWithMultipleIds() {
public void getDocumentWithMultipleIds() throws JSONException {
MultiGet get = new MultiGet.Builder.ById("twitter", "tweet").addId(Arrays.asList("1", "2", "3")).build();

assertEquals("GET", get.getRestMethodName());
assertEquals("twitter/tweet/_mget", get.getURI());
assertEquals("{\"ids\":[\"1\",\"2\",\"3\"]}", get.getData(new Gson()));
JSONAssert.assertEquals("{\"ids\":[\"1\",\"2\",\"3\"]}", get.getData(new Gson()), false);
}

@Test
Expand Down
16 changes: 9 additions & 7 deletions jest-common/src/test/java/io/searchbox/core/MultiSearchTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.searchbox.core;

import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import java.util.Arrays;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

/**
Expand All @@ -13,7 +15,7 @@
public class MultiSearchTest {

@Test
public void singleMultiSearchWithoutIndex() {
public void singleMultiSearchWithoutIndex() throws JSONException {
String expectedData = " {\"index\" : \"_all\"}\n" +
"{\"query\" : {\"match_all\" : {}}}\n";
Search search = new Search.Builder("{\"query\" : {\"match_all\" : {}}}").build();
Expand All @@ -22,11 +24,11 @@ public void singleMultiSearchWithoutIndex() {

assertEquals("POST", multiSearch.getRestMethodName());
assertEquals("/_msearch", multiSearch.getURI());
assertEquals(expectedData.trim(), multiSearch.getData(null).toString().trim());
JSONAssert.assertEquals(expectedData, multiSearch.getData(null).toString(), false);
}

@Test
public void singleMultiSearchWitIndex() {
public void singleMultiSearchWitIndex() throws JSONException {
String expectedData = " {\"index\" : \"twitter\"}\n" +
"{\"query\" : {\"match_all\" : {}}}\n";
Search search = new Search.Builder("{\"query\" : {\"match_all\" : {}}}")
Expand All @@ -37,11 +39,11 @@ public void singleMultiSearchWitIndex() {

assertEquals("POST", multiSearch.getRestMethodName());
assertEquals("/_msearch", multiSearch.getURI());
assertEquals(expectedData.trim(), multiSearch.getData(null).toString().trim());
JSONAssert.assertEquals(expectedData, multiSearch.getData(null).toString(), false);
}

@Test
public void multiSearchWitIndex() {
public void multiSearchWitIndex() throws JSONException {
String expectedData = " {\"index\" : \"twitter\"}\n" +
"{\"query\" : {\"match_all\" : {}}}\n" +
"{\"index\" : \"_all\"}\n" +
Expand All @@ -55,7 +57,7 @@ public void multiSearchWitIndex() {

assertEquals("POST", multiSearch.getRestMethodName());
assertEquals("/_msearch", multiSearch.getURI());
assertEquals(expectedData.trim(), multiSearch.getData(null).toString().trim());
JSONAssert.assertEquals(expectedData, multiSearch.getData(null).toString(), false);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.Test;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

/**
Expand Down
Loading

0 comments on commit 9fa8327

Please sign in to comment.