Project

General

Profile

Download (18.1 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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 java.io.FileNotFoundException;
13
import java.util.List;
14
import java.util.UUID;
15

    
16
import org.apache.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;
24

    
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.DefinedTerm;
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;
49

    
50
/**
51
 * @author a.mueller
52
 *
53
 */
54
@Transactional(TransactionMode.DISABLED)
55
public class IdentifiableServiceBaseTest extends CdmTransactionalIntegrationTest {
56

    
57
    @SuppressWarnings("unused")
58
	private static final Logger logger = Logger.getLogger(IdentifiableServiceBaseTest.class);
59

    
60
	@SpringBeanByType
61
	private INameService nameService;
62

    
63
	@SpringBeanByType
64
	private ITermService termService;
65

    
66
	@SpringBeanByType
67
	private IVocabularyService vocService;
68

    
69
	@SpringBeanByType
70
	private ITaxonService taxonService;
71

    
72
	@SpringBeanByType
73
    private IReferenceService referenceService;
74

    
75
	@SpringBeanByType
76
	private IClassificationService classificationService;
77

    
78
/****************** TESTS *****************************/
79

    
80
	@Test
81
	public final void voidTestSeriveExists(){
82
		Assert.assertNotNull("Service shoulb be initialized", nameService);
83
	}
84

    
85

    
86
	@Test
87
	@DataSet
88
	@ExpectedDataSet
89
	public final void testUpdateTitleCache() {
90
		Assert.assertEquals("There should be 5 TaxonNames in the data set", 5, nameService.count(TaxonName.class));
91
		Class<TaxonName> clazz = TaxonName.class;
92
		int stepSize = 2;
93
		nameService.updateCaches(clazz, stepSize, null, null);
94
		commit();
95
//		commitAndStartNewTransaction(new String[]{"TaxonName","TaxonName_AUD"});
96
	}
97

    
98

    
99
	@Test
100
	@DataSet(value="IdentifiableServiceBaseTest.testFindByIdentifierOrMarker.xml")
101
	public final void testFindByIdentifier(){
102
		UUID uuidIdentifierType1 = UUID.fromString("02bb62db-a229-4eeb-83e6-a9a093943d5e");
103
		UUID uuidIdentifierType2 = UUID.fromString("ef6e960f-5289-456c-b25c-cff7f4de2f63");
104

    
105

    
106
		DefinedTerm it1 = (DefinedTerm)termService.find(uuidIdentifierType1);
107
		Assert.assertNotNull("identifier type must not be null", it1);
108

    
109
		boolean includeEntity = true;
110
		Pager<IdentifiedEntityDTO<Taxon>> taxonPager = taxonService.findByIdentifier(Taxon.class, "ext-1234", it1, null, includeEntity, null, null, null);
111
		Assert.assertTrue("Result should not be empty", taxonPager.getCount() == 1);
112
		IdentifiedEntityDTO<Taxon>.CdmEntity entity = taxonPager.getRecords().get(0).getCdmEntity();
113
		Taxon taxon = entity.getEntity();
114
		Assert.assertEquals(UUID.fromString("888cded1-cadc-48de-8629-e32927919879"), taxon.getUuid());
115
		Assert.assertEquals(UUID.fromString("888cded1-cadc-48de-8629-e32927919879"), entity.getUuid());
116
		Assert.assertEquals("Taxon should have 1 identifier", 1, taxon.getIdentifiers().size());
117
		Identifier<?> identifier = taxon.getIdentifiers().get(0);
118
		DefinedTerm type = CdmBase.deproxy(identifier.getType(), DefinedTerm.class);
119
		Assert.assertEquals(uuidIdentifierType1, type.getUuid());
120

    
121
		Pager<IdentifiedEntityDTO<TaxonName>> names = nameService.findByIdentifier(
122
				TaxonName.class, "ext-1234", null, null, includeEntity, null, null, null);
123
		Assert.assertTrue("Identifier does not exist for TaxonName", names.getCount() == 0);
124

    
125
		taxonPager = taxonService.findByIdentifier(null, "ext-1234", null, null, includeEntity, null, null, null);
126
		Assert.assertEquals("Result size for 'ext-1234' should be 1", 1, taxonPager.getRecords().size());
127

    
128
		taxonPager = taxonService.findByIdentifier(Taxon.class, null, null, null, includeEntity, null, null, null);
129
		Assert.assertEquals("Result should not be empty", 2 , taxonPager.getRecords().size());
130

    
131
		//includeEntity
132
		includeEntity = false;
133
		taxonPager = taxonService.findByIdentifier(Taxon.class, "ext-1234", it1, null, includeEntity, null, null, null);
134
		entity = taxonPager.getRecords().get(0).getCdmEntity();
135
		Assert.assertNull("Taxon must not be returned with includeEntity = false", entity.getEntity());
136

    
137

    
138

    
139
		//Matchmode
140
		includeEntity = false;
141
		MatchMode matchmode = null;
142
		taxonPager = taxonService.findByIdentifier(Taxon.class, "123", null, matchmode, includeEntity, null, null, null);
143
		Assert.assertTrue("Result size for '123' should be 0", taxonPager.getCount() == 0);
144

    
145
		taxonPager = taxonService.findByIdentifier(Taxon.class, "123", null, MatchMode.EXACT, includeEntity, null, null, null);
146
		Assert.assertTrue("Result size for '123' should be 0", taxonPager.getCount() == 0);
147

    
148
		taxonPager = taxonService.findByIdentifier(Taxon.class, "123", null, MatchMode.ANYWHERE, includeEntity, null, null, null);
149
		Assert.assertTrue("Result size for '123' should be 1", taxonPager.getCount() == 1);
150

    
151
		taxonPager = taxonService.findByIdentifier(Taxon.class, "123", null, MatchMode.BEGINNING, includeEntity, null, null, null);
152
		Assert.assertTrue("Result size for '123' should be 0", taxonPager.getCount() == 0);
153

    
154
		taxonPager = taxonService.findByIdentifier(Taxon.class, "ext", null, MatchMode.BEGINNING, includeEntity, null, null, null);
155
		Assert.assertTrue("Result size for 'ext' should be 1", taxonPager.getCount() == 2);
156

    
157
		//Paging
158
		taxonPager = taxonService.findByIdentifier(null, "ext", null, MatchMode.BEGINNING, includeEntity, null, null, null);
159
		Assert.assertEquals("Total result size for starts with 'ext' should be 4", 4, taxonPager.getRecords().size());
160
		taxonPager = taxonService.findByIdentifier(null, "ext", null, MatchMode.BEGINNING, includeEntity, 2, 1, null);
161
		Assert.assertEquals("Total result size for starts with 'ext' should be 4", Long.valueOf(4), taxonPager.getCount());
162
		Assert.assertEquals("Result size for starts with 'ext' second page should be 2", Integer.valueOf(2), taxonPager.getPageSize());
163
		Assert.assertEquals("The third taxon (first on second page) should be ext-syn1", "ext-syn1", taxonPager.getRecords().get(0).getIdentifier().getIdentifier());
164

    
165
		taxonPager = taxonService.findByIdentifier(Taxon.class, "ext", null, MatchMode.BEGINNING, includeEntity, null, null, null);
166
		Assert.assertTrue("Result size for 'ext' should be 2", taxonPager.getCount() == 2);
167

    
168
	}
169

    
170
	@Test
171
	@DataSet(value="IdentifiableServiceBaseTest.testFindByIdentifierOrMarker.xml")
172
	public final void testFindByIdentifierClassification(){
173
		//classification Filter
174
		Classification classification = classificationService.find(5000);
175
		TaxonNode rootNode = classification.getRootNode();
176
		Pager<IdentifiedEntityDTO<Taxon>> taxonPager = taxonService.findByIdentifier(Taxon.class, "ext-1234", null, rootNode, MatchMode.EXACT, false, null, null, null);
177
		Assert.assertEquals("Result size for 'ext' should be 1", Long.valueOf(1), taxonPager.getCount());
178
		Assert.assertEquals("Result size for 'ext' should be 1", 1, taxonPager.getRecords().size());
179

    
180
		Pager<IdentifiedEntityDTO<Taxon>> taxPager = taxonService.findByIdentifier(Taxon.class, "ext-cache1", null, rootNode, MatchMode.EXACT, false, null, null, null);
181
		Assert.assertEquals("Result size for 'ext' should be 0", Long.valueOf(0), taxPager.getCount());
182
		Assert.assertEquals("Result size for 'ext' should be 0", 0, taxPager.getRecords().size());
183

    
184
		rootNode = null;  //check against missing filter
185
		taxPager = taxonService.findByIdentifier(Taxon.class, "ext-cache1", null, rootNode, MatchMode.EXACT, false, null, null, null);
186
		Assert.assertEquals("Result size for 'ext-cache1' without filter should be 1", Long.valueOf(1), taxPager.getCount());
187
		Assert.assertEquals("Result size for 'ext-cache1' without filter should be 1", 1, taxPager.getRecords().size());
188

    
189
		//TaxonBase
190
		rootNode = classification.getRootNode();
191
		Pager<IdentifiedEntityDTO<TaxonBase>> tbPager = taxonService.findByIdentifier(TaxonBase.class, "ext-1234", null, rootNode, MatchMode.EXACT, false, null, null, null);
192
		Assert.assertEquals("Result size for 'ext' should be 1", Long.valueOf(1), tbPager.getCount());
193
		Assert.assertEquals("Result size for 'ext' should be 1", 1, tbPager.getRecords().size());
194

    
195
		tbPager = taxonService.findByIdentifier(TaxonBase.class, "ext-cache1", null, rootNode, MatchMode.EXACT, false, null, null, null);
196
		Assert.assertEquals("Result size for 'ext' should be 0", Long.valueOf(0), tbPager.getCount());
197
		Assert.assertEquals("Result size for 'ext' should be 0", 0, tbPager.getRecords().size());
198

    
199
		//Synonym
200
		Pager<IdentifiedEntityDTO<Synonym>> synPager = taxonService.findByIdentifier(Synonym.class, "ext-syn", null, rootNode, MatchMode.BEGINNING, false, null, null, null);
201
		Assert.assertEquals("1 Synonym should be linked to the according classification", Long.valueOf(1), synPager.getCount());
202
		Assert.assertEquals("1 Synonym should be linked to the according classification", 1, synPager.getRecords().size());
203

    
204
	}
205

    
206
	@Test
207
    @DataSet(value="IdentifiableServiceBaseTest.testFindByIdentifierOrMarker.xml")
208
    public final void testListByIdentifier(){
209
        //classification Filter
210

    
211
        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);
212
        Assert.assertEquals("Result size for 'ext' should be 1", 1, taxonPager.size());
213
     //   Assert.assertEquals("Result size for 'ext' should be 1", 1, taxonPager.getRecords().size());
214
        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);
215
        Assert.assertEquals("Result size for 'ext' should be 1", 1, taxonPager.size());
216
        IdentifiedEntityDTO<Taxon> identifierDTO = taxonPager.get(0);
217
        Assert.assertTrue(identifierDTO.getCdmEntity() instanceof UuidAndTitleCache);
218

    
219
        List<IdentifiedEntityDTO<Taxon>> taxPager = taxonService.listByIdentifier(Taxon.class, "ext-cache1", null, MatchMode.EXACT, false, null, null);
220
        Assert.assertEquals("Result size for 'ext' should be 1", 1, taxPager.size());
221

    
222
        List<IdentifiedEntityDTO<TaxonBase>> tbPager = taxonService.listByIdentifier(TaxonBase.class, "ext-1234", null, MatchMode.EXACT, false, null, null);
223
        Assert.assertEquals("Result size for 'ext' should be 1",1, tbPager.size());
224

    
225

    
226
        //Synonym
227
        List<IdentifiedEntityDTO<Synonym>> synPager = taxonService.listByIdentifier(Synonym.class, "ext-syn", null,  MatchMode.BEGINNING, false, null, null);
228
        Assert.assertEquals("There should be 2 synonyms with identifier ext-syn", 2, synPager.size());
229

    
230
        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);
231
        Assert.assertEquals("Result size for 'ext' should be 1", 1, taxonPager.size());
232

    
233
        referenceList = referenceService.listByIdentifier(Reference.class, "123", null, MatchMode.EXACT, true, null, null);//.listByIdentifier(Taxon.class, "ext-1234", null, rootNode, MatchMode.EXACT, false, null, null);
234
        Assert.assertEquals("Result size for 'ext' should be 1", 1, referenceList.size());
235
        IdentifiedEntityDTO<Reference> identifierDTOReference = referenceList.get(0);
236
        Assert.assertTrue(identifierDTO.getCdmEntity() instanceof UuidAndTitleCache);
237
	}
238

    
239
	@Test
240
    @DataSet(value="IdentifiableServiceBaseTest.testFindByIdentifierOrMarker.xml")
241
    public final void testFindByMarker(){
242
        //classification Filter
243
        Classification classification = classificationService.find(5000);
244
        TaxonNode rootNode = classification.getRootNode();
245
        Boolean markerValue = true;
246

    
247
        UUID uuidMarkerTypeCompleted = MarkerType.uuidComplete;
248
        UUID uuidMarkerTypeDoubtful = UUID.fromString("b51325c8-05fe-421a-832b-d86fc249ef6e");
249

    
250
        MarkerType markerType1 = (MarkerType)termService.find(uuidMarkerTypeCompleted);
251
        MarkerType noMarkerType = null;
252
        MarkerType markerType2 = (MarkerType)termService.find(uuidMarkerTypeDoubtful);
253
        Assert.assertNotNull(markerType2);
254

    
255
        TaxonTitleType titleType = TaxonTitleType.TAXON;
256

    
257
        MarkerType markerType = markerType1;
258
        Pager<MarkedEntityDTO<Taxon>> taxonPager = taxonService.findByMarker(Taxon.class, markerType, markerValue,
259
                rootNode, true, titleType, null, null, null);
260
        Assert.assertEquals("Result size for 'marker1=true' should be 1", Long.valueOf(1), taxonPager.getCount());
261
        Assert.assertEquals("Result size for 'marker1=true' should be 1", 1, taxonPager.getRecords().size());
262
        MarkedEntityDTO<Taxon> dto = taxonPager.getRecords().get(0);
263
        MarkedEntityDTO<Taxon>.Marker marker = dto.getMarker();
264
        Assert.assertTrue("Flag must be true", marker.getFlag());
265
        Assert.assertEquals("Flag must be true", uuidMarkerTypeCompleted, marker.getTypeUuid());
266
        Assert.assertNotNull("the CDM entity in the dto must not be empty if includeEntity=true", dto.getCdmEntity().getEntity());
267
        Assert.assertEquals(5000, dto.getCdmEntity().getEntity().getId());
268

    
269

    
270
        markerValue = false;
271
        taxonPager = taxonService.findByMarker(Taxon.class, markerType, markerValue, rootNode, false, titleType, null, null, null);
272
        Assert.assertEquals("Result size for 'marker1=false' should be 0", Long.valueOf(0), taxonPager.getCount());
273

    
274
        markerValue = true;
275
        markerType = noMarkerType;
276
        taxonPager = taxonService.findByMarker(Taxon.class, markerType, markerValue, rootNode, false, titleType, null, null, null);
277
        Assert.assertEquals("Result size for not existing marker type should be 0", Long.valueOf(0), taxonPager.getCount());
278

    
279
        markerType = markerType2;
280
        taxonPager = taxonService.findByMarker(Taxon.class, markerType, markerValue, rootNode, false, titleType, null, null, null);
281
        Assert.assertEquals("Result size for markerType2 should be 0", Long.valueOf(0), taxonPager.getCount());
282

    
283
        rootNode = null;
284
        markerType = markerType1;
285
        taxonPager = taxonService.findByMarker(Taxon.class, markerType, markerValue, rootNode, false, titleType, null, null, null);
286
        Assert.assertEquals("Result size for no subtree should be 2", Long.valueOf(2), taxonPager.getCount());
287

    
288
        Pager<MarkedEntityDTO<TaxonBase>> taxonBasePager = taxonService.findByMarker(TaxonBase.class, markerType, markerValue, rootNode, false, titleType, null, null, null);
289
        Assert.assertEquals("Result size for taxa and synonyms without subtree filter with flag = true should be 3", Long.valueOf(3), taxonBasePager.getCount());
290

    
291
        markerValue = null;
292
        taxonBasePager = taxonService.findByMarker(TaxonBase.class, markerType, markerValue, rootNode, false, titleType, null, null, null);
293
        Assert.assertEquals("Result size for taxa and synonyms without subtree filter with any flag value should be 4", Long.valueOf(4), taxonBasePager.getCount());
294

    
295
        markerValue = true;
296
        Pager<MarkedEntityDTO<TaxonName>> namePager = nameService.findByMarker(TaxonName.class, markerType, markerValue, false, null, null, null);
297
        Assert.assertEquals("Result size for names with flag = true should be 1", Long.valueOf(1), namePager.getCount());
298

    
299
    }
300

    
301

    
302

    
303
//	@Test
304
    @Override
305
    public void createTestDataSet() throws FileNotFoundException {
306
		TermVocabulary<DefinedTerm> voc = vocService.find(VocabularyEnum.IdentifierType.getUuid());
307

    
308
		DefinedTerm identifierType1 = DefinedTerm.NewIdentifierTypeInstance(null, "identifierType1", null);
309
    	voc.addTerm(identifierType1);
310
		termService.save(identifierType1);
311
    	DefinedTerm identifierType2 = DefinedTerm.NewIdentifierTypeInstance(null, "identifierType2", null);
312
    	voc.addTerm(identifierType2);
313
		termService.save(identifierType2);
314

    
315

    
316
    	IBotanicalName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
317
        Taxon tb = Taxon.NewInstance(name, null);
318
        tb.addIdentifier("ext-1234", identifierType1);
319
        name.addIdentifier("ext-name12", identifierType2);
320
        taxonService.saveOrUpdate(tb);
321

    
322
        Taxon tb2 = Taxon.NewInstance(null, null);
323
        tb2.setTitleCache("Cached taxon", true);
324
        tb2.addIdentifier("ext-cache1", identifierType2);
325
        taxonService.saveOrUpdate(tb2);
326

    
327
        Classification classification = Classification.NewInstance("My classification");
328
        classification.addChildTaxon(tb, null, null);
329
        classificationService.saveOrUpdate(classification);
330

    
331
        tb2.addSynonymName(null, SynonymType.HOMOTYPIC_SYNONYM_OF());
332

    
333
        commitAndStartNewTransaction(null);
334

    
335
        // this will write flat xml file to the same package in the test resources
336
        // the test file is named after the test class like: TestClassName.xml
337
		writeDbUnitDataSetFile(new String[] {
338
		        "TAXONBASE", "TAXONNAME","IDENTIFIER","TAXONBASE_IDENTIFIER",
339
		        "TAXONNAME_IDENTIFIER",
340
		        "REFERENCE",
341
		        "CLASSIFICATION", "TAXONNODE",
342
		        "HOMOTYPICALGROUP",
343
		        "TERMVOCABULARY"
344
		 }, "xxxx", true);
345

    
346
    }
347

    
348

    
349
}
(11-11/39)