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 java.util.Objects.requireNonNull;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThrows;
14 import static org.junit.Assume.assumeThat;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
18 import java.nio.file.Files;
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.regex.Matcher;
25 import java.util.regex.Pattern;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.Parameterized;
30 import org.junit.runners.model.InitializationError;
31 import org.opendaylight.netconf.api.DocumentedException;
32 import org.opendaylight.netconf.api.xml.XmlElement;
33 import org.opendaylight.netconf.api.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.test.util.YangParserTestUtils;
38 import org.w3c.dom.Document;
40 @RunWith(value = Parameterized.class)
41 public class FilterContentValidatorTest {
42 private static final int TEST_CASE_COUNT = 13;
43 private static final Pattern LIST_ENTRY_PATTERN =
44 Pattern.compile("(?<listName>.*)\\[\\{(?<keys>(.*)(, .*)*)\\}\\]");
45 private static final Pattern KEY_VALUE_PATTERN =
46 Pattern.compile("(?<key>\\(.*\\).*)=(?<value>.*)");
47 private final XmlElement filterContent;
48 private final String expected;
49 private FilterContentValidator validator;
51 @Parameterized.Parameters
52 public static Collection<Object[]> data() throws Exception {
53 final var result = new ArrayList<Object[]>();
54 final var expected = Files.readAllLines(
55 Paths.get(FilterContentValidatorTest.class.getResource("/filter/expected.txt").toURI()));
56 if (expected.size() != TEST_CASE_COUNT) {
57 throw new InitializationError("Number of lines in results file must be same as test case count");
59 for (int i = 1; i <= TEST_CASE_COUNT; i++) {
60 final var document = XmlUtil.readXmlToDocument(FilterContentValidatorTest.class.getResourceAsStream(
61 "/filter/f" + i + ".xml"));
62 result.add(new Object[]{document, expected.get(i - 1)});
67 public FilterContentValidatorTest(final Document filterContent, final String expected) {
68 this.filterContent = XmlElement.fromDomDocument(filterContent);
69 this.expected = expected;
73 public void setUp() throws Exception {
74 final var context = YangParserTestUtils.parseYangResources(FilterContentValidatorTest.class,
75 "/yang/filter-validator-test-mod-0.yang", "/yang/filter-validator-test-augment.yang",
76 "/yang/mdsal-netconf-mapping-test.yang");
78 final var currentContext = mock(CurrentSchemaContext.class);
79 doReturn(context).when(currentContext).getCurrentContext();
80 validator = new FilterContentValidator(currentContext);
84 public void testValidateSuccess() throws DocumentedException {
85 assumeThat(expected, startsWith("success"));
87 final String expId = expected.replace("success=", "");
88 final var actual = validator.validate(filterContent);
89 assertEquals(fromString(expId), actual);
93 public void testValidateError() {
94 assumeThat(expected, startsWith("error"));
96 final var ex = assertThrows(DocumentedException.class, () -> validator.validate(filterContent));
97 final String expectedExceptionClass = expected.replace("error=", "");
98 assertEquals(expectedExceptionClass, ex.getClass().getName());
101 private static YangInstanceIdentifier fromString(final String input) {
103 final String yid = input.substring(1);
104 final var pathElements = Arrays.asList(yid.split("/"));
105 final var builder = YangInstanceIdentifier.builder();
106 //if not specified, PathArguments inherit namespace and revision from previous PathArgument
108 for (var pathElement : pathElements) {
109 final var matcher = LIST_ENTRY_PATTERN.matcher(pathElement);
110 if (matcher.matches()) {
111 prev = parseListEntry(builder, prev, matcher);
113 final var qname = createNodeQName(prev, pathElement);
118 return builder.build();
121 private static QName parseListEntry(final YangInstanceIdentifier.InstanceIdentifierBuilder builder,
122 final QName prev, final Matcher matcher) {
123 final var keys = new HashMap<QName, Object>();
124 final String listName = matcher.group("listName");
125 final QName listQName = createNodeQName(prev, listName);
126 final String keysString = matcher.group("keys");
127 for (var str : keysString.split(",")) {
128 final Matcher keyMatcher = KEY_VALUE_PATTERN.matcher(str.trim());
129 if (keyMatcher.matches()) {
130 keys.put(QName.create(keyMatcher.group("key")), keyMatcher.group("value"));
133 builder.nodeWithKey(listQName, keys);
137 private static QName createNodeQName(final QName prev, final String input) {
139 return QName.create(input);
140 } catch (IllegalArgumentException e) {
141 return QName.create(requireNonNull(prev), input);