Project

General

Profile

Download (11.5 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.pager.Pager;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.common.DefinedTerm;
28
import eu.etaxonomy.cdm.model.common.Identifier;
29
import eu.etaxonomy.cdm.model.common.TermVocabulary;
30
import eu.etaxonomy.cdm.model.common.VocabularyEnum;
31
import eu.etaxonomy.cdm.model.name.BotanicalName;
32
import eu.etaxonomy.cdm.model.name.Rank;
33
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
34
import eu.etaxonomy.cdm.model.taxon.Classification;
35
import eu.etaxonomy.cdm.model.taxon.Synonym;
36
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
40
import eu.etaxonomy.cdm.persistence.query.MatchMode;
41
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
42

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

    
52

    
53
	@SpringBeanByType
54
	private INameService nameService;
55

    
56
	@SpringBeanByType
57
	private ITermService termService;
58

    
59
	@SpringBeanByType
60
	private IVocabularyService vocService;
61

    
62
	@SpringBeanByType
63
	private ITaxonService taxonService;
64

    
65
	@SpringBeanByType
66
	private IClassificationService classificationService;
67

    
68
/****************** TESTS *****************************/
69

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

    
75

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

    
88

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

    
95

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

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

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

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

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

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

    
127

    
128

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

    
135
		taxonPager = taxonService.findByIdentifier(Taxon.class, "123", null, MatchMode.EXACT, 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.ANYWHERE, includeEntity, null, null, null);
139
		Assert.assertTrue("Result size for '123' should be 1", taxonPager.getCount() == 1);
140

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

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

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

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

    
158
	}
159

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

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

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

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

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

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

    
194
	}
195

    
196

    
197

    
198
    /* (non-Javadoc)
199
     * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
200
     */
201
//	@Test
202
    @Override
203
    public void createTestDataSet() throws FileNotFoundException {
204
		TermVocabulary<DefinedTerm> voc = vocService.find(VocabularyEnum.IdentifierType.getUuid());
205

    
206
		DefinedTerm identifierType1 = DefinedTerm.NewIdentifierTypeInstance(null, "identifierType1", null);
207
    	voc.addTerm(identifierType1);
208
		termService.save(identifierType1);
209
    	DefinedTerm identifierType2 = DefinedTerm.NewIdentifierTypeInstance(null, "identifierType2", null);
210
    	voc.addTerm(identifierType2);
211
		termService.save(identifierType2);
212

    
213

    
214
    	BotanicalName name = BotanicalName.NewInstance(Rank.SPECIES());
215
        Taxon tb = Taxon.NewInstance(name, null);
216
        tb.addIdentifier("ext-1234", identifierType1);
217
        name.addIdentifier("ext-name12", identifierType2);
218
        taxonService.saveOrUpdate(tb);
219

    
220
        Taxon tb2 = Taxon.NewInstance(null, null);
221
        tb2.setTitleCache("Cached taxon", true);
222
        tb2.addIdentifier("ext-cache1", identifierType2);
223
        taxonService.saveOrUpdate(tb2);
224

    
225
        Classification classification = Classification.NewInstance("My classification");
226
        classification.addChildTaxon(tb, null, null);
227
        classificationService.saveOrUpdate(classification);
228

    
229
        tb2.addSynonymName(null, SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF());
230

    
231
        commitAndStartNewTransaction(null);
232

    
233
        // this will write flat xml file to the same package in the test resources
234
        // the test file is named after the test class like: TestClassName.xml
235
		writeDbUnitDataSetFile(new String[] {
236
		        "TAXONBASE", "TAXONNAMEBASE","IDENTIFIER","TAXONBASE_IDENTIFIER",
237
		        "TAXONNAMEBASE_IDENTIFIER",
238
		        "REFERENCE",
239
		        "CLASSIFICATION", "TAXONNODE",
240
		        "HOMOTYPICALGROUP",
241
		        "TERMVOCABULARY",
242
		        "SYNONYMRELATIONSHIP"
243
		 }, "xxxx");
244

    
245
    }
246

    
247

    
248
}
(10-10/31)