Remove dependency on controller config-util
[netconf.git] / netconf / netconf-util / src / test / java / org / opendaylight / netconf / util / messages / 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
9 package org.opendaylight.netconf.util.messages;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.base.Optional;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import org.custommonkey.xmlunit.Diff;
20 import org.custommonkey.xmlunit.XMLUnit;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.Parameterized;
25 import org.junit.runners.Parameterized.Parameters;
26 import org.opendaylight.netconf.api.xml.XmlElement;
27 import org.opendaylight.netconf.api.xml.XmlUtil;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.w3c.dom.Document;
31 import org.xml.sax.SAXException;
32
33 @RunWith(value = Parameterized.class)
34 public class SubtreeFilterNotificationTest {
35     private static final Logger LOG = LoggerFactory.getLogger(SubtreeFilterRpcTest.class);
36
37     private final int directoryIndex;
38
39     @Parameters
40     public static Collection<Object[]> data() {
41         List<Object[]> result = new ArrayList<>();
42         for (int i = 0; i < 5; i++) {
43             result.add(new Object[]{i});
44         }
45         return result;
46     }
47
48     public SubtreeFilterNotificationTest(int directoryIndex) {
49         this.directoryIndex = directoryIndex;
50     }
51
52     @Before
53     public void setUp() {
54         XMLUnit.setIgnoreWhitespace(true);
55     }
56
57     @Test
58     public void testFilterNotification() throws Exception {
59         XmlElement filter = XmlElement.fromDomDocument(getDocument("filter.xml"));
60         Document preFilterDocument = getDocument("pre-filter.xml");
61         Document postFilterDocument = getDocument("post-filter.xml");
62         Optional<Document> actualPostFilterDocumentOpt =
63                 SubtreeFilter.applySubtreeNotificationFilter(filter, preFilterDocument);
64         if (actualPostFilterDocumentOpt.isPresent()) {
65             Document actualPostFilterDocument = actualPostFilterDocumentOpt.get();
66             LOG.info("Actual document: {}", XmlUtil.toString(actualPostFilterDocument));
67             Diff diff = XMLUnit.compareXML(postFilterDocument, actualPostFilterDocument);
68             assertTrue(diff.toString(), diff.similar());
69         } else {
70             assertEquals("empty", XmlElement.fromDomDocument(postFilterDocument).getName());
71         }
72     }
73
74     public Document getDocument(String fileName) throws SAXException, IOException {
75         return XmlUtil.readXmlToDocument(getClass().getResourceAsStream(
76                 "/subtree/notification/" + directoryIndex + "/" + fileName));
77     }
78 }