Bug 7432 - eliminate use of yang-parser-impl internals
[netconf.git] / netconf / mdsal-netconf-connector / src / test / java / org / opendaylight / netconf / mdsal / connector / ops / get / FilterContentValidatorTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.mdsal.connector.ops.get;
9
10 import static org.mockito.Mockito.doReturn;
11 import static org.mockito.Mockito.mock;
12
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.net.URISyntaxException;
16 import java.nio.file.Files;
17 import java.nio.file.Path;
18 import java.nio.file.Paths;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.Parameterized;
27 import org.junit.runners.model.InitializationError;
28 import org.opendaylight.controller.config.util.xml.XmlElement;
29 import org.opendaylight.controller.config.util.xml.XmlUtil;
30 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33 import org.w3c.dom.Document;
34 import org.xml.sax.SAXException;
35
36 @RunWith(value = Parameterized.class)
37 public class FilterContentValidatorTest {
38
39     private static final int TEST_CASE_COUNT = 8;
40     private final XmlElement filterContent;
41     private final String expected;
42     private FilterContentValidator validator;
43
44     @Parameterized.Parameters
45     public static Collection<Object[]> data() throws IOException, SAXException, URISyntaxException, InitializationError {
46         List<Object[]> result = new ArrayList<>();
47         final Path path = Paths.get(FilterContentValidatorTest.class.getResource("/filter/expected.txt").toURI());
48         final List<String> expected = Files.readAllLines(path);
49         if (expected.size() != TEST_CASE_COUNT) {
50             throw new InitializationError("Number of lines in results file must be same as test case count");
51         }
52         for (int i = 1; i <= TEST_CASE_COUNT; i++) {
53             final Document document = XmlUtil.readXmlToDocument(FilterContentValidatorTest.class.getResourceAsStream("/filter/f" + i + ".xml"));
54             result.add(new Object[]{document, expected.get(i-1)});
55         }
56         return result;
57     }
58
59     public FilterContentValidatorTest(Document filterContent, String expected) {
60         this.filterContent = XmlElement.fromDomDocument(filterContent);
61         this.expected = expected;
62     }
63
64     @Before
65     public void setUp() throws Exception {
66         List<InputStream> sources = new ArrayList<>();
67         sources.add(getClass().getResourceAsStream("/yang/filter-validator-test-mod-0.yang"));
68         sources.add(getClass().getResourceAsStream("/yang/filter-validator-test-augment.yang"));
69         SchemaContext context = YangParserTestUtils.parseYangStreams(sources);
70         CurrentSchemaContext currentContext = mock(CurrentSchemaContext.class);
71         doReturn(context).when(currentContext).getCurrentContext();
72         validator = new FilterContentValidator(currentContext);
73     }
74
75     @Test
76     public void testValidate() throws Exception {
77         if (expected.startsWith("success")) {
78             final String expId = expected.replace("success=", "");
79             Assert.assertEquals(expId, validator.validate(filterContent).toString());
80         } else if (expected.startsWith("error")) {
81             try {
82                 validator.validate(filterContent);
83                 Assert.fail(XmlUtil.toString(filterContent) + " is not valid and should throw exception.");
84             } catch (Exception e) {
85                 final String expectedExceptionClass = expected.replace("error=", "");
86                 Assert.assertEquals(expectedExceptionClass, e.getClass().getName());
87             }
88         }
89
90     }
91 }