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