Newer
Older
package it.inaf.oats.ia2.tapschemamanager.api;
import it.inaf.oats.ia2.tapschemamanager.api.TapSchemaImpl;
import it.inaf.oats.ia2.tapschemamanager.api.contract.DatabaseType;
import it.inaf.oats.ia2.tapschemamanager.api.Credentials;
import it.inaf.oats.ia2.tapschemamanager.api.DBWrapper;
import it.inaf.oats.ia2.tapschemamanager.api.UpdateOperations;
import it.inaf.oats.ia2.tapschemamanager.api.TapSchemaFactory;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Column;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Key;
import it.inaf.oats.ia2.tapschemamanager.api.contract.KeyColumn;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Schema;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Status;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Table;
import it.inaf.oats.ia2.tapschemamanager.api.contract.TapSchema;
import it.inaf.oats.ia2.tapschemamanager.api.contract.TapSchemaVersion;
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
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class TestAll {
private static final Logger log = LoggerFactory.getLogger(TestAll.class);
private static final int SCHEMAS_COUNT = 2; // minimum value: 2
private static final int TABLES_COUNT = 3;
private static List<DBWrapper> dbWrappers;
public TestAll() {
}
@BeforeClass
public static void setUpClass() throws SQLException {
dbWrappers = new ArrayList<>();
// MYSQL
Credentials mysqlCredentials = new Credentials(DatabaseType.MYSQL);
mysqlCredentials.setHostname("localhost");
mysqlCredentials.setUsername("root");
mysqlCredentials.setPassword("root");
// POSTGRES
Credentials postgresCredentials = new Credentials(DatabaseType.POSTGRES);
postgresCredentials.setHostname("localhost");
postgresCredentials.setUsername("postgres");
postgresCredentials.setPassword("pippo");
DBWrapper dbWrapper = new DBWrapper(mysqlCredentials);
dbWrapper.testConnections();
dbWrappers.add(dbWrapper);
dbWrapper = new DBWrapper(postgresCredentials);
dbWrapper.testConnections();
//dbWrappers.add(dbWrapper);
// Mix!
//dbWrappers.add(new DBWrapper(mysqlCredentials, postgresCredentials));
//dbWrappers.add(new DBWrapper(postgresCredentials, mysqlCredentials));
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
private void removeTestingDatabases() throws SQLException {
for (DBWrapper dbWrapper : dbWrappers) {
try (Connection sourceConnection = dbWrapper.getSourceConnection();
Statement statement = sourceConnection.createStatement()) {
DatabaseType dbType = dbWrapper.getSourceDatabaseType();
// Removing keys between schema1 and schema0
if (dbType == DatabaseType.MYSQL) {
try (ResultSet rs = statement.executeQuery("SHOW DATABASES WHERE `Database` = 'sch1'")) {
// The constraint can be removed only if sch1 exists
if (rs.next()) {
statement.executeUpdate("ALTER TABLE sch1.table0 DROP FOREIGN KEY sch0table0id_constraint");
}
}
} else if (dbType == DatabaseType.POSTGRES) {
try (ResultSet rs = statement.executeQuery("SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'sch1'")) {
// The constraint can be removed only if sch1 exists
if (rs.next()) {
statement.executeUpdate("ALTER TABLE sch1.table0 DROP CONSTRAINT sch0table0id_constraint");
}
}
} else {
throw new UnsupportedOperationException("Database type " + dbType + " not supported");
}
for (int i = 0; i < SCHEMAS_COUNT; i++) {
if (dbType == DatabaseType.MYSQL) {
statement.executeUpdate("DROP DATABASE IF EXISTS sch" + i);
} else if (dbType == DatabaseType.POSTGRES) {
statement.executeUpdate("DROP SCHEMA IF EXISTS sch" + i + " CASCADE");
} else {
throw new UnsupportedOperationException("Database type " + dbType + " not supported");
}
}
}
try (Connection tapSchemaConnection = dbWrapper.getTapSchemaConnection();
Statement statement = tapSchemaConnection.createStatement()) {
DatabaseType dbType = dbWrapper.getTapSchemaDatabaseType();
if (dbType == DatabaseType.MYSQL) {
statement.executeUpdate("DROP DATABASE IF EXISTS test_tap_schema");
} else if (dbType == DatabaseType.POSTGRES) {
statement.executeUpdate("DROP SCHEMA IF EXISTS test_tap_schema CASCADE");
} else {
throw new UnsupportedOperationException("Database type " + dbType + " not supported");
}
}
}
}
private void setUpTestingDatabases() throws SQLException {
removeTestingDatabases();
for (DBWrapper dbWrapper : dbWrappers) {
DatabaseType dbType = dbWrapper.getSourceDatabaseType();
try (Connection connection = dbWrapper.getSourceConnection()) {
try (Statement statement = connection.createStatement()) {
if (dbType == DatabaseType.MYSQL) {
for (int i = 0; i < SCHEMAS_COUNT; i++) {
statement.executeUpdate("CREATE DATABASE sch" + i);
for (int j = 0; j < TABLES_COUNT; j++) {
statement.executeUpdate("CREATE TABLE sch" + i + ".table" + j + " (\n"
+ "id INT PRIMARY KEY AUTO_INCREMENT,\n"
+ "value1 VARCHAR(255),\n"
+ "value2 FLOAT\n"
+ ");");
}
}
} else if (dbType == DatabaseType.POSTGRES) {
for (int i = 0; i < SCHEMAS_COUNT; i++) {
statement.executeUpdate("CREATE SCHEMA sch" + i);
for (int j = 0; j < TABLES_COUNT; j++) {
statement.executeUpdate("CREATE TABLE sch" + i + ".table" + j + " (\n"
+ "id BIGSERIAL PRIMARY KEY,\n"
+ "value1 VARCHAR(255),\n"
+ "value2 FLOAT\n"
+ ");");
}
}
} else {
throw new UnsupportedOperationException("Database type " + dbType + " not supported");
}
}
log.info("dbs created");
try (Statement statement = connection.createStatement()) {
for (int j = 0; j < TABLES_COUNT - 1; j++) {
String update1 = "ALTER TABLE sch0.table" + (j + 1) + " ADD COLUMN table" + j + "_id INT";
statement.executeUpdate(update1);
String update2;
if (dbType == DatabaseType.MYSQL || dbType == DatabaseType.POSTGRES) {
update2 = "ALTER TABLE sch0.table" + (j + 1) + " ADD CONSTRAINT id_constraint_" + (j + 1) + " FOREIGN KEY (table" + j + "_id) REFERENCES sch0.table" + j + "(id)";
} else {
throw new UnsupportedOperationException("Database type " + dbType + " not supported");
}
statement.executeUpdate(update2);
}
// Foreign key with multiple columns
statement.executeUpdate("CREATE TABLE sch0.table_x (idx1 INT, idx2 INT)");
statement.executeUpdate("ALTER TABLE sch0.table_x ADD CONSTRAINT pkx PRIMARY KEY (idx1, idx2)");
statement.executeUpdate("CREATE TABLE sch0.table_y (idy1 INT, idy2 INT)");
statement.executeUpdate("ALTER TABLE sch0.table_y ADD CONSTRAINT pky PRIMARY KEY (idy1, idy2)");
statement.executeUpdate("ALTER TABLE sch0.table_y ADD CONSTRAINT fky FOREIGN KEY(idy1, idy2) REFERENCES sch0.table_x(idx1, idx2)");
// Foreign keys between different schemas
statement.executeUpdate("ALTER TABLE sch0.table0 ADD COLUMN sch1table0id INT");
statement.executeUpdate("ALTER TABLE sch0.table0 ADD CONSTRAINT sch1table0id_constraint FOREIGN KEY(sch1table0id) REFERENCES sch1.table0(id)");
statement.executeUpdate("ALTER TABLE sch1.table0 ADD COLUMN sch0table0id INT");
statement.executeUpdate("ALTER TABLE sch1.table0 ADD CONSTRAINT sch0table0id_constraint FOREIGN KEY(sch0table0id) REFERENCES sch0.table0(id)");
}
}
}
}
@Test
public void testTapSchemaSerialization() throws Exception {
for (DBWrapper dbWrapper : dbWrappers) {
if (dbWrapper.getTapSchemaDatabaseType() == DatabaseType.MYSQL) { // currently "tng_TAP_SCHEMA" exists in a MySQL instance
TapSchema tapSchema = TapSchemaFactory.getTapSchema(TapSchemaVersion.TAP_SCHEMA_1_IA2, dbWrapper, "test_tap_schema", true);
File temp = File.createTempFile("test_tap_schema", ".ser");
try (FileOutputStream fileOut = new FileOutputStream(temp);
ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
out.writeObject(tapSchema);
}
try (FileInputStream fileIn = new FileInputStream(temp);
ObjectInputStream in = new ObjectInputStream(fileIn)) {
tapSchema = (TapSchema) in.readObject();
}
log.debug(tapSchema.toString());
}
}
}
private boolean allKeysHaveDifferentId(TapSchema tapSchema) {
boolean differentKeySameId = false;
Map<String, Key> keys = new HashMap<>();
for (Key key : ((TapSchemaImpl) tapSchema).getAllKeys()) {
if (key.getId() != null) {
if (keys.get(key.getId()) != null && !key.equals(keys.get(key.getId()))) {
differentKeySameId = true;
}
keys.put(key.getId(), key);
}
}
if (differentKeySameId) {
log.debug("Found different keys with the same key_id!");
for (Key key : ((TapSchemaImpl) tapSchema).getAllKeys()) {
log.debug(key.toString());
}
}
return !differentKeySameId;
}
private void checkKey(Key key, String fromTableCompleteName, String[] fromColumns, String targetTableCompleteName, String[] targetColumns, boolean isVisible) {
assertEquals(fromTableCompleteName, key.getFromTableCompleteName());
assertEquals(targetTableCompleteName, key.getTargetTableCompleteName());
for (int i = 0; i < key.getKeyColumns().size(); i++) {
KeyColumn keyColumn = key.getKeyColumns().get(i);
assertEquals(fromColumns[i], keyColumn.getFromColumn());
assertEquals(targetColumns[i], keyColumn.getTargetColumn());
}
assertEquals(isVisible, key.isVisible());
for (KeyColumn keyColumn : key.getKeyColumns()) {
assertEquals(key.getId(), keyColumn.getKeyId());
}
}
private void checkKey(Key key, String fromTableCompleteName, String fromColumn, String targetTableCompleteName, String targetColumn, boolean isVisible) {
checkKey(key, fromTableCompleteName, new String[]{fromColumn}, targetTableCompleteName, new String[]{targetColumn}, isVisible);
}
@Test
public void createNewAndUpdate2() throws SQLException {
log.info("TEST createNewAndUpdate2 STARTED");
try {
removeTestingDatabases();
setUpTestingDatabases();
for (DBWrapper dbWrapper : dbWrappers) {
// Initializing a not existing TAP_SCHEMA
TapSchema tapSchema = TapSchemaFactory.getTapSchema(TapSchemaVersion.TAP_SCHEMA_1_IA2, dbWrapper, "test_tap_schema", false);
/////////////////////////////////////
// ADDING A SCHEMA //
/////////////////////////////////////
//
Schema sch0 = tapSchema.addChild("sch0");
assertEquals(Status.ADDED_NOT_PERSISTED, sch0.getStatus());
Set<Key> allKeys = ((TapSchemaImpl) tapSchema).getAllKeys();
log.debug("ALL keys:");
for (Key key : allKeys) {
log.debug(key.toString());
}
// In the testing schemas each numbered table references the id
// of the previous table, except for the first table, so there
// should be "TABLES_COUNT - 1" keys for the numbered tables.
// Moreover, there are also the following keys:
// - sch0.table0.sch1table0id -> sch1.table0.id
// - sch1.table0.sch0table0id -> sch0.table0.id
// - sch0.table_y.(idy1, idy2) -> sch0.table_x.(idyx, idx2)
// so we check for TABLES_COUNT + 2.
assertEquals(TABLES_COUNT + 2, allKeys.size());
// Checking that keys information has been filled correctly.
for (Key schemaKey : allKeys) {
assertFalse(schemaKey.isVisible());
assertNull(schemaKey.getId());
assertEquals(schemaKey.getFromTableCompleteName(), schemaKey.getFromSchemaName() + "." + schemaKey.getFromTableSimpleName());
assertEquals(schemaKey.getTargetTableCompleteName(), schemaKey.getTargetSchemaName() + "." + schemaKey.getTargetTableSimpleName());
assertTrue(schemaKey.getKeyColumns().size() >= 1);
for (KeyColumn keyColumn : schemaKey.getKeyColumns()) {
assertNotNull(keyColumn.getFromColumn());
assertNotNull(keyColumn.getTargetColumn());
}
}
/////////////////////////////////////
// ADDING A TABLE //
/////////////////////////////////////
//
Table sch0table0 = sch0.addChild("table0");
assertEquals(Status.ADDED_NOT_PERSISTED, sch0table0.getStatus());
assertEquals("sch0.table0", sch0table0.getCompleteName());
assertEquals(1, sch0.getChildren().size());
assertEquals(4, sch0table0.getAddableChildrenNames().size());
Column sch0table0id = sch0table0.addChild("id");
assertEquals(Status.ADDED_NOT_PERSISTED, sch0table0id.getStatus());
// Primary key check
assertTrue(sch0table0id.isPrimaryKey());
/////////////////////////////////////
// KEYS MANAGEMENT //
/////////////////////////////////////
//
// CASE 1: Foreign key between two tables in the same schema.
// sch0.table1.t0id -> sch0.table0.id
//
// Adding sch0.table1
Table sch0table1 = sch0.addChild("table1");
assertEquals(2, sch0.getChildren().size());
assertTrue(sch0table0.getVisibleFromKeys().isEmpty());
assertTrue(sch0table0.getVisibleTargetKeys().isEmpty());
assertTrue(sch0table1.getVisibleFromKeys().isEmpty());
assertTrue(sch0table1.getVisibleTargetKeys().isEmpty());
sch0table1.addChild("table0_id");
// Now sch0table0.getVisibleTargetKeys() and sch0table1.getVisibleFromKeys()
// have to return the same Key object with id = 1
assertTrue(sch0table0.getVisibleFromKeys().isEmpty());
assertEquals(1, sch0table0.getVisibleTargetKeys().size());
assertEquals(1, sch0table1.getVisibleFromKeys().size());
assertTrue(sch0table1.getVisibleTargetKeys().isEmpty());
// Check if key and its columns have been properly initialized
Key sch0table0TargetKey = sch0table0.getVisibleTargetKeys().get(0);
Key sch0table1TargetKey = sch0table1.getVisibleFromKeys().get(0);
checkKey(sch0table0TargetKey, "sch0.table1", "table0_id", "sch0.table0", "id", true);
assertEquals("1", sch0table0TargetKey.getId());
assertEquals(sch0table0TargetKey, sch0table1TargetKey);
// Removing sch0.table1
assertEquals(1, sch0table0.getVisibleTargetKeys().size()); // sch0.table1.table0_id -> sch0.table0.id
sch0.removeChild("table1");
assertEquals(1, sch0.getAddedChildren().size());
assertTrue(sch0table0.getVisibleFromKeys().isEmpty());
assertTrue(sch0table0.getVisibleTargetKeys().isEmpty());
assertFalse(sch0table0TargetKey.isVisible());
//
// CASE 2: Foreign key between two tables in different schemas.
// sch1.table0.sch0table0id -> sch0.table0.id
//
// Adding sch1
Schema sch1 = tapSchema.addChild("sch1");
allKeys = ((TapSchemaImpl) tapSchema).getAllKeys();
assertEquals(5, allKeys.size());
// Adding sch1.table0
Table sch1table0 = sch1.addChild("table0");
sch1table0.addChild("id");
sch1table0.addChild("sch0table0id"); // sch1.table0.sch0table0id -> sch0.table0.it obtains keyId = "1"
sch0table0.addChild("sch1table0id");// sch0.table0.sch1table0id -> sch1.table0.it obtains keyId = "2"
assertEquals(0, sch0table1.getVisibleFromKeys().size());
assertEquals(1, sch0table0.getVisibleFromKeys().size());
assertEquals(1, sch0table0.getVisibleTargetKeys().size());
assertEquals(1, sch1table0.getVisibleFromKeys().size());
assertEquals(1, sch1table0.getVisibleTargetKeys().size());
for (Key key : allKeys) {
if (key.getId() == null) {
assertFalse(key.isVisible());
} else {
switch (key.getId()) {
case "1":
checkKey(key, "sch0.table1", "table0_id", "sch0.table0", "id", false);
break;
case "2":
checkKey(key, "sch1.table0", "sch0table0id", "sch0.table0", "id", true);
break;
case "3":
checkKey(key, "sch0.table0", "sch1table0id", "sch1.table0", "id", true);
break;
}
}
}
assertTrue(allKeysHaveDifferentId(tapSchema));
// Removing sch1
tapSchema.removeChild("sch1");
assertEquals(Status.REMOVED_NOT_PERSISTED, sch1.getStatus());
assertTrue(sch0table0.getVisibleTargetKeys().isEmpty());
// Case 2B: Re-adding sch1
// sch1.table0 has not been removed from its schema, so the keys
// should be re-added.
tapSchema.addChild("sch1");
assertEquals(Status.ADDED_NOT_PERSISTED, sch1.getStatus());
assertEquals(1, sch0table0.getVisibleTargetKeys().size());
assertEquals(1, sch1table0.getVisibleFromKeys().size());
for (Key key : allKeys) {
if (key.getId() == null) {
assertFalse(key.isVisible());
} else {
switch (key.getId()) {
case "1":
checkKey(key, "sch0.table1", "table0_id", "sch0.table0", "id", false);
break;
case "2":
checkKey(key, "sch1.table0", "sch0table0id", "sch0.table0", "id", true);
break;
case "3":
checkKey(key, "sch0.table0", "sch1table0id", "sch1.table0", "id", true);
break;
}
}
}
//
// CASE 3: foreign key with multiple columns
//
Table table_x = sch0.addChild("table_x");
Table table_y = sch0.addChild("table_y");
assertTrue(table_x.getVisibleFromKeys().isEmpty());
assertTrue(table_x.getVisibleTargetKeys().isEmpty());
assertTrue(table_y.getVisibleFromKeys().isEmpty());
assertTrue(table_y.getVisibleTargetKeys().isEmpty());
table_x.addChild("idx1");
table_x.addChild("idx2");
table_y.addChild("idy1");
assertTrue(table_x.getVisibleFromKeys().isEmpty());
assertTrue(table_x.getVisibleTargetKeys().isEmpty());
assertTrue(table_y.getVisibleFromKeys().isEmpty());
assertTrue(table_y.getVisibleTargetKeys().isEmpty());
table_y.addChild("idy2");
assertEquals(1, table_y.getVisibleFromKeys().size());
assertEquals(1, table_x.getVisibleTargetKeys().size());
assertTrue(table_x.getVisibleFromKeys().isEmpty());
assertTrue(table_y.getVisibleTargetKeys().isEmpty());
for (Key key : allKeys) {
if (key.getId() == null) {
assertFalse(key.isVisible());
} else {
switch (key.getId()) {
case "1":
checkKey(key, "sch0.table1", "table0_id", "sch0.table0", "id", false);
break;
case "2":
checkKey(key, "sch1.table0", "sch0table0id", "sch0.table0", "id", true);
break;
case "3":
checkKey(key, "sch0.table0", "sch1table0id", "sch1.table0", "id", true);
break;
case "4":
checkKey(key, "sch0.table_y", new String[]{"idy1", "idy2"}, "sch0.table_x", new String[]{"idx1", "idx2"}, true);
break;
}
}
}
assertTrue(allKeysHaveDifferentId(tapSchema));
/////////////////////////////////////
// SAVE //
/////////////////////////////////////
sch0table1 = sch0.addChild("table1");
sch0table1.addChild("id");
Table sch0table2 = sch0.addChild("table2");
sch0table2.addChild("table1_id");
for (Key key : allKeys) {
if (key.getId() == null) {
assertFalse(key.isVisible());
} else {
switch (key.getId()) {
case "1":
checkKey(key, "sch0.table1", "table0_id", "sch0.table0", "id", true);
break;
case "2":
checkKey(key, "sch1.table0", "sch0table0id", "sch0.table0", "id", true);
break;
case "3":
checkKey(key, "sch0.table0", "sch1table0id", "sch1.table0", "id", true);
break;
case "4":
checkKey(key, "sch0.table_y", new String[]{"idy1", "idy2"}, "sch0.table_x", new String[]{"idx1", "idx2"}, true);
break;
case "5":
checkKey(key, "sch0.table2", "table1_id", "sch0.table1", "id", true);
break;
}
}
}
assertTrue(allKeysHaveDifferentId(tapSchema));
UpdateOperations operations = new UpdateOperations(tapSchema);
assertEquals(2, operations.getSchemasToAdd().size());
assertEquals(6, operations.getTablesToAdd().size());
assertEquals(11, operations.getColumnsToAdd().size());
assertEquals(5, operations.getKeysToAdd().size());
tapSchema.save();
assertFalse(new UpdateOperations(tapSchema).getHasOperations());
assertTrue(allKeysHaveDifferentId(tapSchema));
// reloading
log.debug("----- Reloading saved TAP_SCHEMA -----");
tapSchema = TapSchemaFactory.getTapSchema(TapSchemaVersion.TAP_SCHEMA_1_IA2, dbWrapper, "test_tap_schema", true);
allKeys = ((TapSchemaImpl) tapSchema).getAllKeys();
assertTrue(allKeysHaveDifferentId(tapSchema));
log.debug(tapSchema.toString());
assertNotNull(sch0 = tapSchema.getChild("sch0", Status.ADDED_PERSISTED));
assertNotNull(sch1 = tapSchema.getChild("sch1", Status.ADDED_PERSISTED));
assertNotNull(sch0table0 = sch0.getChild("table0", Status.ADDED_PERSISTED));
assertNotNull(sch0table1 = sch0.getChild("table1", Status.ADDED_PERSISTED));
assertNotNull(sch0table2 = sch0.getChild("table2", Status.ADDED_PERSISTED));
assertNotNull(table_x = sch0.getChild("table_x", Status.ADDED_PERSISTED));
assertNotNull(table_y = sch0.getChild("table_y", Status.ADDED_PERSISTED));
assertNotNull(sch1table0 = sch1.getChild("table0", Status.ADDED_PERSISTED));
assertNotNull(sch0table0.getChild("id", Status.ADDED_PERSISTED));
assertNotNull(sch0table0.getChild("sch1table0id", Status.ADDED_PERSISTED));
assertNotNull(sch0table1.getChild("id", Status.ADDED_PERSISTED));
assertNotNull(sch0table1.getChild("table0_id", Status.ADDED_PERSISTED));
assertNotNull(sch0table2.getChild("table1_id", Status.ADDED_PERSISTED));
assertNotNull(table_x.getChild("idx1", Status.ADDED_PERSISTED));
assertNotNull(table_x.getChild("idx2", Status.ADDED_PERSISTED));
assertNotNull(table_y.getChild("idy1", Status.ADDED_PERSISTED));
assertNotNull(table_y.getChild("idy2", Status.ADDED_PERSISTED));
assertNotNull(sch1table0.getChild("id", Status.ADDED_PERSISTED));
assertNotNull(sch1table0.getChild("sch0table0id", Status.ADDED_PERSISTED));
for (Key key : allKeys) {
if (key.getId() == null) {
assertFalse(key.isVisible());
} else {
switch (key.getId()) {
case "1":
checkKey(key, "sch0.table1", "table0_id", "sch0.table0", "id", true);
break;
case "2":
checkKey(key, "sch1.table0", "sch0table0id", "sch0.table0", "id", true);
break;
case "3":
checkKey(key, "sch0.table0", "sch1table0id", "sch1.table0", "id", true);
break;
case "4":
checkKey(key, "sch0.table_y", new String[]{"idy1", "idy2"}, "sch0.table_x", new String[]{"idx1", "idx2"}, true);
break;
case "5":
checkKey(key, "sch0.table2", "table1_id", "sch0.table1", "id", true);
break;
}
}
}
List<Key> sch0table1FromKeys = sch0table1.getVisibleFromKeys();
assertEquals(1, sch0table1FromKeys.size());
assertEquals(1, sch0table1.getVisibleTargetKeys().size());
sch0.removeChild("table1");
assertEquals(0, sch0table1.getVisibleFromKeys().size());
for (Key key : allKeys) {
if (key.getId() == null) {
assertFalse(key.isVisible());
} else {
switch (key.getId()) {
case "1":
checkKey(key, "sch0.table1", "table0_id", "sch0.table0", "id", false);
break;
}
}
}
operations = new UpdateOperations(tapSchema);
assertFalse(operations.getHasEntitiesToUpdate());
assertFalse(operations.getHasEntitiesToAdd());
assertEquals(0, operations.getSchemasToRemove().size());
assertEquals(1, operations.getTablesToRemove().size());
assertEquals(2, operations.getColumnsToRemove().size());
assertEquals(2, operations.getKeysToRemove().size());
tapSchema.save();
log.debug(tapSchema.toString());
assertFalse(new UpdateOperations(tapSchema).getHasOperations());
// reloading
log.debug("----- Reloading saved TAP_SCHEMA -----");
tapSchema = TapSchemaFactory.getTapSchema(TapSchemaVersion.TAP_SCHEMA_1_IA2, dbWrapper, "test_tap_schema", true);
allKeys = ((TapSchemaImpl) tapSchema).getAllKeys();
log.debug(tapSchema.toString());
assertNotNull(sch0 = tapSchema.getChild("sch0", Status.ADDED_PERSISTED));
assertNotNull(sch1 = tapSchema.getChild("sch1", Status.ADDED_PERSISTED));
assertNotNull(sch0table0 = sch0.getChild("table0", Status.ADDED_PERSISTED));
assertNotNull(sch0table2 = sch0.getChild("table2", Status.ADDED_PERSISTED));
assertNotNull(table_x = sch0.getChild("table_x", Status.ADDED_PERSISTED));
assertNotNull(table_y = sch0.getChild("table_y", Status.ADDED_PERSISTED));
assertNotNull(sch1table0 = sch1.getChild("table0", Status.ADDED_PERSISTED));
assertNotNull(sch0table0.getChild("id", Status.ADDED_PERSISTED));
assertNotNull(sch0table0.getChild("sch1table0id", Status.ADDED_PERSISTED));
assertNotNull(sch0table2.getChild("table1_id", Status.ADDED_PERSISTED));
assertNotNull(table_x.getChild("idx1", Status.ADDED_PERSISTED));
assertNotNull(table_x.getChild("idx2", Status.ADDED_PERSISTED));
assertNotNull(table_y.getChild("idy1", Status.ADDED_PERSISTED));
assertNotNull(table_y.getChild("idy2", Status.ADDED_PERSISTED));
assertNotNull(sch1table0.getChild("id", Status.ADDED_PERSISTED));
assertNotNull(sch1table0.getChild("sch0table0id", Status.ADDED_PERSISTED));
for (Key key : allKeys) {
if (key.getId() == null) {
assertFalse(key.isVisible());
} else {
assertNotNull(key.getOriginalValue(Key.ID_KEY, String.class)); // for reloaded keys
switch (key.getId()) {
case "2":
checkKey(key, "sch1.table0", "sch0table0id", "sch0.table0", "id", true);
break;
case "3":
checkKey(key, "sch0.table0", "sch1table0id", "sch1.table0", "id", true);
break;
case "4":
checkKey(key, "sch0.table_y", new String[]{"idy1", "idy2"}, "sch0.table_x", new String[]{"idx1", "idx2"}, true);
break;
case "5":
checkKey(key, "sch0.table2", "table1_id", "sch0.table1", "id", true);
break;
}
}
}
}
} catch (SQLException e) {
throw e;
} finally {
//removeTestingDatabases(credentials);
}
}
}