Project

General

Profile

1
package eu.dnetlib.enabling.is.registry.schema;
2

    
3
import static org.junit.Assert.assertNotNull;
4
import static org.mockito.Matchers.anyString;
5
import static org.mockito.Mockito.when;
6

    
7
import javax.xml.XMLConstants;
8
import javax.xml.transform.stream.StreamSource;
9
import javax.xml.validation.Schema;
10
import javax.xml.validation.SchemaFactory;
11

    
12
import org.junit.Before;
13
import org.junit.Test;
14
import org.junit.runner.RunWith;
15
import org.mockito.Mock;
16
import org.mockito.junit.MockitoJUnitRunner;
17
import org.xml.sax.SAXException;
18

    
19
import eu.dnetlib.enabling.tools.StreamOpaqueResource;
20

    
21
/**
22
 * test schema validator.
23
 *
24
 * @author marko
25
 *
26
 */
27
@RunWith(MockitoJUnitRunner.class)
28
public class OpaqueResourceValidatorImplTest {
29

    
30
	/**
31
	 * instance under test.
32
	 */
33
	private transient OpaqueResourceValidatorImpl validator;
34

    
35
	/**
36
	 * resource schema dao mock.
37
	 */
38
	@Mock
39
	private transient ResourceSchemaDAO schemaDao;
40

    
41
	/**
42
	 * prepare tests.
43
	 *
44
	 * @throws SAXException
45
	 *             schema related
46
	 */
47
	@Before
48
	public void setUp() throws SAXException {
49
		final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
50
		final Schema schema = schemaFactory.newSchema(new StreamSource(getClass().getResourceAsStream("schema.xsd")));
51

    
52
		when(schemaDao.getResourceSchema(anyString())).thenReturn(schema);
53

    
54
		validator = new OpaqueResourceValidatorImpl();
55
		validator.setSchemaDao(schemaDao);
56
	}
57

    
58
	/**
59
	 * test profile validation.
60
	 *
61
	 * @throws Exception
62
	 *             could happen
63
	 */
64
	@Test
65
	public void testValid() throws Exception { // NOPMD
66
		validator.validate(new StreamOpaqueResource(getClass().getResourceAsStream("valid-profile.xml")));
67
		assertNotNull("dummy", validator);
68
	}
69

    
70
	/**
71
	 * test profile validation.
72
	 *
73
	 * @throws Exception
74
	 *             could happen
75
	 */
76
	@Test(expected = ValidationException.class)
77
	public void testInvalid() throws Exception { // NOPMD
78
		validator.validate(new StreamOpaqueResource(getClass().getResourceAsStream("invalid-profile.xml")));
79
	}
80

    
81
	/**
82
	 * Validate a profile which has an optional "any" element, possible non-standard behavior in java validator.
83
	 *
84
	 * @throws Exception could happen
85
	 */
86
	@Test
87
	public void testValidAnyOptional() throws Exception { // NOPMD
88
		final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
89
		final Schema schema = schemaFactory.newSchema(new StreamSource(getClass().getResourceAsStream("any-schema.xsd")));
90

    
91
		when(schemaDao.getResourceSchema(anyString())).thenReturn(schema);
92

    
93
		validator.validate(new StreamOpaqueResource(getClass().getResourceAsStream("valid-any-optional.xml")));
94
	}
95

    
96
}
(1-1/2)