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..0d07f7ce8b 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 @@ -25,6 +25,7 @@ import org.junit.rules.ExpectedException; import com.mongodb.BasicDBList; +import org.bson.Document; import org.fest.assertions.Fail; import org.fest.assertions.MapAssert; @@ -719,6 +720,8 @@ public void testFindWithModifiersWithEntity() { queryWithModifiers.append( "'$query': { } " ); queryWithModifiers.append( ", '$max': { 'year' : 1881 } " ); queryWithModifiers.append( ", '$hint': { 'year' : 1 } " ); + // It's important to have at least one test with the `$explain` modifier set to false because + // if something goes wrong it will return a different result type queryWithModifiers.append( ", '$explain': false " ); queryWithModifiers.append( ", '$snapshot': false " ); queryWithModifiers.append( ", '$comment': 'Testing comment' " ); @@ -726,10 +729,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 +743,34 @@ 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 return different results for the `$explain` operator. + * But, we expect to have a `namespace` key inside a `Document` somewhere in the result. + */ + private static boolean explained(Object[] result) { + if ( result == null ) { + return false; + } + 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 ); } }