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