2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.api
.service
;
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
;
17 import java
.io
.FileNotFoundException
;
20 import java
.util
.UUID
;
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
;
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
;
50 public class TermServiceImplTest
extends CdmTransactionalIntegrationTest
{
51 @SuppressWarnings("unused")
52 private static final Logger logger
= Logger
.getLogger(TermServiceImplTest
.class);
55 private ITermService termService
;
58 private IVocabularyService vocabularyService
;
61 private ITaxonService taxonService
;
63 /* ************************* TESTS *************************************************/
66 * Test method for {@link eu.etaxonomy.cdm.api.service.TermServiceImpl#getTermByUri(java.lang.String)}.
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
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
77 public void testGetTermByUri() {
78 String uriStr
= "http://any.uri.com";
79 URI uri
= URI
.create(uriStr
);
80 DefinedTermBase
<?
> term
= termService
.getByUri(uri
);
83 // TermVocabulary<?> voc = term.getVocabulary();
84 // service.saveOrUpdate(term);
85 // List<MarkerType> list = service.listByTermClass(MarkerType.class, null, null, null, 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
);
97 * Test method for {@link eu.etaxonomy.cdm.api.service.TermServiceImpl#getContinentByUuid(java.util.UUID)}.
102 * the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
103 * and thus will cause unitils empty the according tables
105 public void testGetTermByUuid() {
107 String strUUID
= "ffca6ec8-8b88-417b-a6a0-f7c992aac19b";
108 UUID uuid
= UUID
.fromString(strUUID
);
109 DefinedTermBase
<?
> term
= termService
.find(uuid
);
111 assertEquals(Rank
.DOMAIN(), term
);
113 String strUUIDNotExist
= "00000000-8b88-417b-a6a0-f7c992aac19c";
114 UUID uuidNotExist
= UUID
.fromString(strUUIDNotExist
);
115 DefinedTermBase
<?
> termNotExist
= termService
.find(uuidNotExist
);
116 assertNull(termNotExist
);
121 * Test method for {@link eu.etaxonomy.cdm.api.service.TermServiceImpl#listTerms(java.util.UUID)}.
126 * the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
127 * and thus will cause unitils empty the according tables
129 public void testGetVocabularyUUID() {
131 String rankVocabularyUuid
= "ef0d1ce1-26e3-4e83-b47b-ca74eed40b1b";
132 UUID rankUuid
= UUID
.fromString(rankVocabularyUuid
);
133 TermVocabulary
<Rank
> voc
= vocabularyService
.find(rankUuid
);
135 assertEquals(62, voc
.getTerms().size());
137 String nullVocabularyUuid
= "00000000-26e3-4e83-b47b-ca74eed40b1b";
138 UUID nullUuid
= UUID
.fromString(nullVocabularyUuid
);
139 TermVocabulary
<Rank
> nullVoc
= vocabularyService
.find(nullUuid
);
147 * the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
148 * and thus will cause unitils empty the according tables
150 public void testGetAreaByTdwgAbbreviation(){
151 String tdwgAbbreviation
= "GER-OO";
152 NamedArea germany
= termService
.getAreaByTdwgAbbreviation(tdwgAbbreviation
);
153 assertEquals(tdwgAbbreviation
, germany
.getRepresentation(Language
.DEFAULT()).getAbbreviatedLabel());
159 * the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
160 * and thus will cause unitils empty the according tables
162 public void testListTerms() {
163 Pager
<SpecimenTypeDesignationStatus
> results
= termService
.page(SpecimenTypeDesignationStatus
.class, null,null,null,null);
164 assertNotNull("Results should not be null",results
);
169 public void testTitleCacheUpdate(){
170 String uuid
= "ae787603-3070-4298-9ca6-4cbe73378122";
171 UUID fromString
= UUID
.fromString(uuid
);
172 DefinedTermBase
<?
> termBase
= termService
.find(fromString
);
175 String expectedTitleCache
= termBase
.getLabel() + "append";
176 termBase
.setLabel(expectedTitleCache
);
178 commitAndStartNewTransaction(null);
180 termBase
= termService
.find(fromString
);
181 assertEquals("Title cache did not update after setting the label and saving the term", expectedTitleCache
, termBase
.getTitleCache());
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
);
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()));
193 commitAndStartNewTransaction(null);
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());
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")
205 public void testDeleteTerms(){
206 final String
[] tableNames
= new String
[]{
207 "DefinedTermBase","Representation"};
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){
216 DeleteResult result
= termService
.delete(term
.getRecords().get(0));
217 assertTrue(result
.isOk());
218 commitAndStartNewTransaction(tableNames
);
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();
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
);
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();
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
);
255 public void createTestDataSet() throws FileNotFoundException
{}