Integrate netconf-mapping-api into netconf-server
[netconf.git] / protocol / netconf-server / src / test / java / org / opendaylight / netconf / server / spi / SubtreeFilterRpcTest.java
1 /*
2  * Copyright (c) 2014 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.Assert.assertTrue;
11
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import org.custommonkey.xmlunit.Diff;
17 import org.custommonkey.xmlunit.XMLUnit;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.junit.runners.Parameterized;
22 import org.junit.runners.Parameterized.Parameters;
23 import org.opendaylight.netconf.api.xml.XmlUtil;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.w3c.dom.Document;
27 import org.xml.sax.SAXException;
28
29 @RunWith(value = Parameterized.class)
30 public class SubtreeFilterRpcTest {
31     private static final Logger LOG = LoggerFactory.getLogger(SubtreeFilterRpcTest.class);
32
33     private final int directoryIndex;
34
35     @Parameters
36     public static Collection<Object[]> data() {
37         List<Object[]> result = new ArrayList<>();
38         for (int i = 0; i <= 10; i++) {
39             result.add(new Object[]{i});
40         }
41         return result;
42     }
43
44     public SubtreeFilterRpcTest(final int directoryIndex) {
45         this.directoryIndex = directoryIndex;
46     }
47
48     @Before
49     public void setUp() {
50         XMLUnit.setIgnoreWhitespace(true);
51     }
52
53     @Test
54     public void test() throws Exception {
55         Document requestDocument = getDocument("request.xml");
56         Document preFilterDocument = getDocument("pre-filter.xml");
57         Document postFilterDocument = getDocument("post-filter.xml");
58         Document actualPostFilterDocument = SubtreeFilter.applyRpcSubtreeFilter(requestDocument, preFilterDocument);
59         LOG.info("Actual document: {}", XmlUtil.toString(actualPostFilterDocument));
60         Diff diff = XMLUnit.compareXML(postFilterDocument, actualPostFilterDocument);
61         assertTrue(diff.toString(), diff.similar());
62
63     }
64
65     public Document getDocument(final String fileName) throws SAXException, IOException {
66         return XmlUtil.readXmlToDocument(
67                 getClass().getResourceAsStream("/subtree/rpc/" + directoryIndex + "/" + fileName));
68     }
69 }