2 * Copyright (C) 2009 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.
9 package eu
.etaxonomy
.cdm
.api
.service
;
11 import java
.io
.FileNotFoundException
;
12 import java
.util
.List
;
13 import java
.util
.UUID
;
15 import org
.apache
.logging
.log4j
.LogManager
;
16 import org
.apache
.logging
.log4j
.Logger
;
17 import org
.junit
.Assert
;
18 import org
.junit
.Test
;
19 import org
.unitils
.database
.annotations
.Transactional
;
20 import org
.unitils
.database
.util
.TransactionMode
;
21 import org
.unitils
.dbunit
.annotation
.DataSet
;
22 import org
.unitils
.dbunit
.annotation
.ExpectedDataSet
;
23 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
25 import eu
.etaxonomy
.cdm
.api
.service
.dto
.IdentifiedEntityDTO
;
26 import eu
.etaxonomy
.cdm
.api
.service
.dto
.MarkedEntityDTO
;
27 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.Identifier
;
30 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
31 import eu
.etaxonomy
.cdm
.model
.name
.IBotanicalName
;
32 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
33 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
34 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameFactory
;
35 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymType
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
40 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
41 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
42 import eu
.etaxonomy
.cdm
.model
.term
.IdentifierType
;
43 import eu
.etaxonomy
.cdm
.model
.term
.TermVocabulary
;
44 import eu
.etaxonomy
.cdm
.model
.term
.VocabularyEnum
;
45 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
46 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
47 import eu
.etaxonomy
.cdm
.persistence
.query
.TaxonTitleType
;
48 import eu
.etaxonomy
.cdm
.test
.integration
.CdmTransactionalIntegrationTest
;
53 @Transactional(TransactionMode
.DISABLED
)
54 public class IdentifiableServiceBaseTest
extends CdmTransactionalIntegrationTest
{
56 @SuppressWarnings("unused")
57 private static final Logger logger
= LogManager
.getLogger();
60 private INameService nameService
;
63 private ITermService termService
;
66 private IVocabularyService vocService
;
69 private ITaxonService taxonService
;
72 private IReferenceService referenceService
;
75 private IClassificationService classificationService
;
77 /****************** TESTS *****************************/
80 public final void voidTestSeriveExists(){
81 Assert
.assertNotNull("Service shoulb be initialized", nameService
);
87 public final void testUpdateTitleCache() {
88 Assert
.assertEquals("There should be 5 TaxonNames in the data set", 5, nameService
.count(TaxonName
.class));
89 Class
<TaxonName
> clazz
= TaxonName
.class;
91 nameService
.updateCaches(clazz
, stepSize
, null, null);
93 // commitAndStartNewTransaction(new String[]{"TaxonName","TaxonName_AUD"});
97 @DataSet(value
="IdentifiableServiceBaseTest.testFindByIdentifierOrMarker.xml")
98 public final void testFindByIdentifier(){
99 UUID uuidIdentifierType1
= UUID
.fromString("02bb62db-a229-4eeb-83e6-a9a093943d5e");
100 UUID uuidIdentifierType2
= UUID
.fromString("ef6e960f-5289-456c-b25c-cff7f4de2f63");
102 IdentifierType it1
= (IdentifierType
)termService
.find(uuidIdentifierType1
);
103 Assert
.assertNotNull("identifier type must not be null", it1
);
105 boolean includeEntity
= true;
106 Pager
<IdentifiedEntityDTO
<Taxon
>> taxonPager
= taxonService
.findByIdentifier(Taxon
.class, "ext-1234", it1
, null, includeEntity
, null, null, null);
107 Assert
.assertTrue("Result should not be empty", taxonPager
.getCount() == 1);
108 IdentifiedEntityDTO
<Taxon
>.CdmEntity entity
= taxonPager
.getRecords().get(0).getCdmEntity();
109 Taxon taxon
= entity
.getEntity();
110 Assert
.assertEquals(UUID
.fromString("888cded1-cadc-48de-8629-e32927919879"), taxon
.getUuid());
111 Assert
.assertEquals(UUID
.fromString("888cded1-cadc-48de-8629-e32927919879"), entity
.getUuid());
112 Assert
.assertEquals("Taxon should have 1 identifier", 1, taxon
.getIdentifiers().size());
113 Identifier identifier
= taxon
.getIdentifiers().get(0);
114 IdentifierType type
= CdmBase
.deproxy(identifier
.getType(), IdentifierType
.class);
115 Assert
.assertEquals(uuidIdentifierType1
, type
.getUuid());
117 Pager
<IdentifiedEntityDTO
<TaxonName
>> names
= nameService
.findByIdentifier(
118 TaxonName
.class, "ext-1234", null, null, includeEntity
, null, null, null);
119 Assert
.assertTrue("Identifier does not exist for TaxonName", names
.getCount() == 0);
121 taxonPager
= taxonService
.findByIdentifier(null, "ext-1234", null, null, includeEntity
, null, null, null);
122 Assert
.assertEquals("Result size for 'ext-1234' should be 1", 1, taxonPager
.getRecords().size());
124 taxonPager
= taxonService
.findByIdentifier(Taxon
.class, null, null, null, includeEntity
, null, null, null);
125 Assert
.assertEquals("Result should not be empty", 2 , taxonPager
.getRecords().size());
128 includeEntity
= false;
129 taxonPager
= taxonService
.findByIdentifier(Taxon
.class, "ext-1234", it1
, null, includeEntity
, null, null, null);
130 entity
= taxonPager
.getRecords().get(0).getCdmEntity();
131 Assert
.assertNull("Taxon must not be returned with includeEntity = false", entity
.getEntity());
135 includeEntity
= false;
136 MatchMode matchmode
= null;
137 taxonPager
= taxonService
.findByIdentifier(Taxon
.class, "123", null, matchmode
, includeEntity
, null, null, null);
138 Assert
.assertTrue("Result size for '123' should be 0", taxonPager
.getCount() == 0);
140 taxonPager
= taxonService
.findByIdentifier(Taxon
.class, "123", null, MatchMode
.EXACT
, includeEntity
, null, null, null);
141 Assert
.assertTrue("Result size for '123' should be 0", taxonPager
.getCount() == 0);
143 taxonPager
= taxonService
.findByIdentifier(Taxon
.class, "123", null, MatchMode
.ANYWHERE
, includeEntity
, null, null, null);
144 Assert
.assertTrue("Result size for '123' should be 1", taxonPager
.getCount() == 1);
146 taxonPager
= taxonService
.findByIdentifier(Taxon
.class, "123", null, MatchMode
.BEGINNING
, includeEntity
, null, null, null);
147 Assert
.assertTrue("Result size for '123' should be 0", taxonPager
.getCount() == 0);
149 taxonPager
= taxonService
.findByIdentifier(Taxon
.class, "ext", null, MatchMode
.BEGINNING
, includeEntity
, null, null, null);
150 Assert
.assertTrue("Result size for 'ext' should be 1", taxonPager
.getCount() == 2);
153 taxonPager
= taxonService
.findByIdentifier(null, "ext", null, MatchMode
.BEGINNING
, includeEntity
, null, null, null);
154 Assert
.assertEquals("Total result size for starts with 'ext' should be 4", 4, taxonPager
.getRecords().size());
155 taxonPager
= taxonService
.findByIdentifier(null, "ext", null, MatchMode
.BEGINNING
, includeEntity
, 2, 1, null);
156 Assert
.assertEquals("Total result size for starts with 'ext' should be 4", Long
.valueOf(4), taxonPager
.getCount());
157 Assert
.assertEquals("Result size for starts with 'ext' second page should be 2", Integer
.valueOf(2), taxonPager
.getPageSize());
158 Assert
.assertEquals("The third taxon (first on second page) should be ext-syn1", "ext-syn1", taxonPager
.getRecords().get(0).getIdentifier().getIdentifier());
160 taxonPager
= taxonService
.findByIdentifier(Taxon
.class, "ext", null, MatchMode
.BEGINNING
, includeEntity
, null, null, null);
161 Assert
.assertTrue("Result size for 'ext' should be 2", taxonPager
.getCount() == 2);
166 @DataSet(value
="IdentifiableServiceBaseTest.testFindByIdentifierOrMarker.xml")
167 public final void testFindByIdentifierClassification(){
168 //classification Filter
169 Classification classification
= classificationService
.find(5000);
170 TaxonNode rootNode
= classification
.getRootNode();
171 Pager
<IdentifiedEntityDTO
<Taxon
>> taxonPager
= taxonService
.findByIdentifier(Taxon
.class, "ext-1234", null, rootNode
, MatchMode
.EXACT
, false, null, null, null);
172 Assert
.assertEquals("Result size for 'ext' should be 1", Long
.valueOf(1), taxonPager
.getCount());
173 Assert
.assertEquals("Result size for 'ext' should be 1", 1, taxonPager
.getRecords().size());
175 Pager
<IdentifiedEntityDTO
<Taxon
>> taxPager
= taxonService
.findByIdentifier(Taxon
.class, "ext-cache1", null, rootNode
, MatchMode
.EXACT
, false, null, null, null);
176 Assert
.assertEquals("Result size for 'ext' should be 0", Long
.valueOf(0), taxPager
.getCount());
177 Assert
.assertEquals("Result size for 'ext' should be 0", 0, taxPager
.getRecords().size());
179 rootNode
= null; //check against missing filter
180 taxPager
= taxonService
.findByIdentifier(Taxon
.class, "ext-cache1", null, rootNode
, MatchMode
.EXACT
, false, null, null, null);
181 Assert
.assertEquals("Result size for 'ext-cache1' without filter should be 1", Long
.valueOf(1), taxPager
.getCount());
182 Assert
.assertEquals("Result size for 'ext-cache1' without filter should be 1", 1, taxPager
.getRecords().size());
185 rootNode
= classification
.getRootNode();
186 Pager
<IdentifiedEntityDTO
<TaxonBase
>> tbPager
= taxonService
.findByIdentifier(TaxonBase
.class, "ext-1234", null, rootNode
, MatchMode
.EXACT
, false, null, null, null);
187 Assert
.assertEquals("Result size for 'ext' should be 1", Long
.valueOf(1), tbPager
.getCount());
188 Assert
.assertEquals("Result size for 'ext' should be 1", 1, tbPager
.getRecords().size());
190 tbPager
= taxonService
.findByIdentifier(TaxonBase
.class, "ext-cache1", null, rootNode
, MatchMode
.EXACT
, false, null, null, null);
191 Assert
.assertEquals("Result size for 'ext' should be 0", Long
.valueOf(0), tbPager
.getCount());
192 Assert
.assertEquals("Result size for 'ext' should be 0", 0, tbPager
.getRecords().size());
195 Pager
<IdentifiedEntityDTO
<Synonym
>> synPager
= taxonService
.findByIdentifier(Synonym
.class, "ext-syn", null, rootNode
, MatchMode
.BEGINNING
, false, null, null, null);
196 Assert
.assertEquals("1 Synonym should be linked to the according classification", Long
.valueOf(1), synPager
.getCount());
197 Assert
.assertEquals("1 Synonym should be linked to the according classification", 1, synPager
.getRecords().size());
202 @DataSet(value
="IdentifiableServiceBaseTest.testFindByIdentifierOrMarker.xml")
203 public final void testListByIdentifier(){
204 //classification Filter
206 List
<IdentifiedEntityDTO
<Taxon
>> taxonPager
= taxonService
.listByIdentifier(Taxon
.class, "ext-1234", null, MatchMode
.EXACT
, false, null, null);//.listByIdentifier(Taxon.class, "ext-1234", null, rootNode, MatchMode.EXACT, false, null, null);
207 Assert
.assertEquals("Result size for 'ext' should be 1", 1, taxonPager
.size());
208 // Assert.assertEquals("Result size for 'ext' should be 1", 1, taxonPager.getRecords().size());
209 taxonPager
= taxonService
.listByIdentifier(Taxon
.class, "ext-1234", null, MatchMode
.EXACT
, true, null, null);//.listByIdentifier(Taxon.class, "ext-1234", null, rootNode, MatchMode.EXACT, false, null, null);
210 Assert
.assertEquals("Result size for 'ext' should be 1", 1, taxonPager
.size());
211 IdentifiedEntityDTO
<Taxon
> identifierDTO
= taxonPager
.get(0);
212 Assert
.assertTrue(identifierDTO
.getCdmEntity() instanceof UuidAndTitleCache
);
214 List
<IdentifiedEntityDTO
<Taxon
>> taxPager
= taxonService
.listByIdentifier(Taxon
.class, "ext-cache1", null, MatchMode
.EXACT
, false, null, null);
215 Assert
.assertEquals("Result size for 'ext' should be 1", 1, taxPager
.size());
217 List
<IdentifiedEntityDTO
<TaxonBase
>> tbPager
= taxonService
.listByIdentifier(TaxonBase
.class, "ext-1234", null, MatchMode
.EXACT
, false, null, null);
218 Assert
.assertEquals("Result size for 'ext' should be 1",1, tbPager
.size());
222 List
<IdentifiedEntityDTO
<Synonym
>> synPager
= taxonService
.listByIdentifier(Synonym
.class, "ext-syn", null, MatchMode
.BEGINNING
, false, null, null);
223 Assert
.assertEquals("There should be 2 synonyms with identifier ext-syn", 2, synPager
.size());
225 List
<IdentifiedEntityDTO
<Reference
>> referenceList
= referenceService
.listByIdentifier(Reference
.class, "123", null, MatchMode
.EXACT
, false, null, null);//.listByIdentifier(Taxon.class, "ext-1234", null, rootNode, MatchMode.EXACT, false, null, null);
226 Assert
.assertEquals("Result size for 'ext' should be 1", 1, taxonPager
.size());
228 referenceList
= referenceService
.listByIdentifier(Reference
.class, "123", null, MatchMode
.EXACT
, true, null, null);//.listByIdentifier(Taxon.class, "ext-1234", null, rootNode, MatchMode.EXACT, false, null, null);
229 Assert
.assertEquals("Result size for 'ext' should be 1", 1, referenceList
.size());
230 IdentifiedEntityDTO
<Reference
> identifierDTOReference
= referenceList
.get(0);
231 Assert
.assertTrue(identifierDTO
.getCdmEntity() instanceof UuidAndTitleCache
);
235 @DataSet(value
="IdentifiableServiceBaseTest.testFindByIdentifierOrMarker.xml")
236 public final void testFindByMarker(){
237 //classification Filter
238 Classification classification
= classificationService
.find(5000);
239 TaxonNode rootNode
= classification
.getRootNode();
240 Boolean markerValue
= true;
242 UUID uuidMarkerTypeCompleted
= MarkerType
.uuidComplete
;
243 UUID uuidMarkerTypeDoubtful
= UUID
.fromString("b51325c8-05fe-421a-832b-d86fc249ef6e");
245 MarkerType markerType1
= (MarkerType
)termService
.find(uuidMarkerTypeCompleted
);
246 MarkerType noMarkerType
= null;
247 MarkerType markerType2
= (MarkerType
)termService
.find(uuidMarkerTypeDoubtful
);
248 Assert
.assertNotNull(markerType2
);
250 TaxonTitleType titleType
= TaxonTitleType
.TAXON
;
252 MarkerType markerType
= markerType1
;
253 Pager
<MarkedEntityDTO
<Taxon
>> taxonPager
= taxonService
.findByMarker(Taxon
.class, markerType
, markerValue
,
254 rootNode
, true, titleType
, null, null, null);
255 Assert
.assertEquals("Result size for 'marker1=true' should be 1", Long
.valueOf(1), taxonPager
.getCount());
256 Assert
.assertEquals("Result size for 'marker1=true' should be 1", 1, taxonPager
.getRecords().size());
257 MarkedEntityDTO
<Taxon
> dto
= taxonPager
.getRecords().get(0);
258 MarkedEntityDTO
<Taxon
>.Marker marker
= dto
.getMarker();
259 Assert
.assertTrue("Flag must be true", marker
.getFlag());
260 Assert
.assertEquals("Flag must be true", uuidMarkerTypeCompleted
, marker
.getTypeUuid());
261 Assert
.assertNotNull("the CDM entity in the dto must not be empty if includeEntity=true", dto
.getCdmEntity().getEntity());
262 Assert
.assertEquals(5000, dto
.getCdmEntity().getEntity().getId());
266 taxonPager
= taxonService
.findByMarker(Taxon
.class, markerType
, markerValue
, rootNode
, false, titleType
, null, null, null);
267 Assert
.assertEquals("Result size for 'marker1=false' should be 0", Long
.valueOf(0), taxonPager
.getCount());
270 markerType
= noMarkerType
;
271 taxonPager
= taxonService
.findByMarker(Taxon
.class, markerType
, markerValue
, rootNode
, false, titleType
, null, null, null);
272 Assert
.assertEquals("Result size for not existing marker type should be 0", Long
.valueOf(0), taxonPager
.getCount());
274 markerType
= markerType2
;
275 taxonPager
= taxonService
.findByMarker(Taxon
.class, markerType
, markerValue
, rootNode
, false, titleType
, null, null, null);
276 Assert
.assertEquals("Result size for markerType2 should be 0", Long
.valueOf(0), taxonPager
.getCount());
279 markerType
= markerType1
;
280 taxonPager
= taxonService
.findByMarker(Taxon
.class, markerType
, markerValue
, rootNode
, false, titleType
, null, null, null);
281 Assert
.assertEquals("Result size for no subtree should be 2", Long
.valueOf(2), taxonPager
.getCount());
283 Pager
<MarkedEntityDTO
<TaxonBase
>> taxonBasePager
= taxonService
.findByMarker(TaxonBase
.class, markerType
, markerValue
, rootNode
, false, titleType
, null, null, null);
284 Assert
.assertEquals("Result size for taxa and synonyms without subtree filter with flag = true should be 3", Long
.valueOf(3), taxonBasePager
.getCount());
287 taxonBasePager
= taxonService
.findByMarker(TaxonBase
.class, markerType
, markerValue
, rootNode
, false, titleType
, null, null, null);
288 Assert
.assertEquals("Result size for taxa and synonyms without subtree filter with any flag value should be 4", Long
.valueOf(4), taxonBasePager
.getCount());
291 Pager
<MarkedEntityDTO
<TaxonName
>> namePager
= nameService
.findByMarker(TaxonName
.class, markerType
, markerValue
, false, null, null, null);
292 Assert
.assertEquals("Result size for names with flag = true should be 1", Long
.valueOf(1), namePager
.getCount());
300 public void createTestDataSet() throws FileNotFoundException
{
301 TermVocabulary
<IdentifierType
> voc
= vocService
.find(VocabularyEnum
.IdentifierType
.getUuid());
303 IdentifierType identifierType1
= IdentifierType
.NewInstance(null, "identifierType1", null);
304 voc
.addTerm(identifierType1
);
305 termService
.save(identifierType1
);
306 IdentifierType identifierType2
= IdentifierType
.NewInstance(null, "identifierType2", null);
307 voc
.addTerm(identifierType2
);
308 termService
.save(identifierType2
);
311 IBotanicalName name
= TaxonNameFactory
.NewBotanicalInstance(Rank
.SPECIES());
312 Taxon tb
= Taxon
.NewInstance(name
, null);
313 tb
.addIdentifier("ext-1234", identifierType1
);
314 name
.addIdentifier("ext-name12", identifierType2
);
315 taxonService
.saveOrUpdate(tb
);
317 Taxon tb2
= Taxon
.NewInstance(null, null);
318 tb2
.setTitleCache("Cached taxon", true);
319 tb2
.addIdentifier("ext-cache1", identifierType2
);
320 taxonService
.saveOrUpdate(tb2
);
322 Classification classification
= Classification
.NewInstance("My classification");
323 classification
.addChildTaxon(tb
, null, null);
324 classificationService
.saveOrUpdate(classification
);
326 tb2
.addSynonymName(null, SynonymType
.HOMOTYPIC_SYNONYM_OF
);
328 commitAndStartNewTransaction(null);
330 // this will write flat xml file to the same package in the test resources
331 // the test file is named after the test class like: TestClassName.xml
332 writeDbUnitDataSetFile(new String
[] {
333 "TAXONBASE", "TAXONNAME","IDENTIFIER","TAXONBASE_IDENTIFIER",
334 "TAXONNAME_IDENTIFIER",
336 "CLASSIFICATION", "TAXONNODE",