1818
1919import static com .mongodb .jdbc .MongoDriver .MongoJDBCProperty .*;
2020import static org .junit .jupiter .api .Assertions .assertEquals ;
21+ import static org .junit .jupiter .api .Assertions .assertTrue ;
22+ import static org .junit .jupiter .api .Assertions .fail ;
2123
2224import com .mongodb .jdbc .MongoConnection ;
2325import com .mongodb .jdbc .integration .testharness .IntegrationTestUtils ;
2931import java .nio .file .Paths ;
3032import java .sql .Connection ;
3133import java .sql .DriverManager ;
34+ import java .sql .ResultSet ;
3235import java .sql .SQLException ;
3336import java .sql .Statement ;
3437import java .util .ArrayList ;
3538import java .util .Collection ;
39+ import java .util .HashSet ;
3640import java .util .List ;
3741import java .util .Properties ;
42+ import java .util .Set ;
3843import java .util .concurrent .Callable ;
3944import java .util .concurrent .ExecutorService ;
4045import java .util .concurrent .Executors ;
@@ -59,6 +64,12 @@ public class MongoIntegrationTest {
5964 : LOCAL_HOST ;
6065 static final String DEFAULT_TEST_DB = "integration_test" ;
6166 public static final String TEST_DIRECTORY = "resources/integration_test/tests" ;
67+ private static final String EXPECTED_UUID =
68+ "{\" $uuid\" :\" 71bf369b-2c60-4e6f-b23f-f9e88167cc96\" }" ;
69+ private static final String [] UUID_REPRESENTATIONS = {
70+ "standard" , "javalegacy" , "csharplegacy" , "pythonlegacy" , "default"
71+ };
72+ private static final String UUID_COLLECTION = "uuid" ;
6273
6374 private static List <TestEntry > testEntries ;
6475
@@ -76,14 +87,24 @@ public MongoConnection getBasicConnection(Properties extraProps) throws SQLExcep
7687
7788 public MongoConnection getBasicConnection (String db , Properties extraProps )
7889 throws SQLException {
90+ return getBasicConnection (db , extraProps , null );
91+ }
7992
93+ public MongoConnection getBasicConnection (String db , Properties extraProps , String uriOptions )
94+ throws SQLException {
95+ String fullUrl = URL ;
8096 Properties p = new java .util .Properties (extraProps );
8197 p .setProperty ("user" , System .getenv ("ADF_TEST_LOCAL_USER" ));
8298 p .setProperty ("password" , System .getenv ("ADF_TEST_LOCAL_PWD" ));
8399 p .setProperty ("authSource" , System .getenv ("ADF_TEST_LOCAL_AUTH_DB" ));
84100 p .setProperty ("database" , db );
85101 p .setProperty ("ssl" , "false" );
86- return (MongoConnection ) DriverManager .getConnection (URL , p );
102+
103+ if (uriOptions != null && !uriOptions .isEmpty ()) {
104+ fullUrl += (URL .contains ("?" ) ? "&" : "/?" ) + uriOptions ;
105+ }
106+
107+ return (MongoConnection ) DriverManager .getConnection (fullUrl , p );
87108 }
88109
89110 @ BeforeAll
@@ -92,7 +113,7 @@ public static void loadTestConfigs() throws IOException {
92113 }
93114
94115 @ TestFactory
95- Collection <DynamicTest > runIntegrationTests () throws SQLException {
116+ Collection <DynamicTest > runIntegrationTests () {
96117 List <DynamicTest > dynamicTests = new ArrayList <>();
97118 for (TestEntry testEntry : testEntries ) {
98119 if (testEntry .skip_reason != null ) {
@@ -111,7 +132,7 @@ Collection<DynamicTest> runIntegrationTests() throws SQLException {
111132 }
112133
113134 /** Simple callable used to spawn a new statement and execute a query. */
114- public class SimpleQueryExecutor implements Callable <Void > {
135+ public static class SimpleQueryExecutor implements Callable <Void > {
115136 private final Connection conn ;
116137 private final String query ;
117138
@@ -137,7 +158,7 @@ public Void call() throws Exception {
137158 @ Test
138159 public void testLoggingWithParallelConnectionAndStatementExec () throws Exception {
139160 ExecutorService executor = Executors .newFixedThreadPool (4 );
140- List <Callable <Void >> tasks = new ArrayList <Callable < Void > >();
161+ List <Callable <Void >> tasks = new ArrayList <>();
141162
142163 // Connection with no logging.
143164 MongoConnection noLogging = connect (null );
@@ -231,4 +252,142 @@ private void cleanUp(MongoConnection conn) {
231252 e .printStackTrace ();
232253 }
233254 }
255+
256+ /**
257+ * Tests the handling of different UUID representations specified in the URI. The uuid fields
258+ * have been pre-loaded into the database, stored in their respective uuid representations
259+ * according to their type. This test verifies that each representation is correctly retrieved
260+ * and converted to the expected string format.
261+ */
262+ @ Test
263+ public void testUUIDRepresentationInURI () {
264+ for (String representation : UUID_REPRESENTATIONS ) {
265+ System .out .println ("Testing with UUID representation: " + representation );
266+
267+ try (MongoConnection conn =
268+ representation .equals ("default" )
269+ ? getBasicConnection (DEFAULT_TEST_DB , null )
270+ : getBasicConnection (
271+ DEFAULT_TEST_DB ,
272+ null ,
273+ "uuidRepresentation=" + representation );
274+ Statement stmt = conn .createStatement ()) {
275+
276+ // If no uuidRepresentation is specified in the URI, default to `pythonlegacy`
277+ String type = representation .equals ("default" ) ? "pythonlegacy" : representation ;
278+ String query = "SELECT * FROM " + UUID_COLLECTION + " WHERE type = '" + type + "'" ;
279+
280+ try (ResultSet rs = stmt .executeQuery (query )) {
281+ if (rs .next ()) {
282+ String uuid = rs .getString ("uuid" );
283+ System .out .println (
284+ "Representation: "
285+ + representation
286+ + ", Type: "
287+ + type
288+ + ", UUID: "
289+ + uuid );
290+ assertEquals (
291+ EXPECTED_UUID ,
292+ uuid ,
293+ "Mismatch for " + representation + " representation" );
294+ } else {
295+ fail ("No result found for type: " + type );
296+ }
297+ }
298+ } catch (SQLException e ) {
299+ fail ("Failed to execute query for " + representation + ": " + e .getMessage ());
300+ }
301+ }
302+ }
303+
304+ /**
305+ * Tests the behavior of standard UUID representation when querying legacy UUID types. This test
306+ * ensures that when using the standard representation, legacy UUID types are correctly
307+ * retrieved and represented in the expected $binary format.
308+ */
309+ @ Test
310+ public void testStandardRepresentationWithLegacyTypes () {
311+ try (MongoConnection conn =
312+ getBasicConnection (DEFAULT_TEST_DB , null , "uuidRepresentation=STANDARD" );
313+ Statement stmt = conn .createStatement ()) {
314+
315+ for (String legacyType : UUID_REPRESENTATIONS ) {
316+ if (legacyType .equals ("standard" ) || legacyType .equals ("default" )) continue ;
317+
318+ String query =
319+ "SELECT * FROM " + UUID_COLLECTION + " WHERE type = '" + legacyType + "'" ;
320+ try (ResultSet rs = stmt .executeQuery (query )) {
321+ if (rs .next ()) {
322+ String uuid = rs .getString ("uuid" );
323+ System .out .println (
324+ "STANDARD representation - Type: "
325+ + legacyType
326+ + ", UUID: "
327+ + uuid );
328+ assertTrue (
329+ uuid .startsWith ("{\" $binary\" :" ),
330+ "Expected $binary format for "
331+ + legacyType
332+ + " type with STANDARD representation" );
333+ assertTrue (
334+ uuid .contains ("\" base64\" :" ),
335+ "Expected base64 field in $binary format" );
336+ assertTrue (
337+ uuid .contains ("\" subType\" :" ),
338+ "Expected subType field in $binary format" );
339+ } else {
340+ fail ("No result found for type: " + legacyType );
341+ }
342+ }
343+ }
344+ } catch (SQLException e ) {
345+ fail ("Failed to execute query: " + e .getMessage ());
346+ }
347+ }
348+
349+ /**
350+ * Tests the behavior of different UUID representations when querying the 'javalegacy' UUID
351+ * type. This test verifies that each representation retrieves the 'javalegacy' UUID correctly,
352+ * and that the value of the UUID are different.
353+ */
354+ @ Test
355+ public void testDifferentRepresentationsForJavaLegacy () {
356+ Set <String > uuidValues = new HashSet <>();
357+ for (String representation : UUID_REPRESENTATIONS ) {
358+ if (representation .equals ("default" )) continue ;
359+ try (MongoConnection conn =
360+ getBasicConnection (
361+ DEFAULT_TEST_DB , null , "uuidRepresentation=" + representation );
362+ Statement stmt = conn .createStatement ();
363+ ResultSet rs =
364+ stmt .executeQuery (
365+ "SELECT * FROM "
366+ + UUID_COLLECTION
367+ + " WHERE type = 'javalegacy'" )) {
368+ if (rs .next ()) {
369+ String uuid = rs .getString ("uuid" );
370+ System .out .println (representation + " representation - UUID: " + uuid );
371+ if (representation .equals ("standard" )) {
372+ assertTrue (
373+ uuid .startsWith ("{\" $binary\" :" ),
374+ "Expected $binary format for standard representation" );
375+ } else {
376+ assertTrue (
377+ uuid .startsWith ("{\" $uuid\" :" ),
378+ "Expected $uuid format for non-standard representation" );
379+ }
380+ uuidValues .add (uuid );
381+ } else {
382+ fail (
383+ "No result found for 'javalegacy' type with "
384+ + representation
385+ + " representation" );
386+ }
387+ } catch (SQLException e ) {
388+ fail ("Failed to execute query for " + representation + ": " + e .getMessage ());
389+ }
390+ }
391+ assertEquals (4 , uuidValues .size (), "Expected 4 different UUID values (including standard)" );
392+ }
234393}
0 commit comments