Add NetconfXmlUnitRecursiveQualifier to netconf-util.
[controller.git] / opendaylight / netconf / netconf-util / src / test / java / org / opendaylight / controller / netconf / util / test / NetconfXmlUnitRecursiveQualifier.java
1 /*
2  * Copyright (c) 2015 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 org.custommonkey.xmlunit.ElementNameAndTextQualifier;
12 import org.custommonkey.xmlunit.ElementQualifier;
13 import org.w3c.dom.Element;
14 import org.w3c.dom.Node;
15 import org.w3c.dom.NodeList;
16
17 /**
18  * Custom xmlunit qualifier that doesn't care about order when deeper in the recursion
19  * defaults to comparing element name and text content
20  */
21 public class NetconfXmlUnitRecursiveQualifier implements ElementQualifier {
22
23     private final ElementQualifier qualifier;
24
25     public NetconfXmlUnitRecursiveQualifier() {
26         this.qualifier = new ElementNameAndTextQualifier();
27     }
28
29     public NetconfXmlUnitRecursiveQualifier(final ElementQualifier qualifier) {
30         this.qualifier = qualifier;
31     }
32
33     @Override
34     public boolean qualifyForComparison(Element currentControl,
35                                         Element currentTest) {
36         return compareNodes(currentControl, currentTest);
37     }
38
39     private boolean compareNodes(Node currentControl, Node currentTest) {
40         try {
41
42             if (!qualifier.qualifyForComparison((Element) currentControl,
43                     (Element) currentTest)) {
44                 return false;
45             }
46
47             NodeList controlNodes;
48             NodeList testNodes;
49
50             if (currentControl.hasChildNodes() && currentTest.hasChildNodes()) {
51                 controlNodes = currentControl.getChildNodes();
52                 testNodes = currentTest.getChildNodes();
53             } else {
54                 return !(currentControl.hasChildNodes() || currentTest.hasChildNodes());
55             }
56
57             return (countNodesWithoutConsecutiveTextNodes(controlNodes) == countNodesWithoutConsecutiveTextNodes(testNodes))
58                     && checkChildren(controlNodes, testNodes);
59
60         } catch (Exception e) {
61             return false;
62         }
63     }
64
65     private boolean checkChildren(NodeList controlNodes, NodeList testNodes) {
66         for (int i = 0; i < controlNodes.getLength(); i++) {
67             boolean matchFound = false;
68             for (int j = 0; j < testNodes.getLength(); j++) {
69                 Node controlNode = controlNodes.item(i);
70                 Node testNode = testNodes.item(j);
71
72                 if (controlNode.getNodeType() != testNode.getNodeType()) {
73                     continue;
74                 }
75
76                 if (controlNode.getNodeType() == Node.TEXT_NODE) {
77                     if (concatenateText(controlNode).equals(concatenateText(testNode))) {
78                         matchFound = true;
79                         break;
80                     }
81
82                 } else if (compareNodes(controlNode, testNode)) {
83                     matchFound = true;
84                     break;
85                 }
86             }
87             if (!matchFound) {
88                 return false;
89             }
90         }
91
92         return true;
93     }
94
95     private static String concatenateText(Node textNode) {
96         StringBuilder builder = new StringBuilder();
97         Node next = textNode;
98
99         do {
100             if (next.getNodeValue() != null) {
101                 builder.append(next.getNodeValue().trim());
102                 next = next.getNextSibling();
103             }
104         } while (next != null && next.getNodeType() == Node.TEXT_NODE);
105
106         return builder.toString();
107     }
108
109     private static int countNodesWithoutConsecutiveTextNodes(NodeList l) {
110         int count = 0;
111         boolean lastNodeWasText = false;
112         final int length = l.getLength();
113         for (int i = 0; i < length; i++) {
114             Node n = l.item(i);
115             if (!lastNodeWasText || n.getNodeType() != Node.TEXT_NODE) {
116                 count++;
117             }
118             lastNodeWasText = n.getNodeType() == Node.TEXT_NODE;
119         }
120         return count;
121     }
122 }