better use of generics in service layer and persistence list methods
[cdmlib.git] / cdmlib-persistence / src / test / java / eu / etaxonomy / cdm / persistence / dao / hibernate / common / TermVocabularyDaoImplTest.java
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.common;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.UUID;
21
22 import org.apache.log4j.Logger;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.unitils.dbunit.annotation.DataSet;
26 import org.unitils.spring.annotation.SpringBeanByType;
27
28 import eu.etaxonomy.cdm.model.common.Language;
29 import eu.etaxonomy.cdm.model.common.TermVocabulary;
30 import eu.etaxonomy.cdm.model.location.Country;
31 import eu.etaxonomy.cdm.model.location.NamedArea;
32 import eu.etaxonomy.cdm.model.name.Rank;
33 import eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao;
34 import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
35
36 /**
37 * @author a.babadshanjan
38 * @created 10.02.2009
39 */
40 public class TermVocabularyDaoImplTest extends CdmIntegrationTest {
41 @SuppressWarnings("unused")
42 private static Logger logger = Logger.getLogger(TermVocabularyDaoImplTest.class);
43
44 @SpringBeanByType
45 private ITermVocabularyDao dao;
46
47 @Before
48 public void setUp() {
49 }
50
51 @Test
52 public void testListVocabularyByClass() {
53 //test class with no subclasses
54 List<TermVocabulary<? extends Rank>> rankVocabularies = dao.listByTermClass(Rank.class, false, false, null, null, null, null);
55 assertFalse("There should be at least one vocabulary containing terms of class Rank",rankVocabularies.isEmpty());
56 assertEquals("There should be only one vocabulary containing terms of class Rank",1,rankVocabularies.size());
57
58
59 rankVocabularies = dao.listByTermClass(Rank.class, true, false, null, null, null, null);
60 assertFalse("There should be at least one vocabulary containing terms of class Rank",rankVocabularies.isEmpty());
61 assertEquals("There should be only one vocabulary containing terms of class Rank",1,rankVocabularies.size());
62
63 //with subclasses
64 List<TermVocabulary<? extends NamedArea>> namedAreaVocabularies = dao.listByTermClass(NamedArea.class, true, false, null, null, null, null);
65 int subclassedSize = namedAreaVocabularies.size();
66 assertEquals("There should be 3 vocabularies (TdwgAreas, Continents, WaterbodyOrCountries)", 4, subclassedSize);
67
68 List<TermVocabulary<? extends NamedArea>> namedAreaOnlyVocabularies = dao.listByTermClass(NamedArea.class, false, false, null, null, null, null);
69 List<TermVocabulary<? extends Country>> countryVocabularies = dao.listByTermClass(Country.class, false, false, null, null, null, null);
70 int sumOfSingleSizes = namedAreaOnlyVocabularies.size() + countryVocabularies.size();
71 assertEquals("number of NamedArea and subclasses should be same as sum of all single vocabularies", subclassedSize, sumOfSingleSizes);
72
73 }
74
75 @Test
76 @DataSet("TermVocabularyDaoImplTest.testListVocabularyEmpty.xml")
77 public void testListVocabularyByClassEmpty() {
78 //test include empty
79 List<TermVocabulary<? extends NamedArea>> namedAreaVocabulariesAndEmpty = dao.listByTermClass(NamedArea.class, true, true, null, null, null, null);
80 assertEquals("There should be 1 vocabulary (the empty one)", 1, namedAreaVocabulariesAndEmpty.size());
81
82 List<TermVocabulary<? extends Language>> languageVocabulariesAndEmpty = dao.listByTermClass(Language.class, true, true, null, null, null, null);
83 assertEquals("There should be 2 vocabularies, the empty one and the one that has a language term in", 2, languageVocabulariesAndEmpty.size());
84 }
85
86 @Test
87 @DataSet("TermVocabularyDaoImplTest.testListVocabularyEmpty.xml")
88 public void testListVocabularyEmpty() {
89 //test class with no subclasses
90 List<TermVocabulary> emptyVocs = dao.listEmpty(null, null, null, null);
91 assertFalse("There should be at least one vocabulary containing no terms",emptyVocs.isEmpty());
92 assertEquals("There should be only one vocabulary containing terms of class Rank",1,emptyVocs.size());
93 UUID uuidEmptyVoc = UUID.fromString("f253962f-d787-4b16-b2d2-e645da73ae4f");
94 assertEquals("The empty vocabulary should be the one defined", uuidEmptyVoc, emptyVocs.get(0).getUuid());
95 }
96
97 @Test
98 public void testmissingTermUuids() {
99 Set<UUID> uuidSet = new HashSet<UUID>();
100 uuidSet.add(Language.uuidEnglish);
101 uuidSet.add(Language.uuidFrench);
102 UUID uuidNotExisting = UUID.fromString("e93e8c10-d9d2-4ad6-9907-952da6d139c4");
103 uuidSet.add(uuidNotExisting);
104 Map<UUID, Set<UUID>> uuidVocs = new HashMap<UUID, Set<UUID>>();
105 uuidVocs.put( Language.uuidLanguageVocabulary, uuidSet);
106 Map<UUID, Set<UUID>> notExisting = new HashMap<UUID, Set<UUID>>();
107 Map<UUID, TermVocabulary<?>> vocabularyMap = new HashMap<UUID, TermVocabulary<?>>();
108
109 dao.missingTermUuids(uuidVocs, notExisting, vocabularyMap);
110
111 //assert missing terms
112 assertEquals(Integer.valueOf(1), Integer.valueOf(notExisting.keySet().size()));
113 assertEquals(Language.uuidLanguageVocabulary, notExisting.keySet().iterator().next());
114 Set<UUID> missingLanguageTerms = notExisting.get(Language.uuidLanguageVocabulary );
115 assertEquals(Integer.valueOf(1), Integer.valueOf(missingLanguageTerms.size()));
116 assertEquals(uuidNotExisting, missingLanguageTerms.iterator().next());
117 }
118
119 }