Project

General

Profile

Download (15.2 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.FindByIdentifierDTO;
25
import eu.etaxonomy.cdm.api.service.dto.FindByMarkerDTO;
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.BotanicalName;
34
import eu.etaxonomy.cdm.model.name.Rank;
35
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
36
import eu.etaxonomy.cdm.model.taxon.Classification;
37
import eu.etaxonomy.cdm.model.taxon.Synonym;
38
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
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.persistence.query.MatchMode;
43
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
44

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

    
54

    
55
	@SpringBeanByType
56
	private INameService nameService;
57

    
58
	@SpringBeanByType
59
	private ITermService termService;
60

    
61
	@SpringBeanByType
62
	private IVocabularyService vocService;
63

    
64
	@SpringBeanByType
65
	private ITaxonService taxonService;
66

    
67
	@SpringBeanByType
68
	private IClassificationService classificationService;
69

    
70
/****************** TESTS *****************************/
71

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

    
77

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

    
90

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

    
97

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

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

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

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

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

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

    
129

    
130

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

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

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

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

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

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

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

    
160
	}
161

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

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

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

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

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

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

    
196
	}
197

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

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

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

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

    
226
        markerValue = false;
227
        taxonPager = taxonService.findByMarker(Taxon.class, markerType, markerValue, rootNode, false, null, null, null);
228
        Assert.assertEquals("Result size for 'marker1=false' should be 0", Long.valueOf(0), taxonPager.getCount());
229

    
230
        markerValue = true;
231
        markerType = noMarkerType;
232
        taxonPager = taxonService.findByMarker(Taxon.class, markerType, markerValue, rootNode, false, null, null, null);
233
        Assert.assertEquals("Result size for not existing marker type should be 0", Long.valueOf(0), taxonPager.getCount());
234

    
235
        markerType = markerType2;
236
        taxonPager = taxonService.findByMarker(Taxon.class, markerType, markerValue, rootNode, false, null, null, null);
237
        Assert.assertEquals("Result size for markerType2 should be 0", Long.valueOf(0), taxonPager.getCount());
238

    
239
        rootNode = null;
240
        markerType = markerType1;
241
        taxonPager = taxonService.findByMarker(Taxon.class, markerType, markerValue, rootNode, false, null, null, null);
242
        Assert.assertEquals("Result size for no subtree should be 2", Long.valueOf(2), taxonPager.getCount());
243

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

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

    
251
        markerValue = true;
252
        Pager<FindByMarkerDTO<TaxonNameBase>> namePager = nameService.findByMarker(TaxonNameBase.class, markerType, markerValue, false, null, null, null);
253
        Assert.assertEquals("Result size for names with flag = true should be 1", Long.valueOf(1), namePager.getCount());
254

    
255
    }
256

    
257

    
258

    
259
//	@Test
260
    @Override
261
    public void createTestDataSet() throws FileNotFoundException {
262
		TermVocabulary<DefinedTerm> voc = vocService.find(VocabularyEnum.IdentifierType.getUuid());
263

    
264
		DefinedTerm identifierType1 = DefinedTerm.NewIdentifierTypeInstance(null, "identifierType1", null);
265
    	voc.addTerm(identifierType1);
266
		termService.save(identifierType1);
267
    	DefinedTerm identifierType2 = DefinedTerm.NewIdentifierTypeInstance(null, "identifierType2", null);
268
    	voc.addTerm(identifierType2);
269
		termService.save(identifierType2);
270

    
271

    
272
    	BotanicalName name = BotanicalName.NewInstance(Rank.SPECIES());
273
        Taxon tb = Taxon.NewInstance(name, null);
274
        tb.addIdentifier("ext-1234", identifierType1);
275
        name.addIdentifier("ext-name12", identifierType2);
276
        taxonService.saveOrUpdate(tb);
277

    
278
        Taxon tb2 = Taxon.NewInstance(null, null);
279
        tb2.setTitleCache("Cached taxon", true);
280
        tb2.addIdentifier("ext-cache1", identifierType2);
281
        taxonService.saveOrUpdate(tb2);
282

    
283
        Classification classification = Classification.NewInstance("My classification");
284
        classification.addChildTaxon(tb, null, null);
285
        classificationService.saveOrUpdate(classification);
286

    
287
        tb2.addSynonymName(null, SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF());
288

    
289
        commitAndStartNewTransaction(null);
290

    
291
        // this will write flat xml file to the same package in the test resources
292
        // the test file is named after the test class like: TestClassName.xml
293
		writeDbUnitDataSetFile(new String[] {
294
		        "TAXONBASE", "TAXONNAMEBASE","IDENTIFIER","TAXONBASE_IDENTIFIER",
295
		        "TAXONNAMEBASE_IDENTIFIER",
296
		        "REFERENCE",
297
		        "CLASSIFICATION", "TAXONNODE",
298
		        "HOMOTYPICALGROUP",
299
		        "TERMVOCABULARY",
300
		        "SYNONYMRELATIONSHIP"
301
		 }, "xxxx");
302

    
303
    }
304

    
305

    
306
}
(10-10/32)