25
25
import org .junit .rules .ExpectedException ;
26
26
27
27
import com .mongodb .BasicDBList ;
28
+ import org .bson .Document ;
28
29
import org .fest .assertions .Fail ;
29
30
import org .fest .assertions .MapAssert ;
30
31
@@ -719,17 +720,18 @@ public void testFindWithModifiersWithEntity() {
719
720
queryWithModifiers .append ( "'$query': { } " );
720
721
queryWithModifiers .append ( ", '$max': { 'year' : 1881 } " );
721
722
queryWithModifiers .append ( ", '$hint': { 'year' : 1 } " );
723
+ // It's important to have at least one test with the `$explain` modifier set to false because
724
+ // if something goes wrong it will return a different result type
722
725
queryWithModifiers .append ( ", '$explain': false " );
723
726
queryWithModifiers .append ( ", '$snapshot': false " );
724
727
queryWithModifiers .append ( ", '$comment': 'Testing comment' " );
725
728
queryWithModifiers .append ( ", '$maxScan': 11234" );
726
729
727
730
String nativeQuery = "db." + OscarWildePoem .TABLE_NAME + ".find({" + queryWithModifiers + "})" ;
728
731
729
- NativeQuery query = session .createNativeQuery ( nativeQuery ).addEntity ( OscarWildePoem .class );
730
- @ SuppressWarnings ("unchecked" )
732
+ NativeQuery <OscarWildePoem > query = session .createNativeQuery ( nativeQuery ).addEntity ( OscarWildePoem .class );
731
733
List <OscarWildePoem > result = query .list ();
732
- assertThat ( result ).onProperty ( "id" ). containsExactly ( athanasia . getId () );
734
+ assertThat ( result ).containsExactly ( athanasia );
733
735
} );
734
736
}
735
737
@@ -741,17 +743,34 @@ public void testFindWithExplain() {
741
743
queryWithModifiers .append ( ", '$max': { 'year' : 1881 } " );
742
744
queryWithModifiers .append ( ", '$hint': { 'year' : 1 } " );
743
745
queryWithModifiers .append ( ", '$explain': true " );
744
- String nativeQuery = "db." + OscarWildePoem .TABLE_NAME + ".find({" + queryWithModifiers + "})" ;
745
746
746
- NativeQuery query = session .createNativeQuery ( nativeQuery );
747
- @ SuppressWarnings ("unchecked" )
748
- List <Object []> result = query .list ();
749
- // I'm not sure if we can test the content because this is the result of the explain command
750
- // and I believe it might change among versions
751
- assertThat ( result .get ( 0 ) ).isNotEmpty ();
747
+ String nativeQuery = "db." + OscarWildePoem .TABLE_NAME + ".find({" + queryWithModifiers + "})" ;
748
+ Object [] result = (Object []) session .createNativeQuery ( nativeQuery ).uniqueResult ();
749
+ assertThat ( explained ( result ) )
750
+ .as ( "Unrecognized `$explain` output: " + Arrays .toString ( result ) )
751
+ .isTrue ();
752
752
} );
753
753
}
754
754
755
+ /**
756
+ * Different versions of MongoDB return different results for the `$explain` operator.
757
+ * But, we expect to have a `namespace` key inside a `Document` somewhere in the result.
758
+ */
759
+ private static boolean explained (Object [] result ) {
760
+ if ( result == null ) {
761
+ return false ;
762
+ }
763
+ for ( Object value : result ) {
764
+ if ( value instanceof Document ) {
765
+ Document document = (Document ) value ;
766
+ if ( document .containsKey ( "namespace" ) ) {
767
+ return true ;
768
+ }
769
+ }
770
+ }
771
+ return false ;
772
+ }
773
+
755
774
@ Test
756
775
@ TestForIssue (jiraKey = "OGM-1024" )
757
776
public void testAggregateWithUnwindGroupAndSort () {
0 commit comments