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