Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / netconf-util / src / test / java / org / opendaylight / controller / netconf / util / test / XmlUnitUtil.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.util.test;
10
11 import static org.custommonkey.xmlunit.XMLAssert.assertNodeTestPasses;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
14
15 import org.custommonkey.xmlunit.AbstractNodeTester;
16 import org.custommonkey.xmlunit.NodeTest;
17 import org.custommonkey.xmlunit.NodeTestException;
18 import org.custommonkey.xmlunit.NodeTester;
19 import org.opendaylight.controller.config.util.xml.XmlUtil;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.Node;
23 import org.w3c.dom.Text;
24
25 public class XmlUnitUtil {
26
27     private XmlUnitUtil() {}
28
29     public static void assertContainsElementWithText(final Document doc, final String textToFind) throws NodeTestException {
30         NodeTest nt = new NodeTest(doc);
31         NodeTester tester = new AbstractNodeTester() {
32
33             boolean textFound = false;
34
35             @Override
36             public void testText(Text text) throws NodeTestException {
37                 if(!textFound) {
38                     if (text.getData().equalsIgnoreCase(textToFind)) {
39                         textFound = true;
40                     }
41                 }
42             }
43
44             @Override
45             public void noMoreNodes(NodeTest forTest) throws NodeTestException {
46                 assertTrue(textFound);
47             }
48         };
49         assertNodeTestPasses(nt, tester, new short[]{Node.TEXT_NODE}, true);
50     }
51
52     public static void assertContainsElement(final Document doc, final Element testElement) throws NodeTestException {
53         NodeTest nt = new NodeTest(doc);
54         NodeTester tester = new AbstractNodeTester() {
55
56             private boolean elementFound = false;
57
58             @Override
59             public void testElement(Element element) throws NodeTestException {
60                 if (!elementFound) {
61                     if(element.isEqualNode(testElement)) {
62                         elementFound = true;
63                     }
64                 }
65             }
66
67             @Override
68             public void noMoreNodes(NodeTest forTest) throws NodeTestException {
69                 assertTrue(elementFound);
70             }
71         };
72         assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);
73     }
74
75     public static void assertContainsElementWithName(final Document doc, final String elementName) throws NodeTestException {
76         NodeTest nt = new NodeTest(doc);
77         NodeTester tester = new AbstractNodeTester() {
78
79             private boolean elementFound = false;
80
81             @Override
82             public void testElement(Element element) throws NodeTestException {
83                 if (!elementFound) {
84                     if (element.getNodeName() != null && element.getNodeName().equals(elementName)) {
85                         elementFound = true;
86                     }
87                 }
88             }
89
90             @Override
91             public void noMoreNodes(NodeTest forTest) throws NodeTestException {
92                 assertTrue(XmlUtil.toString(doc), elementFound);
93             }
94         };
95         assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);
96     }
97
98     public static void assertElementsCount(final Document doc, final String elementName, final int expectedCount) {
99         NodeTest nt = new NodeTest(doc);
100         NodeTester tester = new AbstractNodeTester() {
101
102             private int elementFound = 0;
103
104             @Override
105             public void testElement(Element element) throws NodeTestException {
106                 if (element.getNodeName() != null && element.getNodeName().equals(elementName)) {
107                     elementFound++;
108                 }
109             }
110
111             @Override
112             public void noMoreNodes(NodeTest forTest) throws NodeTestException {
113                 assertEquals(expectedCount, elementFound);
114             }
115         };
116         assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);
117     }
118 }