X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fimpl%2Fcodec%2Fxml%2FXmlStreamUtilsTest.java;h=3b436236040f90beda2ddbf2875e4b88bca2a224;hb=e173f129d7d4a64fa711665fadf203cdffc2d355;hp=6832748bdaf49656883a864a016b7b60d670e1f2;hpb=00324e40aa8e12a0e861933888c553964e13a884;p=yangtools.git diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlStreamUtilsTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlStreamUtilsTest.java index 6832748bda..3b43623604 100644 --- a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlStreamUtilsTest.java +++ b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlStreamUtilsTest.java @@ -8,34 +8,42 @@ package org.opendaylight.yangtools.yang.data.impl.codec.xml; +import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; - import com.google.common.base.Optional; -import com.google.common.collect.Lists; import com.google.common.collect.Maps; - import java.io.ByteArrayOutputStream; import java.io.File; import java.net.URI; +import java.net.URISyntaxException; import java.util.AbstractMap; import java.util.Collections; +import java.util.Date; import java.util.Map; - +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamWriter; - import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.XMLUnit; +import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; -import org.opendaylight.yangtools.yang.data.api.Node; -import org.opendaylight.yangtools.yang.data.api.SimpleNode; -import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode; -import org.opendaylight.yangtools.yang.data.impl.NodeFactory; -import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl; +import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; +import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; import org.w3c.dom.Document; @@ -43,6 +51,21 @@ public class XmlStreamUtilsTest { public static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory(); + private static SchemaContext schemaContext; + private static Module leafRefModule; + + @BeforeClass + public static void initialize() throws URISyntaxException { + final YangParserImpl yangParser = new YangParserImpl(); + final File file = new File(XmlStreamUtils.class.getResource("/leafref-test.yang").toURI()); + schemaContext = yangParser.parseFiles(Collections.singletonList(file)); + assertNotNull(schemaContext); + assertEquals(1,schemaContext.getModules().size()); + leafRefModule = schemaContext.getModules().iterator().next(); + assertNotNull(leafRefModule); + } + + @Test public void testWriteAttribute() throws Exception { final ByteArrayOutputStream out = new ByteArrayOutputStream(); @@ -81,34 +104,77 @@ public class XmlStreamUtilsTest { } @Test - public void testEmptyNodeWithAttribute() throws Exception { + public void testWriteIdentityRef() throws Exception { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out); - final Map attrs = Maps.newHashMap(); - attrs.put(QName.create("namespaceAttr", "2012-12-12", "attr1"), "value"); - final QName qName = QName.create("urn:opendaylight:controller:rpc:test", "2014-07-28", "cont"); - final ImmutableCompositeNode dataAttributes = ImmutableCompositeNode.create(qName, attrs, Collections.>emptyList()); - XmlStreamUtils.create(XmlUtils.DEFAULT_XML_CODEC_PROVIDER).writeDocument(writer, dataAttributes); + writer.writeStartElement("element"); + final QNameModule parent = QNameModule.create(URI.create("parent:uri"), new Date()); + XmlStreamUtils.write(writer, null, QName.create(parent, "identity"), Optional.of(parent)); + writer.writeEndElement(); + + writer.writeStartElement("elementDifferent"); + XmlStreamUtils.write(writer, null, QName.create("different:namespace", "identity"), Optional.of(parent)); + writer.writeEndElement(); writer.close(); out.close(); - final String xmlAsString = new String(out.toByteArray()); + final String xmlAsString = new String(out.toByteArray()).replaceAll("\\s*", ""); + assertThat(xmlAsString, containsString("element>identity")); - // TODO why resulting xml does not have namespace definition ? If sending xml by e.g. netconf the namespace is there but not here in test - final String expectedXmlAsString = ""; + final Pattern prefixedIdentityPattern = Pattern.compile(".*\"different:namespace\">(.*):identity.*"); + final Matcher matcher = prefixedIdentityPattern.matcher(xmlAsString); + assertTrue("Xml: " + xmlAsString + " should match: " + prefixedIdentityPattern, matcher.matches()); + } - XMLUnit.setIgnoreAttributeOrder(true); - final Document control = XMLUnit.buildControlDocument(expectedXmlAsString); - final Document test = XMLUnit.buildTestDocument(xmlAsString); - final Diff diff = XMLUnit.compareXML(control, test); + /** + * One leafref reference to other leafref via relative references + */ + @Test + public void testLeafRefRelativeChaining() { + getTargetNodeForLeafRef("leafname3", StringTypeDefinition.class); + } - final boolean identical = diff.identical(); - assertTrue("Xml differs: " + diff.toString(), identical); + @Test + public void testLeafRefRelative() { + getTargetNodeForLeafRef("pointToStringLeaf", StringTypeDefinition.class); + } + + @Test + public void testLeafRefAbsoluteWithSameTarget() { + getTargetNodeForLeafRef("absname", InstanceIdentifierTypeDefinition.class); + } + + /** + * Tests relative path with double point inside path (e. g. "../../lf:interface/../lf:cont2/lf:stringleaf") + */ + @Ignore //ignored because this isn't implemented + @Test + public void testLeafRefWithDoublePointInPath() { + getTargetNodeForLeafRef("lf-with-double-point-inside", StringTypeDefinition.class); + } + + @Test + public void testLeafRefRelativeAndAbsoluteWithSameTarget() { + final TypeDefinition targetNodeForAbsname = getTargetNodeForLeafRef("absname", + InstanceIdentifierTypeDefinition.class); + final TypeDefinition targetNodeForRelname = getTargetNodeForLeafRef("relname", + InstanceIdentifierTypeDefinition.class); + assertEquals(targetNodeForAbsname, targetNodeForRelname); + } + + private TypeDefinition getTargetNodeForLeafRef(final String nodeName, final Class clas) { + final LeafSchemaNode schemaNode = findSchemaNodeWithLeafrefType(leafRefModule, nodeName); + assertNotNull(schemaNode); + final LeafrefTypeDefinition leafrefTypedef = findLeafrefType(schemaNode); + assertNotNull(leafrefTypedef); + final TypeDefinition targetBaseType = SchemaContextUtil.getBaseTypeForLeafRef(leafrefTypedef, schemaContext, schemaNode); + assertTrue("Wrong class found.", clas.isInstance(targetBaseType)); + return targetBaseType; } - private Map mapPrefixed(final Iterable> prefixes) { + private static Map mapPrefixed(final Iterable> prefixes) { final Map mappedPrefixes = Maps.newHashMap(); for (final Map.Entry prefix : prefixes) { mappedPrefixes.put(prefix.getKey().toString(), prefix.getValue()); @@ -116,14 +182,38 @@ public class XmlStreamUtilsTest { return mappedPrefixes; } - private QName getAttrQName(final String namespace, final String revision, final String localName, final Optional prefix) { - - if(prefix.isPresent()) { + private static QName getAttrQName(final String namespace, final String revision, final String localName, final Optional prefix) { + if (prefix.isPresent()) { final QName moduleQName = QName.create(namespace, revision, "module"); final QNameModule module = QNameModule.create(moduleQName.getNamespace(), moduleQName.getRevision()); - return QName.create(module, prefix.get(), localName); + return QName.create(module, localName); } else { return QName.create(namespace, revision, localName); } } + + private LeafSchemaNode findSchemaNodeWithLeafrefType(final DataNodeContainer module, final String nodeName) { + for (final DataSchemaNode childNode : module.getChildNodes()) { + if (childNode instanceof DataNodeContainer) { + LeafSchemaNode leafrefFromRecursion = findSchemaNodeWithLeafrefType((DataNodeContainer)childNode, nodeName); + if (leafrefFromRecursion != null) { + return leafrefFromRecursion; + } + } else if (childNode.getQName().getLocalName().equals(nodeName) && childNode instanceof LeafSchemaNode) { + final TypeDefinition leafSchemaNodeType = ((LeafSchemaNode)childNode).getType(); + if (leafSchemaNodeType instanceof LeafrefTypeDefinition) { + return (LeafSchemaNode)childNode; + } + } + } + return null; + } + + private static LeafrefTypeDefinition findLeafrefType(final LeafSchemaNode schemaNode) { + final TypeDefinition type = schemaNode.getType(); + if (type instanceof LeafrefTypeDefinition) { + return (LeafrefTypeDefinition)type; + } + return null; + } }