Bug 8084 - FilterContentValidator.getKeyValues creates invalid YII key values
[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         sources.add(getClass().getResourceAsStream("/yang/mdsal-netconf-mapping-test.yang"));
82         final SchemaContext context = YangParserTestUtils.parseYangStreams(sources);
83         final CurrentSchemaContext currentContext = mock(CurrentSchemaContext.class);
84         doReturn(context).when(currentContext).getCurrentContext();
85         validator = new FilterContentValidator(currentContext);
86     }
87
88     @Test
89     public void testValidate() throws Exception {
90         if (expected.startsWith("success")) {
91             final String expId = expected.replace("success=", "");
92             final YangInstanceIdentifier actual = validator.validate(filterContent);
93             final YangInstanceIdentifier expected = fromString(expId);
94             Assert.assertEquals(expected, actual);
95         } else if (expected.startsWith("error")) {
96             try {
97                 validator.validate(filterContent);
98                 Assert.fail(XmlUtil.toString(filterContent) + " is not valid and should throw exception.");
99             } catch (final Exception e) {
100                 final String expectedExceptionClass = expected.replace("error=", "");
101                 Assert.assertEquals(expectedExceptionClass, e.getClass().getName());
102             }
103         }
104     }
105
106     private static YangInstanceIdentifier fromString(final String input) {
107         //remove first /
108         final String yid = input.substring(1);
109         final List<String> pathElements = Arrays.asList(yid.split("/"));
110         final YangInstanceIdentifier.InstanceIdentifierBuilder builder = YangInstanceIdentifier.builder();
111         //if not specified, PathArguments inherit namespace and revision from previous PathArgument
112         QName prev = null;
113         for (final String pathElement : pathElements) {
114             final Matcher matcher = LIST_ENTRY_PATTERN.matcher(pathElement);
115             if (matcher.matches()) {
116                 prev = parseListEntry(builder, prev, matcher);
117             } else {
118                 final QName qName = createNodeQName(prev, pathElement);
119                 builder.node(qName);
120                 prev = qName;
121             }
122         }
123         return builder.build();
124     }
125
126     private static QName parseListEntry(final YangInstanceIdentifier.InstanceIdentifierBuilder builder,
127                                         final QName prev, final Matcher matcher) {
128         final Map<QName, Object> keys = new HashMap<>();
129         final String listName = matcher.group("listName");
130         final QName listQName = createNodeQName(prev, listName);
131         final String keysString = matcher.group("keys");
132         final String[] split = keysString.split(",");
133         for (final String s : split) {
134             final Matcher keyMatcher = KEY_VALUE_PATTERN.matcher(s.trim());
135             if (keyMatcher.matches()) {
136                 final QName keyName = QName.create(keyMatcher.group("key"));
137                 final String keyValue = keyMatcher.group("value");
138                 keys.put(keyName, keyValue);
139             }
140         }
141         builder.nodeWithKey(listQName, keys);
142         return prev;
143     }
144
145     private static QName createNodeQName(final QName prev, final String qNameString) {
146         final QName qName = QName.create(qNameString);
147         if (qName.getModule().getNamespace() != null) {
148             return qName;
149         } else {
150             return QName.create(Preconditions.checkNotNull(prev), qNameString);
151         }
152     }
153 }