diff --git a/mongodb/src/test/java/org/hibernate/ogm/datastore/mongodb/test/query/nativequery/MongoDBSessionCLIQueryTest.java b/mongodb/src/test/java/org/hibernate/ogm/datastore/mongodb/test/query/nativequery/MongoDBSessionCLIQueryTest.java index c4905cc425..4e7802b340 100644 --- a/mongodb/src/test/java/org/hibernate/ogm/datastore/mongodb/test/query/nativequery/MongoDBSessionCLIQueryTest.java +++ b/mongodb/src/test/java/org/hibernate/ogm/datastore/mongodb/test/query/nativequery/MongoDBSessionCLIQueryTest.java @@ -9,6 +9,7 @@ import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.Objects; import org.hibernate.Session; import org.hibernate.Transaction; @@ -25,6 +26,7 @@ import org.junit.rules.ExpectedException; import com.mongodb.BasicDBList; +import org.bson.Document; import org.fest.assertions.Fail; import org.fest.assertions.MapAssert; @@ -726,10 +728,9 @@ public void testFindWithModifiersWithEntity() { String nativeQuery = "db." + OscarWildePoem.TABLE_NAME + ".find({" + queryWithModifiers + "})"; - NativeQuery query = session.createNativeQuery( nativeQuery ).addEntity( OscarWildePoem.class ); - @SuppressWarnings("unchecked") + NativeQuery query = session.createNativeQuery( nativeQuery ).addEntity( OscarWildePoem.class ); List result = query.list(); - assertThat( result ).onProperty( "id" ).containsExactly( athanasia.getId() ); + assertThat( result ).containsExactly( athanasia ); } ); } @@ -741,17 +742,32 @@ public void testFindWithExplain() { queryWithModifiers.append( ", '$max': { 'year' : 1881 } " ); queryWithModifiers.append( ", '$hint': { 'year' : 1 } " ); queryWithModifiers.append( ", '$explain': true " ); - String nativeQuery = "db." + OscarWildePoem.TABLE_NAME + ".find({" + queryWithModifiers + "})"; - NativeQuery query = session.createNativeQuery( nativeQuery ); - @SuppressWarnings("unchecked") - List result = query.list(); - // I'm not sure if we can test the content because this is the result of the explain command - // and I believe it might change among versions - assertThat( result.get( 0 ) ).isNotEmpty(); + String nativeQuery = "db." + OscarWildePoem.TABLE_NAME + ".find({" + queryWithModifiers + "})"; + Object[] result = (Object[]) session.createNativeQuery( nativeQuery ).uniqueResult(); + assertThat( explained( result ) ) + .as( "Unrecognized `$explain` output: " + Arrays.toString( result ) ) + .isTrue(); } ); } + /** + * Different versions of MongoDB returns different results for the `$explain` operator. + * We still expect to have a `namespace` key inside a `Document` somewhere in the result. + */ + private static boolean explained(Object[] result) { + Objects.requireNonNull( result ); + for ( Object value : result ) { + if ( value instanceof Document ) { + Document document = (Document) value; + if ( document.containsKey( "namespace" ) ) { + return true; + } + } + } + return false; + } + @Test @TestForIssue(jiraKey = "OGM-1024") public void testAggregateWithUnwindGroupAndSort() { diff --git a/mongodb/src/test/java/org/hibernate/ogm/datastore/mongodb/test/query/nativequery/OscarWildePoem.java b/mongodb/src/test/java/org/hibernate/ogm/datastore/mongodb/test/query/nativequery/OscarWildePoem.java index f6ecb6917a..d106d6c746 100644 --- a/mongodb/src/test/java/org/hibernate/ogm/datastore/mongodb/test/query/nativequery/OscarWildePoem.java +++ b/mongodb/src/test/java/org/hibernate/ogm/datastore/mongodb/test/query/nativequery/OscarWildePoem.java @@ -151,14 +151,14 @@ public boolean equals(Object o) { return false; } OscarWildePoem that = (OscarWildePoem) o; - return Objects.equals( name, that.name ) && - Objects.equals( author, that.author ) && - Objects.equals( year, that.year ); + return rating == that.rating && Objects.equals( name, that.name ) && Objects.equals( + author, + that.author + ) && Objects.equals( year, that.year ) && Objects.equals( copiesSold, that.copiesSold ); } @Override public int hashCode() { - - return Objects.hash( name, author, year ); + return Objects.hash( name, author, rating, year, copiesSold ); } }