1
|
/**
|
2
|
* Copyright (C) 2021 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.io.cdm2cdm;
|
10
|
|
11
|
import static org.junit.Assert.assertNotNull;
|
12
|
|
13
|
import java.io.FileNotFoundException;
|
14
|
import java.util.Arrays;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
19
|
import org.junit.Assert;
|
20
|
import org.junit.Before;
|
21
|
import org.junit.BeforeClass;
|
22
|
import org.junit.Ignore;
|
23
|
import org.junit.Test;
|
24
|
import org.springframework.transaction.TransactionStatus;
|
25
|
import org.unitils.spring.annotation.SpringBeanByName;
|
26
|
import org.unitils.spring.annotation.SpringBeanByType;
|
27
|
|
28
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
29
|
import eu.etaxonomy.cdm.api.service.ITermTreeService;
|
30
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
31
|
import eu.etaxonomy.cdm.database.CdmDataSource;
|
32
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
33
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
34
|
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultImport;
|
35
|
import eu.etaxonomy.cdm.io.common.ImportResult;
|
36
|
import eu.etaxonomy.cdm.io.common.events.LoggingIoObserver;
|
37
|
import eu.etaxonomy.cdm.model.term.DefinedTerm;
|
38
|
import eu.etaxonomy.cdm.model.term.TermNode;
|
39
|
import eu.etaxonomy.cdm.model.term.TermTree;
|
40
|
import eu.etaxonomy.cdm.model.term.TermType;
|
41
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
42
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
43
|
|
44
|
/**
|
45
|
* @author a.mueller
|
46
|
* @since 18.09.2021
|
47
|
*/
|
48
|
@Ignore //preliminary ignored as it does not run on jenkins yet
|
49
|
public class Cdm2CdmVocabularyImportTest extends CdmTransactionalIntegrationTest {
|
50
|
|
51
|
@SuppressWarnings("unused")
|
52
|
private static final Logger logger = LogManager.getLogger(Cdm2CdmVocabularyImportTest.class);
|
53
|
|
54
|
private static CdmApplicationController otherRepository;
|
55
|
|
56
|
@SpringBeanByName
|
57
|
private CdmApplicationAwareDefaultImport<?> defaultImport;
|
58
|
@SpringBeanByType
|
59
|
private IVocabularyService vocService;
|
60
|
@SpringBeanByType
|
61
|
private ITermTreeService treeService;
|
62
|
|
63
|
private Cdm2CdmImportConfigurator configurator;
|
64
|
|
65
|
private static UUID uuidStructVoc = UUID.fromString("4373c232-fbf3-4f1d-b766-ad603f7aa866");
|
66
|
private static UUID uuidStructFirst = UUID.fromString("74c917aa-670a-4dc5-9bd1-c26b98072349");
|
67
|
|
68
|
private static UUID uuidStructGraph = UUID.fromString("1fdf67c7-e267-44ca-8d35-be66e3746847");
|
69
|
|
70
|
@BeforeClass
|
71
|
@Ignore
|
72
|
public static void setUpClass() throws Exception {
|
73
|
// this.startH2Server();
|
74
|
boolean omitTermLoading = true;
|
75
|
//TODO remove user directory
|
76
|
ICdmDataSource dataSource = CdmDataSource.NewH2EmbeddedInstance("testVoc", "sa", "", "C:\\Users\\a.mueller\\tmp\\testVoc");
|
77
|
// int a = dataSource.executeUpdate("CREATE TABLE HIBERNATE_SEQUENCES ("
|
78
|
// + " sequence_name VARCHAR(255), next_val BIGINT )");
|
79
|
// try {
|
80
|
// ResultSet rs = dataSource.executeQuery("SELECT * FROM INFORMATION_SCHEMA.TABLES");
|
81
|
// while(rs.next()){
|
82
|
// for (int i = 1; i<=12; i++){
|
83
|
// System.out.print(";"+rs.getObject(i));
|
84
|
// }
|
85
|
// System.out.println();
|
86
|
// }
|
87
|
// dataSource.executeQuery("select tbl.next_val from hibernate_sequences tbl where tbl.sequence_name='a'");
|
88
|
otherRepository = CdmApplicationController.NewInstance(dataSource,
|
89
|
DbSchemaValidation.CREATE, omitTermLoading);
|
90
|
// } catch (Exception e) {
|
91
|
// // TODO Auto-generated catch block
|
92
|
// e.printStackTrace();
|
93
|
// }
|
94
|
System.out.println("started");
|
95
|
TermVocabulary<DefinedTerm> voc = createTestVocabulary(otherRepository);
|
96
|
createTestGraph(otherRepository, voc);
|
97
|
}
|
98
|
|
99
|
private static TermVocabulary<DefinedTerm> createTestVocabulary(CdmApplicationController app) {
|
100
|
TermType termType = TermType.Structure;
|
101
|
TermVocabulary<DefinedTerm> voc = TermVocabulary.NewInstance(termType, DefinedTerm.class);
|
102
|
DefinedTerm term = getStructure("1.", uuidStructFirst);
|
103
|
voc.addTerm(term);
|
104
|
voc.setUuid(uuidStructVoc);
|
105
|
|
106
|
app.getVocabularyService().saveOrUpdate(voc);
|
107
|
return voc;
|
108
|
}
|
109
|
|
110
|
private static void createTestGraph(CdmApplicationController app, TermVocabulary<DefinedTerm> voc) {
|
111
|
TermType termType = TermType.Structure;
|
112
|
TermTree<DefinedTerm> graph = TermTree.NewInstance(termType, DefinedTerm.class);
|
113
|
DefinedTerm firstStruct = voc.getTerms().stream().filter(t->t.getUuid().equals(uuidStructFirst)).findFirst().get();
|
114
|
TermNode<DefinedTerm> firstRoot = graph.getRoot().addChild(firstStruct);
|
115
|
graph.setUuid(uuidStructGraph);
|
116
|
|
117
|
app.getTermTreeService().saveOrUpdate(graph);
|
118
|
}
|
119
|
|
120
|
// @AfterClass
|
121
|
public static void tearDownClass() throws Exception {
|
122
|
otherRepository.close();
|
123
|
otherRepository = null;
|
124
|
}
|
125
|
|
126
|
@Before
|
127
|
public void setUp() throws Exception {
|
128
|
configurator = Cdm2CdmImportConfigurator.NewInstace(otherRepository, null);
|
129
|
configurator.setDoTaxa(false);
|
130
|
configurator.setDoDescriptions(false);
|
131
|
configurator.setVocabularyFilter(new HashSet<>(Arrays.asList(uuidStructVoc)));
|
132
|
|
133
|
configurator.addObserver(new LoggingIoObserver());
|
134
|
assertNotNull("Configurator could not be created", configurator);
|
135
|
}
|
136
|
|
137
|
@Test
|
138
|
public void testInit() {
|
139
|
assertNotNull("import instance should not be null", defaultImport);
|
140
|
assertNotNull("configurator instance should not be null", configurator);
|
141
|
}
|
142
|
|
143
|
@Test
|
144
|
public void testInvokeVocabulary() {
|
145
|
@SuppressWarnings("unchecked")
|
146
|
TermVocabulary<DefinedTerm> voc = vocService.find(uuidStructVoc);
|
147
|
Assert.assertNull("Vocabulary must not exist before invoke", voc);
|
148
|
ImportResult result = defaultImport.invoke(this.configurator);
|
149
|
Assert.assertTrue(result.isSuccess());
|
150
|
commitAndStartNewTransaction();
|
151
|
voc = vocService.find(uuidStructVoc);
|
152
|
Assert.assertNotNull("Vocabulary must exist after invoke", voc);
|
153
|
@SuppressWarnings("unchecked")
|
154
|
TermVocabulary<DefinedTerm> otherVoc = otherRepository.getVocabularyService().find(uuidStructVoc);
|
155
|
Assert.assertNotSame(otherVoc, voc);
|
156
|
Assert.assertEquals(1, voc.getTerms().size());
|
157
|
|
158
|
//add term in other
|
159
|
UUID uuidSecond = UUID.fromString("56546e58-e4ea-47f9-ae49-de772a416003");
|
160
|
DefinedTerm secondTerm = getStructure("2.", uuidSecond);
|
161
|
TransactionStatus tx = otherRepository.startTransaction();
|
162
|
otherVoc = otherRepository.getVocabularyService().find(uuidStructVoc);
|
163
|
otherVoc.addTerm(secondTerm);
|
164
|
otherRepository.getTermService().saveOrUpdate(secondTerm);
|
165
|
otherRepository.commitTransaction(tx);
|
166
|
|
167
|
//test if added term gets imported
|
168
|
commitAndStartNewTransaction();
|
169
|
voc = vocService.find(uuidStructVoc);
|
170
|
Assert.assertEquals(1, voc.getTerms().size());
|
171
|
commitAndStartNewTransaction();
|
172
|
result = defaultImport.invoke(this.configurator);
|
173
|
commitAndStartNewTransaction();
|
174
|
voc = vocService.find(uuidStructVoc);
|
175
|
Assert.assertEquals(2, voc.getTerms().size());
|
176
|
Assert.assertTrue("As contains works on equal() the term should be contained", voc.getTerms().contains(secondTerm));
|
177
|
voc.getTerms().stream().filter(a->a.getUuid().equals(uuidSecond)).forEach(t->{
|
178
|
Assert.assertEquals(secondTerm, t);
|
179
|
Assert.assertNotSame(secondTerm, t);
|
180
|
});
|
181
|
|
182
|
//test invoke for graph
|
183
|
configurator.setGraphFilter(new HashSet<>(Arrays.asList(uuidStructGraph)));
|
184
|
TermTree<DefinedTerm> graph = treeService.find(uuidStructGraph);
|
185
|
Assert.assertNull("Graph must not exist before invoke", graph);
|
186
|
result = defaultImport.invoke(this.configurator);
|
187
|
Assert.assertTrue(result.isSuccess());
|
188
|
commitAndStartNewTransaction();
|
189
|
graph = treeService.find(uuidStructGraph);
|
190
|
Assert.assertNotNull("Graph must exist after invoke", graph);
|
191
|
TransactionStatus txOther = otherRepository.startTransaction();
|
192
|
@SuppressWarnings("unchecked")
|
193
|
TermTree<DefinedTerm> otherGraph = otherRepository.getTermTreeService().find(uuidStructGraph);
|
194
|
Assert.assertNotSame(otherGraph, graph);
|
195
|
Assert.assertEquals(otherGraph.getRoot(), graph.getRoot());
|
196
|
Assert.assertNotSame(otherGraph.getRoot(), graph.getRoot());
|
197
|
Assert.assertEquals(1, graph.getRootChildren().size());
|
198
|
TermNode<DefinedTerm> otherSingleChild = otherGraph.getRootChildren().iterator().next();
|
199
|
TermNode<DefinedTerm> thisSingleChild = graph.getRootChildren().iterator().next();
|
200
|
Assert.assertEquals(otherSingleChild, thisSingleChild);
|
201
|
Assert.assertNotSame(otherSingleChild, thisSingleChild);
|
202
|
|
203
|
|
204
|
otherRepository.commitTransaction(txOther);
|
205
|
}
|
206
|
|
207
|
private static DefinedTerm getStructure(String distinct, UUID uuid) {
|
208
|
DefinedTerm term = DefinedTerm.NewInstance(TermType.Structure,
|
209
|
distinct + " structure description", distinct + " Struc", distinct + "St.");
|
210
|
term.getRepresentations().iterator().next().setLanguage(null); //as long as we have static languages
|
211
|
uuid = (uuid == null? UUID.randomUUID() : uuid);
|
212
|
term.setUuid(uuid);
|
213
|
return term;
|
214
|
}
|
215
|
|
216
|
@Override
|
217
|
public void createTestDataSet() throws FileNotFoundException {}
|
218
|
}
|