Migrate plugins/netconf-server-mdsal/operations tests to JUnit5
[netconf.git] / plugins / netconf-server-mdsal / src / test / java / org / opendaylight / netconf / server / mdsal / operations / 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.server.mdsal.operations;
9
10 import static java.util.Objects.requireNonNull;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.junit.Assume.assumeThat;
13 import static org.junit.jupiter.api.Assertions.assertEquals;
14 import static org.junit.jupiter.api.Assertions.assertThrows;
15 import static org.junit.jupiter.params.provider.Arguments.arguments;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18
19 import java.nio.file.Files;
20 import java.nio.file.Paths;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.HashMap;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import java.util.stream.Stream;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.params.ParameterizedTest;
29 import org.junit.jupiter.params.provider.Arguments;
30 import org.junit.jupiter.params.provider.MethodSource;
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.server.mdsal.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;
39
40 class FilterContentValidatorTest {
41     private static final int TEST_CASE_COUNT = 13;
42     private static final Pattern LIST_ENTRY_PATTERN =
43             Pattern.compile("(?<listName>.*)\\[\\{(?<keys>(.*)(, .*)*)\\}\\]");
44     private static final Pattern KEY_VALUE_PATTERN =
45             Pattern.compile("(?<key>\\(.*\\).*)=(?<value>.*)");
46     private FilterContentValidator validator;
47
48     static Stream<Arguments> data() throws Exception {
49         final var result = new ArrayList<Arguments>();
50         final var expected = Files.readAllLines(
51             Paths.get(FilterContentValidatorTest.class.getResource("/filter/expected.txt").toURI()));
52         if (expected.size() != TEST_CASE_COUNT) {
53             throw new IllegalArgumentException("Number of lines in results file must be same as test case count");
54         }
55         for (int i = 1; i <= TEST_CASE_COUNT; i++) {
56             final var document = XmlUtil.readXmlToDocument(FilterContentValidatorTest.class.getResourceAsStream(
57                     "/filter/f" + i + ".xml"));
58             result.add(arguments(document, expected.get(i - 1)));
59         }
60         return result.stream();
61     }
62
63     @BeforeEach
64     void setUp() {
65         final var context = YangParserTestUtils.parseYangResources(FilterContentValidatorTest.class,
66             "/yang/filter-validator-test-mod-0.yang", "/yang/filter-validator-test-augment.yang",
67             "/yang/mdsal-netconf-mapping-test.yang");
68
69         final var currentContext = mock(CurrentSchemaContext.class);
70         doReturn(context).when(currentContext).getCurrentContext();
71         validator = new FilterContentValidator(currentContext);
72     }
73
74     @ParameterizedTest
75     @MethodSource("data")
76     void testValidateSuccess(final Document filterContent, final String expected) throws DocumentedException {
77         final var filter = XmlElement.fromDomDocument(filterContent);
78         assumeThat(expected, startsWith("success"));
79
80         final String expId = expected.replace("success=", "");
81         final var actual = validator.validate(filter);
82         assertEquals(fromString(expId), actual);
83     }
84
85     @ParameterizedTest
86     @MethodSource("data")
87     void testValidateError(final Document filterContent, final String expected) {
88         final var filter = XmlElement.fromDomDocument(filterContent);
89         assumeThat(expected, startsWith("error"));
90
91         final var ex = assertThrows(DocumentedException.class, () -> validator.validate(filter));
92         final String expectedExceptionClass = expected.replace("error=", "");
93         assertEquals(expectedExceptionClass, ex.getClass().getName());
94     }
95
96     private static YangInstanceIdentifier fromString(final String input) {
97         //remove first /
98         final String yid = input.substring(1);
99         final var pathElements = Arrays.asList(yid.split("/"));
100         final var builder = YangInstanceIdentifier.builder();
101         //if not specified, PathArguments inherit namespace and revision from previous PathArgument
102         QName prev = null;
103         for (var pathElement : pathElements) {
104             final var matcher = LIST_ENTRY_PATTERN.matcher(pathElement);
105             if (matcher.matches()) {
106                 prev = parseListEntry(builder, prev, matcher);
107             } else {
108                 final var qname = createNodeQName(prev, pathElement);
109                 builder.node(qname);
110                 prev = qname;
111             }
112         }
113         return builder.build();
114     }
115
116     private static QName parseListEntry(final YangInstanceIdentifier.InstanceIdentifierBuilder builder,
117                                         final QName prev, final Matcher matcher) {
118         final var keys = new HashMap<QName, Object>();
119         final String listName = matcher.group("listName");
120         final QName listQName = createNodeQName(prev, listName);
121         final String keysString = matcher.group("keys");
122         for (var str : keysString.split(",")) {
123             final Matcher keyMatcher = KEY_VALUE_PATTERN.matcher(str.trim());
124             if (keyMatcher.matches()) {
125                 keys.put(QName.create(keyMatcher.group("key")), keyMatcher.group("value"));
126             }
127         }
128         builder.nodeWithKey(listQName, keys);
129         return prev;
130     }
131
132     private static QName createNodeQName(final QName prev, final String input) {
133         try {
134             return QName.create(input);
135         } catch (IllegalArgumentException e) {
136             return QName.create(requireNonNull(prev), input);
137         }
138     }
139 }