Project

General

Profile

Download (9.26 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.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.HashSet;
19
import java.util.List;
20
import java.util.Map;
21
import java.util.Set;
22
import java.util.UUID;
23

    
24
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
25
import org.junit.Assert;
26
import org.junit.Before;
27
import org.junit.Test;
28
import org.unitils.dbunit.annotation.DataSet;
29
import org.unitils.dbunit.annotation.DataSets;
30
import org.unitils.spring.annotation.SpringBeanByType;
31

    
32
import eu.etaxonomy.cdm.model.common.Language;
33
import eu.etaxonomy.cdm.model.location.NamedArea;
34
import eu.etaxonomy.cdm.model.term.Representation;
35
import eu.etaxonomy.cdm.model.term.TermType;
36
import eu.etaxonomy.cdm.model.term.TermVocabulary;
37
import eu.etaxonomy.cdm.persistence.dao.term.ITermVocabularyDao;
38
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
39
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
40
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
41

    
42
/**
43
 * @author a.babadshanjan
44
 * @since 10.02.2009
45
 */
46
public class TermVocabularyDaoImplTest extends CdmTransactionalIntegrationTest {
47
	@SuppressWarnings("unused")
48
	private static Logger logger = LogManager.getLogger(TermVocabularyDaoImplTest.class);
49

    
50
	@SpringBeanByType
51
	private ITermVocabularyDao dao;
52

    
53
	@Before
54
	public void setUp() {}
55

    
56
    @Test
57
    @DataSets({
58
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
59
        @DataSet("/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml")}
60
    )
61
    public void testListVocabularyByType() {
62
        //test class with no subclasses
63
        List<TermVocabulary> rankVocabularies = dao.listByTermType(TermType.Rank, false, null, null, null, null);
64
        assertFalse("There should be at least one vocabulary containing terms of type Rank", rankVocabularies.isEmpty());
65
        assertEquals("There should be only one vocabulary containing terms of type Rank", 1, rankVocabularies.size());
66

    
67
        //include subtype, but termtype has no subtype
68
        rankVocabularies = dao.listByTermType(TermType.Rank, true, null, null, null, null);
69
        assertFalse("There should be at least one vocabulary containing terms of type Rank", rankVocabularies.isEmpty());
70
        assertEquals("There should be only one vocabulary containing terms of type Rank", 1, rankVocabularies.size());
71

    
72
        //with different classes
73
        List<TermVocabulary> namedAreaVocabularies = dao.listByTermType(TermType.NamedArea, true, null, null, null, null);
74
        int subclassedSize = namedAreaVocabularies.size();
75
        assertEquals("There should be 4 vocabularies (TdwgAreas, Continents, Waterbody, Countries)", 4, subclassedSize);
76

    
77
        //with sub types
78
        List<TermVocabulary> scopeVocabularies = dao.listByTermType(TermType.Scope, true, null, null, null, null);
79
        int subtypeSize = scopeVocabularies.size();
80
        assertEquals("There should be 6 vocabularies (Scope, Sex, Stage, 3 x KindOfUnit)", 6, subtypeSize);
81

    
82
        List<TermVocabulary> scopeOnlyVocabularies = dao.listByTermType(TermType.Scope, false, null, null, null, null);
83
        assertEquals("Scope only vocabularies w/o subtypes should be 1", 1, scopeOnlyVocabularies.size());
84
        List<TermVocabulary> stageVocabularies = dao.listByTermType(TermType.Stage, false, null, null, null, null);
85
        assertEquals("Stage only vocabularies should be 1", 1, stageVocabularies.size());
86
    }
87

    
88

    
89
    @Test
90
    @DataSet("TermVocabularyDaoImplTest.testListVocabularyEmpty.xml")
91
    public void testListVocabularyByTypeEmpty() {
92
        List<TermVocabulary> emptyNamedAreas = dao.listByTermType(TermType.NamedArea, true, null, null, null, null);
93
        assertEquals("There should be no vocabulary, as we do not return ALL empty vocabularies of ANY type anymore", 0, emptyNamedAreas.size());
94

    
95
        List<TermVocabulary> languageVocabulariesAndEmpty = dao.listByTermType(TermType.Language, true, null, null, null, null);
96
        assertEquals("There should be 2 vocabularies, the empty one and the one that has a language term in", 2, languageVocabulariesAndEmpty.size());
97
    }
98

    
99
	@Test()
100
	@DataSets({
101
	      @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
102
	      @DataSet("/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml")}
103
	)
104
	public void testMissingTermUuids() {
105
		List<UUID> uuidSet = new ArrayList<>();
106
		uuidSet.add(Language.uuidEnglish);
107
		uuidSet.add(Language.uuidFrench);
108
		UUID uuidNotExisting = UUID.fromString("e93e8c10-d9d2-4ad6-9907-952da6d139c4");
109
		uuidSet.add(uuidNotExisting);
110
		Map<UUID, List<UUID>> uuidVocs = new HashMap<>();
111
		uuidVocs.put( Language.uuidLanguageVocabulary, uuidSet);
112
		Map<UUID, Set<UUID>> notExisting = new HashMap<>();
113
		Map<UUID, TermVocabulary<?>> vocabularyMap = new HashMap<>();
114

    
115
		dao.missingTermUuids(uuidVocs, notExisting, vocabularyMap);
116

    
117
		//assert missing terms
118
		assertEquals(Integer.valueOf(1), Integer.valueOf(notExisting.keySet().size()));
119
		assertEquals(Language.uuidLanguageVocabulary, notExisting.keySet().iterator().next());
120
		Set<UUID> missingLanguageTerms = notExisting.get(Language.uuidLanguageVocabulary );
121
		assertEquals(Integer.valueOf(1), Integer.valueOf(missingLanguageTerms.size()));
122
		assertEquals(uuidNotExisting, missingLanguageTerms.iterator().next());
123
	}
124

    
125
	@Test
126
    public void testTitleCacheCreation() {
127

    
128
	    //prepare
129
	    UUID vocUuid = UUID.fromString("a7a2fbe4-3a35-4ec0-b2b2-2298c3ebdf57");
130
	    TermVocabulary<?> newVoc = TermVocabulary.NewInstance(TermType.Modifier);
131
	    newVoc.setUuid(vocUuid);
132

    
133
	    //default titleCache
134
	    dao.save(newVoc);
135
	    newVoc.setProtectedTitleCache(true);  //make sure we use the title cache created during save by listeners
136
	    String emptyLabel = newVoc.getTitleCache();
137
	    //this value may need to be changed when the default cache generation changes
138
	    Assert.assertEquals("TitleCache should use default title generation", "TermVocabulary<a7a2fbe4-3a35-4ec0-b2b2-2298c3ebdf57>", emptyLabel);
139

    
140
	    //only German
141
	    newVoc.setProtectedTitleCache(false);
142
	    Representation newRepresentation = Representation.NewInstance("Beschreibung", "Deutsches Label", "Abk.", Language.GERMAN());
143
	    newVoc.addRepresentation(newRepresentation);
144
	    dao.saveOrUpdate(newVoc);
145
	    newVoc.setProtectedTitleCache(true);
146
	    Assert.assertEquals("German Label should be new title cache", "Deutsches Label", newVoc.getTitleCache());
147

    
148
	    //German and English
149
	    newVoc.setProtectedTitleCache(false);
150
        Representation englishRepresentation = Representation.NewInstance("Description", "English label", "Abbrev.", Language.DEFAULT());
151
        newVoc.addRepresentation(englishRepresentation);
152
        dao.saveOrUpdate(newVoc);
153
        newVoc.setProtectedTitleCache(true);
154
        Assert.assertEquals("English Label should be new title cache", "English label", newVoc.getTitleCache());
155

    
156
        //Change English label
157
        newVoc.setProtectedTitleCache(false);
158
        newVoc.setLabel("New English label");
159
        dao.saveOrUpdate(newVoc);
160
        newVoc.setProtectedTitleCache(true);
161
        Assert.assertEquals("English (default language) label should be new English label", "New English label", newVoc.getTitleCache());
162

    
163
        //Remove English
164
        newVoc.setProtectedTitleCache(false);
165
        newVoc.removeRepresentation(englishRepresentation);
166
        dao.saveOrUpdate(newVoc);
167
        newVoc.setProtectedTitleCache(true);
168
        Assert.assertEquals("German Label should be new title cache again as English representation is not there anymore", "Deutsches Label", newVoc.getTitleCache());
169

    
170
	}
171

    
172
	@Test
173
    @DataSets({
174
          @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
175
          @DataSet("/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml")}
176
    )
177
	public void testFindVocabularyDtoByTermTypes(){
178
	    Set<TermType> termTypes = new HashSet<>();
179
	    termTypes.add(TermType.NamedArea);
180
	    List<TermVocabularyDto> vocDtos = dao.findVocabularyDtoByTermTypes(termTypes, true);
181
	    Assert.assertEquals(4, vocDtos.size());
182

    
183
	    //#9825 test deduplication
184
        @SuppressWarnings("unchecked")
185
        TermVocabulary<NamedArea> continentVoc = dao.findByUuid(NamedArea.uuidContinentVocabulary);
186
        continentVoc.addRepresentation(Representation.NewInstance("Kontinente", "Kontinente", "Kont.", Language.GERMAN()));
187
        vocDtos = dao.findVocabularyDtoByTermTypes(termTypes, true);
188
        Assert.assertEquals("Deduplication for representations does not work", 4, vocDtos.size());
189
        int repSize = vocDtos.stream().filter(voc->voc.getUuid().equals(NamedArea.uuidContinentVocabulary)).findFirst().get().getRepresentations().size();
190
        Assert.assertEquals("There should be 2 representations for continent vocabulary", 2, repSize);
191
	}
192

    
193
    @Override
194
    public void createTestDataSet() throws FileNotFoundException {}
195
}
(3-3/3)