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.persistence.dao.hibernate.term;
|
11
|
|
12
|
import static org.junit.Assert.assertEquals;
|
13
|
import static org.junit.Assert.assertFalse;
|
14
|
|
15
|
import java.io.FileNotFoundException;
|
16
|
import java.util.ArrayList;
|
17
|
import java.util.HashMap;
|
18
|
import java.util.List;
|
19
|
import java.util.Map;
|
20
|
import java.util.Set;
|
21
|
import java.util.UUID;
|
22
|
|
23
|
import org.apache.log4j.Logger;
|
24
|
import org.junit.Assert;
|
25
|
import org.junit.Before;
|
26
|
import org.junit.Test;
|
27
|
import org.unitils.dbunit.annotation.DataSet;
|
28
|
import org.unitils.dbunit.annotation.DataSets;
|
29
|
import org.unitils.spring.annotation.SpringBeanByType;
|
30
|
|
31
|
import eu.etaxonomy.cdm.model.common.Language;
|
32
|
import eu.etaxonomy.cdm.model.term.Representation;
|
33
|
import eu.etaxonomy.cdm.model.term.TermType;
|
34
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
35
|
import eu.etaxonomy.cdm.persistence.dao.term.ITermVocabularyDao;
|
36
|
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
|
37
|
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
|
38
|
|
39
|
/**
|
40
|
* @author a.babadshanjan
|
41
|
* @since 10.02.2009
|
42
|
*/
|
43
|
public class TermVocabularyDaoImplTest extends CdmIntegrationTest {
|
44
|
@SuppressWarnings("unused")
|
45
|
private static Logger logger = Logger.getLogger(TermVocabularyDaoImplTest.class);
|
46
|
|
47
|
@SpringBeanByType
|
48
|
private ITermVocabularyDao dao;
|
49
|
|
50
|
@Before
|
51
|
public void setUp() {}
|
52
|
|
53
|
@Test
|
54
|
@DataSets({
|
55
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
|
56
|
@DataSet("/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml")}
|
57
|
)
|
58
|
public void testListVocabularyByType() {
|
59
|
//test class with no subclasses
|
60
|
List<TermVocabulary> rankVocabularies = dao.listByTermType(TermType.Rank, false, null, null, null, null);
|
61
|
assertFalse("There should be at least one vocabulary containing terms of type Rank", rankVocabularies.isEmpty());
|
62
|
assertEquals("There should be only one vocabulary containing terms of type Rank", 1, rankVocabularies.size());
|
63
|
|
64
|
//include subtype, but termtype has no subtype
|
65
|
rankVocabularies = dao.listByTermType(TermType.Rank, true, null, null, null, null);
|
66
|
assertFalse("There should be at least one vocabulary containing terms of type Rank", rankVocabularies.isEmpty());
|
67
|
assertEquals("There should be only one vocabulary containing terms of type Rank", 1, rankVocabularies.size());
|
68
|
|
69
|
//with different classes
|
70
|
List<TermVocabulary> namedAreaVocabularies = dao.listByTermType(TermType.NamedArea, true, null, null, null, null);
|
71
|
int subclassedSize = namedAreaVocabularies.size();
|
72
|
assertEquals("There should be 4 vocabularies (TdwgAreas, Continents, Waterbody, Countries)", 4, subclassedSize);
|
73
|
|
74
|
//with sub types
|
75
|
List<TermVocabulary> scopeVocabularies = dao.listByTermType(TermType.Scope, true, null, null, null, null);
|
76
|
int subtypeSize = scopeVocabularies.size();
|
77
|
assertEquals("There should be 6 vocabularies (Scope, Sex, Stage, 3 x KindOfUnit)", 6, subtypeSize);
|
78
|
|
79
|
List<TermVocabulary> scopeOnlyVocabularies = dao.listByTermType(TermType.Scope, false, null, null, null, null);
|
80
|
assertEquals("Scope only vocabularies w/o subtypes should be 1", 1, scopeOnlyVocabularies.size());
|
81
|
List<TermVocabulary> stageVocabularies = dao.listByTermType(TermType.Stage, false, null, null, null, null);
|
82
|
assertEquals("Stage only vocabularies should be 1", 1, stageVocabularies.size());
|
83
|
}
|
84
|
|
85
|
|
86
|
@Test
|
87
|
@DataSet("TermVocabularyDaoImplTest.testListVocabularyEmpty.xml")
|
88
|
public void testListVocabularyByTypeEmpty() {
|
89
|
List<TermVocabulary> emptyNamedAreas = dao.listByTermType(TermType.NamedArea, true, null, null, null, null);
|
90
|
assertEquals("There should be no vocabulary, as we do not return ALL empty vocabularies of ANY type anymore", 0, emptyNamedAreas.size());
|
91
|
|
92
|
List<TermVocabulary> languageVocabulariesAndEmpty = dao.listByTermType(TermType.Language, true, null, null, null, null);
|
93
|
assertEquals("There should be 2 vocabularies, the empty one and the one that has a language term in", 2, languageVocabulariesAndEmpty.size());
|
94
|
}
|
95
|
|
96
|
@Test()
|
97
|
@DataSets({
|
98
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
|
99
|
@DataSet("/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml")}
|
100
|
)
|
101
|
public void testMissingTermUuids() {
|
102
|
List<UUID> uuidSet = new ArrayList<>();
|
103
|
uuidSet.add(Language.uuidEnglish);
|
104
|
uuidSet.add(Language.uuidFrench);
|
105
|
UUID uuidNotExisting = UUID.fromString("e93e8c10-d9d2-4ad6-9907-952da6d139c4");
|
106
|
uuidSet.add(uuidNotExisting);
|
107
|
Map<UUID, List<UUID>> uuidVocs = new HashMap<>();
|
108
|
uuidVocs.put( Language.uuidLanguageVocabulary, uuidSet);
|
109
|
Map<UUID, Set<UUID>> notExisting = new HashMap<>();
|
110
|
Map<UUID, TermVocabulary<?>> vocabularyMap = new HashMap<>();
|
111
|
|
112
|
dao.missingTermUuids(uuidVocs, notExisting, vocabularyMap);
|
113
|
|
114
|
//assert missing terms
|
115
|
assertEquals(Integer.valueOf(1), Integer.valueOf(notExisting.keySet().size()));
|
116
|
assertEquals(Language.uuidLanguageVocabulary, notExisting.keySet().iterator().next());
|
117
|
Set<UUID> missingLanguageTerms = notExisting.get(Language.uuidLanguageVocabulary );
|
118
|
assertEquals(Integer.valueOf(1), Integer.valueOf(missingLanguageTerms.size()));
|
119
|
assertEquals(uuidNotExisting, missingLanguageTerms.iterator().next());
|
120
|
}
|
121
|
|
122
|
@Test
|
123
|
public void testTitleCacheCreation() {
|
124
|
|
125
|
//prepare
|
126
|
UUID vocUuid = UUID.fromString("a7a2fbe4-3a35-4ec0-b2b2-2298c3ebdf57");
|
127
|
TermVocabulary<?> newVoc = TermVocabulary.NewInstance(TermType.Modifier);
|
128
|
newVoc.setUuid(vocUuid);
|
129
|
|
130
|
//default titleCache
|
131
|
dao.save(newVoc);
|
132
|
newVoc.setProtectedTitleCache(true); //make sure we use the title cache created during save by listeners
|
133
|
String emptyLabel = newVoc.getTitleCache();
|
134
|
//this value may need to be changed when the default cache generation changes
|
135
|
Assert.assertEquals("TitleCache should use default title generation", "TermVocabulary<a7a2fbe4-3a35-4ec0-b2b2-2298c3ebdf57>", emptyLabel);
|
136
|
|
137
|
//only German
|
138
|
newVoc.setProtectedTitleCache(false);
|
139
|
Representation newRepresentation = Representation.NewInstance("Beschreibung", "Deutsches Label", "Abk.", Language.GERMAN());
|
140
|
newVoc.addRepresentation(newRepresentation);
|
141
|
dao.saveOrUpdate(newVoc);
|
142
|
newVoc.setProtectedTitleCache(true);
|
143
|
Assert.assertEquals("German Label should be new title cache", "Deutsches Label", newVoc.getTitleCache());
|
144
|
|
145
|
//German and English
|
146
|
newVoc.setProtectedTitleCache(false);
|
147
|
Representation englishRepresentation = Representation.NewInstance("Description", "English label", "Abbrev.", Language.DEFAULT());
|
148
|
newVoc.addRepresentation(englishRepresentation);
|
149
|
dao.saveOrUpdate(newVoc);
|
150
|
newVoc.setProtectedTitleCache(true);
|
151
|
Assert.assertEquals("English Label should be new title cache", "English label", newVoc.getTitleCache());
|
152
|
|
153
|
//Change English label
|
154
|
newVoc.setProtectedTitleCache(false);
|
155
|
newVoc.setLabel("New English label");
|
156
|
dao.saveOrUpdate(newVoc);
|
157
|
newVoc.setProtectedTitleCache(true);
|
158
|
Assert.assertEquals("English (default language) label should be new English label", "New English label", newVoc.getTitleCache());
|
159
|
|
160
|
//Remove English
|
161
|
newVoc.setProtectedTitleCache(false);
|
162
|
newVoc.removeRepresentation(englishRepresentation);
|
163
|
dao.saveOrUpdate(newVoc);
|
164
|
newVoc.setProtectedTitleCache(true);
|
165
|
Assert.assertEquals("German Label should be new title cache again as English representation is not there anymore", "Deutsches Label", newVoc.getTitleCache());
|
166
|
|
167
|
}
|
168
|
|
169
|
@Override
|
170
|
public void createTestDataSet() throws FileNotFoundException {}
|
171
|
}
|