2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.data.codec.xml;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Sets;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.StringWriter;
17 import java.net.URISyntaxException;
18 import java.text.ParseException;
19 import java.util.HashMap;
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import javax.xml.parsers.ParserConfigurationException;
24 import javax.xml.stream.XMLOutputFactory;
25 import javax.xml.stream.XMLStreamException;
26 import javax.xml.stream.XMLStreamWriter;
27 import javax.xml.transform.OutputKeys;
28 import javax.xml.transform.Transformer;
29 import javax.xml.transform.TransformerException;
30 import javax.xml.transform.TransformerFactory;
31 import javax.xml.transform.TransformerFactoryConfigurationError;
32 import javax.xml.transform.dom.DOMResult;
33 import javax.xml.transform.dom.DOMSource;
34 import javax.xml.transform.stream.StreamResult;
35 import org.custommonkey.xmlunit.Diff;
36 import org.custommonkey.xmlunit.DifferenceListener;
37 import org.custommonkey.xmlunit.IgnoreTextAndAttributeValuesDifferenceListener;
38 import org.custommonkey.xmlunit.XMLTestCase;
39 import org.custommonkey.xmlunit.XMLUnit;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.common.QNameModule;
44 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
49 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
57 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
58 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
59 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
60 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
61 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
62 import org.w3c.dom.Document;
63 import org.w3c.dom.Node;
64 import org.xml.sax.SAXException;
66 public class NormalizedNodesToXmlTest {
68 private QNameModule bazModule;
70 private QName outerContainer;
72 private QName myContainer1;
73 private QName myKeyedList;
74 private QName myKeyLeaf;
75 private QName myLeafInList1;
76 private QName myLeafInList2;
77 private QName myLeaf1;
78 private QName myLeafList;
80 private QName myContainer2;
81 private QName innerContainer;
82 private QName myLeaf2;
83 private QName myLeaf3;
84 private QName myChoice;
85 private QName myLeafInCase2;
87 private QName myContainer3;
88 private QName myDoublyKeyedList;
89 private QName myFirstKeyLeaf;
90 private QName mySecondKeyLeaf;
91 private QName myLeafInList3;
94 public void setup() throws URISyntaxException, ParseException {
95 bazModule = QNameModule.create(new URI("baz-namespace"), SimpleDateFormatUtil.getRevisionFormat()
96 .parse("1970-01-01"));
98 outerContainer = QName.create(bazModule, "outer-container");
100 myContainer1 = QName.create(bazModule, "my-container-1");
101 myKeyedList = QName.create(bazModule, "my-keyed-list");
102 myKeyLeaf = QName.create(bazModule, "my-key-leaf");
103 myLeafInList1 = QName.create(bazModule, "my-leaf-in-list-1");
104 myLeafInList2 = QName.create(bazModule, "my-leaf-in-list-2");
105 myLeaf1 = QName.create(bazModule, "my-leaf-1");
106 myLeafList = QName.create(bazModule, "my-leaf-list");
108 myContainer2 = QName.create(bazModule, "my-container-2");
109 innerContainer = QName.create(bazModule, "inner-container");
110 myLeaf2 = QName.create(bazModule, "my-leaf-2");
111 myLeaf3 = QName.create(bazModule, "my-leaf-3");
112 myChoice = QName.create(bazModule, "my-choice");
113 myLeafInCase2 = QName.create(bazModule, "my-leaf-in-case-2");
115 myContainer3 = QName.create(bazModule, "my-container-3");
116 myDoublyKeyedList = QName.create(bazModule, "my-doubly-keyed-list");
117 myFirstKeyLeaf = QName.create(bazModule, "my-first-key-leaf");
118 mySecondKeyLeaf = QName.create(bazModule, "my-second-key-leaf");
119 myLeafInList3 = QName.create(bazModule, "my-leaf-in-list-3");
123 public void testNormalizedNodeToXmlSerialization() throws ReactorException, XMLStreamException, IOException,
124 SAXException, URISyntaxException {
125 final SchemaContext schemaContext = YangParserTestUtils.parseYangSource("/baz.yang");
127 final Document doc = loadDocument("/baz.xml");
129 final Document document = getDocument();
130 final DOMResult domResult = new DOMResult(document);
132 final XMLOutputFactory factory = XMLOutputFactory.newInstance();
133 factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
135 final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(domResult);
137 final NormalizedNodeStreamWriter xmlNormalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create
138 (xmlStreamWriter, schemaContext);
140 final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter
141 (xmlNormalizedNodeStreamWriter);
143 normalizedNodeWriter.write(buildOuterContainerNode());
145 XMLUnit.setIgnoreWhitespace(true);
146 XMLUnit.setNormalize(true);
148 final String expectedXml = toString(doc.getDocumentElement().getElementsByTagName("outer-container").item(0));
149 final String serializedXml = toString(domResult.getNode());
150 final Diff diff = new Diff(expectedXml, serializedXml);
152 final DifferenceListener differenceListener = new IgnoreTextAndAttributeValuesDifferenceListener();
153 diff.overrideDifferenceListener(differenceListener);
155 new XMLTestCase() {}.assertXMLEqual(diff, true);
158 private NormalizedNode<?, ?> buildOuterContainerNode() {
160 MapNode myKeyedListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(myKeyedList))
161 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
162 new NodeIdentifierWithPredicates(myKeyedList, myKeyLeaf, "listkeyvalue1"))
163 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInList1))
164 .withValue("listleafvalue1").build())
165 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInList2))
166 .withValue("listleafvalue2").build()).build())
167 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
168 new NodeIdentifierWithPredicates(myKeyedList, myKeyLeaf, "listkeyvalue2"))
169 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInList1))
170 .withValue("listleafvalue12").build())
171 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInList2))
172 .withValue("listleafvalue22").build()).build()).build();
174 LeafNode<?> myLeaf1Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeaf1))
175 .withValue("value1").build();
177 LeafSetNode<?> myLeafListNode = Builders.leafSetBuilder().withNodeIdentifier(new NodeIdentifier(myLeafList))
178 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
179 new NodeWithValue<>(myLeafList, "lflvalue1")).withValue("lflvalue1").build())
180 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
181 new NodeWithValue<>(myLeafList, "lflvalue2")).withValue("lflvalue2").build()).build();
183 ContainerNode myContainer1Node = Builders.containerBuilder().withNodeIdentifier(
184 new NodeIdentifier(myContainer1))
185 .withChild(myKeyedListNode)
186 .withChild(myLeaf1Node)
187 .withChild(myLeafListNode).build();
190 ContainerNode innerContainerNode = Builders.containerBuilder().withNodeIdentifier(
191 new NodeIdentifier(innerContainer))
192 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeaf2))
193 .withValue("value2").build()).build();
195 LeafNode<?> myLeaf3Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeaf3))
196 .withValue("value3").build();
198 ChoiceNode myChoiceNode = Builders.choiceBuilder().withNodeIdentifier(new NodeIdentifier(myChoice))
199 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInCase2))
200 .withValue("case2value").build()).build();
202 ContainerNode myContainer2Node = Builders.containerBuilder().withNodeIdentifier(
203 new NodeIdentifier(myContainer2))
204 .withChild(innerContainerNode)
205 .withChild(myLeaf3Node)
206 .withChild(myChoiceNode).build();
209 Map<QName, Object> keys = new HashMap<>();
210 keys.put(myFirstKeyLeaf, "listkeyvalue1");
211 keys.put(mySecondKeyLeaf, "listkeyvalue2");
213 MapNode myDoublyKeyedListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(myDoublyKeyedList))
214 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
215 new NodeIdentifierWithPredicates(myDoublyKeyedList, keys))
216 .withChild(Builders.leafBuilder().withNodeIdentifier(
217 new NodeIdentifier(myLeafInList3)).withValue("listleafvalue1").build()).build())
220 AugmentationNode myDoublyKeyedListAugNode = Builders.augmentationBuilder().withNodeIdentifier(
221 new AugmentationIdentifier(Sets.newHashSet(myDoublyKeyedList)))
222 .withChild(myDoublyKeyedListNode).build();
224 ContainerNode myContainer3Node = Builders.containerBuilder().withNodeIdentifier(
225 new NodeIdentifier(myContainer3))
226 .withChild(myDoublyKeyedListAugNode).build();
228 AugmentationNode myContainer3AugNode = Builders.augmentationBuilder().withNodeIdentifier(
229 new AugmentationIdentifier(Sets.newHashSet(myContainer3)))
230 .withChild(myContainer3Node).build();
232 ContainerNode outerContainerNode = Builders.containerBuilder().withNodeIdentifier(
233 new NodeIdentifier(outerContainer))
234 .withChild(myContainer1Node)
235 .withChild(myContainer2Node)
236 .withChild(myContainer3AugNode).build();
238 return outerContainerNode;
241 private static Document loadDocument(final String xmlPath) throws IOException, SAXException {
242 final InputStream resourceAsStream = NormalizedNodesToXmlTest.class.getResourceAsStream(xmlPath);
243 final Document currentConfigElement = readXmlToDocument(resourceAsStream);
244 Preconditions.checkNotNull(currentConfigElement);
245 return currentConfigElement;
248 private static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
249 final DocumentBuilder dBuilder;
251 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
252 factory.setNamespaceAware(true);
253 factory.setCoalescing(true);
254 factory.setIgnoringElementContentWhitespace(true);
255 factory.setIgnoringComments(true);
256 dBuilder = factory.newDocumentBuilder();
257 } catch (final ParserConfigurationException e) {
258 throw new RuntimeException("Failed to parse XML document", e);
260 final Document doc = dBuilder.parse(xmlContent);
262 doc.getDocumentElement().normalize();
266 private static String toString(final Node xml) {
268 final Transformer transformer = TransformerFactory.newInstance().newTransformer();
269 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
270 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
272 final StreamResult result = new StreamResult(new StringWriter());
273 final DOMSource source = new DOMSource(xml);
274 transformer.transform(source, result);
276 return result.getWriter().toString();
277 } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) {
278 throw new RuntimeException("Unable to serialize xml element " + xml, e);
282 private static Document getDocument() {
283 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
286 DocumentBuilder bob = dbf.newDocumentBuilder();
287 doc = bob.newDocument();
288 } catch (ParserConfigurationException e) {
289 throw new RuntimeException(e);