1
|
/**
|
2
|
* Copyright (C) 2007 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
|
|
10
|
package eu.etaxonomy.cdm.api.service;
|
11
|
|
12
|
import static org.junit.Assert.assertEquals;
|
13
|
import static org.junit.Assert.assertNotNull;
|
14
|
import static org.junit.Assert.assertNull;
|
15
|
import static org.junit.Assert.assertTrue;
|
16
|
|
17
|
import java.io.FileNotFoundException;
|
18
|
import java.net.URI;
|
19
|
import java.util.Set;
|
20
|
import java.util.UUID;
|
21
|
|
22
|
import org.apache.log4j.Logger;
|
23
|
import org.junit.Ignore;
|
24
|
import org.junit.Test;
|
25
|
import org.unitils.spring.annotation.SpringBeanByType;
|
26
|
|
27
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
28
|
import eu.etaxonomy.cdm.model.common.DefinedTerm;
|
29
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
30
|
import eu.etaxonomy.cdm.model.common.Language;
|
31
|
import eu.etaxonomy.cdm.model.common.Representation;
|
32
|
import eu.etaxonomy.cdm.model.common.TermType;
|
33
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
34
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
35
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
36
|
import eu.etaxonomy.cdm.model.name.Rank;
|
37
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
|
38
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
39
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
40
|
|
41
|
/**
|
42
|
* @author a.mueller
|
43
|
* @created 27.05.2008
|
44
|
* @version 1.0
|
45
|
*/
|
46
|
public class TermServiceImplTest extends CdmTransactionalIntegrationTest{
|
47
|
@SuppressWarnings("unused")
|
48
|
private static final Logger logger = Logger.getLogger(TermServiceImplTest.class);
|
49
|
|
50
|
@SpringBeanByType
|
51
|
private ITermService termService;
|
52
|
|
53
|
@SpringBeanByType
|
54
|
private IVocabularyService vocabularyService;
|
55
|
|
56
|
@SpringBeanByType
|
57
|
private ITaxonService taxonService;
|
58
|
|
59
|
/* ************************* TESTS *************************************************/
|
60
|
|
61
|
/**
|
62
|
* Test method for {@link eu.etaxonomy.cdm.api.service.TermServiceImpl#getTermByUri(java.lang.String)}.
|
63
|
*/
|
64
|
@Ignore //second part of test throws unexpected exception & also first part fails since language(406)
|
65
|
//is also not found here, for an explanation see comment below
|
66
|
@Test
|
67
|
/* @DataSet
|
68
|
* WARNING:
|
69
|
* the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
|
70
|
* and thus will cause unitils to empty the according tables, thus all terms etc will be deleted for the
|
71
|
* following tests, thus it might be a good idea moving this test to the end
|
72
|
*/
|
73
|
public void testGetTermByUri() {
|
74
|
String uriStr = "http://any.uri.com";
|
75
|
URI uri = URI.create(uriStr);
|
76
|
DefinedTermBase<?> term = termService.getByUri(uri);
|
77
|
assertNotNull(term);
|
78
|
//for testing only
|
79
|
// TermVocabulary<?> voc = term.getVocabulary();
|
80
|
// service.saveOrUpdate(term);
|
81
|
// List<MarkerType> list = service.listByTermClass(MarkerType.class, null, null, null, null);
|
82
|
|
83
|
//NULL
|
84
|
//FIXME throws object not found exception. Wants to load term.voc(11).representation(496).language(124) which does not exist
|
85
|
//I do not understand where the vocabulary data comes from (checked persistence TermsDataSet-with_auditing_info.xml) but somehow this does not apply
|
86
|
String uriNotExistStr = "http://www.notExisting.com";
|
87
|
URI uriNotExist = URI.create(uriNotExistStr);
|
88
|
DefinedTermBase<?> termNotExist = termService.getByUri(uriNotExist);
|
89
|
assertNull(termNotExist);
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* Test method for {@link eu.etaxonomy.cdm.api.service.TermServiceImpl#getContinentByUuid(java.util.UUID)}.
|
94
|
*/
|
95
|
@Test
|
96
|
/* @DataSet
|
97
|
* WARNING:
|
98
|
* the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
|
99
|
* and thus will cause unitils empty the according tables
|
100
|
*/
|
101
|
public void testGetTermByUuid() {
|
102
|
// Rank.Domain
|
103
|
String strUUID = "ffca6ec8-8b88-417b-a6a0-f7c992aac19b";
|
104
|
UUID uuid = UUID.fromString(strUUID);
|
105
|
DefinedTermBase<?> term = termService.find(uuid);
|
106
|
assertNotNull(term);
|
107
|
assertEquals(Rank.DOMAIN(), term);
|
108
|
//NULL
|
109
|
String strUUIDNotExist = "00000000-8b88-417b-a6a0-f7c992aac19c";
|
110
|
UUID uuidNotExist = UUID.fromString(strUUIDNotExist);
|
111
|
DefinedTermBase<?> termNotExist = termService.find(uuidNotExist);
|
112
|
assertNull(termNotExist);
|
113
|
}
|
114
|
|
115
|
|
116
|
/**
|
117
|
* Test method for {@link eu.etaxonomy.cdm.api.service.TermServiceImpl#listTerms(java.util.UUID)}.
|
118
|
*/
|
119
|
// @Ignore
|
120
|
@Test
|
121
|
/* @DataSet
|
122
|
* WARNING:
|
123
|
* the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
|
124
|
* and thus will cause unitils empty the according tables
|
125
|
*/
|
126
|
public void testGetVocabularyUUID() {
|
127
|
//Rank
|
128
|
String rankVocabularyUuid = "ef0d1ce1-26e3-4e83-b47b-ca74eed40b1b";
|
129
|
UUID rankUuid = UUID.fromString(rankVocabularyUuid);
|
130
|
TermVocabulary<Rank> voc = vocabularyService.find(rankUuid);
|
131
|
assertNotNull(voc);
|
132
|
assertEquals(62, voc.getTerms().size());
|
133
|
//Null
|
134
|
String nullVocabularyUuid = "00000000-26e3-4e83-b47b-ca74eed40b1b";
|
135
|
UUID nullUuid = UUID.fromString(nullVocabularyUuid);
|
136
|
TermVocabulary<Rank> nullVoc = vocabularyService.find(nullUuid);
|
137
|
assertNull(nullVoc);
|
138
|
}
|
139
|
|
140
|
|
141
|
@Test
|
142
|
/* @DataSet
|
143
|
* WARNING:
|
144
|
* the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
|
145
|
* and thus will cause unitils empty the according tables
|
146
|
*/
|
147
|
public void testGetAreaByTdwgAbbreviation(){
|
148
|
String tdwgAbbreviation = "GER-OO";
|
149
|
NamedArea germany = termService.getAreaByTdwgAbbreviation(tdwgAbbreviation);
|
150
|
assertEquals(tdwgAbbreviation, germany.getRepresentation(Language.DEFAULT()).getAbbreviatedLabel());
|
151
|
}
|
152
|
|
153
|
@Test
|
154
|
/* @DataSet
|
155
|
* WARNING:
|
156
|
* the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
|
157
|
* and thus will cause unitils empty the according tables
|
158
|
*/
|
159
|
public void testListTerms() {
|
160
|
Pager<SpecimenTypeDesignationStatus> results = termService.page(SpecimenTypeDesignationStatus.class, null,null,null,null);
|
161
|
assertNotNull("Results should not be null",results);
|
162
|
}
|
163
|
|
164
|
@Ignore
|
165
|
@Test
|
166
|
public void testTitleCacheUpdate(){
|
167
|
String uuid = "ae787603-3070-4298-9ca6-4cbe73378122";
|
168
|
UUID fromString = UUID.fromString(uuid);
|
169
|
DefinedTermBase<?> termBase = termService.find(fromString);
|
170
|
|
171
|
// change label
|
172
|
String expectedTitleCache = termBase.getLabel() + "append";
|
173
|
termBase.setLabel(expectedTitleCache);
|
174
|
|
175
|
commitAndStartNewTransaction(null);
|
176
|
|
177
|
termBase = termService.find(fromString);
|
178
|
assertEquals("Title cache did not update after setting the label and saving the term", expectedTitleCache, termBase.getTitleCache());
|
179
|
|
180
|
// add new representation for default language
|
181
|
String expecteTitleCacheAfterRepresentationChange = "new label";
|
182
|
Representation representation = termBase.getRepresentation(Language.DEFAULT());
|
183
|
representation.setLabel(expecteTitleCacheAfterRepresentationChange);
|
184
|
termBase.addRepresentation(representation);
|
185
|
|
186
|
//this will create another termBase in the DB which has the same UUID -> test failure
|
187
|
// termBase.addRepresentation(Representation.NewInstance(expecteTitleCacheAfterRepresentationChange, "", "", Language.DEFAULT()));////new Representation(expecteTitleCacheAfterRepresentationChange, "", "", Language.DEFAULT()));
|
188
|
|
189
|
|
190
|
commitAndStartNewTransaction(null);
|
191
|
|
192
|
termBase = termService.find(fromString);
|
193
|
assertEquals("Title cache did not update after adding a new representation for default language and saving the term", expecteTitleCacheAfterRepresentationChange, termBase.getTitleCache());
|
194
|
}
|
195
|
|
196
|
|
197
|
@Test
|
198
|
@Ignore
|
199
|
public void testDeleteTerms(){
|
200
|
final String[] tableNames = new String[]{
|
201
|
"DefinedTermBase","Representation"};
|
202
|
|
203
|
//commitAndStartNewTransaction(tableNames);
|
204
|
/*TermVocabulary<DefinedTerm> vocs = TermVocabulary.NewInstance(TermType.Feature, "TestFeatures", null, null, null);
|
205
|
vocs.addTerm(DefinedTerm.NewInstance(TermType.State, "green", "green", "gn"));
|
206
|
UUID vocUUIDs = vocabularyService.save(vocs);*/
|
207
|
Pager<DefinedTermBase> term = termService.findByRepresentationText("green", DefinedTermBase.class, null, null);
|
208
|
if (term.getCount() != 0){
|
209
|
|
210
|
DeleteResult result = termService.delete(term.getRecords().get(0));
|
211
|
assertTrue(result.isOk());
|
212
|
commitAndStartNewTransaction(tableNames);
|
213
|
}
|
214
|
TermVocabulary<DefinedTerm> voc = TermVocabulary.NewInstance(TermType.Feature, "TestFeatures", null, null, null);
|
215
|
voc.addTerm(DefinedTerm.NewDnaMarkerInstance("test", "marker", "t"));
|
216
|
UUID vocUUID = vocabularyService.save(voc).getUuid();
|
217
|
|
218
|
voc = vocabularyService.find(vocUUID);
|
219
|
Set<DefinedTerm> terms = voc.getTerms();
|
220
|
DefinedTermBase termBase =terms.iterator().next();
|
221
|
UUID termUUID = termBase.getUuid();
|
222
|
termService.delete(termBase, null);
|
223
|
//commitAndStartNewTransaction(tableNames);
|
224
|
termBase = termService.load(termUUID);
|
225
|
assertNull(termBase);
|
226
|
|
227
|
|
228
|
//TermVocabulary<DefinedTerm> voc = TermVocabulary.NewInstance(TermType.Feature, "TestFeatures", null, null, null);
|
229
|
voc.addTerm(DefinedTerm.NewDnaMarkerInstance("test", "marker", "t"));
|
230
|
vocUUID = vocabularyService.save(voc).getUuid();
|
231
|
|
232
|
voc = vocabularyService.find(vocUUID);
|
233
|
terms = voc.getTerms();
|
234
|
termBase =terms.iterator().next();
|
235
|
termUUID = termBase.getUuid();
|
236
|
termBase = termService.load(termUUID);
|
237
|
BotanicalName testName = BotanicalName.NewInstance(Rank.SPECIES());
|
238
|
Taxon testTaxon = Taxon.NewInstance(testName,null);
|
239
|
testTaxon.addIdentifier("Test", (DefinedTerm) termBase);
|
240
|
taxonService.save(testTaxon);
|
241
|
termService.delete(termBase, null);
|
242
|
//commitAndStartNewTransaction(tableNames);
|
243
|
termBase = termService.load(termUUID);
|
244
|
assertNotNull(termBase);
|
245
|
}
|
246
|
|
247
|
/* (non-Javadoc)
|
248
|
* @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
|
249
|
*/
|
250
|
@Override
|
251
|
public void createTestDataSet() throws FileNotFoundException {
|
252
|
// TODO Auto-generated method stub
|
253
|
|
254
|
}
|
255
|
|
256
|
}
|