5d803e53c3ef40aed6219cdf0da0e170953e3f2d
[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
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.UUID;
22
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.DefinedTermBase;
29 import eu.etaxonomy.cdm.model.common.Language;
30 import eu.etaxonomy.cdm.model.location.NamedArea;
31 import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
32 import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
33
34 @DataSet
35 public class DefinedTermDaoImplTest extends CdmIntegrationTest {
36
37 @SpringBeanByType
38 private IDefinedTermDao dao;
39
40 private UUID uuid;
41 private UUID armUuid;
42 private UUID northernEuropeUuid;
43 private UUID middleEuropeUuid;
44 private UUID westTropicalAfricaUuid;
45 private Set<NamedArea> namedAreas;
46
47 @Before
48 public void setUp() {
49 uuid = UUID.fromString("d6781519-ec60-4afa-b5ea-375f4d2a1729");
50 armUuid = UUID.fromString("7a0fde13-26e9-4382-a5c9-5640fc2b3334");
51 northernEuropeUuid = UUID.fromString("22524ba2-6e57-4b71-89ab-89fc50fba6b4");
52 middleEuropeUuid = UUID.fromString("d292f237-da3d-408b-93a1-3257a8c80b97");
53 westTropicalAfricaUuid = UUID.fromString("931164ad-ec16-4133-afab-bdef25d67636");
54 namedAreas = new HashSet<NamedArea>();
55 }
56
57 @Test
58 public void findByTitle() throws Exception {
59 List<DefinedTermBase> terms = dao.findByTitle("nomenclature");
60 assertNotNull("findByTitle should return a List", terms);
61 assertEquals("findByTitle should return one term ",terms.size(),1);
62 assertEquals("findByTitle should return a term with uuid " + uuid,terms.get(0).getUuid(),uuid);
63 }
64
65 /**
66 * FIXME Should list() be tested in CdmEntityDaoBaseTest?
67 * Also - how is this list sorted? Should we supply an enum that allows
68 * the list to be sorted by different fields (titleCache? label? text? uri?)
69 */
70 @Test
71 public void listOneTerm() {
72 List<DefinedTermBase> terms = dao.list(1,2017);
73 assertNotNull("list should return a list",terms);
74 assertEquals("list should return one term",1, terms.size());
75 assertEquals("list should return one term with uuid " + uuid, uuid,terms.get(0).getUuid());
76 }
77
78 @Test
79 public void listManyTerms() {
80 List<DefinedTermBase> terms = dao.list(5,2013);
81 assertNotNull("list should return a list",terms);
82 assertEquals("list should return five terms",5, terms.size());
83 assertEquals("list should return a term with uuid " + uuid + " at position 5", uuid,terms.get(4).getUuid());
84 }
85
86 @Test
87 public void getTermByUUID() {
88 DefinedTermBase term = dao.findByUuid(uuid);
89 assertNotNull("findByUuid should return a term",term);
90 }
91
92
93 @Test
94 public void getLanguageByIso2() {
95 Language lang = dao.getLanguageByIso("arm");
96 assertEquals("getLanguageByIso should return the correct Language instance",lang.getUuid(), armUuid);
97 }
98
99 @Test
100 public void getLanguageByIso1() {
101 Language lang = dao.getLanguageByIso("hy");
102 assertEquals("getLanguageByIso should return the correct Language instance",lang.getUuid(), armUuid);
103 }
104
105 @Test
106 public void getLanguageByMalformedIso1() {
107 Language lang = dao.getLanguageByIso("a");
108 assertNull("getLanguageByIso should return null for this malformed Iso \'a\'",lang);
109 }
110
111 @Test
112 public void getLanguageByMalformedIso2() {
113 Language lang = dao.getLanguageByIso("abcd");
114 assertNull("getLanguageByIso should return null for this malformed Iso \'abcd\'",lang);
115 }
116
117 @Test
118 public void testGetIncludes() {
119 NamedArea northernEurope = (NamedArea)dao.findByUuid(northernEuropeUuid);
120 assert northernEurope != null : "NamedArea must exist";
121 namedAreas.add(northernEurope);
122
123 List<NamedArea> includes = dao.getIncludes(namedAreas, null, null);
124
125 assertNotNull("getIncludes should return a List",includes);
126 assertFalse("The list should not be empty",includes.isEmpty());
127 assertEquals("getIncludes should return 9 NamedArea entities",9,includes.size());
128 }
129
130 @Test
131 public void countIncludes() {
132 NamedArea northernEurope = (NamedArea)dao.findByUuid(northernEuropeUuid);
133 assert northernEurope != null : "NamedArea must exist";
134 namedAreas.add(northernEurope);
135
136 int numberOfIncludes = dao.countIncludes(namedAreas);
137 assertEquals("countIncludes should return 9",9, numberOfIncludes);
138
139 }
140
141 @Test
142 public void testGetPartOf() {
143 NamedArea northernEurope = (NamedArea)dao.findByUuid(northernEuropeUuid);
144 NamedArea middleEurope = (NamedArea)dao.findByUuid(middleEuropeUuid);
145 NamedArea westTropicalAfrica = (NamedArea)dao.findByUuid(westTropicalAfricaUuid);
146 assert northernEurope != null : "NamedArea must exist";
147 assert middleEurope != null : "NamedArea must exist";
148 assert westTropicalAfrica != null : "NamedArea must exist";
149 namedAreas.add(northernEurope);
150 namedAreas.add(middleEurope);
151 namedAreas.add(westTropicalAfrica);
152
153 List<NamedArea> partOf = dao.getPartOf(namedAreas, null, null);
154
155 assertNotNull("getPartOf should return a List",partOf);
156 assertFalse("The list should not be empty",partOf.isEmpty());
157 assertEquals("getPartOf should return 2 NamedArea entities",2,partOf.size());
158 }
159
160 @Test
161 public void countPartOf() {
162 NamedArea northernEurope = (NamedArea)dao.findByUuid(northernEuropeUuid);
163 NamedArea middleEurope = (NamedArea)dao.findByUuid(middleEuropeUuid);
164 NamedArea westTropicalAfrica = (NamedArea)dao.findByUuid(westTropicalAfricaUuid);
165 assert northernEurope != null : "NamedArea must exist";
166 assert middleEurope != null : "NamedArea must exist";
167 assert westTropicalAfrica != null : "NamedArea must exist";
168 namedAreas.add(northernEurope);
169 namedAreas.add(middleEurope);
170 namedAreas.add(westTropicalAfrica);
171
172 int numberOfPartOf = dao.countPartOf(namedAreas);
173 assertEquals("countPartOf should return 2",2,numberOfPartOf);
174 }
175 }