Project

General

Profile

Download (15.3 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.UUID;
14

    
15
import org.apache.log4j.Logger;
16
import org.junit.Assert;
17
import org.junit.Test;
18
import org.unitils.database.annotations.Transactional;
19
import org.unitils.database.util.TransactionMode;
20
import org.unitils.dbunit.annotation.DataSet;
21
import org.unitils.dbunit.annotation.ExpectedDataSet;
22
import org.unitils.spring.annotation.SpringBeanByType;
23

    
24
import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO;
25
import eu.etaxonomy.cdm.api.service.dto.MarkedEntityDTO;
26
import eu.etaxonomy.cdm.api.service.pager.Pager;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.DefinedTerm;
29
import eu.etaxonomy.cdm.model.common.Identifier;
30
import eu.etaxonomy.cdm.model.common.MarkerType;
31
import eu.etaxonomy.cdm.model.common.TermVocabulary;
32
import eu.etaxonomy.cdm.model.common.VocabularyEnum;
33
import eu.etaxonomy.cdm.model.name.IBotanicalName;
34
import eu.etaxonomy.cdm.model.name.Rank;
35
import eu.etaxonomy.cdm.model.name.TaxonName;
36
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
37
import eu.etaxonomy.cdm.model.taxon.Classification;
38
import eu.etaxonomy.cdm.model.taxon.Synonym;
39
import eu.etaxonomy.cdm.model.taxon.SynonymType;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
42
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
43
import eu.etaxonomy.cdm.persistence.query.MatchMode;
44
import eu.etaxonomy.cdm.persistence.query.TaxonTitleType;
45
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
46

    
47
/**
48
 * @author a.mueller
49
 *
50
 */
51
@Transactional(TransactionMode.DISABLED)
52
public class IdentifiableServiceBaseTest extends CdmTransactionalIntegrationTest {
53
	@SuppressWarnings("unused")
54
	private static final Logger logger = Logger.getLogger(IdentifiableServiceBaseTest.class);
55

    
56

    
57
	@SpringBeanByType
58
	private INameService nameService;
59

    
60
	@SpringBeanByType
61
	private ITermService termService;
62

    
63
	@SpringBeanByType
64
	private IVocabularyService vocService;
65

    
66
	@SpringBeanByType
67
	private ITaxonService taxonService;
68

    
69
	@SpringBeanByType
70
	private IClassificationService classificationService;
71

    
72
/****************** TESTS *****************************/
73

    
74
	@Test
75
	public final void voidTestSeriveExists(){
76
		Assert.assertNotNull("Service shoulb be initialized", nameService);
77
	}
78

    
79

    
80
	@Test
81
	@DataSet
82
	@ExpectedDataSet
83
	public final void testUpdateTitleCache() {
84
		Assert.assertEquals("There should be 5 TaxonNames in the data set", 5, nameService.count(TaxonName.class));
85
		Class clazz = TaxonName.class;
86
		int stepSize = 2;
87
		nameService.updateTitleCache(clazz, stepSize, null, null);
88
		commit();
89
//		commitAndStartNewTransaction(new String[]{"TaxonName","TaxonName_AUD"});
90
	}
91

    
92

    
93
	@Test
94
	@DataSet(value="IdentifiableServiceBaseTest.testFindByIdentifierOrMarker.xml")
95
	public final void testFindByIdentifier(){
96
		UUID uuidIdentifierType1 = UUID.fromString("02bb62db-a229-4eeb-83e6-a9a093943d5e");
97
		UUID uuidIdentifierType2 = UUID.fromString("ef6e960f-5289-456c-b25c-cff7f4de2f63");
98

    
99

    
100
		DefinedTerm it1 = (DefinedTerm)termService.find(uuidIdentifierType1);
101
		Assert.assertNotNull("identifier type must not be null", it1);
102

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

    
115
		Pager<IdentifiedEntityDTO<TaxonName>> names = nameService.findByIdentifier(
116
				TaxonName.class, "ext-1234", null, null, includeEntity, null, null, null);
117
		Assert.assertTrue("Identifier does not exist for TaxonName", names.getCount() == 0);
118

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

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

    
125
		//includeEntity
126
		includeEntity = false;
127
		taxonPager = taxonService.findByIdentifier(Taxon.class, "ext-1234", it1, null, includeEntity, null, null, null);
128
		entity = taxonPager.getRecords().get(0).getCdmEntity();
129
		Assert.assertNull("Taxon must not be returned with includeEntity = false", entity.getEntity());
130

    
131

    
132

    
133
		//Matchmode
134
		includeEntity = false;
135
		MatchMode matchmode = null;
136
		taxonPager = taxonService.findByIdentifier(Taxon.class, "123", null, matchmode, includeEntity, null, null, null);
137
		Assert.assertTrue("Result size for '123' should be 0", taxonPager.getCount() == 0);
138

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

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

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

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

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

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

    
162
	}
163

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

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

    
178
		rootNode = null;  //check against missing filter
179
		taxPager = taxonService.findByIdentifier(Taxon.class, "ext-cache1", null, rootNode, MatchMode.EXACT, false, null, null, null);
180
		Assert.assertEquals("Result size for 'ext-cache1' without filter should be 1", Long.valueOf(1), taxPager.getCount());
181
		Assert.assertEquals("Result size for 'ext-cache1' without filter should be 1", 1, taxPager.getRecords().size());
182

    
183
		//TaxonBase
184
		rootNode = classification.getRootNode();
185
		Pager<IdentifiedEntityDTO<TaxonBase>> tbPager = taxonService.findByIdentifier(TaxonBase.class, "ext-1234", null, rootNode, MatchMode.EXACT, false, null, null, null);
186
		Assert.assertEquals("Result size for 'ext' should be 1", Long.valueOf(1), tbPager.getCount());
187
		Assert.assertEquals("Result size for 'ext' should be 1", 1, tbPager.getRecords().size());
188

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

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

    
198
	}
199

    
200
	@Test
201
    @DataSet(value="IdentifiableServiceBaseTest.testFindByIdentifierOrMarker.xml")
202
    public final void testFindByMarker(){
203
        //classification Filter
204
        Classification classification = classificationService.find(5000);
205
        TaxonNode rootNode = classification.getRootNode();
206
        Boolean markerValue = true;
207

    
208
        UUID uuidMarkerTypeCompleted = MarkerType.uuidComplete;
209
        UUID uuidMarkerTypeDoubtful = UUID.fromString("b51325c8-05fe-421a-832b-d86fc249ef6e");
210

    
211
        MarkerType markerType1 = (MarkerType)termService.find(uuidMarkerTypeCompleted);
212
        MarkerType noMarkerType = null;
213
        MarkerType markerType2 = (MarkerType)termService.find(uuidMarkerTypeDoubtful);
214
        Assert.assertNotNull(markerType2);
215

    
216
        TaxonTitleType titleType = TaxonTitleType.TAXON;
217

    
218
        MarkerType markerType = markerType1;
219
        Pager<MarkedEntityDTO<Taxon>> taxonPager = taxonService.findByMarker(Taxon.class, markerType, markerValue,
220
                rootNode, true, titleType, null, null, null);
221
        Assert.assertEquals("Result size for 'marker1=true' should be 1", Long.valueOf(1), taxonPager.getCount());
222
        Assert.assertEquals("Result size for 'marker1=true' should be 1", 1, taxonPager.getRecords().size());
223
        MarkedEntityDTO<Taxon> dto = taxonPager.getRecords().get(0);
224
        MarkedEntityDTO<Taxon>.Marker marker = dto.getMarker();
225
        Assert.assertTrue("Flag must be true", marker.getFlag());
226
        Assert.assertEquals("Flag must be true", uuidMarkerTypeCompleted, marker.getTypeUuid());
227
        Assert.assertNotNull("the CDM entity in the dto must not be empty if includeEntity=true", dto.getCdmEntity().getEntity());
228
        Assert.assertEquals(5000, dto.getCdmEntity().getEntity().getId());
229

    
230

    
231
        markerValue = false;
232
        taxonPager = taxonService.findByMarker(Taxon.class, markerType, markerValue, rootNode, false, titleType, null, null, null);
233
        Assert.assertEquals("Result size for 'marker1=false' should be 0", Long.valueOf(0), taxonPager.getCount());
234

    
235
        markerValue = true;
236
        markerType = noMarkerType;
237
        taxonPager = taxonService.findByMarker(Taxon.class, markerType, markerValue, rootNode, false, titleType, null, null, null);
238
        Assert.assertEquals("Result size for not existing marker type should be 0", Long.valueOf(0), taxonPager.getCount());
239

    
240
        markerType = markerType2;
241
        taxonPager = taxonService.findByMarker(Taxon.class, markerType, markerValue, rootNode, false, titleType, null, null, null);
242
        Assert.assertEquals("Result size for markerType2 should be 0", Long.valueOf(0), taxonPager.getCount());
243

    
244
        rootNode = null;
245
        markerType = markerType1;
246
        taxonPager = taxonService.findByMarker(Taxon.class, markerType, markerValue, rootNode, false, titleType, null, null, null);
247
        Assert.assertEquals("Result size for no subtree should be 2", Long.valueOf(2), taxonPager.getCount());
248

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

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

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

    
260
    }
261

    
262

    
263

    
264
//	@Test
265
    @Override
266
    public void createTestDataSet() throws FileNotFoundException {
267
		TermVocabulary<DefinedTerm> voc = vocService.find(VocabularyEnum.IdentifierType.getUuid());
268

    
269
		DefinedTerm identifierType1 = DefinedTerm.NewIdentifierTypeInstance(null, "identifierType1", null);
270
    	voc.addTerm(identifierType1);
271
		termService.save(identifierType1);
272
    	DefinedTerm identifierType2 = DefinedTerm.NewIdentifierTypeInstance(null, "identifierType2", null);
273
    	voc.addTerm(identifierType2);
274
		termService.save(identifierType2);
275

    
276

    
277
    	IBotanicalName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
278
        Taxon tb = Taxon.NewInstance(name, null);
279
        tb.addIdentifier("ext-1234", identifierType1);
280
        name.addIdentifier("ext-name12", identifierType2);
281
        taxonService.saveOrUpdate(tb);
282

    
283
        Taxon tb2 = Taxon.NewInstance(null, null);
284
        tb2.setTitleCache("Cached taxon", true);
285
        tb2.addIdentifier("ext-cache1", identifierType2);
286
        taxonService.saveOrUpdate(tb2);
287

    
288
        Classification classification = Classification.NewInstance("My classification");
289
        classification.addChildTaxon(tb, null, null);
290
        classificationService.saveOrUpdate(classification);
291

    
292
        tb2.addSynonymName(null, SynonymType.HOMOTYPIC_SYNONYM_OF());
293

    
294
        commitAndStartNewTransaction(null);
295

    
296
        // this will write flat xml file to the same package in the test resources
297
        // the test file is named after the test class like: TestClassName.xml
298
		writeDbUnitDataSetFile(new String[] {
299
		        "TAXONBASE", "TAXONNAME","IDENTIFIER","TAXONBASE_IDENTIFIER",
300
		        "TAXONNAME_IDENTIFIER",
301
		        "REFERENCE",
302
		        "CLASSIFICATION", "TAXONNODE",
303
		        "HOMOTYPICALGROUP",
304
		        "TERMVOCABULARY"
305
		 }, "xxxx");
306

    
307
    }
308

    
309

    
310
}
(11-11/34)