Improve TreeNode and Version documentation
[yangtools.git] / codec / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / XmlStreamUtilsTest.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 package org.opendaylight.yangtools.yang.data.codec.xml;
9
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.CoreMatchers.instanceOf;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertSame;
16 import static org.junit.Assert.assertTrue;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22 import javax.xml.stream.XMLStreamException;
23 import javax.xml.stream.XMLStreamWriter;
24 import org.junit.AfterClass;
25 import org.junit.BeforeClass;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.QNameModule;
30 import org.opendaylight.yangtools.yang.common.Revision;
31 import org.opendaylight.yangtools.yang.common.XMLNamespace;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.Module;
35 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
38 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
42 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
43
44 public class XmlStreamUtilsTest {
45     @FunctionalInterface
46     interface XMLStreamWriterConsumer {
47         void accept(XMLStreamWriter writer) throws XMLStreamException;
48     }
49
50     private static EffectiveModelContext schemaContext;
51     private static Module leafRefModule;
52
53     @BeforeClass
54     public static void initialize() {
55         schemaContext = YangParserTestUtils.parseYangResource("/leafref-test.yang");
56         assertNotNull(schemaContext);
57         assertEquals(1, schemaContext.getModules().size());
58         leafRefModule = schemaContext.getModules().iterator().next();
59         assertNotNull(leafRefModule);
60     }
61
62     @AfterClass
63     public static void cleanup() {
64         leafRefModule = null;
65         schemaContext = null;
66     }
67
68     @Test
69     public void testWriteIdentityRef() throws Exception {
70         final QNameModule parent = QNameModule.create(XMLNamespace.of("parent:uri"), Revision.of("2000-01-01"));
71
72         String xmlAsString = createXml(writer -> {
73             writer.writeStartElement("element");
74             final StreamWriterFacade facade = new StreamWriterFacade(writer);
75             facade.writeCharacters(XMLStreamWriterUtils.encode(facade, QName.create(parent, "identity"), parent));
76             facade.flush();
77             writer.writeEndElement();
78         });
79
80         assertThat(xmlAsString, containsString("element>identity"));
81
82         xmlAsString = createXml(writer -> {
83             writer.writeStartElement("elementDifferent");
84             final StreamWriterFacade facade = new StreamWriterFacade(writer);
85             facade.writeCharacters(XMLStreamWriterUtils.encode(facade, QName.create("different:namespace", "identity"),
86                 parent));
87             facade.flush();
88             writer.writeEndElement();
89         });
90
91         final Pattern prefixedIdentityPattern = Pattern.compile(".*\"different:namespace\">(.*):identity.*");
92         final Matcher matcher = prefixedIdentityPattern.matcher(xmlAsString);
93         assertTrue("Xml: " + xmlAsString + " should match: " + prefixedIdentityPattern, matcher.matches());
94     }
95
96     private static String createXml(final XMLStreamWriterConsumer consumer) throws XMLStreamException, IOException {
97         final ByteArrayOutputStream out = new ByteArrayOutputStream();
98         final XMLStreamWriter writer = TestFactories.DEFAULT_OUTPUT_FACTORY.createXMLStreamWriter(out);
99
100         consumer.accept(writer);
101
102         writer.close();
103         out.close();
104
105         return new String(out.toByteArray()).replaceAll("\\s*", "");
106     }
107
108     /**
109      * One leafref reference to other leafref via relative references.
110      */
111     @Test
112     public void testLeafRefRelativeChaining() {
113         getTargetNodeForLeafRef(StringTypeDefinition.class, "cont3", "leafname3");
114     }
115
116     @Test
117     public void testLeafRefRelative() {
118         getTargetNodeForLeafRef(StringTypeDefinition.class, "pointToStringLeaf");
119     }
120
121     @Test
122     public void testLeafRefAbsoluteWithSameTarget() {
123         getTargetNodeForLeafRef(InstanceIdentifierTypeDefinition.class, "absname");
124     }
125
126     /**
127      * Tests relative path with double point inside path (e. g. "../../lf:interface/../lf:cont2/lf:stringleaf")
128      */
129     // ignored because this isn't implemented
130     @Ignore
131     @Test
132     public void testLeafRefWithDoublePointInPath() {
133         getTargetNodeForLeafRef(StringTypeDefinition.class, "lf-with-double-point-inside");
134     }
135
136     @Test
137     public void testLeafRefRelativeAndAbsoluteWithSameTarget() {
138         assertSame(getTargetNodeForLeafRef(InstanceIdentifierTypeDefinition.class, "absname"),
139             getTargetNodeForLeafRef(InstanceIdentifierTypeDefinition.class, "relname"));
140     }
141
142     private static TypeDefinition<?> getTargetNodeForLeafRef(final Class<?> clas, final String... names) {
143         final SchemaInferenceStack stack = SchemaInferenceStack.of(schemaContext);
144         stack.enterDataTree(QName.create(leafRefModule.getQNameModule(), "cont2"));
145         for (String name : names) {
146             stack.enterDataTree(QName.create(leafRefModule.getQNameModule(), name));
147         }
148
149         final EffectiveStatement<?, ?> leaf = stack.currentStatement();
150         assertThat(leaf, instanceOf(LeafSchemaNode.class));
151         final TypeDefinition<? extends TypeDefinition<?>> type = ((TypedDataSchemaNode) leaf).getType();
152         assertThat(type, instanceOf(LeafrefTypeDefinition.class));
153
154         final TypeDefinition<?> resolved = stack.resolveLeafref((LeafrefTypeDefinition) type);
155         assertThat(resolved, instanceOf(clas));
156         return resolved;
157     }
158 }