-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathTestCaseResourceIT.java
More file actions
4567 lines (3843 loc) · 172 KB
/
TestCaseResourceIT.java
File metadata and controls
4567 lines (3843 loc) · 172 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.assertNotEquals;
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 java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.openmetadata.it.bootstrap.SharedEntities;
import org.openmetadata.it.util.SdkClients;
import org.openmetadata.it.util.TestNamespace;
import org.openmetadata.schema.api.classification.CreateClassification;
import org.openmetadata.schema.api.classification.CreateTag;
import org.openmetadata.schema.api.data.CreateTable;
import org.openmetadata.schema.api.tests.CreateTestCase;
import org.openmetadata.schema.api.tests.CreateTestSuite;
import org.openmetadata.schema.entity.classification.Classification;
import org.openmetadata.schema.entity.classification.Tag;
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.tests.TestCase;
import org.openmetadata.schema.tests.TestCaseParameterValue;
import org.openmetadata.schema.tests.TestSuite;
import org.openmetadata.schema.type.ApiStatus;
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.TagLabel;
import org.openmetadata.schema.type.csv.CsvImportResult;
import org.openmetadata.schema.utils.JsonUtils;
import org.openmetadata.sdk.client.OpenMetadataClient;
import org.openmetadata.sdk.fluent.builders.TestCaseBuilder;
import org.openmetadata.sdk.models.ListParams;
import org.openmetadata.sdk.models.ListResponse;
import org.openmetadata.sdk.network.HttpMethod;
import org.openmetadata.sdk.network.RequestOptions;
import org.openmetadata.service.Entity;
import org.openmetadata.service.resources.dqtests.TestCaseResource;
/**
* Integration tests for TestCase entity operations.
*
* <p>Extends BaseEntityIT to inherit common entity tests. Adds test case-specific tests.
*
* <p>Migrated from: org.openmetadata.service.resources.dqtests.TestCaseResourceTest
*/
@Execution(ExecutionMode.CONCURRENT)
public class TestCaseResourceIT extends BaseEntityIT<TestCase, CreateTestCase> {
// Disable tests that don't apply to TestCase
{
supportsFollowers = false; // TestCase doesn't support followers
supportsTags = false; // TestCase tags are handled differently
supportsDataProducts = false; // TestCase doesn't support dataProducts
supportsNameLengthValidation = false; // TestCase FQN includes table FQN, no strict name length
supportsImportExport = true;
supportsBatchImport = true;
supportsRecursiveImport = false; // TestCase doesn't support recursive import
supportsListHistoryByTimestamp = true;
}
@Override
protected String getResourcePath() {
return TestCaseResource.COLLECTION_PATH;
}
@Override
protected CsvImportResult performImportCsv(TestNamespace ns, String csvData, boolean dryRun) {
try {
String containerName = getImportExportContainerName(ns);
String result =
SdkClients.adminClient()
.testCases()
.importCsv(containerName, csvData, dryRun, "testSuite");
return JsonUtils.readValue(result, CsvImportResult.class);
} catch (Exception e) {
throw new RuntimeException("CSV import failed: " + e.getMessage(), e);
}
}
private TestSuite lastCreatedTestSuite;
// ===================================================================
// ABSTRACT METHOD IMPLEMENTATIONS (Required by BaseEntityIT)
// ===================================================================
@Override
protected CreateTestCase createMinimalRequest(TestNamespace ns) {
Table table = createTable(ns);
return TestCaseBuilder.create(SdkClients.adminClient())
.name(ns.prefix("testcase"))
.description("Test case created by integration test")
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.build();
}
@Override
protected CreateTestCase createRequest(String name, TestNamespace ns) {
Table table = createTable(ns);
// For invalid name tests, bypass builder validation to test server-side validation
if (name == null || name.isEmpty() || name.contains("\n")) {
CreateTestCase request = new CreateTestCase();
request.setName(name);
request.setDescription("Test case");
request.setEntityLink("<#E::table::" + table.getFullyQualifiedName() + ">");
request.setTestDefinition("tableRowCountToEqual");
request.setParameterValues(
List.of(new TestCaseParameterValue().withName("value").withValue("100")));
return request;
}
return TestCaseBuilder.create(SdkClients.adminClient())
.name(name)
.description("Test case")
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.build();
}
private Table createTable(TestNamespace ns) {
return createTable(ns, null);
}
private Table createTable(TestNamespace ns, List<org.openmetadata.schema.type.TagLabel> tags) {
// Use short names to avoid FQN length limit (256 chars)
String shortId = ns.uniqueShortId();
// Create service with short name using fluent API
org.openmetadata.schema.services.connections.database.PostgresConnection conn =
org.openmetadata.sdk.fluent.DatabaseServices.postgresConnection()
.hostPort("localhost:5432")
.username("test")
.build();
DatabaseService service =
org.openmetadata.sdk.fluent.DatabaseServices.builder()
.name("pg_" + shortId)
.connection(conn)
.description("Test Postgres service")
.create();
// Create database with short name
org.openmetadata.schema.api.data.CreateDatabase dbReq =
new org.openmetadata.schema.api.data.CreateDatabase();
dbReq.setName("db_" + shortId);
dbReq.setService(service.getFullyQualifiedName());
org.openmetadata.schema.entity.data.Database database =
SdkClients.adminClient().databases().create(dbReq);
// Create schema with short name
org.openmetadata.schema.api.data.CreateDatabaseSchema schemaReq =
new org.openmetadata.schema.api.data.CreateDatabaseSchema();
schemaReq.setName("s_" + shortId);
schemaReq.setDatabase(database.getFullyQualifiedName());
DatabaseSchema schema = SdkClients.adminClient().databaseSchemas().create(schemaReq);
CreateTable tableRequest = new CreateTable();
tableRequest.setName("t_" + shortId);
tableRequest.setDatabaseSchema(schema.getFullyQualifiedName());
tableRequest.setColumns(
List.of(
new Column().withName("id").withDataType(ColumnDataType.INT),
new Column()
.withName("name")
.withDataType(ColumnDataType.VARCHAR)
.withDataLength(255)));
tableRequest.setTags(tags);
return SdkClients.adminClient().tables().create(tableRequest);
}
private TestSuite createBasicTestSuite(CreateTestSuite request) {
OpenMetadataClient client = SdkClients.adminClient();
org.openmetadata.sdk.network.RequestOptions options =
org.openmetadata.sdk.network.RequestOptions.builder()
.queryParam("name", request.getName())
.build();
return client
.getHttpClient()
.execute(
org.openmetadata.sdk.network.HttpMethod.POST,
"/v1/dataQuality/testSuites/basic",
request,
TestSuite.class,
options);
}
@Override
protected TestCase createEntity(CreateTestCase createRequest) {
return SdkClients.adminClient().testCases().create(createRequest);
}
@Override
protected TestCase getEntity(String id) {
return SdkClients.adminClient().testCases().get(id);
}
@Override
protected TestCase getEntityByName(String fqn) {
return SdkClients.adminClient().testCases().getByName(fqn);
}
@Override
protected TestCase patchEntity(String id, TestCase entity) {
return SdkClients.adminClient().testCases().update(id, entity);
}
@Override
protected void deleteEntity(String id) {
SdkClients.adminClient().testCases().delete(id);
}
@Override
protected void restoreEntity(String id) {
SdkClients.adminClient().testCases().restore(id);
}
@Override
protected void hardDeleteEntity(String id) {
java.util.Map<String, String> params = new java.util.HashMap<>();
params.put("hardDelete", "true");
SdkClients.adminClient().testCases().delete(id, params);
}
@Override
protected String getEntityType() {
return "testCase";
}
@Override
protected void validateCreatedEntity(TestCase entity, CreateTestCase createRequest) {
assertEquals(createRequest.getName(), entity.getName());
assertNotNull(entity.getEntityLink(), "TestCase must have an entity link");
assertNotNull(entity.getTestDefinition(), "TestCase must have a test definition");
if (createRequest.getDescription() != null) {
assertEquals(createRequest.getDescription(), entity.getDescription());
}
assertTrue(
entity.getFullyQualifiedName().contains(entity.getName()),
"FQN should contain test case name");
}
@Override
protected ListResponse<TestCase> listEntities(ListParams params) {
return SdkClients.adminClient().testCases().list(params);
}
@Override
protected TestCase getEntityWithFields(String id, String fields) {
return SdkClients.adminClient().testCases().get(id, fields);
}
@Override
protected TestCase getEntityByNameWithFields(String fqn, String fields) {
return SdkClients.adminClient().testCases().getByName(fqn, fields);
}
protected TestCase updateEntity(String id, CreateTestCase updateRequest) {
return SdkClients.adminClient().testCases().upsert(updateRequest);
}
protected EntityHistory getEntityHistory(String id) throws Exception {
OpenMetadataClient client = SdkClients.adminClient();
String response =
client
.getHttpClient()
.executeForString(
HttpMethod.GET, "/v1/dataQuality/testCases/" + id + "/versions", null);
return new com.fasterxml.jackson.databind.ObjectMapper()
.readValue(response, EntityHistory.class);
}
@Override
protected TestCase getEntityIncludeDeleted(String id) {
return SdkClients.adminClient().testCases().get(id, null, "deleted");
}
@Override
protected EntityHistory getVersionHistory(java.util.UUID id) {
return SdkClients.adminClient().testCases().getVersionList(id);
}
@Override
protected TestCase getVersion(java.util.UUID id, Double version) {
return SdkClients.adminClient().testCases().getVersion(id, version);
}
@Override
protected org.openmetadata.sdk.services.EntityServiceBase<TestCase> getEntityService() {
return SdkClients.adminClient().testCases();
}
@Override
protected String getImportExportContainerName(TestNamespace ns) {
if (lastCreatedTestSuite == null) {
CreateTestSuite request = new CreateTestSuite();
request.setName(ns.prefix("export_suite"));
request.setDescription("Test suite for export testing");
lastCreatedTestSuite = SdkClients.adminClient().testSuites().create(request);
}
return lastCreatedTestSuite.getFullyQualifiedName();
}
// ===================================================================
// TEST CASE OVERRIDEN TESTS
// ===================================================================
@Override
@Test
void test_importCsvDryRun(TestNamespace ns) {
org.junit.jupiter.api.Assumptions.assumeTrue(
supportsImportExport, "Entity does not support import/export");
String containerName = getImportExportContainerName(ns);
org.junit.jupiter.api.Assumptions.assumeTrue(
containerName != null, "Container name not provided");
// Create an entity first
CreateTestCase createRequest = createMinimalRequest(ns);
TestCase entity = createEntity(createRequest);
assertNotNull(entity, "Entity should be created");
try {
// Export to get valid CSV format
String exportedCsv = SdkClients.adminClient().testCases().exportCsv(containerName);
assertNotNull(exportedCsv, "Export should return CSV data");
// Import with dry run - TestCase requires targetEntityType
String result =
SdkClients.adminClient()
.testCases()
.importCsv(containerName, exportedCsv, true, "testSuite");
assertNotNull(result, "Import dry run should return a result");
} catch (org.openmetadata.sdk.exceptions.OpenMetadataException e) {
org.junit.jupiter.api.Assertions.fail("Import/export failed: " + e.getMessage());
}
}
@Override
@Test
void test_importExportRoundTrip(TestNamespace ns) {
org.junit.jupiter.api.Assumptions.assumeTrue(
supportsImportExport, "Entity does not support import/export");
String containerName = getImportExportContainerName(ns);
org.junit.jupiter.api.Assumptions.assumeTrue(
containerName != null, "Container name not provided");
// Create an entity first
CreateTestCase createRequest = createMinimalRequest(ns);
TestCase entity = createEntity(createRequest);
assertNotNull(entity, "Entity should be created");
try {
// Export current state
String exportedCsv = SdkClients.adminClient().testCases().exportCsv(containerName);
assertNotNull(exportedCsv, "Export should return CSV data");
// Import the exported data - TestCase requires targetEntityType
String result =
SdkClients.adminClient()
.testCases()
.importCsv(containerName, exportedCsv, false, "testSuite");
assertNotNull(result, "Import should return a result");
// Export again and verify consistency
String reExportedCsv = SdkClients.adminClient().testCases().exportCsv(containerName);
assertNotNull(reExportedCsv, "Re-export should return CSV data");
// Headers should match after round-trip
String[] originalLines = exportedCsv.split("\n");
String[] reExportedLines = reExportedCsv.split("\n");
assertEquals(
originalLines[0], reExportedLines[0], "CSV headers should match after round-trip");
} catch (org.openmetadata.sdk.exceptions.OpenMetadataException e) {
org.junit.jupiter.api.Assertions.fail("Import/export round-trip failed: " + e.getMessage());
}
}
// ===================================================================
// TEST CASE SPECIFIC TESTS
// ===================================================================
@Test
void testListFluentAPI(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
Table table = createTable(ns);
TestCaseBuilder.create(client)
.name(ns.prefix("test1"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
TestCaseBuilder.create(client)
.name(ns.prefix("test2"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "200")
.create();
ListResponse<TestCase> response =
client
.testCases()
.list(new ListParams().setFields("testDefinition,testSuite").setLimit(10));
assertNotNull(response);
assertTrue(response.getData().size() >= 2);
}
@Test
void testAutoPaginationFluentAPI(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
Table table = createTable(ns);
for (int i = 0; i < 5; i++) {
TestCaseBuilder.create(client)
.name(ns.prefix("autopaging_test_" + i))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", String.valueOf((i + 1) * 100))
.create();
}
ListResponse<TestCase> response =
client.testCases().list(new ListParams().setLimit(10).setFields("testDefinition"));
assertNotNull(response);
assertNotNull(response.getData());
assertTrue(response.getData().size() >= 5);
}
@Test
void post_testCaseWithoutEntityLink_4xx(TestNamespace ns) {
CreateTestCase request = new CreateTestCase();
request.setName(ns.prefix("no_entity_link"));
request.setTestDefinition("tableRowCountToEqual");
request.setParameterValues(
List.of(new TestCaseParameterValue().withName("value").withValue("100")));
assertThrows(Exception.class, () -> createEntity(request), "Should fail without entity link");
}
@Test
void post_testCaseWithParameters_200_OK(TestNamespace ns) {
Table table = createTable(ns);
CreateTestCase request =
TestCaseBuilder.create(SdkClients.adminClient())
.name(ns.prefix("with_params"))
.forTable(table)
.testDefinition("tableRowCountToBeBetween")
.parameter("minValue", "50")
.parameter("maxValue", "150")
.build();
TestCase created = createEntity(request);
assertNotNull(created.getParameterValues());
assertEquals(2, created.getParameterValues().size());
}
@Test
void put_testCaseDescription_200_OK(TestNamespace ns) {
Table table = createTable(ns);
TestCase testCase =
TestCaseBuilder.create(SdkClients.adminClient())
.name(ns.prefix("update_desc"))
.description("Original description")
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
testCase.setDescription("Updated description");
TestCase updated = patchEntity(testCase.getId().toString(), testCase);
assertEquals("Updated description", updated.getDescription());
}
@Test
void test_testCaseLinksToTable(TestNamespace ns) {
Table table = createTable(ns);
TestCase testCase =
TestCaseBuilder.create(SdkClients.adminClient())
.name(ns.prefix("links_to_table"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
assertTrue(testCase.getEntityLink().contains(table.getFullyQualifiedName()));
assertNotNull(testCase.getTestSuite());
}
@Test
void post_testWithInvalidEntityLink_4xx(TestNamespace ns) {
CreateTestCase request = new CreateTestCase();
request.setName(ns.prefix("invalid_link"));
request.setEntityLink("<invalid::link>");
request.setTestDefinition("tableRowCountToEqual");
request.setParameterValues(
List.of(new TestCaseParameterValue().withName("value").withValue("100")));
assertThrows(Exception.class, () -> createEntity(request), "Should fail with invalid link");
}
@Test
void post_testWithNonExistentTable_4xx(TestNamespace ns) {
CreateTestCase request = new CreateTestCase();
request.setName(ns.prefix("nonexistent_table"));
request.setEntityLink("<#E::table::nonexistent.table>");
request.setTestDefinition("tableRowCountToEqual");
request.setParameterValues(
List.of(new TestCaseParameterValue().withName("value").withValue("100")));
assertThrows(
Exception.class, () -> createEntity(request), "Should fail with nonexistent table");
}
@Test
void post_testWithInvalidTestDefinition_4xx(TestNamespace ns) {
Table table = createTable(ns);
CreateTestCase request = new CreateTestCase();
request.setName(ns.prefix("invalid_def"));
request.setEntityLink("<#E::table::" + table.getFullyQualifiedName() + ">");
request.setTestDefinition("nonExistentTestDefinition");
request.setParameterValues(
List.of(new TestCaseParameterValue().withName("value").withValue("100")));
assertThrows(
Exception.class, () -> createEntity(request), "Should fail with invalid test definition");
}
@Test
void post_columnLevelTest_200_OK(TestNamespace ns) {
Table table = createTable(ns);
String columnLink =
String.format("<#E::table::%s::columns::%s>", table.getFullyQualifiedName(), "id");
CreateTestCase request = new CreateTestCase();
request.setName(ns.prefix("column_test"));
request.setEntityLink(columnLink);
request.setTestDefinition("columnValuesToBeBetween");
request.setParameterValues(
List.of(
new TestCaseParameterValue().withName("minValue").withValue("1"),
new TestCaseParameterValue().withName("maxValue").withValue("1000")));
TestCase created = createEntity(request);
assertTrue(created.getEntityLink().contains("::columns::id"));
}
@Test
void post_testWithInvalidColumnName_4xx(TestNamespace ns) {
Table table = createTable(ns);
String invalidColumnLink =
String.format(
"<#E::table::%s::columns::%s>", table.getFullyQualifiedName(), "nonExistentColumn");
CreateTestCase request = new CreateTestCase();
request.setName(ns.prefix("invalid_column"));
request.setEntityLink(invalidColumnLink);
request.setTestDefinition("columnValuesToBeBetween");
request.setParameterValues(
List.of(new TestCaseParameterValue().withName("minValue").withValue("1")));
assertThrows(Exception.class, () -> createEntity(request), "Should fail with invalid column");
}
@Test
void test_createMultipleTestsOnSameTable_200_OK(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
Table table = createTable(ns);
TestCase test1 =
TestCaseBuilder.create(client)
.name(ns.prefix("multi_test1"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
TestCase test2 =
TestCaseBuilder.create(client)
.name(ns.prefix("multi_test2"))
.forTable(table)
.testDefinition("tableColumnCountToEqual")
.parameter("columnCount", "2")
.create();
assertEquals(test1.getTestSuite().getId(), test2.getTestSuite().getId());
assertNotEquals(test1.getId(), test2.getId());
}
@Test
void test_updateTestCaseParameterValues_200_OK(TestNamespace ns) {
Table table = createTable(ns);
TestCase testCase =
TestCaseBuilder.create(SdkClients.adminClient())
.name(ns.prefix("update_params"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
testCase.setParameterValues(
List.of(new TestCaseParameterValue().withName("value").withValue("200")));
TestCase updated = patchEntity(testCase.getId().toString(), testCase);
assertEquals("200", updated.getParameterValues().get(0).getValue());
}
@Test
void test_testCaseVersionHistory_200_OK(TestNamespace ns) throws Exception {
Table table = createTable(ns);
TestCase testCase =
TestCaseBuilder.create(SdkClients.adminClient())
.name(ns.prefix("version_test"))
.description("Original")
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
testCase.setDescription("Updated");
patchEntity(testCase.getId().toString(), testCase);
testCase.setDescription("Updated again");
patchEntity(testCase.getId().toString(), testCase);
EntityHistory history = getEntityHistory(testCase.getId().toString());
assertNotNull(history);
assertTrue(history.getVersions().size() >= 2);
}
@Test
void test_testCaseSoftDeleteAndRestore_200_OK(TestNamespace ns) {
Table table = createTable(ns);
TestCase testCase =
TestCaseBuilder.create(SdkClients.adminClient())
.name(ns.prefix("soft_delete_test"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
String testCaseId = testCase.getId().toString();
deleteEntity(testCaseId);
assertThrows(Exception.class, () -> getEntity(testCaseId), "Should not find deleted entity");
restoreEntity(testCaseId);
TestCase restored = getEntity(testCaseId);
assertEquals(testCase.getName(), restored.getName());
}
@Test
void test_testCaseHardDelete_200_OK(TestNamespace ns) {
Table table = createTable(ns);
TestCase testCase =
TestCaseBuilder.create(SdkClients.adminClient())
.name(ns.prefix("hard_delete_test"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
String testCaseId = testCase.getId().toString();
hardDeleteEntity(testCaseId);
assertThrows(Exception.class, () -> getEntity(testCaseId), "Should not find deleted entity");
assertThrows(
Exception.class, () -> restoreEntity(testCaseId), "Should not restore hard-deleted entity");
}
@Test
void test_listTestCases_200_OK(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
Table table = createTable(ns);
TestCaseBuilder.create(client)
.name(ns.prefix("list_test1"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
TestCaseBuilder.create(client)
.name(ns.prefix("list_test2"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "200")
.create();
// Filter by entityLink to only get test cases for this test's table
// This avoids test pollution from parallel tests that may have deleted their tables
String entityLink = String.format("<#E::table::%s>", table.getFullyQualifiedName());
ListResponse<TestCase> testCases =
client
.testCases()
.list(new ListParams().setLimit(100).addQueryParam("entityLink", entityLink));
assertNotNull(testCases);
assertTrue(testCases.getData().size() >= 2);
}
@Test
void test_testCaseWithDifferentTestDefinitions(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
Table table = createTable(ns);
TestCase rowCountTest =
TestCaseBuilder.create(client)
.name(ns.prefix("row_count"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
TestCase columnCountTest =
TestCaseBuilder.create(client)
.name(ns.prefix("column_count"))
.forTable(table)
.testDefinition("tableColumnCountToEqual")
.parameter("columnCount", "2")
.create();
assertNotEquals(
rowCountTest.getTestDefinition().getId(), columnCountTest.getTestDefinition().getId());
}
@Test
void post_testWithMissingRequiredParameter_4xx(TestNamespace ns) {
Table table = createTable(ns);
// columnValuesMissingCount has a required parameter 'missingCountValue'
// Table has columns 'id' and 'name' - use 'id' for the column link
String columnLink =
String.format("<#E::table::%s::columns::%s>", table.getFullyQualifiedName(), "id");
CreateTestCase request = new CreateTestCase();
request.setName(ns.prefix("missing_param"));
request.setEntityLink(columnLink);
request.setTestDefinition("columnValuesMissingCount");
request.setParameterValues(List.of()); // Missing required 'missingCountValue' parameter
assertThrows(
Exception.class,
() -> createEntity(request),
"Should fail with missing required parameter");
}
@Test
void post_testWithInvalidParameterValue_4xx(TestNamespace ns) {
Table table = createTable(ns);
CreateTestCase request = new CreateTestCase();
request.setName(ns.prefix("invalid_param_value"));
request.setEntityLink("<#E::table::" + table.getFullyQualifiedName() + ">");
request.setTestDefinition("tableRowCountToEqual");
request.setParameterValues(
List.of(new TestCaseParameterValue().withName("invalidParam").withValue("100")));
assertThrows(
Exception.class, () -> createEntity(request), "Should fail with invalid parameter name");
}
@Test
void test_testCaseWithOwner(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
Table table = createTable(ns);
TestCase testCase =
TestCaseBuilder.create(client)
.name(ns.prefix("with_owner"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
assertNotNull(testCase);
}
@Test
void test_testCaseDisplayName(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
Table table = createTable(ns);
TestCase testCase =
TestCaseBuilder.create(client)
.name(ns.prefix("displayname_test"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
testCase.setDisplayName("Custom Display Name");
TestCase updated = patchEntity(testCase.getId().toString(), testCase);
assertEquals("Custom Display Name", updated.getDisplayName());
}
@Test
void test_getTestCaseByName(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
Table table = createTable(ns);
TestCase testCase =
TestCaseBuilder.create(client)
.name(ns.prefix("get_by_name"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
TestCase fetched = getEntityByName(testCase.getFullyQualifiedName());
assertEquals(testCase.getId(), fetched.getId());
assertEquals(testCase.getName(), fetched.getName());
}
@Test
void test_testCaseComputePassedFailedRowCount(TestNamespace ns) {
Table table = createTable(ns);
CreateTestCase request = new CreateTestCase();
request.setName(ns.prefix("compute_rows"));
request.setEntityLink("<#E::table::" + table.getFullyQualifiedName() + ">");
request.setTestDefinition("tableRowCountToEqual");
request.setParameterValues(
List.of(new TestCaseParameterValue().withName("value").withValue("100")));
request.setComputePassedFailedRowCount(true);
TestCase created = createEntity(request);
assertTrue(created.getComputePassedFailedRowCount());
}
@Test
void test_patchTestCaseComputePassedFailedRowCount(TestNamespace ns) {
Table table = createTable(ns);
TestCase testCase =
TestCaseBuilder.create(SdkClients.adminClient())
.name(ns.prefix("patch_compute"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
testCase.setComputePassedFailedRowCount(true);
TestCase updated = patchEntity(testCase.getId().toString(), testCase);
assertTrue(updated.getComputePassedFailedRowCount());
}
@Test
void test_createTestCaseWithUseDynamicAssertion(TestNamespace ns) {
Table table = createTable(ns);
CreateTestCase request = new CreateTestCase();
request.setName(ns.prefix("dynamic_assertion"));
request.setEntityLink("<#E::table::" + table.getFullyQualifiedName() + ">");
request.setTestDefinition("tableRowCountToEqual");
request.setParameterValues(
List.of(new TestCaseParameterValue().withName("value").withValue("100")));
request.setUseDynamicAssertion(true);
TestCase created = createEntity(request);
assertTrue(created.getUseDynamicAssertion());
}
@Test
void test_updateTestCaseDisplayName(TestNamespace ns) {
Table table = createTable(ns);
TestCase testCase =
TestCaseBuilder.create(SdkClients.adminClient())
.name(ns.prefix("update_display"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
testCase.setDisplayName("New Display Name");
TestCase updated = patchEntity(testCase.getId().toString(), testCase);
assertEquals("New Display Name", updated.getDisplayName());
}
@Test
void test_testCaseEntityFQN(TestNamespace ns) {
Table table = createTable(ns);
TestCase testCase =
TestCaseBuilder.create(SdkClients.adminClient())
.name(ns.prefix("fqn_test"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
assertNotNull(testCase.getFullyQualifiedName());
assertTrue(testCase.getFullyQualifiedName().contains(testCase.getName()));
assertTrue(testCase.getFullyQualifiedName().contains(table.getFullyQualifiedName()));
}
@Test
void test_testCaseHasTestSuite(TestNamespace ns) {
Table table = createTable(ns);
TestCase testCase =
TestCaseBuilder.create(SdkClients.adminClient())
.name(ns.prefix("has_suite"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.create();
assertNotNull(testCase.getTestSuite());
assertNotNull(testCase.getTestSuite().getId());
}
@Test
void test_listTestCasesWithPagination(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
Table table = createTable(ns);
for (int i = 0; i < 5; i++) {
TestCaseBuilder.create(client)
.name(ns.prefix("page_test_" + i))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", String.valueOf((i + 1) * 100))
.create();
}
ListResponse<TestCase> page1 = client.testCases().list(new ListParams().setLimit(2));
assertTrue(page1.getData().size() <= 2);
if (page1.getPaging() != null && page1.getPaging().getAfter() != null) {
ListResponse<TestCase> page2 =
client
.testCases()
.list(new ListParams().setLimit(2).setAfter(page1.getPaging().getAfter()));
assertNotNull(page2);
}
}
@Test
void test_createTestCaseWithLogicalTestSuites(TestNamespace ns) throws Exception {
OpenMetadataClient client = SdkClients.adminClient();
Table table = createTable(ns);
CreateTestSuite logicalSuiteReq = new CreateTestSuite();
logicalSuiteReq.setName(ns.prefix("logical_suite_create_request"));
TestSuite logicalSuite = client.testSuites().create(logicalSuiteReq);
EntityReference logicalSuiteRef =
new EntityReference()
.withId(logicalSuite.getId())
.withType(Entity.TEST_SUITE)
.withName(logicalSuite.getName());
CreateTestCase request =
TestCaseBuilder.create(client)
.name(ns.prefix("create_suite_request_tc"))
.forTable(table)
.testDefinition("tableRowCountToEqual")
.parameter("value", "100")
.testSuites(List.of(logicalSuiteRef))
.build();