Cleanup use of Guava library
[yangtools.git] / yang / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / NormalizedNodeXmlTranslationTest.java
1 /*
2  * Copyright (c) 2016 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.yangtools.yang.data.codec.xml;
10
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
13 import static org.junit.Assert.assertNotNull;
14 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.augmentationBuilder;
15 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.choiceBuilder;
16 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.containerBuilder;
17 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
18
19 import com.google.common.collect.ImmutableSet;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.StringWriter;
23 import java.net.URI;
24 import java.text.ParseException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34 import javax.xml.stream.XMLInputFactory;
35 import javax.xml.stream.XMLOutputFactory;
36 import javax.xml.stream.XMLStreamReader;
37 import javax.xml.stream.XMLStreamWriter;
38 import javax.xml.transform.OutputKeys;
39 import javax.xml.transform.Transformer;
40 import javax.xml.transform.TransformerException;
41 import javax.xml.transform.TransformerFactory;
42 import javax.xml.transform.TransformerFactoryConfigurationError;
43 import javax.xml.transform.dom.DOMResult;
44 import javax.xml.transform.dom.DOMSource;
45 import javax.xml.transform.stream.StreamResult;
46 import org.custommonkey.xmlunit.Diff;
47 import org.custommonkey.xmlunit.ElementNameAndTextQualifier;
48 import org.custommonkey.xmlunit.IgnoreTextAndAttributeValuesDifferenceListener;
49 import org.custommonkey.xmlunit.XMLUnit;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.junit.runners.Parameterized;
53 import org.opendaylight.yangtools.util.xml.UntrustedXML;
54 import org.opendaylight.yangtools.yang.common.QName;
55 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
56 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
58 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
60 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
62 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
64 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
66 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
67 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
68 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
69 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
70 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
71 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
72 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
73 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
74 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
75 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
76 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
77 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
78 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
79 import org.opendaylight.yangtools.yang.model.api.Module;
80 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
81 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
82 import org.w3c.dom.Document;
83 import org.w3c.dom.Node;
84 import org.xml.sax.SAXException;
85
86 @RunWith(Parameterized.class)
87 public class NormalizedNodeXmlTranslationTest {
88     private final SchemaContext schema;
89
90     @Parameterized.Parameters()
91     public static Collection<Object[]> data() {
92         return Arrays.asList(new Object[][] {
93                 { "/schema/augment_choice_hell.yang", "/schema/augment_choice_hell_ok.xml", augmentChoiceHell() },
94                 { "/schema/augment_choice_hell.yang", "/schema/augment_choice_hell_ok2.xml", null },
95                 { "/schema/augment_choice_hell.yang", "/schema/augment_choice_hell_ok3.xml", augmentChoiceHell2() },
96                 { "/schema/test.yang", "/schema/simple.xml", null },
97                 { "/schema/test.yang", "/schema/simple2.xml", null },
98                 // TODO check attributes
99                 { "/schema/test.yang", "/schema/simple_xml_with_attributes.xml", withAttributes() }
100         });
101     }
102
103     private static final String NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:test";
104     private static final Date REVISION;
105
106     static {
107         try {
108             REVISION = SimpleDateFormatUtil.getRevisionFormat().parse("2014-03-13");
109         } catch (final ParseException e) {
110             throw new ExceptionInInitializerError(e);
111         }
112     }
113
114     static final XMLOutputFactory XML_FACTORY;
115
116     static {
117         XML_FACTORY = XMLOutputFactory.newFactory();
118         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
119     }
120
121     private static ContainerNode augmentChoiceHell2() {
122         final NodeIdentifier container = getNodeIdentifier("container");
123         final QName augmentChoice1QName = QName.create(container.getNodeType(), "augment-choice1");
124         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
125         final QName containerQName = QName.create(augmentChoice1QName, "case11-choice-case-container");
126         final QName leafQName = QName.create(augmentChoice1QName, "case11-choice-case-leaf");
127
128         final AugmentationIdentifier aug1Id = new AugmentationIdentifier(ImmutableSet.of(augmentChoice1QName));
129         final AugmentationIdentifier aug2Id = new AugmentationIdentifier(ImmutableSet.of(augmentChoice2QName));
130         final NodeIdentifier augmentChoice1Id = new NodeIdentifier(augmentChoice1QName);
131         final NodeIdentifier augmentChoice2Id = new NodeIdentifier(augmentChoice2QName);
132         final NodeIdentifier containerId = new NodeIdentifier(containerQName);
133
134         return containerBuilder().withNodeIdentifier(container)
135                 .withChild(augmentationBuilder().withNodeIdentifier(aug1Id)
136                         .withChild(choiceBuilder().withNodeIdentifier(augmentChoice1Id)
137                                 .withChild(augmentationBuilder().withNodeIdentifier(aug2Id)
138                                         .withChild(choiceBuilder().withNodeIdentifier(augmentChoice2Id)
139                                                 .withChild(containerBuilder().withNodeIdentifier(containerId)
140                                                         .withChild(leafNode(leafQName, "leaf-value"))
141                                                         .build())
142                                                 .build())
143                                         .build())
144                                 .build())
145                         .build()).build();
146     }
147
148     private static ContainerNode withAttributes() {
149         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> b = containerBuilder();
150         b.withNodeIdentifier(getNodeIdentifier("container"));
151
152         final CollectionNodeBuilder<MapEntryNode, MapNode> listBuilder = Builders.mapBuilder().withNodeIdentifier(
153                 getNodeIdentifier("list"));
154
155         final Map<QName, Object> predicates = new HashMap<>();
156         predicates.put(getNodeIdentifier("uint32InList").getNodeType(), 3L);
157
158         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> list1Builder = Builders
159                 .mapEntryBuilder().withNodeIdentifier(
160                         new NodeIdentifierWithPredicates(
161                                 getNodeIdentifier("list").getNodeType(), predicates));
162         final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> uint32InListBuilder = Builders
163                 .leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList"));
164
165         list1Builder.withChild(uint32InListBuilder.withValue(3L).build());
166
167         listBuilder.withChild(list1Builder.build());
168         b.withChild(listBuilder.build());
169
170         final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> booleanBuilder = Builders
171                 .leafBuilder().withNodeIdentifier(getNodeIdentifier("boolean"));
172         booleanBuilder.withValue(Boolean.FALSE);
173         b.withChild(booleanBuilder.build());
174
175         final ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafListBuilder = Builders.leafSetBuilder()
176                 .withNodeIdentifier(getNodeIdentifier("leafList"));
177
178         final NormalizedNodeBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafList1Builder = Builders
179                 .leafSetEntryBuilder().withNodeIdentifier(
180                         new NodeWithValue(getNodeIdentifier("leafList").getNodeType(), "a"));
181
182         leafList1Builder.withValue("a");
183
184         leafListBuilder.withChild(leafList1Builder.build());
185         b.withChild(leafListBuilder.build());
186
187         return b.build();
188     }
189
190     private static ContainerNode augmentChoiceHell() {
191
192         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> b = containerBuilder();
193         b.withNodeIdentifier(getNodeIdentifier("container"));
194
195         b.withChild(choiceBuilder()
196                 .withNodeIdentifier(getNodeIdentifier("ch2"))
197                 .withChild(
198                         Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("c2Leaf")).withValue("2").build())
199                 .withChild(
200                         choiceBuilder()
201                                 .withNodeIdentifier(getNodeIdentifier("c2DeepChoice"))
202                                 .withChild(
203                                         Builders.leafBuilder()
204                                                 .withNodeIdentifier(getNodeIdentifier("c2DeepChoiceCase1Leaf2"))
205                                                 .withValue("2").build()).build()).build());
206
207         b.withChild(choiceBuilder()
208                 .withNodeIdentifier(getNodeIdentifier("ch3"))
209                 .withChild(
210                         Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("c3Leaf")).withValue("3").build())
211                 .build());
212
213         b.withChild(augmentationBuilder()
214                 .withNodeIdentifier(getAugmentIdentifier("augLeaf"))
215                 .withChild(
216                         Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("augLeaf")).withValue("augment")
217                                 .build()).build());
218
219         b.withChild(augmentationBuilder()
220                 .withNodeIdentifier(getAugmentIdentifier("ch"))
221                 .withChild(
222                         choiceBuilder()
223                                 .withNodeIdentifier(getNodeIdentifier("ch"))
224                                 .withChild(
225                                         Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("c1Leaf"))
226                                                 .withValue("1").build())
227                                 .withChild(
228                                         augmentationBuilder()
229                                                 .withNodeIdentifier(
230                                                         getAugmentIdentifier("c1Leaf_AnotherAugment", "deepChoice"))
231                                                 .withChild(
232                                                         Builders.leafBuilder()
233                                                                 .withNodeIdentifier(
234                                                                         getNodeIdentifier("c1Leaf_AnotherAugment"))
235                                                                 .withValue("1").build())
236                                                 .withChild(
237                                                         choiceBuilder()
238                                                                 .withNodeIdentifier(getNodeIdentifier("deepChoice"))
239                                                                 .withChild(
240                                                                         Builders.leafBuilder()
241                                                                                 .withNodeIdentifier(
242                                                                                         getNodeIdentifier("deepLeafc1"))
243                                                                                 .withValue("1").build()).build())
244                                                 .build()).build()).build());
245
246         return b.build();
247     }
248
249     private static NodeIdentifier getNodeIdentifier(final String localName) {
250         return new NodeIdentifier(QName.create(URI.create(NAMESPACE), REVISION, localName));
251     }
252
253     private static AugmentationIdentifier getAugmentIdentifier(final String... childNames) {
254         final Set<QName> qn = new HashSet<>();
255
256         for (final String childName : childNames) {
257             qn.add(getNodeIdentifier(childName).getNodeType());
258         }
259
260         return new AugmentationIdentifier(qn);
261     }
262
263     public NormalizedNodeXmlTranslationTest(final String yangPath, final String xmlPath,
264             final ContainerNode expectedNode) {
265         this.schema = YangParserTestUtils.parseYangResource(yangPath);
266         this.xmlPath = xmlPath;
267         this.containerNode = (ContainerSchemaNode) getSchemaNode(schema, "test", "container");
268         this.expectedNode = expectedNode;
269     }
270
271     private final ContainerNode expectedNode;
272     private final ContainerSchemaNode containerNode;
273     private final String xmlPath;
274
275     @Test
276     public void testTranslation() throws Exception {
277         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream(xmlPath);
278
279         final XMLInputFactory factory = XMLInputFactory.newInstance();
280         final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
281
282         final NormalizedNodeResult result = new NormalizedNodeResult();
283         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
284
285         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schema, containerNode);
286         xmlParser.parse(reader);
287
288         final NormalizedNode<?, ?> built = result.getResult();
289         assertNotNull(built);
290
291         if (expectedNode != null) {
292             org.junit.Assert.assertEquals(expectedNode, built);
293         }
294
295         final Document document = UntrustedXML.newDocumentBuilder().newDocument();
296         final DOMResult domResult = new DOMResult(document);
297
298         final XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
299         outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
300
301         final XMLStreamWriter xmlStreamWriter = outputFactory.createXMLStreamWriter(domResult);
302
303         final NormalizedNodeStreamWriter xmlNormalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter
304                 .create(xmlStreamWriter, schema);
305
306         final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(
307                 xmlNormalizedNodeStreamWriter);
308
309         normalizedNodeWriter.write(built);
310
311         final Document doc = loadDocument(xmlPath);
312
313         XMLUnit.setIgnoreWhitespace(true);
314         XMLUnit.setIgnoreComments(true);
315         XMLUnit.setIgnoreAttributeOrder(true);
316         XMLUnit.setNormalize(true);
317
318         final String expectedXml = toString(doc.getDocumentElement());
319         final String serializedXml = toString(domResult.getNode());
320
321         final Diff diff = new Diff(expectedXml, serializedXml);
322         diff.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener());
323         diff.overrideElementQualifier(new ElementNameAndTextQualifier());
324
325         // FIXME the comparison cannot be performed, since the qualifiers supplied by XMlUnit do not work correctly in
326         // this case
327         // We need to implement custom qualifier so that the element ordering does not mess the DIFF
328         // dd.overrideElementQualifier(new MultiLevelElementNameAndTextQualifier(100, true));
329         // assertTrue(dd.toString(), dd.similar());
330
331         //new XMLTestCase() {}.assertXMLEqual(diff, true);
332     }
333
334     private static Document loadDocument(final String xmlPath) throws IOException, SAXException {
335         final InputStream resourceAsStream = NormalizedNodeXmlTranslationTest.class.getResourceAsStream(xmlPath);
336         return requireNonNull(readXmlToDocument(resourceAsStream));
337     }
338
339     private static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
340         final Document doc = UntrustedXML.newDocumentBuilder().parse(xmlContent);
341         doc.getDocumentElement().normalize();
342         return doc;
343     }
344
345     private static String toString(final Node xml) {
346         try {
347             final Transformer transformer = TransformerFactory.newInstance().newTransformer();
348             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
349             transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
350
351             final StreamResult result = new StreamResult(new StringWriter());
352             final DOMSource source = new DOMSource(xml);
353             transformer.transform(source, result);
354
355             return result.getWriter().toString();
356         } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) {
357             throw new RuntimeException("Unable to serialize xml element " + xml, e);
358         }
359     }
360
361     private static DataSchemaNode getSchemaNode(final SchemaContext context, final String moduleName,
362                                                final String childNodeName) {
363         for (Module module : context.getModules()) {
364             if (module.getName().equals(moduleName)) {
365                 DataSchemaNode found = findChildNode(module.getChildNodes(), childNodeName);
366                 checkState(found != null, "Unable to find %s", childNodeName);
367                 return found;
368             }
369         }
370         throw new IllegalStateException("Unable to find child node " + childNodeName);
371     }
372
373     private static DataSchemaNode findChildNode(final Iterable<DataSchemaNode> children, final String name) {
374         List<DataNodeContainer> containers = new ArrayList<>();
375
376         for (DataSchemaNode dataSchemaNode : children) {
377             if (dataSchemaNode.getQName().getLocalName().equals(name)) {
378                 return dataSchemaNode;
379             }
380             if (dataSchemaNode instanceof DataNodeContainer) {
381                 containers.add((DataNodeContainer) dataSchemaNode);
382             } else if (dataSchemaNode instanceof ChoiceSchemaNode) {
383                 containers.addAll(((ChoiceSchemaNode) dataSchemaNode).getCases());
384             }
385         }
386
387         for (DataNodeContainer container : containers) {
388             DataSchemaNode retVal = findChildNode(container.getChildNodes(), name);
389             if (retVal != null) {
390                 return retVal;
391             }
392         }
393
394         return null;
395     }
396
397 }