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.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;
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;
44 public class XmlStreamUtilsTest {
46 interface XMLStreamWriterConsumer {
47 void accept(XMLStreamWriter writer) throws XMLStreamException;
50 private static EffectiveModelContext schemaContext;
51 private static Module leafRefModule;
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);
63 public static void cleanup() {
69 public void testWriteIdentityRef() throws Exception {
70 final QNameModule parent = QNameModule.create(XMLNamespace.of("parent:uri"), Revision.of("2000-01-01"));
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));
77 writer.writeEndElement();
80 assertThat(xmlAsString, containsString("element>identity"));
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"),
88 writer.writeEndElement();
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());
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);
100 consumer.accept(writer);
105 return new String(out.toByteArray()).replaceAll("\\s*", "");
109 * One leafref reference to other leafref via relative references.
112 public void testLeafRefRelativeChaining() {
113 getTargetNodeForLeafRef(StringTypeDefinition.class, "cont3", "leafname3");
117 public void testLeafRefRelative() {
118 getTargetNodeForLeafRef(StringTypeDefinition.class, "pointToStringLeaf");
122 public void testLeafRefAbsoluteWithSameTarget() {
123 getTargetNodeForLeafRef(InstanceIdentifierTypeDefinition.class, "absname");
127 * Tests relative path with double point inside path (e. g. "../../lf:interface/../lf:cont2/lf:stringleaf")
129 // ignored because this isn't implemented
132 public void testLeafRefWithDoublePointInPath() {
133 getTargetNodeForLeafRef(StringTypeDefinition.class, "lf-with-double-point-inside");
137 public void testLeafRefRelativeAndAbsoluteWithSameTarget() {
138 assertSame(getTargetNodeForLeafRef(InstanceIdentifierTypeDefinition.class, "absname"),
139 getTargetNodeForLeafRef(InstanceIdentifierTypeDefinition.class, "relname"));
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));
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));
154 final TypeDefinition<?> resolved = stack.resolveLeafref((LeafrefTypeDefinition) type);
155 assertThat(resolved, instanceOf(clas));