Project

General

Profile

Download (9.82 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.validation;
11

    
12
import static org.junit.Assert.assertFalse;
13
import static org.junit.Assert.assertTrue;
14

    
15
import java.util.Set;
16

    
17
import javax.validation.ConstraintViolation;
18

    
19
import org.apache.log4j.Logger;
20
import org.junit.Assert;
21
import org.junit.Before;
22
import org.junit.Test;
23

    
24
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
25
import eu.etaxonomy.cdm.model.name.HybridRelationshipType;
26
import eu.etaxonomy.cdm.model.name.Rank;
27
import eu.etaxonomy.cdm.model.name.TaxonName;
28
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
29
import eu.etaxonomy.cdm.validation.constraint.NameMustFollowCodeValidator;
30

    
31

    
32

    
33
/**
34
 * Test class for {@link NameMustFollowCodeValidator}
35
 *
36
 * @author a.mueller
37
 * @date 11.03.2017
38
 */
39
public class NameMustFollowCodeTest extends ValidationTestBase {
40
	@SuppressWarnings("unused")
41
    private static final Logger logger = Logger.getLogger(NameMustFollowCodeTest.class);
42

    
43
	private TaxonName nonViralName;
44
    private TaxonName viralName;
45
    private TaxonName bacterialName;
46
    private TaxonName zoologicalName;
47
    private TaxonName cultivarName;
48

    
49
	@Before
50
	public void setUp() {
51
		DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
52
		vocabularyStore.initialize();
53
		nonViralName = TaxonNameFactory.NewNonViralInstance(Rank.SPECIES());
54
		viralName = TaxonNameFactory.NewViralInstance(Rank.SPECIES());
55
		zoologicalName = TaxonNameFactory.NewZoologicalInstance(Rank.SPECIES());
56
		bacterialName = TaxonNameFactory.NewBacterialInstance(Rank.SPECIES());
57
	    cultivarName = TaxonNameFactory.NewCultivarInstance(Rank.SPECIES());
58
	}
59

    
60

    
61
/****************** TESTS *****************************/
62

    
63
    @Test
64
    public void testValidEmptyNames() {
65
        Set<ConstraintViolation<TaxonName>> constraintViolations
66
                            = validator.validate(cultivarName);
67
        assertTrue("There should be no constraint violations as this name has data set and therefore no unvalid attributes set", constraintViolations.isEmpty());
68

    
69
        constraintViolations  = validator.validate(nonViralName);
70
        assertTrue("There should be no constraint violations as this name has data set and therefore no unvalid attributes set", constraintViolations.isEmpty());
71

    
72
        constraintViolations  = validator.validate(viralName);
73
        assertTrue("There should be no constraint violations as this name has data set and therefore no unvalid attributes set", constraintViolations.isEmpty());
74

    
75
        constraintViolations  = validator.validate(zoologicalName);
76
        assertTrue("There should be no constraint violations as this name has data set and therefore no unvalid attributes set", constraintViolations.isEmpty());
77

    
78
        constraintViolations  = validator.validate(bacterialName);
79
        assertTrue("There should be no constraint violations as this name has data set and therefore no unvalid attributes set", constraintViolations.isEmpty());
80
    }
81

    
82
    @Test
83
    public void testMessage() {
84
        nonViralName.setAcronym("acronym");
85
        Set<ConstraintViolation<TaxonName>> constraintViolations  = validator.validate(nonViralName);
86
        assertFalse("There should be a constraint violation as a nonViralName must not have an acronym", constraintViolations.isEmpty());
87
        String message = constraintViolations.iterator().next().getMessage();
88
        String expected = "Taxon name must only have attributes set that are available according to their code. E.g. 'acronym name' should only be available for viral names.";
89
        Assert.assertEquals(expected, message);
90
    }
91

    
92
	@Test
93
	public void testValidNonViralName() {
94
	    nonViralName.setAcronym("acronym");
95
	    Set<ConstraintViolation<TaxonName>> constraintViolations  = validator.validate(nonViralName);
96
        assertFalse("There should be a constraint violation as a nonViralName must not have an acronym", constraintViolations.isEmpty());
97

    
98
        nonViralName = TaxonNameFactory.NewNonViralInstance(Rank.SPECIES());
99
        nonViralName.setBreed("Breed");
100
        constraintViolations  = validator.validate(nonViralName);
101
        assertFalse("There should be a constraint violation as pure NonViralNames must not have a breed", constraintViolations.isEmpty());
102

    
103
        nonViralName = TaxonNameFactory.NewNonViralInstance(Rank.SPECIES());
104
        nonViralName.setOriginalPublicationYear(1987);
105
        constraintViolations  = validator.validate(nonViralName);
106
        assertFalse("There should be a constraint violation as pure NonViralNames must not have an original publication year", constraintViolations.isEmpty());
107

    
108
        nonViralName = TaxonNameFactory.NewNonViralInstance(Rank.SPECIES());
109
        nonViralName.setPublicationYear(2001);
110
        constraintViolations  = validator.validate(nonViralName);
111
        assertFalse("There should be a constraint violation as pure NonViralNames must not have a publication year", constraintViolations.isEmpty());
112

    
113
        nonViralName = TaxonNameFactory.NewNonViralInstance(Rank.SPECIES());
114
        nonViralName.setSubGenusAuthorship("SubGenusAuthor");
115
        constraintViolations  = validator.validate(nonViralName);
116
        assertFalse("There should be a constraint violation as pure NonViralNames must not have a subgenus author", constraintViolations.isEmpty());
117

    
118
        nonViralName = TaxonNameFactory.NewNonViralInstance(Rank.SPECIES());
119
        nonViralName.setNameApprobation("Name approbation");
120
        constraintViolations  = validator.validate(nonViralName);
121
        assertFalse("There should be a constraint violation as pure NonViralNames must not have a name approbation", constraintViolations.isEmpty());
122

    
123
        //Valid
124
        nonViralName = TaxonNameFactory.NewNonViralInstance(Rank.SPECIES());
125
        nonViralName.setMonomHybrid(true);
126
        constraintViolations  = validator.validate(nonViralName);
127
        assertTrue("There should be no constraint violation as NonViralNames may have a hybrid flag set", constraintViolations.isEmpty());
128

    
129
        nonViralName = TaxonNameFactory.NewNonViralInstance(Rank.SPECIES());
130
        nonViralName.setGenusOrUninomial("Genus");
131
        constraintViolations  = validator.validate(nonViralName);
132
        assertTrue("There should be no constraint violation as NonViralNames may have a genus name set", constraintViolations.isEmpty());
133

    
134
        nonViralName = TaxonNameFactory.NewNonViralInstance(Rank.SPECIES());
135
        nonViralName.setNameCache("NameCache");
136
        constraintViolations  = validator.validate(nonViralName);
137
        assertTrue("There should be no constraint violation as NonViralNames may have the name cache set", constraintViolations.isEmpty());
138

    
139
        nonViralName = TaxonNameFactory.NewNonViralInstance(Rank.SPECIES());
140
        TaxonName childName = TaxonNameFactory.NewViralInstance(Rank.SPECIES());
141
        nonViralName.addHybridChild(childName, HybridRelationshipType.FIRST_PARENT(), null);
142
        constraintViolations  = validator.validate(nonViralName);
143
        assertTrue("There should be no constraint violation as NonViralNames may have a hybrid child", constraintViolations.isEmpty());
144

    
145
        //TBC
146
	}
147

    
148
    @Test
149
    public void testValidViralName() {
150
        viralName.setAcronym("acronym");
151
        Set<ConstraintViolation<TaxonName>> constraintViolations  = validator.validate(viralName);
152
        assertTrue("There should be no constraint violation as a viral name may have acronym set", constraintViolations.isEmpty());
153

    
154
        //Invalid
155
        viralName = TaxonNameFactory.NewViralInstance(Rank.SPECIES());
156
        viralName.setMonomHybrid(true);
157
        constraintViolations  = validator.validate(viralName);
158
        assertFalse("There should be a constraint violation as a ViralName must not have a hybrid flag set", constraintViolations.isEmpty());
159

    
160
        viralName = TaxonNameFactory.NewViralInstance(Rank.SPECIES());
161
        viralName.setGenusOrUninomial("Genus");
162
        constraintViolations  = validator.validate(viralName);
163
        assertFalse("There should be a constraint violation as a ViralName must not have the genus name set", constraintViolations.isEmpty());
164

    
165
        viralName = TaxonNameFactory.NewViralInstance(Rank.SPECIES());
166
        viralName.setNameCache("NameCache");
167
        constraintViolations  = validator.validate(viralName);
168
        assertFalse("There should be a constraint violation as a ViralName must not have the nameCache set", constraintViolations.isEmpty());
169

    
170
        viralName = TaxonNameFactory.NewViralInstance(Rank.SPECIES());
171
        TaxonName childName = TaxonNameFactory.NewViralInstance(Rank.SPECIES());
172
        viralName.addHybridChild(childName, HybridRelationshipType.FIRST_PARENT(), null);
173
        constraintViolations  = validator.validate(viralName);
174
        assertFalse("There should be a constraint violation as a ViralName must not have hybrid child", constraintViolations.isEmpty());
175

    
176
        //TBC
177

    
178
    }
179

    
180
    @Test
181
    public void testValidZoologicalName() {
182
        zoologicalName.setBreed("Breed");
183
        zoologicalName.setOriginalPublicationYear(1987);
184
        zoologicalName.setPublicationYear(2001);
185

    
186
        Set<ConstraintViolation<TaxonName>> constraintViolations  = validator.validate(zoologicalName);
187
        assertTrue("There should be no constraint violation as a zoological name may have breed and years set", constraintViolations.isEmpty());
188
    }
189

    
190
    @Test
191
    public void testValidBacterialName() {
192
        bacterialName.setSubGenusAuthorship("Subgenus author");
193
        bacterialName.setNameApprobation("Name approbation");
194

    
195
        Set<ConstraintViolation<TaxonName>> constraintViolations  = validator.validate(bacterialName);
196
        assertTrue("There should be no constraint violation as a bacterial name may have subgenus authorship or name approbation set", constraintViolations.isEmpty());
197
    }
198

    
199
}
(4-4/11)