c3656cfd4c4dcc9de769eb7285bd74290e950af9
[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 com.google.common.base.Preconditions;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.net.URISyntaxException;
17 import java.nio.file.Files;
18 import java.nio.file.Path;
19 import java.nio.file.Paths;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.Parameterized;
33 import org.junit.runners.model.InitializationError;
34 import org.opendaylight.controller.config.util.xml.XmlElement;
35 import org.opendaylight.controller.config.util.xml.XmlUtil;
36 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
41 import org.w3c.dom.Document;
42 import org.xml.sax.SAXException;
43
44 @RunWith(value = Parameterized.class)
45 public class FilterContentValidatorTest {
46
47     private static final int TEST_CASE_COUNT = 13;
48     private static final Pattern LIST_ENTRY_PATTERN =
49             Pattern.compile("(?<listName>.*)\\[\\{(?<keys>(.*)(, .*)*)\\}\\]");
50     private static final Pattern KEY_VALUE_PATTERN =
51             Pattern.compile("(?<key>\\(.*\\).*)=(?<value>.*)");
52     private final XmlElement filterContent;
53     private final String expected;
54     private FilterContentValidator validator;
55
56     @Parameterized.Parameters
57     public static Collection<Object[]> data() throws IOException, SAXException, URISyntaxException, InitializationError {
58         final List<Object[]> result = new ArrayList<>();
59         final Path path = Paths.get(FilterContentValidatorTest.class.getResource("/filter/expected.txt").toURI());
60         final List<String> expected = Files.readAllLines(path);
61         if (expected.size() != TEST_CASE_COUNT) {
62             throw new InitializationError("Number of lines in results file must be same as test case count");
63         }
64         for (int i = 1; i <= TEST_CASE_COUNT; i++) {
65             final Document document = XmlUtil.readXmlToDocument(FilterContentValidatorTest.class.getResourceAsStream("/filter/f" + i + ".xml"));
66             result.add(new Object[]{document, expected.get(i-1)});
67         }
68         return result;
69     }
70
71     public FilterContentValidatorTest(final Document filterContent, final String expected) {
72         this.filterContent = XmlElement.fromDomDocument(filterContent);
73         this.expected = expected;
74     }
75
76     @Before
77     public void setUp() throws Exception {
78         final List<InputStream> sources = new ArrayList<>();
79         sources.add(getClass().getResourceAsStream("/yang/filter-validator-test-mod-0.yang"));
80         sources.add(getClass().getResourceAsStream("/yang/filter-validator-test-augment.yang"));
81         final SchemaContext context = YangParserTestUtils.parseYangStreams(sources);
82         final CurrentSchemaContext currentContext = mock(CurrentSchemaContext.class);
83         doReturn(context).when(currentContext).getCurrentContext();
84         validator = new FilterContentValidator(currentContext);
85     }
86
87     @Test
88     public void testValidate() throws Exception {
89         if (expected.startsWith("success")) {
90             final String expId = expected.replace("success=", "");
91             final YangInstanceIdentifier actual = validator.validate(filterContent);
92             final YangInstanceIdentifier expected = fromString(expId);
93             Assert.assertEquals(expected, actual);
94         } else if (expected.startsWith("error")) {
95             try {
96                 validator.validate(filterContent);
97                 Assert.fail(XmlUtil.toString(filterContent) + " is not valid and should throw exception.");
98             } catch (final Exception e) {
99                 final String expectedExceptionClass = expected.replace("error=", "");
100                 Assert.assertEquals(expectedExceptionClass, e.getClass().getName());
101             }
102         }
103     }
104
105     private static YangInstanceIdentifier fromString(final String input) {
106         //remove first /
107         final String yid = input.substring(1);
108         final List<String> pathElements = Arrays.asList(yid.split("/"));
109         final YangInstanceIdentifier.InstanceIdentifierBuilder builder = YangInstanceIdentifier.builder();
110         //if not specified, PathArguments inherit namespace and revision from previous PathArgument
111         QName prev = null;
112         for (final String pathElement : pathElements) {
113             final Matcher matcher = LIST_ENTRY_PATTERN.matcher(pathElement);
114             if (matcher.matches()) {
115                 prev = parseListEntry(builder, prev, matcher);
116             } else {
117                 final QName qName = createNodeQName(prev, pathElement);
118                 builder.node(qName);
119                 prev = qName;
120             }
121         }
122         return builder.build();
123     }
124
125     private static QName parseListEntry(final YangInstanceIdentifier.InstanceIdentifierBuilder builder,
126                                         final QName prev, final Matcher matcher) {
127         final Map<QName, Object> keys = new HashMap<>();
128         final String listName = matcher.group("listName");
129         final QName listQName = createNodeQName(prev, listName);
130         final String keysString = matcher.group("keys");
131         final String[] split = keysString.split(",");
132         for (final String s : split) {
133             final Matcher keyMatcher = KEY_VALUE_PATTERN.matcher(s.trim());
134             if (keyMatcher.matches()) {
135                 final QName keyName = QName.create(keyMatcher.group("key"));
136                 final String keyValue = keyMatcher.group("value");
137                 keys.put(keyName, keyValue);
138             }
139         }
140         builder.nodeWithKey(listQName, keys);
141         return prev;
142     }
143
144     private static QName createNodeQName(final QName prev, final String qNameString) {
145         final QName qName = QName.create(qNameString);
146         if (qName.getModule().getNamespace() != null) {
147             return qName;
148         } else {
149             return QName.create(Preconditions.checkNotNull(prev), qNameString);
150         }
151     }
152 }