1
|
package eu.dnetlib.enabling.is.registry.schema;
|
2
|
|
3
|
import javax.xml.XMLConstants;
|
4
|
import javax.xml.transform.stream.StreamSource;
|
5
|
import javax.xml.validation.Schema;
|
6
|
import javax.xml.validation.SchemaFactory;
|
7
|
|
8
|
import eu.dnetlib.enabling.tools.StreamOpaqueResource;
|
9
|
import org.junit.Before;
|
10
|
import org.junit.Test;
|
11
|
import org.junit.runner.RunWith;
|
12
|
import org.mockito.Mock;
|
13
|
import org.mockito.junit.MockitoJUnitRunner;
|
14
|
import org.xml.sax.SAXException;
|
15
|
|
16
|
import static org.junit.Assert.assertNotNull;
|
17
|
import static org.mockito.Matchers.anyString;
|
18
|
import static org.mockito.Mockito.when;
|
19
|
|
20
|
/**
|
21
|
* test schema validator.
|
22
|
*
|
23
|
* @author marko
|
24
|
*
|
25
|
*/
|
26
|
@RunWith(MockitoJUnitRunner.class)
|
27
|
public class OpaqueResourceValidatorImplTest {
|
28
|
|
29
|
/**
|
30
|
* instance under test.
|
31
|
*/
|
32
|
private transient OpaqueResourceValidatorImpl validator;
|
33
|
|
34
|
/**
|
35
|
* resource schema dao mock.
|
36
|
*/
|
37
|
@Mock
|
38
|
private transient ResourceSchemaDAO schemaDao;
|
39
|
|
40
|
/**
|
41
|
* prepare tests.
|
42
|
*
|
43
|
* @throws SAXException
|
44
|
* schema related
|
45
|
*/
|
46
|
@Before
|
47
|
public void setUp() throws SAXException {
|
48
|
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
49
|
final Schema schema = schemaFactory.newSchema(new StreamSource(getClass().getResourceAsStream("schema.xsd")));
|
50
|
|
51
|
when(schemaDao.getResourceSchema(anyString())).thenReturn(schema);
|
52
|
|
53
|
validator = new OpaqueResourceValidatorImpl();
|
54
|
validator.setSchemaDao(schemaDao);
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* test profile validation.
|
59
|
*
|
60
|
* @throws Exception
|
61
|
* could happen
|
62
|
*/
|
63
|
@Test
|
64
|
public void testValid() throws Exception { // NOPMD
|
65
|
validator.validate(new StreamOpaqueResource(getClass().getResourceAsStream("valid-profile.xml")));
|
66
|
assertNotNull("dummy", validator);
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* test profile validation.
|
71
|
*
|
72
|
* @throws Exception
|
73
|
* could happen
|
74
|
*/
|
75
|
@Test(expected = ValidationException.class)
|
76
|
public void testInvalid() throws Exception { // NOPMD
|
77
|
validator.validate(new StreamOpaqueResource(getClass().getResourceAsStream("invalid-profile.xml")));
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Validate a profile which has an optional "any" element, possible non-standard behavior in java validator.
|
82
|
*
|
83
|
* @throws Exception could happen
|
84
|
*/
|
85
|
@Test
|
86
|
public void testValidAnyOptional() throws Exception { // NOPMD
|
87
|
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
88
|
final Schema schema = schemaFactory.newSchema(new StreamSource(getClass().getResourceAsStream("any-schema.xsd")));
|
89
|
|
90
|
when(schemaDao.getResourceSchema(anyString())).thenReturn(schema);
|
91
|
|
92
|
validator.validate(new StreamOpaqueResource(getClass().getResourceAsStream("valid-any-optional.xml")));
|
93
|
}
|
94
|
|
95
|
}
|