X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2Fcnsn%2Fto%2Fxml%2Ftest%2FCnSnToXmlTest.java;h=96e03a5a3ca4b8b6b0d619232457a8669f3dae06;hp=d04337865a9e549c71838dc749d073218a9130c9;hb=86b407a7355a4931a59a9fa462c9c06248456727;hpb=1ff9939abc7a4072b07df6b79516fe344b1b42e3 diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlTest.java index d04337865a..96e03a5a3c 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlTest.java @@ -1,24 +1,21 @@ package org.opendaylight.controller.sal.restconf.impl.cnsn.to.xml.test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; -import java.io.StringWriter; -import java.util.Set; +import java.io.IOException; -import javax.activation.UnsupportedDataTypeException; -import javax.xml.transform.*; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; +import javax.ws.rs.WebApplicationException; +import javax.xml.transform.TransformerFactoryConfigurationError; import org.junit.BeforeClass; import org.junit.Test; -import org.opendaylight.controller.sal.rest.impl.XmlMapper; +import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider; import org.opendaylight.controller.sal.restconf.impl.test.TestUtils; +import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader; import org.opendaylight.yangtools.yang.data.api.*; import org.opendaylight.yangtools.yang.data.impl.NodeFactory; import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec; -import org.opendaylight.yangtools.yang.model.api.*; -import org.w3c.dom.Document; /** * @@ -27,25 +24,31 @@ import org.w3c.dom.Document; * XML file * */ -public class CnSnToXmlTest { - - private static Set modules; - private static DataSchemaNode dataSchemaNode; - +public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader { @BeforeClass public static void initialization() { - modules = TestUtils.resolveModules("/cnsn-to-xml/yang"); - assertEquals(2, modules.size()); - Module module = TestUtils.resolveModule("basic-module", modules); - assertNotNull(module); - dataSchemaNode = TestUtils.resolveDataSchemaNode(module, "cont"); - assertNotNull(dataSchemaNode); - + dataLoad("/cnsn-to-xml/yang", 2, "basic-module", "cont"); } @Test public void snAsYangIdentityrefToXMLTest() { - serializeToXml(prepareIdentityrefData(), "x:iden"); + serializeToXml(prepareIdentityrefData(null, true), "x:iden"); + } + + @Test + public void snAsYangIdentityrefWithQNamePrefixToXMLTest() { + serializeToXml(prepareIdentityrefData("prefix", true), + "prefix:iden"); + } + + @Test + public void snAsYangIdentityrefWithPrefixToXMLTest() { + serializeToXml(prepareIdentityrefData("prefix", false), "no qname value"); + } + + @Test + public void snAsYangLeafrefWithPrefixToXMLTest() { + serializeToXml(prepareLeafrefData(), "true", "true"); } @Test @@ -186,22 +189,13 @@ public class CnSnToXmlTest { private void serializeToXml(CompositeNode compositeNode, String... xmlRepresentation) throws TransformerFactoryConfigurationError { - XmlMapper xmlMapper = new XmlMapper(); - String xmlString = null; - if (dataSchemaNode instanceof DataNodeContainer) { - try { - Document doc = xmlMapper.write(compositeNode, (DataNodeContainer) dataSchemaNode); - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - xmlString = writer.toString(); - } catch (UnsupportedDataTypeException | TransformerException e) { - } + String xmlString = ""; + try { + xmlString = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode, + StructuredDataToXmlProvider.INSTANCE); + } catch (WebApplicationException | IOException e) { } - assertNotNull(xmlMapper); + assertNotNull(xmlString); boolean containSearchedStr = false; String strRepresentation = ""; for (String searchedStr : xmlRepresentation) { @@ -215,16 +209,21 @@ public class CnSnToXmlTest { } - private CompositeNode prepareIdentityrefData() { + private CompositeNode prepareIdentityrefData(String prefix, boolean valueAsQName) { MutableCompositeNode cont = NodeFactory.createMutableCompositeNode( TestUtils.buildQName("cont", "basic:module", "2013-12-2"), null, null, ModifyAction.CREATE, null); MutableCompositeNode cont1 = NodeFactory.createMutableCompositeNode( TestUtils.buildQName("cont1", "basic:module", "2013-12-2"), cont, null, ModifyAction.CREATE, null); cont.getChildren().add(cont1); + Object value = null; + if (valueAsQName) { + value = TestUtils.buildQName("iden", "referenced:module", "2013-12-2", prefix); + } else { + value = "no qname value"; + } MutableSimpleNode lf11 = NodeFactory.createMutableSimpleNode( - TestUtils.buildQName("lf11", "basic:module", "2013-12-2"), cont1, - TestUtils.buildQName("iden", "referenced:module", "2013-12-2"), ModifyAction.CREATE, null); + TestUtils.buildQName("lf11", "basic:module", "2013-12-2"), cont1, value, ModifyAction.CREATE, null); cont1.getChildren().add(lf11); cont1.init(); cont.init(); @@ -244,4 +243,19 @@ public class CnSnToXmlTest { return cont; } + private CompositeNode prepareLeafrefData() { + MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null, + ModifyAction.CREATE, null); + + MutableSimpleNode lfBoolean = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfBoolean"), + cont, Boolean.TRUE, ModifyAction.CREATE, null); + MutableSimpleNode lfLfref = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfLfref"), cont, + "true", ModifyAction.CREATE, null); + cont.getChildren().add(lfBoolean); + cont.getChildren().add(lfLfref); + cont.init(); + + return cont; + } + }