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