da7a3b16651568aa97e50019e5e2bc0217d487c4
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / validation / ValidationTest.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.validation;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16
17 import java.util.Set;
18 import java.util.UUID;
19
20 import javax.validation.ConstraintViolation;
21 import javax.validation.Validation;
22 import javax.validation.Validator;
23 import javax.validation.ValidatorFactory;
24 import javax.validation.groups.Default;
25
26 import junit.framework.Assert;
27
28 import org.apache.log4j.Logger;
29 import org.junit.Before;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.unitils.UnitilsJUnit4;
33 import org.unitils.dbunit.annotation.DataSet;
34 import org.unitils.spring.annotation.SpringBeanByType;
35
36 import eu.etaxonomy.cdm.api.service.ITermService;
37 import eu.etaxonomy.cdm.model.agent.AgentBase;
38 import eu.etaxonomy.cdm.model.agent.Person;
39 import eu.etaxonomy.cdm.model.common.Annotation;
40 import eu.etaxonomy.cdm.model.common.CdmBase;
41 import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
42 import eu.etaxonomy.cdm.model.common.Group;
43 import eu.etaxonomy.cdm.model.common.Language;
44 import eu.etaxonomy.cdm.model.common.User;
45 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
46 import eu.etaxonomy.cdm.model.name.BotanicalName;
47 import eu.etaxonomy.cdm.model.name.Rank;
48 import eu.etaxonomy.cdm.model.occurrence.Specimen;
49 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
50
51 import eu.etaxonomy.cdm.model.reference.Reference;
52 import eu.etaxonomy.cdm.model.taxon.Taxon;
53 import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
54
55 /**
56 * NOTE: In this test, the words "valid" and "invalid", loaded though
57 * these terms are when applied to taxonomic names, only mean "passes the
58 * rules of this validator" or not and should not be confused with the strict
59 * nomenclatural and taxonomic sense of these words.
60 *
61 * @author ben.clark
62 *
63 */
64 @SuppressWarnings("unused")
65 //@Ignore //FIXME ignoring just for today 9.6.2010 a.kohlbecker !!!!!!!!!!!!!!!!!!!!!
66 public class ValidationTest extends CdmIntegrationTest {
67 private static final Logger logger = Logger.getLogger(ValidationTest.class);
68
69 @SpringBeanByType
70 private Validator validator;
71
72 @SpringBeanByType
73 private ITermService termService;
74
75 private BotanicalName name;
76
77 @Before
78 public void setUp() {
79 /*DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
80 vocabularyStore.initialize();
81 ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
82 validator = validatorFactory.getValidator();*/
83
84 Rank speciesRank = (Rank)termService.find(Rank.uuidSpecies);
85 name = BotanicalName.NewInstance(speciesRank);
86 }
87
88
89 /****************** TESTS *****************************/
90
91 /**
92 * Test validation factory initialization and autowiring
93 * into an instance of javax.valdation.Validator
94 */
95 @Test
96 @DataSet
97 public final void testValidatorAutowire() {
98 assertNotNull("the validator should not be null", validator);
99 }
100
101 /**
102 * Test validation at the "default" level with a valid name
103 */
104 @Test
105 @DataSet
106 public final void testDefaultValidationWithValidName() {
107 Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name);
108 assertTrue("There should be no constraint violations as this name is valid at the default level",constraintViolations.isEmpty());
109 }
110
111 /**
112 * Test validation at the "default" level with an invalid name
113 */
114 @Test
115 @DataSet
116 public final void testDefaultValidationWithInValidName() {
117 name.setGenusOrUninomial("");
118 Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name);
119 assertFalse("There should be a constraint violation as this name is invalid at the default level",constraintViolations.isEmpty());
120 }
121
122 /**
123 * Test validation at level2 with a valid name
124 */
125 @Test
126 @DataSet
127 public final void testLevel2ValidationWithValidName() {
128 name.setGenusOrUninomial("Abies");
129 name.setSpecificEpithet("balsamea");
130 name.setNameCache("Abies balsamea");
131 name.setAuthorshipCache("L.");
132 name.setTitleCache("Abies balsamea L.", true);
133 name.setFullTitleCache("Abies balsamea L.");
134
135 Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class,Level2.class);
136 assertTrue("There should not be a constraint violation as this name is valid at the default and at the second level",constraintViolations.isEmpty());
137 }
138
139 /**
140 * Test validation at level2 with an invalid name
141 */
142 @Test
143 @DataSet
144 public final void testLevel2ValidationWithInValidName() {
145 name.setGenusOrUninomial("Abies");
146 name.setSpecificEpithet("balsamea");
147
148
149 Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class);
150 assertTrue("There should not be a constraint violation as this name is valid at the default level",constraintViolations.isEmpty());
151
152 constraintViolations = validator.validate(name, Default.class,Level2.class);
153 assertFalse("There should be a constraint violation as this name is valid at the default level, but invalid at the second level",constraintViolations.isEmpty());
154 }
155
156 /**
157 * Test validation at level3 with a valid name
158 */
159 @Test
160 @DataSet
161 public final void testLevel3ValidationWithValidName() {
162 name.setGenusOrUninomial("Abies");
163 name.setSpecificEpithet("balsamea");
164 name.setNameCache("Abies balsamea");
165 name.setAuthorshipCache("L.");
166 name.setTitleCache("Abies balsamea L.", true);
167 name.setFullTitleCache("Abies balsamea L.");
168
169 Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class,Level2.class, Level3.class);
170 assertTrue("There should not be a constraint violation as this name is valid at all levels",constraintViolations.isEmpty());
171 }
172
173 /**
174 * Test validation at the level3 with an invalid name
175 */
176 @Test
177 @DataSet
178 public final void testLevel3ValidationWithInValidName() {
179 name.setGenusOrUninomial("Abies");
180 name.setSpecificEpithet("alba");
181 name.setNameCache("Abies alba");
182 name.setAuthorshipCache("Mill.");
183 name.setTitleCache("Abies alba Mill.", true);
184 name.setFullTitleCache("Abies alba Mill.");
185 name.setNomenclaturalReference(null);
186 //name.setNomenclaturalMicroReference(" ");
187
188 Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class, Level2.class);
189 assertTrue("There should not be a constraint violation as this name is valid at the default and second level",constraintViolations.isEmpty());
190 constraintViolations = validator.validate(name, Default.class,Level2.class, Level3.class);
191 assertFalse("There should be a constraint violation as this name is valid at the default and second level, but invalid at the third level",constraintViolations.isEmpty());
192
193 }
194 }