summaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/config/model/application/provider/SchemaValidatorTest.java
blob: cbf7f4e9cd5c3323d95b7931752c0c43abf48a2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model.application.provider;

import com.yahoo.component.Version;
import org.junit.Test;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.StringReader;

/**
 * @author hmusum
 * @since 5.1.9
 */
public class SchemaValidatorTest {

    private static final String okServices = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<services>" +
            "  <config name=\"standard\">" +
            "    <basicStruct>" +
            "      <stringVal>default</stringVal>" +
            "    </basicStruct>" +
            "  </config> " +
            "  <admin version=\"2.0\">" +
            "    <adminserver hostalias=\"node1\" />" +
            "  </admin>" +
            "</services>";

    private static final String badServices = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<services>" +
            "  <config name=\"standard\">" +
            "    <basicStruct>" +
            "      <stringVal>default</stringVal>" +
            "    </basicStruct>" +
            "  </config> " +
            "  <admin version=\"2.0\">" +
            "    <adminserver hostalias=\"node1\"" +
            "  </admin>" +
            "</services>";


    @Test
    public void testXMLParse() throws SAXException, IOException {
        SchemaValidator validator = createValidator();
        validator.validate(new InputSource(new StringReader(okServices)), "services.xml");
    }

    @Test(expected = RuntimeException.class)
    public void testXMLParseError() throws SAXException, IOException {
        SchemaValidator validator = createValidator();
        validator.validate(new InputSource(new StringReader(badServices)), "services.xml");
    }

    @Test
    public void testXMLParseWithReader() throws SAXException, IOException {
        SchemaValidator validator = createValidator();
        validator.validate(new StringReader(okServices));
    }

    @Test(expected = RuntimeException.class)
    public void testXMLParseErrorWithReader() throws SAXException, IOException {
        SchemaValidator validator = createValidator();
        validator.validate(new StringReader(badServices));
    }

    private SchemaValidator createValidator() throws IOException {
        return SchemaValidator.createTestValidatorServices(new Version(6));
    }
}