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
|
package eu.etaxonomy.cdm.validation;
|
10
|
|
11
|
import static org.junit.Assert.assertFalse;
|
12
|
import static org.junit.Assert.assertTrue;
|
13
|
|
14
|
import java.net.URISyntaxException;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import javax.validation.ConstraintViolation;
|
18
|
import javax.validation.groups.Default;
|
19
|
|
20
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
21
|
import org.junit.Assert;
|
22
|
import org.junit.Before;
|
23
|
import org.junit.Test;
|
24
|
|
25
|
import eu.etaxonomy.cdm.common.URI;
|
26
|
import eu.etaxonomy.cdm.model.reference.IBook;
|
27
|
import eu.etaxonomy.cdm.model.reference.IBookSection;
|
28
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
29
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
30
|
import eu.etaxonomy.cdm.validation.constraint.InReferenceValidator;
|
31
|
import eu.etaxonomy.cdm.validation.constraint.NoRecursiveInReferenceValidator;
|
32
|
|
33
|
/**
|
34
|
* @author ben.clark
|
35
|
*/
|
36
|
@SuppressWarnings("unused")
|
37
|
public class ReferenceValidationTest extends ValidationTestBase {
|
38
|
private static final Logger logger = LogManager.getLogger(ReferenceValidationTest.class);
|
39
|
|
40
|
private IBook book;
|
41
|
|
42
|
@Before
|
43
|
public void setUp() {
|
44
|
book = ReferenceFactory.newBook();
|
45
|
book.setTitleCache("Lorem ipsum", true);
|
46
|
book.setIsbn("1-919795-99-5");
|
47
|
}
|
48
|
|
49
|
/****************** TESTS *****************************/
|
50
|
|
51
|
/**
|
52
|
* Test validation at the second level with a valid reference
|
53
|
*/
|
54
|
@Test
|
55
|
public final void testLevel2ValidationWithValidBook() {
|
56
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class, Default.class);
|
57
|
assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
|
58
|
}
|
59
|
|
60
|
@Test
|
61
|
public final void testLevel2ValidationWithValidISBN() {
|
62
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
63
|
assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
|
64
|
}
|
65
|
|
66
|
@Test
|
67
|
public final void testLevel2ValidationWithInValidISBN() {
|
68
|
book.setIsbn("1-9197954-99-5");
|
69
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
70
|
assertFalse("There should be a constraint violation as this book has an invalid ISBN number",constraintViolations.isEmpty());
|
71
|
}
|
72
|
|
73
|
@Test
|
74
|
public final void testLevel2ValidationWithValidUri() {
|
75
|
try {
|
76
|
book.setUri(new URI("http://www.e-taxonomy.eu"));
|
77
|
} catch (URISyntaxException e) {
|
78
|
Assert.fail("URI is not valid");
|
79
|
}
|
80
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
81
|
assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
|
82
|
}
|
83
|
|
84
|
@Test
|
85
|
public final void testLevel3ValidationWithInValidInReference() {
|
86
|
|
87
|
IBookSection bookSection = ReferenceFactory.newBookSection();
|
88
|
bookSection.setTitleCache("test", true);
|
89
|
bookSection.setTitle("");
|
90
|
bookSection.setInReference((Reference)book);
|
91
|
Set<ConstraintViolation<IBookSection>> constraintViolations = validator.validate(bookSection, Level3.class);
|
92
|
assertTrue("There should be one constraint violation as this book has a valid Ref",constraintViolations.size() == 0);
|
93
|
|
94
|
Reference article = ReferenceFactory.newArticle();
|
95
|
article.setTitleCache("article", true);
|
96
|
bookSection.setInReference(article);
|
97
|
constraintViolations = validator.validate(bookSection, Level3.class);
|
98
|
// assertTrue("There should be a constraint violation as this book has an invalid inReference",constraintViolations.size() == 1);
|
99
|
assertHasConstraintOnValidator((Set)constraintViolations, InReferenceValidator.class);
|
100
|
}
|
101
|
|
102
|
@Test
|
103
|
public final void testValidationAfterCasting(){
|
104
|
((Reference)book).castReferenceToArticle();
|
105
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
106
|
assertFalse("There should be one constraint violations as this article is not valid at level 2 (has an isbn)", constraintViolations.isEmpty());
|
107
|
}
|
108
|
|
109
|
@Test
|
110
|
public final void testNoRecursiveInReference(){
|
111
|
Reference myRef = ReferenceFactory.newBookSection();
|
112
|
myRef.setInReference(myRef);
|
113
|
myRef.setTitle("My book section");
|
114
|
assertHasConstraintOnValidator((Set)validator.validate(myRef, Level3.class), NoRecursiveInReferenceValidator.class);
|
115
|
|
116
|
myRef.setInBook(book);
|
117
|
assertNoConstraintOnValidator((Set)validator.validate(myRef, Level3.class), NoRecursiveInReferenceValidator.class);
|
118
|
}
|
119
|
}
|