1
|
/**
|
2
|
* Copyright (C) 2007 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.persistence.dao.hibernate.common;
|
11
|
|
12
|
import java.io.FileNotFoundException;
|
13
|
import java.util.List;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.junit.Assert;
|
17
|
import org.junit.Test;
|
18
|
import org.unitils.dbunit.annotation.DataSet;
|
19
|
import org.unitils.spring.annotation.SpringBeanByType;
|
20
|
|
21
|
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
|
22
|
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
|
23
|
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
|
24
|
import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
|
25
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
26
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
27
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
28
|
import eu.etaxonomy.cdm.persistence.dao.common.IPreferenceDao;
|
29
|
import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
|
30
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
31
|
|
32
|
/**
|
33
|
* @author a.mueller
|
34
|
*
|
35
|
*/
|
36
|
public class PreferenceDaoTest extends CdmTransactionalIntegrationTest {
|
37
|
|
38
|
@SpringBeanByType
|
39
|
IPreferenceDao dao;
|
40
|
|
41
|
@SpringBeanByType
|
42
|
IClassificationDao classificationDao;
|
43
|
|
44
|
/************ TESTS ********************************/
|
45
|
|
46
|
@Test
|
47
|
@DataSet
|
48
|
public void testDao() {
|
49
|
Assert.assertNotNull(dao);
|
50
|
}
|
51
|
|
52
|
@Test
|
53
|
@DataSet
|
54
|
public void testGet() {
|
55
|
PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
|
56
|
// PrefKey key = CdmPreference.NewKey(PreferenceSubjectEnum.Database, PreferencePredicate.NomenclaturalCode);
|
57
|
CdmPreference pref = dao.get(key);
|
58
|
Assert.assertNotNull("CdmPreference for given key must exist", pref);
|
59
|
Assert.assertEquals("ICNAFP", pref.getValue());
|
60
|
|
61
|
key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.Test);
|
62
|
// key = CdmPreference.NewKey(PreferenceSubjectEnum.Database, PreferencePredicate.Test);
|
63
|
pref = dao.get(key);
|
64
|
Assert.assertNull("CdmPreference for given key must not exist", pref);
|
65
|
}
|
66
|
|
67
|
|
68
|
@Test
|
69
|
@DataSet
|
70
|
public void testCount() {
|
71
|
int countStart = dao.count();
|
72
|
Assert.assertEquals("There should be 1 preference in the CDM store", 1, countStart);
|
73
|
}
|
74
|
|
75
|
@Test
|
76
|
@DataSet
|
77
|
public void testList() {
|
78
|
List<CdmPreference> list = dao.list();
|
79
|
Assert.assertEquals("There should be 1 preference in the CDM store", 1, list.size());
|
80
|
CdmPreference pref = list.get(0);
|
81
|
Assert.assertNotNull("CdmPreference for given key must exist", pref);
|
82
|
Assert.assertEquals("ICNAFP", pref.getValue());
|
83
|
Assert.assertTrue(pref.isDatabasePref());
|
84
|
}
|
85
|
|
86
|
@Test
|
87
|
@DataSet
|
88
|
public void testSet() {
|
89
|
int countStart = dao.count();
|
90
|
Assert.assertEquals(1, countStart);
|
91
|
|
92
|
CdmPreference pref = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.Test, "200");
|
93
|
// CdmPreference pref = CdmPreference.NewInstance(PreferenceSubjectEnum.Database, PreferencePredicate.Test, "200");
|
94
|
dao.set(pref);
|
95
|
int count = dao.count();
|
96
|
Assert.assertEquals("There should be 1 new preference", countStart + 1, count);
|
97
|
|
98
|
pref = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode, "ICZN");
|
99
|
// pref = CdmPreference.NewInstance(PreferenceSubjectEnum.Database, PreferencePredicate.NomenclaturalCode, "ICZN");
|
100
|
dao.set(pref);
|
101
|
|
102
|
count = dao.count();
|
103
|
Assert.assertEquals("There should be only 1 new preference", countStart + 1, count);
|
104
|
}
|
105
|
|
106
|
@Test
|
107
|
@DataSet(value="eu.etaxonomy.cdm.persistence.dao.hibernate.common.PreferenceDaoTest.testFindTaxonNodeString.xml")
|
108
|
public void testFindTaxonNodeString() {
|
109
|
Classification classification = classificationDao.findByUuid(uuidClassification);
|
110
|
TaxonNode genusNode = classification.getRootNode().getChildNodes().get(0);
|
111
|
TaxonNode speciesNode = genusNode.getChildNodes().get(0);
|
112
|
|
113
|
CdmPreference pref = dao.find(speciesNode, PreferencePredicate.NomenclaturalCode.getKey());
|
114
|
Assert.assertNotNull("CdmPreference for given key must exist", pref);
|
115
|
Assert.assertEquals("ICZN", pref.getValue());
|
116
|
|
117
|
pref = dao.find(genusNode, PreferencePredicate.NomenclaturalCode.getKey());
|
118
|
Assert.assertNotNull("CdmPreference for given key must exist", pref);
|
119
|
Assert.assertEquals("ICVCN", pref.getValue());
|
120
|
|
121
|
pref = dao.find(classification.addChildTaxon(null, null, null), PreferencePredicate.NomenclaturalCode.getKey());
|
122
|
Assert.assertNotNull("CdmPreference for given key must exist", pref);
|
123
|
Assert.assertEquals("ICNAFP", pref.getValue());
|
124
|
Assert.assertTrue(pref.isDatabasePref());
|
125
|
|
126
|
}
|
127
|
|
128
|
private UUID uuidClassification = UUID.fromString("bbd2cdb4-8b83-4ef9-a553-c9629c3890aa");
|
129
|
|
130
|
// @Test
|
131
|
@Override
|
132
|
public void createTestDataSet() throws FileNotFoundException {
|
133
|
// 1. create the entities and save them
|
134
|
Classification classification = Classification.NewInstance("European Abies");
|
135
|
classification.setUuid(uuidClassification);
|
136
|
|
137
|
Taxon taxonGenus = Taxon.NewInstance(null, null);
|
138
|
TaxonNode genusNode = classification.addChildTaxon(taxonGenus, null, null);
|
139
|
|
140
|
Taxon taxonSpecies = Taxon.NewInstance(null, null);
|
141
|
TaxonNode speciesNode = genusNode.addChildTaxon(taxonSpecies, null, null);
|
142
|
|
143
|
Taxon taxonSubSpecies = Taxon.NewInstance(null, null);
|
144
|
speciesNode.addChildTaxon(taxonSubSpecies, null, null);
|
145
|
|
146
|
classificationDao.save(classification);
|
147
|
|
148
|
|
149
|
// 2. end the transaction so that all data is actually written to the db
|
150
|
setComplete();
|
151
|
endTransaction();
|
152
|
|
153
|
// use the fileNameAppendix if you are creating a data set file which need to be named differently
|
154
|
// from the standard name. For example if a single test method needs different data then the other
|
155
|
// methods the test class you may want to set the fileNameAppendix when creating the data for this method.
|
156
|
String fileNameAppendix = "xxx";
|
157
|
|
158
|
// 3.
|
159
|
writeDbUnitDataSetFile(new String[] {
|
160
|
"CLASSIFICATION","TAXONBASE", "TAXONNAME",
|
161
|
"TAXONNODE",
|
162
|
"HOMOTYPICALGROUP",
|
163
|
"HIBERNATE_SEQUENCES" // IMPORTANT!!!
|
164
|
},
|
165
|
fileNameAppendix );
|
166
|
}
|
167
|
}
|