Skip to content

Commit

Permalink
HSEARCH-3662 Use JsonHelper.assertJsonEquals rather than JSONAssert.a…
Browse files Browse the repository at this point in the history
…ssertEquals in tests

Because the latter throws nasty checked exceptions and gives less
information when an assertion fails.
  • Loading branch information
yrodiere committed Nov 12, 2019
1 parent 8185d09 commit 1a37207
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import static org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThat;
import static org.hibernate.search.util.impl.integrationtest.common.stub.mapper.StubMapperUtils.referenceProvider;
import static org.hibernate.search.util.impl.test.JsonHelper.assertJsonEquals;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -56,8 +57,6 @@
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

public class ElasticsearchExtensionIT {

Expand Down Expand Up @@ -558,7 +557,7 @@ public void projection_document() throws JSONException {

List<String> result = query.fetchAll().getHits();
Assertions.assertThat( result ).hasSize( 1 );
JSONAssert.assertEquals(
assertJsonEquals(
"{"
+ "'nativeField_string': 'text 2',"
+ "'nativeField_integer': 1,"
Expand All @@ -567,8 +566,7 @@ public void projection_document() throws JSONException {
+ "'nativeField_unsupportedType': 'foobar',"
+ "'nativeField_sort5': 'z'"
+ "}",
result.get( 0 ),
JSONCompareMode.STRICT
result.get( 0 )
);
}

Expand All @@ -577,7 +575,7 @@ public void projection_document() throws JSONException {
* even if there is a field projection, which would usually trigger source filtering.
*/
@Test
public void projection_documentAndField() throws JSONException {
public void projection_documentAndField() {
StubMappingScope scope = indexManager.createScope();

SearchQuery<List<?>> query = scope.query()
Expand All @@ -594,7 +592,7 @@ public void projection_documentAndField() throws JSONException {
.map( list -> (String) list.get( 0 ) )
.collect( Collectors.toList() );
Assertions.assertThat( result ).hasSize( 1 );
JSONAssert.assertEquals(
assertJsonEquals(
"{"
+ "'nativeField_string': 'text 2',"
+ "'nativeField_integer': 1,"
Expand All @@ -603,8 +601,7 @@ public void projection_documentAndField() throws JSONException {
+ "'nativeField_unsupportedType': 'foobar',"
+ "'nativeField_sort5': 'z'"
+ "}",
result.get( 0 ),
JSONCompareMode.STRICT
result.get( 0 )
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package org.hibernate.search.integrationtest.backend.elasticsearch.testsupport.util;

import static org.hibernate.search.util.impl.test.JsonHelper.assertJsonEquals;

import java.util.List;

import org.hibernate.search.backend.elasticsearch.client.spi.ElasticsearchRequest;
Expand All @@ -15,8 +17,6 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.assertj.core.api.Assertions;
import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

class ElasticsearchClientSubmitCall extends Call<ElasticsearchClientSubmitCall> {
Expand Down Expand Up @@ -59,29 +59,19 @@ void verify(ElasticsearchClientSubmitCall actualCall) {
Assertions.assertThat( actualCall.request.getParameters() )
.containsAllEntriesOf( request.getParameters() );
}
try {
JSONAssert.assertEquals(
toComparableJson( request.getBodyParts() ),
toComparableJson( actualCall.request.getBodyParts() ),
JSONCompareMode.STRICT_ORDER
);
}
catch (JSONException e) {
throw new IllegalStateException( "Error handling JSON for testing purposes", e );
}
assertJsonEquals(
toComparableJson( request.getBodyParts() ),
toComparableJson( actualCall.request.getBodyParts() ),
JSONCompareMode.STRICT_ORDER
);
break;
case STRICT:
Assertions.assertThat( actualCall.request.getParameters() ).isEqualTo( request.getParameters() );
try {
JSONAssert.assertEquals(
toComparableJson( request.getBodyParts() ),
toComparableJson( actualCall.request.getBodyParts() ),
JSONCompareMode.STRICT
);
}
catch (JSONException e) {
throw new IllegalStateException( "Error handling JSON for testing purposes", e );
}
assertJsonEquals(
toComparableJson( request.getBodyParts() ),
toComparableJson( actualCall.request.getBodyParts() ),
JSONCompareMode.STRICT
);
break;
}
}
Expand Down

0 comments on commit 1a37207

Please sign in to comment.