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.Ignore;
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.FindByIdentifierDTO;
26
import eu.etaxonomy.cdm.api.service.dto.FindByMarkerDTO;
27
import eu.etaxonomy.cdm.api.service.pager.Pager;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.common.DefinedTerm;
30
import eu.etaxonomy.cdm.model.common.Identifier;
31
import eu.etaxonomy.cdm.model.common.MarkerType;
32
import eu.etaxonomy.cdm.model.common.TermVocabulary;
33
import eu.etaxonomy.cdm.model.common.VocabularyEnum;
34
import eu.etaxonomy.cdm.model.name.BotanicalName;
35
import eu.etaxonomy.cdm.model.name.Rank;
36
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
37
import eu.etaxonomy.cdm.model.taxon.Classification;
38
import eu.etaxonomy.cdm.model.taxon.Synonym;
39
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
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.test.integration.CdmTransactionalIntegrationTest;
45

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

    
55

    
56
	@SpringBeanByType
57
	private INameService nameService;
58

    
59
	@SpringBeanByType
60
	private ITermService termService;
61

    
62
	@SpringBeanByType
63
	private IVocabularyService vocService;
64

    
65
	@SpringBeanByType
66
	private ITaxonService taxonService;
67

    
68
	@SpringBeanByType
69
	private IClassificationService classificationService;
70

    
71
/****************** TESTS *****************************/
72

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

    
78

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

    
91

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

    
98

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

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

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

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

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

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

    
130

    
131

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

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

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

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

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

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

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

    
161
	}
162

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

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

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

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

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

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

    
197
	}
198

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

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

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

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

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

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

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

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

    
257
    }
258

    
259

    
260

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

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

    
273

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

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

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

    
289
        tb2.addSynonymName(null, SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF());
290

    
291
        commitAndStartNewTransaction(null);
292

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

    
305
    }
306

    
307

    
308
}
(10-10/32)