2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.mdsal.connector.ops.get;
10 import static org.mockito.Mockito.doReturn;
11 import static org.mockito.Mockito.mock;
13 import com.google.common.base.Preconditions;
14 import java.io.InputStream;
15 import java.nio.file.Files;
16 import java.nio.file.Path;
17 import java.nio.file.Paths;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.List;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.Parameterized;
31 import org.junit.runners.model.InitializationError;
32 import org.opendaylight.controller.config.util.xml.XmlElement;
33 import org.opendaylight.controller.config.util.xml.XmlUtil;
34 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
39 import org.w3c.dom.Document;
41 @RunWith(value = Parameterized.class)
42 public class FilterContentValidatorTest {
44 private static final int TEST_CASE_COUNT = 13;
45 private static final Pattern LIST_ENTRY_PATTERN =
46 Pattern.compile("(?<listName>.*)\\[\\{(?<keys>(.*)(, .*)*)\\}\\]");
47 private static final Pattern KEY_VALUE_PATTERN =
48 Pattern.compile("(?<key>\\(.*\\).*)=(?<value>.*)");
49 private final XmlElement filterContent;
50 private final String expected;
51 private FilterContentValidator validator;
53 @Parameterized.Parameters
54 public static Collection<Object[]> data() throws Exception {
55 final List<Object[]> result = new ArrayList<>();
56 final Path path = Paths.get(FilterContentValidatorTest.class.getResource("/filter/expected.txt").toURI());
57 final List<String> expected = Files.readAllLines(path);
58 if (expected.size() != TEST_CASE_COUNT) {
59 throw new InitializationError("Number of lines in results file must be same as test case count");
61 for (int i = 1; i <= TEST_CASE_COUNT; i++) {
62 final Document document = XmlUtil.readXmlToDocument(FilterContentValidatorTest.class.getResourceAsStream(
63 "/filter/f" + i + ".xml"));
64 result.add(new Object[]{document, expected.get(i - 1)});
69 public FilterContentValidatorTest(final Document filterContent, final String expected) {
70 this.filterContent = XmlElement.fromDomDocument(filterContent);
71 this.expected = expected;
75 public void setUp() throws Exception {
76 final List<InputStream> sources = new ArrayList<>();
77 sources.add(getClass().getResourceAsStream("/yang/filter-validator-test-mod-0.yang"));
78 sources.add(getClass().getResourceAsStream("/yang/filter-validator-test-augment.yang"));
79 sources.add(getClass().getResourceAsStream("/yang/mdsal-netconf-mapping-test.yang"));
80 final SchemaContext context = YangParserTestUtils.parseYangStreams(sources);
81 final CurrentSchemaContext currentContext = mock(CurrentSchemaContext.class);
82 doReturn(context).when(currentContext).getCurrentContext();
83 validator = new FilterContentValidator(currentContext);
86 @SuppressWarnings("checkstyle:IllegalCatch")
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")) {
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());
105 private static YangInstanceIdentifier fromString(final String input) {
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
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);
117 final QName qName = createNodeQName(prev, pathElement);
122 return builder.build();
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);
140 builder.nodeWithKey(listQName, keys);
144 private static QName createNodeQName(final QName prev, final String input) {
145 final QName qName = QName.create(input);
146 if (qName.getModule().getNamespace() != null) {
149 return QName.create(Preconditions.checkNotNull(prev), input);