18
18
19
19
import static com .mongodb .jdbc .MongoDriver .MongoJDBCProperty .*;
20
20
import 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 ;
21
23
22
24
import com .mongodb .jdbc .MongoConnection ;
23
25
import com .mongodb .jdbc .integration .testharness .IntegrationTestUtils ;
29
31
import java .nio .file .Paths ;
30
32
import java .sql .Connection ;
31
33
import java .sql .DriverManager ;
34
+ import java .sql .ResultSet ;
32
35
import java .sql .SQLException ;
33
36
import java .sql .Statement ;
34
37
import java .util .ArrayList ;
35
38
import java .util .Collection ;
39
+ import java .util .HashSet ;
36
40
import java .util .List ;
37
41
import java .util .Properties ;
42
+ import java .util .Set ;
38
43
import java .util .concurrent .Callable ;
39
44
import java .util .concurrent .ExecutorService ;
40
45
import java .util .concurrent .Executors ;
@@ -59,6 +64,12 @@ public class MongoIntegrationTest {
59
64
: LOCAL_HOST ;
60
65
static final String DEFAULT_TEST_DB = "integration_test" ;
61
66
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" ;
62
73
63
74
private static List <TestEntry > testEntries ;
64
75
@@ -76,14 +87,24 @@ public MongoConnection getBasicConnection(Properties extraProps) throws SQLExcep
76
87
77
88
public MongoConnection getBasicConnection (String db , Properties extraProps )
78
89
throws SQLException {
90
+ return getBasicConnection (db , extraProps , null );
91
+ }
79
92
93
+ public MongoConnection getBasicConnection (String db , Properties extraProps , String uriOptions )
94
+ throws SQLException {
95
+ String fullUrl = URL ;
80
96
Properties p = new java .util .Properties (extraProps );
81
97
p .setProperty ("user" , System .getenv ("ADF_TEST_LOCAL_USER" ));
82
98
p .setProperty ("password" , System .getenv ("ADF_TEST_LOCAL_PWD" ));
83
99
p .setProperty ("authSource" , System .getenv ("ADF_TEST_LOCAL_AUTH_DB" ));
84
100
p .setProperty ("database" , db );
85
101
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 );
87
108
}
88
109
89
110
@ BeforeAll
@@ -92,7 +113,7 @@ public static void loadTestConfigs() throws IOException {
92
113
}
93
114
94
115
@ TestFactory
95
- Collection <DynamicTest > runIntegrationTests () throws SQLException {
116
+ Collection <DynamicTest > runIntegrationTests () {
96
117
List <DynamicTest > dynamicTests = new ArrayList <>();
97
118
for (TestEntry testEntry : testEntries ) {
98
119
if (testEntry .skip_reason != null ) {
@@ -111,7 +132,7 @@ Collection<DynamicTest> runIntegrationTests() throws SQLException {
111
132
}
112
133
113
134
/** 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 > {
115
136
private final Connection conn ;
116
137
private final String query ;
117
138
@@ -137,7 +158,7 @@ public Void call() throws Exception {
137
158
@ Test
138
159
public void testLoggingWithParallelConnectionAndStatementExec () throws Exception {
139
160
ExecutorService executor = Executors .newFixedThreadPool (4 );
140
- List <Callable <Void >> tasks = new ArrayList <Callable < Void > >();
161
+ List <Callable <Void >> tasks = new ArrayList <>();
141
162
142
163
// Connection with no logging.
143
164
MongoConnection noLogging = connect (null );
@@ -231,4 +252,142 @@ private void cleanUp(MongoConnection conn) {
231
252
e .printStackTrace ();
232
253
}
233
254
}
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
+ }
234
393
}
0 commit comments