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