Use keyed list as a selection node in filter
[netconf.git] / netconf / mdsal-netconf-connector / src / test / java / org / opendaylight / netconf / mdsal / connector / ops / get / FilterContentValidatorTest.java
1 package org.opendaylight.netconf.mdsal.connector.ops.get;
2
3 import static org.mockito.Mockito.doReturn;
4 import static org.mockito.Mockito.mock;
5
6 import java.io.FileNotFoundException;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.net.URISyntaxException;
10 import java.nio.file.Files;
11 import java.nio.file.Path;
12 import java.nio.file.Paths;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.junit.runners.Parameterized;
21 import org.junit.runners.model.InitializationError;
22 import org.opendaylight.controller.config.util.xml.XmlElement;
23 import org.opendaylight.controller.config.util.xml.XmlUtil;
24 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
31 import org.w3c.dom.Document;
32 import org.xml.sax.SAXException;
33
34 @RunWith(value = Parameterized.class)
35 public class FilterContentValidatorTest {
36
37     private static final int TEST_CASE_COUNT = 8;
38     private final XmlElement filterContent;
39     private final String expected;
40     private FilterContentValidator validator;
41
42     @Parameterized.Parameters
43     public static Collection<Object[]> data() throws IOException, SAXException, URISyntaxException, InitializationError {
44         List<Object[]> result = new ArrayList<>();
45         final Path path = Paths.get(FilterContentValidatorTest.class.getResource("/filter/expected.txt").toURI());
46         final List<String> expected = Files.readAllLines(path);
47         if (expected.size() != TEST_CASE_COUNT) {
48             throw new InitializationError("Number of lines in results file must be same as test case count");
49         }
50         for (int i = 1; i <= TEST_CASE_COUNT; i++) {
51             final Document document = XmlUtil.readXmlToDocument(FilterContentValidatorTest.class.getResourceAsStream("/filter/f" + i + ".xml"));
52             result.add(new Object[]{document, expected.get(i-1)});
53         }
54         return result;
55     }
56
57     public FilterContentValidatorTest(Document filterContent, String expected) {
58         this.filterContent = XmlElement.fromDomDocument(filterContent);
59         this.expected = expected;
60     }
61
62     @Before
63     public void setUp() throws Exception {
64         List<InputStream> sources = new ArrayList<>();
65         sources.add(getClass().getResourceAsStream("/yang/filter-validator-test-mod-0.yang"));
66         sources.add(getClass().getResourceAsStream("/yang/filter-validator-test-augment.yang"));
67         SchemaContext context = parseYangSources(sources);
68         CurrentSchemaContext currentContext = mock(CurrentSchemaContext.class);
69         doReturn(context).when(currentContext).getCurrentContext();
70         validator = new FilterContentValidator(currentContext);
71     }
72
73     @Test
74     public void testValidate() throws Exception {
75         if (expected.startsWith("success")) {
76             final String expId = expected.replace("success=", "");
77             Assert.assertEquals(expId, validator.validate(filterContent).toString());
78         } else if (expected.startsWith("error")) {
79             try {
80                 validator.validate(filterContent);
81                 Assert.fail(XmlUtil.toString(filterContent) + " is not valid and should throw exception.");
82             } catch (Exception e) {
83                 final String expectedExceptionClass = expected.replace("error=", "");
84                 Assert.assertEquals(expectedExceptionClass, e.getClass().getName());
85             }
86         }
87
88     }
89
90     public static SchemaContext parseYangSources(Collection<InputStream> testFiles)
91             throws SourceException, ReactorException, FileNotFoundException {
92         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
93                 .newBuild();
94         for (InputStream testFile : testFiles) {
95             reactor.addSource(new YangStatementSourceImpl(testFile));
96         }
97         return reactor.buildEffective();
98     }
99 }