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