b2d833eab9f58fe1b804cbef832487e0db5a4f43
[netconf.git] / protocol / netconf-server / src / test / java / org / opendaylight / netconf / server / spi / SubtreeFilterNotificationTest.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.spi;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12
13 import java.util.stream.Stream;
14 import org.junit.jupiter.params.ParameterizedTest;
15 import org.junit.jupiter.params.provider.MethodSource;
16 import org.opendaylight.netconf.api.xml.XmlElement;
17 import org.opendaylight.netconf.api.xml.XmlUtil;
18 import org.w3c.dom.Document;
19 import org.xmlunit.builder.DiffBuilder;
20
21 class SubtreeFilterNotificationTest {
22
23     @MethodSource
24     @ParameterizedTest
25     void testFilterNotification(final int directoryIndex) throws Exception {
26         final var filter = XmlElement.fromDomDocument(getDocument(directoryIndex, "filter.xml"));
27         final var preFilter = getDocument(directoryIndex, "pre-filter.xml");
28         final var expectedPostFilter = getDocument(directoryIndex, "post-filter.xml");
29         final var postFilterOpt = SubtreeFilter.applySubtreeNotificationFilter(filter, preFilter);
30         if (postFilterOpt.isPresent()) {
31             final var diff = DiffBuilder.compare(postFilterOpt.orElseThrow())
32                 .withTest(expectedPostFilter)
33                 .ignoreWhitespace()
34                 .checkForIdentical()
35                 .build();
36             assertFalse(diff.hasDifferences(), diff.toString());
37         } else {
38             assertEquals("empty", XmlElement.fromDomDocument(expectedPostFilter).getName());
39         }
40     }
41
42     private static Stream<Integer> testFilterNotification() {
43         return Stream.of(0, 1, 2, 3, 4);
44     }
45
46     private Document getDocument(final int directoryIndex, final String fileName) throws Exception {
47         return XmlUtil.readXmlToDocument(SubtreeFilterNotificationTest.class.getResourceAsStream(
48                 "/subtree/notification/" + directoryIndex + "/" + fileName));
49     }
50 }