From 6c0c438071d2e85ef2f08070935cb310165f25cf Mon Sep 17 00:00:00 2001 From: Jakub Toth Date: Mon, 1 Jun 2015 09:34:50 +0200 Subject: [PATCH] Bug 2358 - Remove tests cnsn to xml and add tests nn to xml Tests of codecs are included in codecs yangtools yang-data-codec-gson and yang-data-impl but we test codec on input data and also with presentation of Rest path. * remove CnSnInstanceIdentifierToXmlTest test * add NnInstanceIdentifierToXmlTest to nn/to/xml/test/ * positive test for instance identifier Change-Id: Iceb73ed4d96f7686fca04047524716532297a2f8 Signed-off-by: Jakub Toth --- .../test/CnSnInstanceIdentifierToXmlTest.java | 27 -- .../test/NnInstanceIdentifierToXmlTest.java | 274 ++++++++++++++++++ 2 files changed, 274 insertions(+), 27 deletions(-) delete mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnInstanceIdentifierToXmlTest.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnInstanceIdentifierToXmlTest.java diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnInstanceIdentifierToXmlTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnInstanceIdentifierToXmlTest.java deleted file mode 100644 index a5f3285051..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnInstanceIdentifierToXmlTest.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.sal.restconf.impl.cnsn.to.xml.test; - -import java.net.URISyntaxException; -import org.junit.BeforeClass; -import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader; - -/** - * - * CnSn = Composite node and Simple node data structure Class contains test of serializing simple nodes data values - * according data types from YANG schema to XML file - * - */ -public class CnSnInstanceIdentifierToXmlTest extends YangAndXmlAndDataSchemaLoader { - - @BeforeClass - public static void initialization() throws URISyntaxException { - dataLoad("/instanceidentifier/yang", 4, "instance-identifier-module", "cont"); - } - -} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnInstanceIdentifierToXmlTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnInstanceIdentifierToXmlTest.java new file mode 100644 index 0000000000..db5e4d0a38 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnInstanceIdentifierToXmlTest.java @@ -0,0 +1,274 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.sal.restconf.impl.nn.to.xml.test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import com.google.common.collect.Iterables; +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.net.URISyntaxException; +import java.util.List; +import javax.ws.rs.core.MediaType; +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.NormalizedNodeXmlBodyWriter; +import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest; +import org.opendaylight.controller.sal.restconf.impl.ControllerContext; +import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext; +import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; +import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.MapNode; +import org.opendaylight.yangtools.yang.data.impl.schema.Builders; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder; +import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; +import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; +import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; +import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; + +public class NnInstanceIdentifierToXmlTest extends AbstractBodyReaderTest { + + private static SchemaContext schemaContext; + NormalizedNodeXmlBodyWriter xmlBodyWriter; + + public NnInstanceIdentifierToXmlTest() throws NoSuchFieldException, + SecurityException { + super(); + xmlBodyWriter = new NormalizedNodeXmlBodyWriter(); + } + + @BeforeClass + public static void initialization() throws URISyntaxException { + schemaContext = schemaContextLoader("/instanceidentifier/yang", + schemaContext); + controllerContext.setSchemas(schemaContext); + } + + @Test + public void nnAsYangInstanceIdentifierAugmentLeafList() throws Exception { + final NormalizedNodeContext normalizedNodeContext = prepareNNCLeafList(); + + final OutputStream output = new ByteArrayOutputStream(); + + xmlBodyWriter.writeTo(normalizedNodeContext, null, null, null, + mediaType, null, output); + + assertNotNull(output); + + final String outputJson = output.toString(); + + assertTrue(outputJson.contains("")); + + assertTrue(outputJson.contains("")); + + assertTrue(outputJson.contains("")); + assertTrue(outputJson.contains("/instanceidentifier/")); + assertTrue(outputJson.contains("")); + + assertTrue(outputJson.contains("")); + assertTrue(outputJson.contains("lflst11 value")); + assertTrue(outputJson.contains("")); + + assertTrue(outputJson.contains("")); + assertTrue(outputJson.contains("")); + } + + private NormalizedNodeContext prepareNNCLeafList() + throws URISyntaxException { + final QName cont = QName.create("instance:identifier:module", "2014-01-17", + "cont"); + final QName cont1 = QName.create("instance:identifier:module", "2014-01-17", + "cont1"); + final QName lflst11 = QName.create("augment:module:leaf:list", "2014-01-17", + "lflst11"); + final QName lf11 = QName.create("augment:module:leaf:list", "2014-01-17", + "lf11"); + + final DataSchemaNode schemaCont = schemaContext.getDataChildByName(cont); + + final DataContainerNodeAttrBuilder dataCont = Builders + .containerBuilder((ContainerSchemaNode) schemaCont); + + final DataSchemaNode schemaCont1 = ((ContainerSchemaNode) schemaCont) + .getDataChildByName(cont1); + + final DataContainerNodeAttrBuilder dataCont1 = Builders + .containerBuilder((ContainerSchemaNode) schemaCont1); + + final List instanceLfLst11 = ControllerContext + .findInstanceDataChildrenByName( + (DataNodeContainer) schemaCont1, lflst11.getLocalName()); + + final DataSchemaNode lfLst11Schema = Iterables + .getFirst(instanceLfLst11, null); + final ListNodeBuilder> lfLst11Data = Builders + .leafSetBuilder((LeafListSchemaNode) lfLst11Schema); + + lfLst11Data.withChild(Builders + .leafSetEntryBuilder((LeafListSchemaNode) lfLst11Schema) + .withValue("lflst11 value").build()); + dataCont1.withChild(lfLst11Data.build()); + + final List instanceLf11 = ControllerContext + .findInstanceDataChildrenByName( + (DataNodeContainer) schemaCont1, lf11.getLocalName()); + final DataSchemaNode lf11Schema = Iterables.getFirst(instanceLf11, null); + + dataCont1.withChild(Builders.leafBuilder((LeafSchemaNode) lf11Schema) + .withValue("/instanceidentifier/").build()); + dataCont.withChild(dataCont1.build()); + + final NormalizedNodeContext testNormalizedNodeContext = new NormalizedNodeContext( + new InstanceIdentifierContext(null, schemaCont, + null, schemaContext), dataCont.build()); + + return testNormalizedNodeContext; + } + + @Test + public void nnAsYangInstanceIdentifierAugment() throws Exception { + + final NormalizedNodeContext normalizedNodeContext = preparNNC(); + final NormalizedNodeXmlBodyWriter xmlBodyWriter = new NormalizedNodeXmlBodyWriter(); + final OutputStream output = new ByteArrayOutputStream(); + + xmlBodyWriter.writeTo(normalizedNodeContext, null, null, null, + mediaType, null, output); + + assertNotNull(output); + + final String outputJson = output.toString(); + + assertTrue(outputJson.contains("")); + + assertTrue(outputJson.contains("")); + + assertTrue(outputJson.contains("")); + + assertTrue(outputJson.contains( + "keyvalue111")); + assertTrue(outputJson.contains( + "keyvalue112")); + + assertTrue(outputJson.contains("/cont/cont1/lf12")); + + assertTrue(outputJson.contains("lf12 value")); + + assertTrue(outputJson.contains("")); + } + + private NormalizedNodeContext preparNNC() { + final QName cont = QName.create("instance:identifier:module", "2014-01-17", + "cont"); + final QName cont1 = QName.create("instance:identifier:module", "2014-01-17", + "cont1"); + final QName lst11 = QName.create("augment:module", "2014-01-17", "lst11"); + final QName lf11 = QName.create("augment:augment:module", "2014-01-17", + "lf111"); + final QName lf12 = QName.create("augment:augment:module", "2014-01-17", + "lf112"); + final QName keyvalue111 = QName.create("augment:module", "2014-01-17", + "keyvalue111"); + final QName keyvalue112 = QName.create("augment:module", "2014-01-17", + "keyvalue112"); + + final DataSchemaNode schemaCont = schemaContext.getDataChildByName(cont); + + final DataContainerNodeAttrBuilder dataCont = Builders + .containerBuilder((ContainerSchemaNode) schemaCont); + + final DataSchemaNode schemaCont1 = ((ContainerSchemaNode) schemaCont) + .getDataChildByName(cont1); + + final DataContainerNodeAttrBuilder dataCont1 = Builders + .containerBuilder((ContainerSchemaNode) schemaCont1); + + final List instanceLst11 = ControllerContext + .findInstanceDataChildrenByName( + (DataNodeContainer) schemaCont1, lst11.getLocalName()); + final DataSchemaNode lst11Schema = Iterables.getFirst(instanceLst11, null); + + final CollectionNodeBuilder dataLst11 = Builders + .mapBuilder((ListSchemaNode) lst11Schema); + + final DataContainerNodeAttrBuilder dataLst11Vaule = Builders + .mapEntryBuilder((ListSchemaNode) lst11Schema); + + dataLst11Vaule.withChild(buildLeaf(lst11Schema, keyvalue111, dataLst11, + "keyvalue111")); + + dataLst11Vaule.withChild(buildLeaf(lst11Schema, keyvalue112, dataLst11, + "keyvalue112")); + + dataLst11Vaule.withChild(buildLeaf(lst11Schema, lf11, dataLst11, + "/cont/cont1/lf12")); + + dataLst11Vaule.withChild(buildLeaf(lst11Schema, lf12, dataLst11, + "lf12 value")); + + dataLst11.withChild(dataLst11Vaule.build()); + + dataCont1.withChild(dataLst11.build()); + dataCont.withChild(dataCont1.build()); + + final NormalizedNodeContext testNormalizedNodeContext = new NormalizedNodeContext( + new InstanceIdentifierContext(null, schemaCont, + null, schemaContext), dataCont.build()); + + return testNormalizedNodeContext; + } + + private DataContainerChild buildLeaf(final DataSchemaNode lst11Schema, + final QName qname, + final CollectionNodeBuilder dataLst11, final Object value) { + + final List instanceLf = ControllerContext + .findInstanceDataChildrenByName( + (DataNodeContainer) lst11Schema, qname.getLocalName()); + final DataSchemaNode schemaLf = Iterables.getFirst(instanceLf, null); + + return Builders.leafBuilder((LeafSchemaNode) schemaLf).withValue(value) + .build(); + } + + @Override + protected MediaType getMediaType() { + return null; + } +} -- 2.36.6