remove ignore
[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 public class ValidationTest extends CdmIntegrationTest {
66 private static final Logger logger = Logger.getLogger(ValidationTest.class);
67
68 @SpringBeanByType
69 private Validator validator;
70
71 @SpringBeanByType
72 private ITermService termService;
73
74 private BotanicalName name;
75
76 @Before
77 public void setUp() {
78 /*DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
79 vocabularyStore.initialize();
80 ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
81 validator = validatorFactory.getValidator();*/
82
83 Rank speciesRank = (Rank)termService.find(Rank.uuidSpecies);
84 name = BotanicalName.NewInstance(speciesRank);
85 }
86
87
88 /****************** TESTS *****************************/
89
90 /**
91 * Test validation factory initialization and autowiring
92 * into an instance of javax.valdation.Validator
93 */
94 @Test
95 @DataSet
96 public final void testValidatorAutowire() {
97 assertNotNull("the validator should not be null", validator);
98 }
99
100 /**
101 * Test validation at the "default" level with a valid name
102 */
103 @Test
104 @DataSet
105 public final void testDefaultValidationWithValidName() {
106 Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name);
107 assertTrue("There should be no constraint violations as this name is valid at the default level",constraintViolations.isEmpty());
108 }
109
110 /**
111 * Test validation at the "default" level with an invalid name
112 */
113 @Test
114 @DataSet
115 public final void testDefaultValidationWithInValidName() {
116 name.setGenusOrUninomial("");
117 Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name);
118 assertFalse("There should be a constraint violation as this name is invalid at the default level",constraintViolations.isEmpty());
119 }
120
121 /**
122 * Test validation at level2 with a valid name
123 */
124 @Test
125 @DataSet
126 public final void testLevel2ValidationWithValidName() {
127 name.setGenusOrUninomial("Abies");
128 name.setSpecificEpithet("balsamea");
129 name.setNameCache("Abies balsamea");
130 name.setAuthorshipCache("L.");
131 name.setTitleCache("Abies balsamea L.", true);
132 name.setFullTitleCache("Abies balsamea L.");
133
134 Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class,Level2.class);
135 assertTrue("There should not be a constraint violation as this name is valid at the default and at the second level",constraintViolations.isEmpty());
136 }
137
138 /**
139 * Test validation at level2 with an invalid name
140 */
141 @Test
142 @DataSet
143 public final void testLevel2ValidationWithInValidName() {
144 name.setGenusOrUninomial("Abies");
145 name.setSpecificEpithet("balsamea");
146
147
148 Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class);
149 assertTrue("There should not be a constraint violation as this name is valid at the default level",constraintViolations.isEmpty());
150
151 constraintViolations = validator.validate(name, Default.class,Level2.class);
152 assertFalse("There should be a constraint violation as this name is valid at the default level, but invalid at the second level",constraintViolations.isEmpty());
153 }
154
155 /**
156 * Test validation at level3 with a valid name
157 */
158 @Test
159 @DataSet
160 public final void testLevel3ValidationWithValidName() {
161 name.setGenusOrUninomial("Abies");
162 name.setSpecificEpithet("balsamea");
163 name.setNameCache("Abies balsamea");
164 name.setAuthorshipCache("L.");
165 name.setTitleCache("Abies balsamea L.", true);
166 name.setFullTitleCache("Abies balsamea L.");
167
168 Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class,Level2.class, Level3.class);
169 assertTrue("There should not be a constraint violation as this name is valid at all levels",constraintViolations.isEmpty());
170 }
171
172 /**
173 * Test validation at the level3 with an invalid name
174 */
175 @Test
176 @DataSet
177 public final void testLevel3ValidationWithInValidName() {
178 name.setGenusOrUninomial("Abies");
179 name.setSpecificEpithet("alba");
180 name.setNameCache("Abies alba");
181 name.setAuthorshipCache("Mill.");
182 name.setTitleCache("Abies alba Mill.", true);
183 name.setFullTitleCache("Abies alba Mill.");
184 name.setNomenclaturalReference(null);
185 //name.setNomenclaturalMicroReference(" ");
186
187 Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class, Level2.class);
188 assertTrue("There should not be a constraint violation as this name is valid at the default and second level",constraintViolations.isEmpty());
189 constraintViolations = validator.validate(name, Default.class,Level2.class, Level3.class);
190 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());
191
192 }
193 }