a31631b2d2f917019c4d9e140229f844efd3be9f
[cdmlib.git] / cdmlib-persistence / src / test / java / eu / etaxonomy / cdm / persistence / dao / hibernate / common / DefinedTermDaoImplTest.java
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
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.assertTrue;
18
19 import java.io.FileNotFoundException;
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.UUID;
25
26 import org.hibernate.Hibernate;
27 import org.junit.After;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.unitils.spring.annotation.SpringBeanByType;
32
33 import eu.etaxonomy.cdm.model.common.DefinedTerm;
34 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
35 import eu.etaxonomy.cdm.model.common.ExtensionType;
36 import eu.etaxonomy.cdm.model.common.Language;
37 import eu.etaxonomy.cdm.model.common.Representation;
38 import eu.etaxonomy.cdm.model.common.TermType;
39 import eu.etaxonomy.cdm.model.common.TermVocabulary;
40 import eu.etaxonomy.cdm.model.description.Feature;
41 import eu.etaxonomy.cdm.model.location.NamedArea;
42 import eu.etaxonomy.cdm.model.view.AuditEvent;
43 import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
44 import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
45 import eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao;
46 import eu.etaxonomy.cdm.persistence.query.OrderHint;
47 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
48 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
49
50 public class DefinedTermDaoImplTest extends CdmTransactionalIntegrationTest {
51
52 @SpringBeanByType
53 private IDefinedTermDao dao;
54
55 @SpringBeanByType
56 private ITermVocabularyDao vocabularyDao;
57
58 private UUID uuid;
59 private UUID armUuid;
60 private UUID northernEuropeUuid;
61 private UUID middleEuropeUuid;
62 private UUID westTropicalAfricaUuid;
63 private Set<NamedArea> namedAreas;
64 private AuditEvent auditEvent;
65
66 @Before
67 public void setUp() {
68 uuid = UUID.fromString("910307f1-dc3c-452c-a6dd-af5ac7cd365c");
69 armUuid = UUID.fromString("7a0fde13-26e9-4382-a5c9-5640fc2b3334");
70 northernEuropeUuid = UUID.fromString("22524ba2-6e57-4b71-89ab-89fc50fba6b4");
71 middleEuropeUuid = UUID.fromString("d292f237-da3d-408b-93a1-3257a8c80b97");
72 westTropicalAfricaUuid = UUID.fromString("931164ad-ec16-4133-afab-bdef25d67636");
73 namedAreas = new HashSet<NamedArea>();
74 auditEvent = new AuditEvent();
75 auditEvent.setUuid(UUID.fromString("6456c23d-6424-42dc-a240-36d34e77b249"));
76 auditEvent.setRevisionNumber(1001);
77 AuditEventContextHolder.clearContext();
78 }
79
80 @After
81 public void cleantUp() {
82 AuditEventContextHolder.clearContext();
83 }
84
85 @Test
86 public void findByTitle() throws Exception {
87 List<DefinedTermBase> terms = dao.findByTitle("Diagnosis");
88 assertNotNull("findByTitle should return a List", terms);
89 assertEquals("findByTitle should return one term ",terms.size(),1);
90 assertEquals("findByTitle should return Feature.DIAGNOSIS",terms.get(0),Feature.DIAGNOSIS());
91 }
92
93 @Test
94 public void getTermByUUID() {
95 DefinedTermBase<?> term = dao.findByUuid(uuid);
96 assertNotNull("findByUuid should return a term",term);
97 assertEquals("findByUuid should return Feature.UNKNOWN",Feature.UNKNOWN(),term);
98 }
99
100
101 @Test
102 public void getLanguageByIso2() {
103 Language lang = dao.getLanguageByIso("arm");
104 assertNotNull(lang);
105 assertEquals("getLanguageByIso should return the correct Language instance",lang.getUuid(), armUuid);
106 }
107
108 @Test
109 public void getLanguageByIso1() {
110 Language lang = dao.getLanguageByIso("hy");
111 assertNotNull(lang);
112 assertEquals("getLanguageByIso should return the correct Language instance",lang.getUuid(), armUuid);
113 }
114
115 @Test
116 public void getLanguageByMalformedIso1() {
117 Language lang = dao.getLanguageByIso("a");
118 assertNull("getLanguageByIso should return null for this malformed Iso \'a\'",lang);
119 }
120
121 @Test
122 public void getLanguageByMalformedIso2() {
123 Language lang = dao.getLanguageByIso("abcd");
124 assertNull("getLanguageByIso should return null for this malformed Iso \'abcd\'",lang);
125 }
126
127 @Test
128 public void getDefinedTermByIdInVocabulary(){
129 UUID tdwgVocUuid = NamedArea.uuidTdwgAreaVocabulary;
130 List<NamedArea> list = dao.getDefinedTermByIdInVocabulary("GER", tdwgVocUuid, NamedArea.class, null, null);
131 assertNotNull("Method should return a result", list);
132 assertEquals("Method should return exactly 1 area", 1, list.size());
133 NamedArea area = list.get(0);
134 assertEquals("GER", area.getIdInVocabulary());
135 assertEquals(tdwgVocUuid, area.getVocabulary().getUuid());
136 }
137
138 @Test
139 public void testGetIncludes() {
140 NamedArea northernEurope = (NamedArea)dao.findByUuid(northernEuropeUuid);
141 assert northernEurope != null : "NamedArea must exist";
142 namedAreas.add(northernEurope);
143
144 List<String> propertyPaths = new ArrayList<String>();
145 propertyPaths.add("level");
146
147 List<NamedArea> includes = dao.getIncludes(namedAreas, null, null,propertyPaths);
148
149 assertNotNull("getIncludes should return a List",includes);
150 assertFalse("The list should not be empty",includes.isEmpty());
151 assertEquals("getIncludes should return 9 NamedArea entities",9,includes.size());
152 assertTrue("NamedArea.level should be initialized",Hibernate.isInitialized(includes.get(0).getLevel()));
153 }
154
155 @Test
156 public void countIncludes() {
157 NamedArea northernEurope = (NamedArea)dao.findByUuid(northernEuropeUuid);
158 assert northernEurope != null : "NamedArea must exist";
159 namedAreas.add(northernEurope);
160
161 long numberOfIncludes = dao.countIncludes(namedAreas);
162 assertEquals("countIncludes should return 9",9, numberOfIncludes);
163
164 }
165
166 @Test
167 public void testGetPartOf() {
168 NamedArea northernEurope = (NamedArea)dao.findByUuid(northernEuropeUuid);
169 NamedArea middleEurope = (NamedArea)dao.findByUuid(middleEuropeUuid);
170 NamedArea westTropicalAfrica = (NamedArea)dao.findByUuid(westTropicalAfricaUuid);
171 assert northernEurope != null : "NamedArea must exist";
172 assert middleEurope != null : "NamedArea must exist";
173 assert westTropicalAfrica != null : "NamedArea must exist";
174 namedAreas.add(northernEurope);
175 namedAreas.add(middleEurope);
176 namedAreas.add(westTropicalAfrica);
177
178 List<String> propertyPaths = new ArrayList<String>();
179 propertyPaths.add("level");
180
181 List<NamedArea> partOf = dao.getPartOf(namedAreas, null, null,propertyPaths);
182
183 assertNotNull("getPartOf should return a List",partOf);
184 assertFalse("The list should not be empty",partOf.isEmpty());
185 assertEquals("getPartOf should return 2 NamedArea entities",2,partOf.size());
186 assertTrue("NamedArea.level should be initialized",Hibernate.isInitialized(partOf.get(0).getLevel()));
187 }
188
189 @Test
190 public void countPartOf() {
191 NamedArea northernEurope = (NamedArea)dao.findByUuid(northernEuropeUuid);
192 NamedArea middleEurope = (NamedArea)dao.findByUuid(middleEuropeUuid);
193 NamedArea westTropicalAfrica = (NamedArea)dao.findByUuid(westTropicalAfricaUuid);
194 assert northernEurope != null : "NamedArea must exist";
195 assert middleEurope != null : "NamedArea must exist";
196 assert westTropicalAfrica != null : "NamedArea must exist";
197
198 namedAreas.add(northernEurope);
199 namedAreas.add(middleEurope);
200 namedAreas.add(westTropicalAfrica);
201
202 long numberOfPartOf = dao.countPartOf(namedAreas);
203 assertEquals("countPartOf should return 2",2,numberOfPartOf);
204 }
205
206 @Test
207 // NOTE: if this test is failing see
208 // http://dev.e-taxonomy.eu/trac/changeset/13291/trunk/cdmlib/cdmlib-persistence/src/test/resources/eu/etaxonomy/cdm/persistence/dao/hibernate/dataset.dtd
209 public void testListInitialization() {
210 AuditEventContextHolder.getContext().setAuditEvent(auditEvent);
211 List<OrderHint> orderHints = new ArrayList<OrderHint>();
212 orderHints.add(new OrderHint("titleCache",SortOrder.ASCENDING));
213
214 List<String> propertyPaths = new ArrayList<String>();
215 propertyPaths.add("representations");
216 propertyPaths.add("representations.language");
217 List<ExtensionType> extensionTypes = dao.list(ExtensionType.class,null, null, orderHints, propertyPaths);
218
219
220 assertTrue(Hibernate.isInitialized(extensionTypes.get(0).getRepresentations()));
221 Set<Representation> representations = extensionTypes.get(0).getRepresentations();
222 //TODO currently the representations list is empty, is this wanted? If not,
223 //we should first check, if the list is not empty and then iterate.
224 //Why is it empty? Does ExtensionType not have representations?
225 for(Representation representation : representations) {
226 assertTrue(Hibernate.isInitialized(representation.getLanguage()));
227 }
228 }
229
230 @Test
231 public void testTitleCacheCreation() {
232
233 //prepare
234 @SuppressWarnings("unchecked")
235 TermVocabulary<DefinedTerm> newVoc = TermVocabulary.NewInstance(TermType.Modifier);
236 UUID vocUuid = UUID.fromString("6ced4c45-9c1b-4053-9dc3-6b8c51d286ed");
237 newVoc.setUuid(vocUuid);
238 UUID termUuid = UUID.fromString("2ab69720-c06c-4cfc-8928-d2ae6f1e4a48");
239 DefinedTerm newModifier = DefinedTerm.NewModifierInstance("Test Modifier Description", "English Modifier", "TM");
240 Representation englishRepresentation = newModifier.getRepresentations().iterator().next();
241 newModifier.setUuid(termUuid);
242 newVoc.addTerm(newModifier);
243 vocabularyDao.save(newVoc);
244
245 //only English
246 newModifier.setProtectedTitleCache(false);
247 newModifier.setProtectedTitleCache(true);
248 Assert.assertEquals("English Label should be the title cache", "English Modifier", newModifier.getTitleCache());
249
250 //Change English label
251 newModifier.setProtectedTitleCache(false);
252 newModifier.setLabel("New English label");
253 dao.saveOrUpdate(newModifier);
254 newModifier.setProtectedTitleCache(true);
255 Assert.assertEquals("English (default language) label should still be the title cache", "New English label", newModifier.getTitleCache());
256
257 //Add German
258 newModifier.setProtectedTitleCache(false);
259 Representation newRepresentation = Representation.NewInstance("Beschreibung", "Deutscher Modifier", "Abk.", Language.GERMAN());
260 newModifier.addRepresentation(newRepresentation);
261 dao.saveOrUpdate(newModifier);
262 newModifier.setProtectedTitleCache(true);
263 Assert.assertEquals("English (default language) label should still be the title cache", "New English label", newModifier.getTitleCache());
264
265 //Remove English
266 newModifier.setProtectedTitleCache(false);
267 newModifier.removeRepresentation(englishRepresentation);
268 dao.saveOrUpdate(newModifier);
269 newVoc.setProtectedTitleCache(true);
270 Assert.assertEquals("German Label should be the new title cache again as English representation is not there anymore", "Deutscher Modifier", newModifier.getTitleCache());
271 }
272
273 @Test
274 public void testListByTermType(){
275
276 TermType termType = TermType.Modifier;
277
278 List<DefinedTermBase> existingList = this.dao.listByTermType(termType, null, null, null, null);
279 int nExisting = existingList.size();
280 int nExistingTerms = this.dao.list(DefinedTerm.class, null, null, null, null).size();
281
282
283 //prepare
284 @SuppressWarnings("unchecked")
285 TermVocabulary<DefinedTerm> newVoc = TermVocabulary.NewInstance(termType);
286 UUID vocUuid = UUID.fromString("6ced4c45-9c1b-4053-9dc3-6b8c51d286ed");
287 newVoc.setUuid(vocUuid);
288 UUID termUuid = UUID.fromString("2ab69720-c06c-4cfc-8928-d2ae6f1e4a48");
289 DefinedTerm newModifier = DefinedTerm.NewModifierInstance("Test Modifier Description", "English Modifier", "TM");
290 newModifier.setUuid(termUuid);
291 newVoc.addTerm(newModifier);
292 vocabularyDao.save(newVoc);
293 this.commitAndStartNewTransaction(null);
294
295 //assert 1 more
296 int nNow = this.dao.listByTermType(termType, null, null, null, null).size();
297 Assert.assertEquals("There should be exactly 1 more term now", nExisting + 1 , nNow);
298 int nTermsNow = this.dao.list(DefinedTerm.class, null, null, null, null).size();
299 Assert.assertEquals("There should be exactly 1 more term now", nExistingTerms + 1 , nTermsNow);
300 this.commitAndStartNewTransaction(null);
301
302 //Add German representation
303 Representation newRepresentation = Representation.NewInstance("Beschreibung", "Deutscher Modifier", "Abk.", Language.GERMAN());
304 newModifier.addRepresentation(newRepresentation);
305 dao.saveOrUpdate(newModifier);
306 this.commitAndStartNewTransaction(null);
307
308 nNow = this.dao.listByTermType(termType, null, null, null, null).size();
309 Assert.assertEquals("There should still be only one more term (but with 2 representations)", nExisting + 1 , nNow);
310 nTermsNow = this.dao.list(DefinedTerm.class, null, null, null, null).size();
311 Assert.assertEquals("There should be exactly 1 more term now", nExistingTerms + 1 , nTermsNow);
312
313
314 List<DefinedTerm> languages = this.dao.listByTermType(TermType.Language, null, null, null, null);
315 Assert.assertNotNull(languages);
316 Assert.assertEquals(485, languages.size());
317 }
318
319
320 @Override
321 public void createTestDataSet() throws FileNotFoundException {}
322 }