-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathDatabaseResourceIT.java
More file actions
1752 lines (1491 loc) · 69 KB
/
DatabaseResourceIT.java
File metadata and controls
1752 lines (1491 loc) · 69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.openmetadata.it.tests;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.openmetadata.it.factories.DatabaseServiceTestFactory;
import org.openmetadata.it.util.EntityValidation;
import org.openmetadata.it.util.SdkClients;
import org.openmetadata.it.util.TestNamespace;
import org.openmetadata.it.util.UpdateType;
import org.openmetadata.schema.api.data.CreateDatabase;
import org.openmetadata.schema.api.data.CreateDatabaseSchema;
import org.openmetadata.schema.api.data.CreateTable;
import org.openmetadata.schema.api.services.CreateDatabaseService;
import org.openmetadata.schema.api.services.CreateDatabaseService.DatabaseServiceType;
import org.openmetadata.schema.api.services.DatabaseConnection;
import org.openmetadata.schema.entity.data.Database;
import org.openmetadata.schema.entity.data.DatabaseSchema;
import org.openmetadata.schema.entity.data.Table;
import org.openmetadata.schema.entity.services.DatabaseService;
import org.openmetadata.schema.services.connections.database.PostgresConnection;
import org.openmetadata.schema.type.ApiStatus;
import org.openmetadata.schema.type.ChangeDescription;
import org.openmetadata.schema.type.Column;
import org.openmetadata.schema.type.ColumnDataType;
import org.openmetadata.schema.type.EntityHistory;
import org.openmetadata.schema.type.EntityReference;
import org.openmetadata.schema.type.api.BulkOperationResult;
import org.openmetadata.schema.type.csv.CsvImportResult;
import org.openmetadata.sdk.client.OpenMetadataClient;
import org.openmetadata.sdk.exceptions.InvalidRequestException;
import org.openmetadata.sdk.fluent.Databases;
import org.openmetadata.sdk.models.ListParams;
import org.openmetadata.sdk.models.ListResponse;
import org.openmetadata.sdk.network.HttpMethod;
import org.openmetadata.service.util.FullyQualifiedName;
/**
* Integration tests for Database entity operations.
*
* Extends BaseEntityIT to inherit all common entity tests.
* Adds database-specific tests.
*
* Migrated from: org.openmetadata.service.resources.databases.DatabaseResourceTest
* Migration status: 8 entity-specific tests migrated, 6 tests not migrated (see details below)
*
* MIGRATED TESTS (8):
* - post_databaseFQN_as_admin_200_OK
* - post_databaseWithoutRequiredService_4xx
* - post_databaseWithDifferentService_200_ok
* - test_bulkServiceFetching_200_OK
* - test_fieldFetchersForServiceAndName
* - testDatabaseRdfRelationships (RDF-enabled environments only)
* - testDatabaseRdfSoftDeleteAndRestore (RDF-enabled environments only)
* - testDatabaseRdfHardDelete (RDF-enabled environments only)
*
* NOT MIGRATED (6):
* - 3 CSV import/export tests (SDK endpoint mismatch)
* - 3 Bulk API tests (require WebTarget for detailed verification)
*
* Test isolation: Uses TestNamespace for unique entity names
* Parallelization: Safe for concurrent execution via @Execution(ExecutionMode.CONCURRENT)
*/
@Execution(ExecutionMode.CONCURRENT)
public class DatabaseResourceIT extends BaseEntityIT<Database, CreateDatabase> {
{
supportsImportExport = true;
supportsBatchImport = true;
supportsRecursiveImport = true; // Database supports recursive import with nested schemas/tables
supportsLifeCycle = true;
supportsListHistoryByTimestamp = true;
supportsBulkAPI = true;
supportsDataContract = true;
}
// Store last created database for import/export tests
private Database lastCreatedDatabase;
// ===================================================================
// ABSTRACT METHOD IMPLEMENTATIONS (Required by BaseEntityIT)
// ===================================================================
@Override
protected CreateDatabase createMinimalRequest(TestNamespace ns) {
// Database requires a database service as parent
DatabaseService service = DatabaseServiceTestFactory.createPostgres(ns);
CreateDatabase request = new CreateDatabase();
request.setName(ns.prefix("db"));
request.setService(service.getFullyQualifiedName());
request.setDescription("Test database created by integration test");
return request;
}
@Override
protected CreateDatabase createRequest(String name, TestNamespace ns) {
// Note: This creates a new service each time, which means duplicate detection
// only applies within the same service. For cross-service duplicates,
// the test would need to be customized.
DatabaseService service = DatabaseServiceTestFactory.createPostgres(ns);
CreateDatabase request = new CreateDatabase();
request.setName(name);
request.setService(service.getFullyQualifiedName());
return request;
}
/**
* Create request with specific service (for duplicate testing).
*/
protected CreateDatabase createRequestWithService(String name, String serviceFQN) {
CreateDatabase request = new CreateDatabase();
request.setName(name);
request.setService(serviceFQN);
return request;
}
@Override
protected Database createEntity(CreateDatabase createRequest) {
return SdkClients.adminClient().databases().create(createRequest);
}
@Override
protected Database getEntity(String id) {
return SdkClients.adminClient().databases().get(id);
}
@Override
protected Database getEntityByName(String fqn) {
return SdkClients.adminClient().databases().getByName(fqn);
}
@Override
protected Database patchEntity(String id, Database entity) {
return SdkClients.adminClient().databases().update(id, entity);
}
@Override
protected void deleteEntity(String id) {
SdkClients.adminClient().databases().delete(id);
}
@Override
protected void restoreEntity(String id) {
SdkClients.adminClient().databases().restore(id);
}
@Override
protected void hardDeleteEntity(String id) {
// Hard delete requires hardDelete=true query parameter
Map<String, String> params = new HashMap<>();
params.put("hardDelete", "true");
SdkClients.adminClient().databases().delete(id, params);
}
@Override
protected String getEntityType() {
return "database";
}
@Override
protected void validateCreatedEntity(Database entity, CreateDatabase createRequest) {
// Database-specific validations
assertEquals(createRequest.getName(), entity.getName());
assertNotNull(entity.getService(), "Database must have a service");
if (createRequest.getDescription() != null) {
assertEquals(createRequest.getDescription(), entity.getDescription());
}
// FQN should be: serviceName.databaseName
assertTrue(
entity.getFullyQualifiedName().contains(entity.getName()),
"FQN should contain database name");
}
// ===================================================================
// OVERRIDE COMMON TESTS (to fix database-specific behavior)
// ===================================================================
/**
* Override: Databases are scoped by service, so duplicates only conflict within same service.
*/
@Override
@Test
public void post_duplicateEntity_409(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
// Create service once
DatabaseService service = DatabaseServiceTestFactory.createPostgres(ns);
// Create first database
CreateDatabase createRequest =
createRequestWithService(ns.prefix("db"), service.getFullyQualifiedName());
Database entity = createEntity(createRequest);
assertNotNull(entity.getId());
// Attempt to create duplicate database in the SAME service
CreateDatabase duplicateRequest =
createRequestWithService(entity.getName(), service.getFullyQualifiedName());
assertThrows(
Exception.class,
() -> createEntity(duplicateRequest),
"Creating duplicate database in same service should fail");
}
// ===================================================================
// DATABASE-SPECIFIC TESTS (11 tests from DatabaseResourceTest)
// ===================================================================
/**
* Test: post_databaseFQN_as_admin_200_OK
* Original: DatabaseResourceTest line 85
*
* Validates FQN construction: serviceFQN.databaseName
*/
@Test
void post_databaseFQN_as_admin_200_OK(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
// Create Snowflake service
DatabaseService service = DatabaseServiceTestFactory.createSnowflake(ns);
assertNotNull(service.getId());
assertNotNull(service.getFullyQualifiedName());
// Create database
CreateDatabase createRequest = new CreateDatabase();
createRequest.setName(ns.prefix("db"));
createRequest.setService(service.getFullyQualifiedName());
Database db = createEntity(createRequest);
// Verify FQN format
String expectedFQN = service.getFullyQualifiedName() + "." + db.getName();
assertEquals(expectedFQN, db.getFullyQualifiedName());
// Verify retrieval
Database fetched = getEntity(db.getId().toString());
assertEquals(db.getFullyQualifiedName(), fetched.getFullyQualifiedName());
}
/**
* Test: post_databaseWithoutRequiredService_4xx
* Original: DatabaseResourceTest line 96
*
* Validates that service is required
*/
@Test
void post_databaseWithoutRequiredService_4xx(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
// Create request without service
CreateDatabase createRequest = new CreateDatabase();
createRequest.setName(ns.prefix("db"));
// Missing: createRequest.setService(...)
InvalidRequestException exception =
assertThrows(InvalidRequestException.class, () -> createEntity(createRequest));
assertEquals(400, exception.getStatusCode());
}
/**
* Test: post_databaseWithDifferentService_200_ok
* Original: DatabaseResourceTest line 105
*
* Validates databases can be created with different service types
* AND that list filtering by service works correctly
*/
@Test
void post_databaseWithDifferentService_200_ok(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
// Test with multiple service types
DatabaseService postgresService = DatabaseServiceTestFactory.createPostgres(ns);
DatabaseService snowflakeService = DatabaseServiceTestFactory.createSnowflake(ns);
// Create databases for each service
CreateDatabase postgresDbRequest = new CreateDatabase();
postgresDbRequest.setName(ns.prefix("db_postgres"));
postgresDbRequest.setService(postgresService.getFullyQualifiedName());
Database postgresDb = createEntity(postgresDbRequest);
assertNotNull(postgresDb.getId());
assertEquals(postgresService.getName(), postgresDb.getService().getName());
CreateDatabase snowflakeDbRequest = new CreateDatabase();
snowflakeDbRequest.setName(ns.prefix("db_snowflake"));
snowflakeDbRequest.setService(snowflakeService.getFullyQualifiedName());
Database snowflakeDb = createEntity(snowflakeDbRequest);
assertNotNull(snowflakeDb.getId());
assertEquals(snowflakeService.getName(), snowflakeDb.getService().getName());
// List databases by filtering on postgres service and ensure only postgres databases returned
org.openmetadata.sdk.models.ListParams postgresParams =
new org.openmetadata.sdk.models.ListParams();
postgresParams.setService(postgresService.getName());
postgresParams.setLimit(100);
org.openmetadata.sdk.models.ListResponse<Database> postgresList =
SdkClients.adminClient().databases().list(postgresParams);
assertNotNull(postgresList.getData());
assertTrue(postgresList.getData().size() > 0, "Should have at least one postgres database");
// Verify ALL returned databases belong to postgres service
for (Database db : postgresList.getData()) {
assertEquals(
postgresService.getName(),
db.getService().getName(),
"All databases in filtered list should belong to postgres service");
}
// Verify our postgres database is in the list
boolean foundPostgresDb =
postgresList.getData().stream().anyMatch(db -> db.getId().equals(postgresDb.getId()));
assertTrue(foundPostgresDb, "Postgres database should be in postgres-filtered list");
// List databases by filtering on snowflake service
org.openmetadata.sdk.models.ListParams snowflakeParams =
new org.openmetadata.sdk.models.ListParams();
snowflakeParams.setService(snowflakeService.getName());
snowflakeParams.setLimit(100);
org.openmetadata.sdk.models.ListResponse<Database> snowflakeList =
SdkClients.adminClient().databases().list(snowflakeParams);
assertNotNull(snowflakeList.getData());
assertTrue(snowflakeList.getData().size() > 0, "Should have at least one snowflake database");
// Verify ALL returned databases belong to snowflake service
for (Database db : snowflakeList.getData()) {
assertEquals(
snowflakeService.getName(),
db.getService().getName(),
"All databases in filtered list should belong to snowflake service");
}
// Verify our snowflake database is in the list
boolean foundSnowflakeDb =
snowflakeList.getData().stream().anyMatch(db -> db.getId().equals(snowflakeDb.getId()));
assertTrue(foundSnowflakeDb, "Snowflake database should be in snowflake-filtered list");
// Verify postgres database is NOT in snowflake list
boolean postgresInSnowflake =
snowflakeList.getData().stream().anyMatch(db -> db.getId().equals(postgresDb.getId()));
assertFalse(postgresInSnowflake, "Postgres database should NOT be in snowflake-filtered list");
}
/**
* Test: Change Events API
*
* Validates that change events API is accessible and returns data.
* Note: Events are generated asynchronously, so we test API functionality
* rather than specific event presence.
*/
@Test
void test_changeEvents_200_OK(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
// Get current timestamp (for filtering events)
long startTime = System.currentTimeMillis();
// Test listCreated - should return without error
var createdEvents = client.changeEvents().listCreated("database", startTime);
assertNotNull(createdEvents, "Created events response should not be null");
assertNotNull(createdEvents.getData(), "Created events data should not be null");
// Test listUpdated - should return without error
var updatedEvents = client.changeEvents().listUpdated("database", startTime);
assertNotNull(updatedEvents, "Updated events response should not be null");
assertNotNull(updatedEvents.getData(), "Updated events data should not be null");
// Test list with multiple filters
var allEvents = client.changeEvents().list("database", "database", null, null, startTime);
assertNotNull(allEvents, "All events response should not be null");
assertNotNull(allEvents.getData(), "All events data should not be null");
}
/**
* Test: Entity History API
*
* Validates that version history is tracked correctly:
* - getVersionList() returns all versions
* - getVersion() retrieves specific versions
* - Version history reflects updates
*/
@Test
void test_entityVersionHistory_200_OK(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
// Create database
CreateDatabase createRequest = createMinimalRequest(ns);
Database created = createEntity(createRequest);
assertEquals(0.1, created.getVersion(), 0.001, "Initial version should be 0.1");
// Update: Change description
String newDescription = "Updated description";
String oldDescription = createRequest.getDescription();
created.setDescription(newDescription);
ChangeDescription expectedChange =
EntityValidation.getChangeDescription(created, UpdateType.MINOR_UPDATE);
EntityValidation.fieldUpdated(expectedChange, "description", oldDescription, newDescription);
Database updated = patchEntityAndCheck(created, UpdateType.MINOR_UPDATE, expectedChange);
assertEquals(0.2, updated.getVersion(), 0.001, "Version should be 0.2 after update");
// Test getVersionList - should return history with all versions
EntityHistory history = client.databases().getVersionList(updated.getId());
assertNotNull(history, "Version history should not be null");
assertEquals("database", history.getEntityType(), "Entity type should be 'database'");
assertNotNull(history.getVersions(), "Versions list should not be null");
assertTrue(
history.getVersions().size() >= 2,
"Should have at least 2 versions (0.1, 0.2), got: " + history.getVersions().size());
// Test getVersion - retrieve version 0.1
Database version1 = client.databases().getVersion(updated.getId().toString(), 0.1);
assertNotNull(version1, "Version 0.1 should exist");
assertEquals(0.1, version1.getVersion(), 0.001, "Retrieved version should be 0.1");
assertEquals(
createRequest.getDescription(),
version1.getDescription(),
"Version 0.1 should have original description");
// Test getVersion - retrieve version 0.2
Database version2 = client.databases().getVersion(updated.getId().toString(), 0.2);
assertNotNull(version2, "Version 0.2 should exist");
assertEquals(0.2, version2.getVersion(), 0.001, "Retrieved version should be 0.2");
assertEquals(
newDescription, version2.getDescription(), "Version 0.2 should have updated description");
}
/**
* Test: Bulk Service Fetching
*
* Validates that when databases are fetched in bulk with service field,
* each database maintains its correct service reference.
*/
@Test
void test_bulkServiceFetching_200_OK(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
// Create databases with different services
DatabaseService postgresService = DatabaseServiceTestFactory.createPostgres(ns);
DatabaseService snowflakeService = DatabaseServiceTestFactory.createSnowflake(ns);
Database db1 =
createEntity(
createRequestWithService(ns.prefix("db1"), postgresService.getFullyQualifiedName()));
Database db2 =
createEntity(
createRequestWithService(ns.prefix("db2"), snowflakeService.getFullyQualifiedName()));
// Fetch our created databases individually with service field
Database fetchedDb1 = client.databases().get(db1.getId().toString(), "service");
assertNotNull(fetchedDb1.getService(), "Database should have service reference");
assertEquals(
postgresService.getName(),
fetchedDb1.getService().getName(),
"Database 1 should have PostgreSQL service");
Database fetchedDb2 = client.databases().get(db2.getId().toString(), "service");
assertNotNull(fetchedDb2.getService(), "Database should have service reference");
assertEquals(
snowflakeService.getName(),
fetchedDb2.getService().getName(),
"Database 2 should have Snowflake service");
}
/**
* Test: Field Fetchers for Service and Name
* Original: testFieldFetchersForServiceAndName line 563
*
* Validates that field fetchers properly populate service and name fields
* when listing databases with specific field parameters.
*/
@Test
void test_fieldFetchersForServiceAndName(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
// Create custom service for this test
DatabaseService service = DatabaseServiceTestFactory.createPostgres(ns);
String dbName1 = ns.prefix("fieldFetcherDb1");
String dbName2 = ns.prefix("fieldFetcherDb2");
Database db1 = createEntity(createRequestWithService(dbName1, service.getFullyQualifiedName()));
Database db2 = createEntity(createRequestWithService(dbName2, service.getFullyQualifiedName()));
// List databases with service field requested
org.openmetadata.sdk.models.ListParams params = new org.openmetadata.sdk.models.ListParams();
params.setFields("service");
params.setService(service.getName());
params.setLimit(100);
org.openmetadata.sdk.models.ListResponse<Database> dbList = client.databases().list(params);
// Find our databases in the list
Database foundDb1 = null;
Database foundDb2 = null;
for (Database db : dbList.getData()) {
if (db.getId().equals(db1.getId())) {
foundDb1 = db;
}
if (db.getId().equals(db2.getId())) {
foundDb2 = db;
}
}
assertNotNull(foundDb1, "Database 1 should be found in list");
assertNotNull(foundDb2, "Database 2 should be found in list");
// Verify name is always present
assertNotNull(foundDb1.getName(), "Database name should always be present in list response");
assertEquals(dbName1, foundDb1.getName(), "Database 1 name should match");
assertNotNull(foundDb2.getName(), "Database name should always be present in list response");
assertEquals(dbName2, foundDb2.getName(), "Database 2 name should match");
// Verify service is fetched via fieldFetcher when requested
assertNotNull(
foundDb1.getService(),
"Service should be fetched via fieldFetcher when 'service' field is requested");
assertEquals(
service.getName(),
foundDb1.getService().getName(),
"Database 1 service name should be correct via field fetcher");
assertEquals(
service.getId(),
foundDb1.getService().getId(),
"Database 1 service ID should be correct via field fetcher");
assertNotNull(
foundDb2.getService(),
"Service should be fetched via fieldFetcher when 'service' field is requested");
assertEquals(
service.getName(),
foundDb2.getService().getName(),
"Database 2 service name should be correct via field fetcher");
assertEquals(
service.getId(),
foundDb2.getService().getId(),
"Database 2 service ID should be correct via field fetcher");
// Test listing without service field (empty fields)
org.openmetadata.sdk.models.ListParams paramsWithoutService =
new org.openmetadata.sdk.models.ListParams();
paramsWithoutService.setFields("");
paramsWithoutService.setService(service.getName());
paramsWithoutService.setLimit(100);
org.openmetadata.sdk.models.ListResponse<Database> dbListWithoutService =
client.databases().list(paramsWithoutService);
Database foundDb1WithoutService = null;
for (Database db : dbListWithoutService.getData()) {
if (db.getId().equals(db1.getId())) {
foundDb1WithoutService = db;
break;
}
}
assertNotNull(foundDb1WithoutService, "Database should be found even without service field");
assertNotNull(
foundDb1WithoutService.getName(),
"Database name should always be present regardless of fields");
assertEquals(
dbName1,
foundDb1WithoutService.getName(),
"Database name should match even without service field");
// Service is a default field, so it should still be fetched
assertNotNull(
foundDb1WithoutService.getService(),
"Service is always fetched as default field even when not in fields param");
}
// ===================================================================
// PHASE 2: Advanced GET Operations Support
// ===================================================================
@Override
protected Database getEntityWithFields(String id, String fields) {
return SdkClients.adminClient().databases().get(id, fields);
}
@Override
protected Database getEntityByNameWithFields(String fqn, String fields) {
return SdkClients.adminClient().databases().getByName(fqn, fields);
}
@Override
protected Database getEntityIncludeDeleted(String id) {
return SdkClients.adminClient().databases().get(id, "owners,tags,databaseSchemas", "deleted");
}
@Override
protected org.openmetadata.sdk.models.ListResponse<Database> listEntities(
org.openmetadata.sdk.models.ListParams params) {
return SdkClients.adminClient().databases().list(params);
}
// ===================================================================
// NOT MIGRATED - Tests that remain in DatabaseResourceTest
// ===================================================================
//
// CSV IMPORT/EXPORT TESTS (3 tests):
// - testImportInvalidCsv
// - testImportExport
// - testImportExportRecursive
//
// Reason: SDK endpoint mismatch. SDK uses /v1/databases/{id}/export but server expects
// /v1/databases/{fqn}/import with PUT method. Will migrate when SDK endpoints are corrected.
//
// BULK API TESTS (3 tests):
// - testBulk_PreservesUserEditsOnUpdate
// - testBulk_TagMergeBehavior
// - testBulk_AdminCanOverrideDescription
//
// Reason: These tests require detailed verification of bot protection, tag merge behavior,
// and auth-specific behavior that is not easily testable via SDK (SDK bulk API returns
// unstructured String responses). These are better suited as WebTarget-based REST API tests.
//
// Total NOT migrated: 6 tests (3 CSV + 3 Bulk)
// ===================================================================
/*
@Test
void test_csvExport_200_OK(TestNamespace ns) {
// CSV export test - validates database hierarchy export to CSV
// READY - just needs SDK endpoint fix from /v1/databases/{id}/export to /v1/databases/{fqn}/export
}
@Test
void test_csvImportInvalid_400(TestNamespace ns) {
// CSV import validation - tests error handling for invalid data
// READY - needs SDK endpoint fix and PUT method support
}
@Test
void test_csvImportExportRoundTrip_200_OK(TestNamespace ns) {
// Full CSV import/export cycle validation
// READY - needs SDK endpoint fix
}
*/
// ===================================================================
// CSV EXPORT/IMPORT TESTS WITH DOT IN SERVICE NAME
// Tests for issue #24401
// ===================================================================
/**
* Test: CSV export and import with dot in service name.
*
* <p>This test reproduces issue #24401 where CSV import fails with:
* "Invalid character between encapsulated token and delimiter"
*
* <p>The issue occurs because FQNs with dots are quoted (e.g., "asd.asd"),
* and these quotes can conflict with CSV's field encapsulation when not
* properly escaped during export.
*
* @see <a href="https://github.com/open-metadata/OpenMetadata/issues/24401">Issue #24401</a>
*/
@Test
void test_csvExportImportWithDotInServiceName(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
String serviceNameWithDot = ns.prefix("asd.asd");
PostgresConnection conn =
new PostgresConnection().withHostPort("localhost:5432").withUsername("test");
CreateDatabaseService serviceRequest =
new CreateDatabaseService()
.withName(serviceNameWithDot)
.withServiceType(DatabaseServiceType.Postgres)
.withConnection(new DatabaseConnection().withConfig(conn));
DatabaseService service = client.databaseServices().create(serviceRequest);
assertNotNull(service);
String expectedServiceFqn = FullyQualifiedName.quoteName(serviceNameWithDot);
assertEquals(expectedServiceFqn, service.getFullyQualifiedName());
CreateDatabase dbRequest =
new CreateDatabase()
.withName(ns.prefix("testdb"))
.withService(service.getFullyQualifiedName())
.withDescription("Database for CSV export/import test");
Database database = client.databases().create(dbRequest);
assertNotNull(database);
CreateDatabaseSchema schemaRequest =
new CreateDatabaseSchema()
.withName(ns.prefix("testschema"))
.withDatabase(database.getFullyQualifiedName())
.withDescription("Schema for CSV export/import test");
DatabaseSchema schema = client.databaseSchemas().create(schemaRequest);
assertNotNull(schema);
CreateTable tableRequest =
new CreateTable()
.withName(ns.prefix("testtable"))
.withDatabaseSchema(schema.getFullyQualifiedName())
.withDescription("Table for CSV export/import test")
.withColumns(
List.of(
new Column()
.withName("id")
.withDataType(ColumnDataType.INT)
.withDescription("Primary key"),
new Column()
.withName("name")
.withDataType(ColumnDataType.VARCHAR)
.withDataLength(100)
.withDescription("Name field")));
Table table = client.tables().create(tableRequest);
assertNotNull(table);
try {
String exportedCsv = client.databases().exportCsv(database.getFullyQualifiedName());
assertNotNull(exportedCsv, "CSV export should succeed");
assertFalse(exportedCsv.isEmpty(), "Exported CSV should not be empty");
String importResult =
client.databases().importCsv(database.getFullyQualifiedName(), exportedCsv, true);
assertNotNull(importResult, "CSV import dry run should succeed");
assertFalse(
importResult.toLowerCase().contains("failure"),
"CSV import should not contain failures. Result: " + importResult);
String actualImportResult =
client.databases().importCsv(database.getFullyQualifiedName(), exportedCsv, false);
assertNotNull(actualImportResult, "CSV import should succeed");
assertFalse(
actualImportResult.toLowerCase().contains("failure"),
"CSV actual import should not contain failures. Result: " + actualImportResult);
} catch (Exception e) {
fail(
"CSV export/import failed with dot in service name. "
+ "This reproduces issue #24401. Error: "
+ e.getMessage());
}
}
/**
* Test: Full database hierarchy with dot in service name and verify FQN propagation.
*
* <p>Creates: "service.name" -> database -> schema -> table
* <p>Verifies that FQNs are correctly built with proper quoting throughout the hierarchy.
*
* @see <a href="https://github.com/open-metadata/OpenMetadata/issues/24401">Issue #24401</a>
*/
@Test
void test_hierarchyWithDotInServiceName_fqnPropagation(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
String serviceNameWithDot = ns.prefix("snowflake.prod");
PostgresConnection conn =
new PostgresConnection().withHostPort("localhost:5432").withUsername("test");
CreateDatabaseService serviceRequest =
new CreateDatabaseService()
.withName(serviceNameWithDot)
.withServiceType(DatabaseServiceType.Postgres)
.withConnection(new DatabaseConnection().withConfig(conn));
DatabaseService service = client.databaseServices().create(serviceRequest);
assertNotNull(service);
CreateDatabase dbRequest =
new CreateDatabase()
.withName(ns.prefix("customers"))
.withService(service.getFullyQualifiedName());
Database database = client.databases().create(dbRequest);
assertNotNull(database);
String expectedDbFqn =
FullyQualifiedName.add(service.getFullyQualifiedName(), database.getName());
assertEquals(expectedDbFqn, database.getFullyQualifiedName());
CreateDatabaseSchema schemaRequest =
new CreateDatabaseSchema()
.withName(ns.prefix("orders"))
.withDatabase(database.getFullyQualifiedName());
DatabaseSchema schema = client.databaseSchemas().create(schemaRequest);
assertNotNull(schema);
String expectedSchemaFqn =
FullyQualifiedName.add(database.getFullyQualifiedName(), schema.getName());
assertEquals(expectedSchemaFqn, schema.getFullyQualifiedName());
CreateTable tableRequest =
new CreateTable()
.withName(ns.prefix("history"))
.withDatabaseSchema(schema.getFullyQualifiedName())
.withColumns(
List.of(
new Column()
.withName("id")
.withDataType(ColumnDataType.INT)
.withDescription("Primary key")));
Table table = client.tables().create(tableRequest);
assertNotNull(table);
String expectedTableFqn =
FullyQualifiedName.add(schema.getFullyQualifiedName(), table.getName());
assertEquals(expectedTableFqn, table.getFullyQualifiedName());
String[] fqnParts = FullyQualifiedName.split(table.getFullyQualifiedName());
assertEquals(4, fqnParts.length);
assertEquals(FullyQualifiedName.quoteName(serviceNameWithDot), fqnParts[0]);
Database fetchedDb = client.databases().getByName(database.getFullyQualifiedName());
assertNotNull(fetchedDb);
assertEquals(database.getId(), fetchedDb.getId());
DatabaseSchema fetchedSchema =
client.databaseSchemas().getByName(schema.getFullyQualifiedName());
assertNotNull(fetchedSchema);
assertEquals(schema.getId(), fetchedSchema.getId());
Table fetchedTable = client.tables().getByName(table.getFullyQualifiedName());
assertNotNull(fetchedTable);
assertEquals(table.getId(), fetchedTable.getId());
}
/**
* Test: Comprehensive CSV export and import with dot in service name including stored procedures.
*
* <p>This test creates a full database hierarchy with:
* - Service with dot in name (e.g., "test.sample")
* - Database
* - Schema
* - Multiple tables with columns
* - Stored procedures
*
* <p>Then exports to CSV and imports back, verifying the CSV parsing handles
* quoted FQNs correctly without throwing:
* "Invalid character between encapsulated token and delimiter"
*
* @see <a href="https://github.com/open-metadata/OpenMetadata/issues/24401">Issue #24401</a>
*/
@Test
void test_csvExportImportWithDotInServiceName_fullHierarchy(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
String serviceNameWithDot = ns.prefix("test.sample");
PostgresConnection conn =
new PostgresConnection().withHostPort("localhost:5432").withUsername("test");
CreateDatabaseService serviceRequest =
new CreateDatabaseService()
.withName(serviceNameWithDot)
.withServiceType(DatabaseServiceType.Postgres)
.withConnection(new DatabaseConnection().withConfig(conn));
DatabaseService service = client.databaseServices().create(serviceRequest);
assertNotNull(service);
String expectedServiceFqn = FullyQualifiedName.quoteName(serviceNameWithDot);
assertEquals(
expectedServiceFqn,
service.getFullyQualifiedName(),
"Service FQN should be quoted when name contains dot");
CreateDatabase dbRequest =
new CreateDatabase()
.withName(ns.prefix("mydb"))
.withService(service.getFullyQualifiedName())
.withDescription("Database for comprehensive CSV export/import test");
Database database = client.databases().create(dbRequest);
assertNotNull(database);
CreateDatabaseSchema schemaRequest =
new CreateDatabaseSchema()
.withName(ns.prefix("myschema"))
.withDatabase(database.getFullyQualifiedName())
.withDescription("Schema for comprehensive CSV export/import test");
DatabaseSchema schema = client.databaseSchemas().create(schemaRequest);
assertNotNull(schema);
CreateTable usersTableRequest =
new CreateTable()
.withName(ns.prefix("users"))
.withDatabaseSchema(schema.getFullyQualifiedName())
.withDescription("Users table for CSV test")
.withColumns(
List.of(
new Column()
.withName("id")
.withDataType(ColumnDataType.BIGINT)
.withDescription("Primary key"),
new Column()
.withName("username")
.withDataType(ColumnDataType.VARCHAR)
.withDataLength(100)
.withDescription("Username"),
new Column()
.withName("email")
.withDataType(ColumnDataType.VARCHAR)
.withDataLength(255)
.withDescription("Email address"),
new Column()
.withName("created_at")
.withDataType(ColumnDataType.TIMESTAMP)
.withDescription("Creation timestamp")));
Table usersTable = client.tables().create(usersTableRequest);
assertNotNull(usersTable);
CreateTable ordersTableRequest =
new CreateTable()
.withName(ns.prefix("orders"))
.withDatabaseSchema(schema.getFullyQualifiedName())
.withDescription("Orders table for CSV test")
.withColumns(
List.of(
new Column()
.withName("order_id")
.withDataType(ColumnDataType.BIGINT)
.withDescription("Order ID"),
new Column()
.withName("user_id")
.withDataType(ColumnDataType.BIGINT)
.withDescription("Foreign key to users"),
new Column()
.withName("total_amount")
.withDataType(ColumnDataType.DECIMAL)
.withDescription("Total order amount"),
new Column()
.withName("status")
.withDataType(ColumnDataType.VARCHAR)
.withDataLength(50)
.withDescription("Order status")));
Table ordersTable = client.tables().create(ordersTableRequest);
assertNotNull(ordersTable);
org.openmetadata.schema.api.data.StoredProcedureCode procCode1 =
new org.openmetadata.schema.api.data.StoredProcedureCode()
.withCode(
"CREATE OR REPLACE PROCEDURE get_user_orders(user_id BIGINT) "
+ "LANGUAGE plpgsql AS $$ "
+ "BEGIN "
+ " SELECT * FROM orders WHERE user_id = user_id; "
+ "END; $$;")
.withLanguage(org.openmetadata.schema.type.StoredProcedureLanguage.SQL);
org.openmetadata.schema.api.data.CreateStoredProcedure proc1Request =
new org.openmetadata.schema.api.data.CreateStoredProcedure()
.withName(ns.prefix("get_user_orders"))
.withDatabaseSchema(schema.getFullyQualifiedName())
.withDescription("Get orders for a user")
.withStoredProcedureCode(procCode1);
org.openmetadata.schema.entity.data.StoredProcedure proc1 =
client.storedProcedures().create(proc1Request);
assertNotNull(proc1);
org.openmetadata.schema.api.data.StoredProcedureCode procCode2 =
new org.openmetadata.schema.api.data.StoredProcedureCode()
.withCode(
"CREATE OR REPLACE PROCEDURE update_order_status(order_id BIGINT, new_status VARCHAR) "
+ "LANGUAGE plpgsql AS $$ "
+ "BEGIN "
+ " UPDATE orders SET status = new_status WHERE order_id = order_id; "
+ "END; $$;")
.withLanguage(org.openmetadata.schema.type.StoredProcedureLanguage.SQL);
org.openmetadata.schema.api.data.CreateStoredProcedure proc2Request =
new org.openmetadata.schema.api.data.CreateStoredProcedure()
.withName(ns.prefix("update_order_status"))
.withDatabaseSchema(schema.getFullyQualifiedName())
.withDescription("Update order status")
.withStoredProcedureCode(procCode2);
org.openmetadata.schema.entity.data.StoredProcedure proc2 =