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
8 package org.opendaylight.yangtools.yang.data.codec.xml;
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
19 import java.util.HashMap;
21 import java.util.Optional;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24 import javax.xml.stream.XMLStreamException;
25 import javax.xml.stream.XMLStreamWriter;
26 import org.junit.AfterClass;
27 import org.junit.BeforeClass;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.QNameModule;
32 import org.opendaylight.yangtools.yang.common.Revision;
33 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
42 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45 public class XmlStreamUtilsTest {
47 interface XMLStreamWriterConsumer {
48 void accept(XMLStreamWriter writer) throws XMLStreamException;
51 private static SchemaContext schemaContext;
52 private static Module leafRefModule;
55 public static void initialize() {
56 schemaContext = YangParserTestUtils.parseYangResource("/leafref-test.yang");
57 assertNotNull(schemaContext);
58 assertEquals(1, schemaContext.getModules().size());
59 leafRefModule = schemaContext.getModules().iterator().next();
60 assertNotNull(leafRefModule);
64 public static void cleanup() {
70 public void testWriteIdentityRef() throws Exception {
71 final QNameModule parent = QNameModule.create(URI.create("parent:uri"), Revision.of("2000-01-01"));
73 String xmlAsString = createXml(writer -> {
74 writer.writeStartElement("element");
75 final StreamWriterFacade facade = new StreamWriterFacade(writer);
76 facade.writeCharacters(XMLStreamWriterUtils.encode(facade, QName.create(parent, "identity"), parent));
78 writer.writeEndElement();
81 assertThat(xmlAsString, containsString("element>identity"));
83 xmlAsString = createXml(writer -> {
84 writer.writeStartElement("elementDifferent");
85 final StreamWriterFacade facade = new StreamWriterFacade(writer);
86 facade.writeCharacters(XMLStreamWriterUtils.encode(facade, QName.create("different:namespace", "identity"),
89 writer.writeEndElement();
92 final Pattern prefixedIdentityPattern = Pattern.compile(".*\"different:namespace\">(.*):identity.*");
93 final Matcher matcher = prefixedIdentityPattern.matcher(xmlAsString);
94 assertTrue("Xml: " + xmlAsString + " should match: " + prefixedIdentityPattern, matcher.matches());
97 private static String createXml(final XMLStreamWriterConsumer consumer) throws XMLStreamException, IOException {
98 final ByteArrayOutputStream out = new ByteArrayOutputStream();
99 final XMLStreamWriter writer = TestFactories.DEFAULT_OUTPUT_FACTORY.createXMLStreamWriter(out);
101 consumer.accept(writer);
106 return new String(out.toByteArray()).replaceAll("\\s*", "");
110 * One leafref reference to other leafref via relative references.
113 public void testLeafRefRelativeChaining() {
114 getTargetNodeForLeafRef("leafname3", StringTypeDefinition.class);
118 public void testLeafRefRelative() {
119 getTargetNodeForLeafRef("pointToStringLeaf", StringTypeDefinition.class);
123 public void testLeafRefAbsoluteWithSameTarget() {
124 getTargetNodeForLeafRef("absname", InstanceIdentifierTypeDefinition.class);
128 * Tests relative path with double point inside path (e. g. "../../lf:interface/../lf:cont2/lf:stringleaf")
130 // ignored because this isn't implemented
133 public void testLeafRefWithDoublePointInPath() {
134 getTargetNodeForLeafRef("lf-with-double-point-inside", StringTypeDefinition.class);
138 public void testLeafRefRelativeAndAbsoluteWithSameTarget() {
139 final TypeDefinition<?> targetNodeForAbsname = getTargetNodeForLeafRef("absname",
140 InstanceIdentifierTypeDefinition.class);
141 final TypeDefinition<?> targetNodeForRelname = getTargetNodeForLeafRef("relname",
142 InstanceIdentifierTypeDefinition.class);
143 assertEquals(targetNodeForAbsname, targetNodeForRelname);
146 private TypeDefinition<?> getTargetNodeForLeafRef(final String nodeName, final Class<?> clas) {
147 final LeafSchemaNode schemaNode = findSchemaNodeWithLeafrefType(leafRefModule, nodeName);
148 assertNotNull(schemaNode);
149 final LeafrefTypeDefinition leafrefTypedef = findLeafrefType(schemaNode);
150 assertNotNull(leafrefTypedef);
151 final TypeDefinition<?> targetBaseType = SchemaContextUtil.getBaseTypeForLeafRef(leafrefTypedef, schemaContext,
153 assertTrue("Wrong class found.", clas.isInstance(targetBaseType));
154 return targetBaseType;
157 private static Map<String, String> mapPrefixed(final Iterable<Map.Entry<URI, String>> prefixes) {
158 final Map<String, String> mappedPrefixes = new HashMap<>();
159 for (final Map.Entry<URI, String> prefix : prefixes) {
160 mappedPrefixes.put(prefix.getKey().toString(), prefix.getValue());
162 return mappedPrefixes;
165 private static QName getAttrQName(final String namespace, final String revision, final String localName,
166 final Optional<String> prefix) {
167 if (prefix.isPresent()) {
168 final QName moduleQName = QName.create(namespace, revision, "module");
169 final QNameModule module = QNameModule.create(moduleQName.getNamespace(), moduleQName.getRevision());
170 return QName.create(module, localName);
172 return QName.create(namespace, revision, localName);
175 private LeafSchemaNode findSchemaNodeWithLeafrefType(final DataNodeContainer module, final String nodeName) {
176 for (final DataSchemaNode childNode : module.getChildNodes()) {
177 if (childNode instanceof DataNodeContainer) {
178 LeafSchemaNode leafrefFromRecursion = findSchemaNodeWithLeafrefType((DataNodeContainer) childNode,
180 if (leafrefFromRecursion != null) {
181 return leafrefFromRecursion;
183 } else if (childNode.getQName().getLocalName().equals(nodeName) && childNode instanceof LeafSchemaNode) {
184 final TypeDefinition<?> leafSchemaNodeType = ((LeafSchemaNode) childNode).getType();
185 if (leafSchemaNodeType instanceof LeafrefTypeDefinition) {
186 return (LeafSchemaNode) childNode;
193 private static LeafrefTypeDefinition findLeafrefType(final LeafSchemaNode schemaNode) {
194 final TypeDefinition<?> type = schemaNode.getType();
195 if (type instanceof LeafrefTypeDefinition) {
196 return (LeafrefTypeDefinition) type;