diff options
author | Dan Timoney <dtimoney@att.com> | 2023-01-05 09:31:25 -0500 |
---|---|---|
committer | Dan Timoney <dtimoney@att.com> | 2023-01-05 09:31:25 -0500 |
commit | d4d6fbd430eb502cce6cb01a667ec799d487a510 (patch) | |
tree | aea94837cd313f27f68e625d2b8277960fdf7af6 /netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn | |
parent | d0508aaeb7af3f29095d1d0ef7dbf69af18f0d99 (diff) |
Seed code for biermann restconf
Seed initial code from OpenDaylight netconf project for
Biermann draft version of restconf API
Issue-ID: CCSDK-3783
Signed-off-by: Dan Timoney <dtimoney@att.com>
Change-Id: I8a1ad2050ee7addbb480f01bd448922803bff31f
Diffstat (limited to 'netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn')
7 files changed, 1267 insertions, 0 deletions
diff --git a/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnJsonChoiceCaseTest.java b/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnJsonChoiceCaseTest.java new file mode 100644 index 0000000..8fd5db8 --- /dev/null +++ b/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnJsonChoiceCaseTest.java @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2015 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.json.test; + +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import javax.ws.rs.core.MediaType; +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; +import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest; +import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext; +import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; + +public class NnJsonChoiceCaseTest extends AbstractBodyReaderTest { + + private static EffectiveModelContext schemaContext; + private final NormalizedNodeJsonBodyWriter jsonBodyWriter; + + public NnJsonChoiceCaseTest() { + super(schemaContext, null); + jsonBodyWriter = new NormalizedNodeJsonBodyWriter(); + } + + @BeforeClass + public static void initialization() { + schemaContext = schemaContextLoader("/nn-to-json/choice", schemaContext); + } + + /** + * Test when some data are in one case node and other in another. This isn't + * correct. Next Json validator should return error because nodes has to be + * from one case below concrete choice. + */ + @Test(expected = NullPointerException.class) + public void nodeSchemasOnVariousChoiceCasePathTest() throws Exception { + getJson("/nn-to-json/choice/xml/data_various_path_err.xml"); + } + + /** + * Test when some data are in one case node and other in another. + * Additionally data are loadef from various choices. This isn't correct. + * Next Json validator should return error because nodes has to be from one + * case below concrete choice. + */ + @Test(expected = NullPointerException.class) + public void nodeSchemasOnVariousChoiceCasePathAndMultipleChoicesTest() + throws Exception { + getJson("/nn-to-json/choice/xml/data_more_choices_same_level_various_paths_err.xml"); + } + + /** + * Test when second level data are red first, then first and at the end + * third level. Level represents pass through couple choice-case + */ + + @Test + public void nodeSchemasWithRandomOrderAccordingLevel() throws Exception { + final String json = getJson("/nn-to-json/choice/xml/data_random_level.xml"); + + assertTrue(json.contains("cont")); + assertTrue(json.contains("\"lf1\":\"lf1 val\"")); + assertTrue(json.contains("\"lf1aaa\":\"lf1aaa val\"")); + assertTrue(json.contains("\"lf1aa\":\"lf1aa val\"")); + assertTrue(json.contains("\"lf1a\":121")); + } + + /** + * Test when element from no first case is used. + */ + @Test + public void nodeSchemasNotInFirstCase() throws Exception { + final String json = getJson("/nn-to-json/choice/xml/data_no_first_case.xml"); + + assertTrue(json.contains("cont")); + assertTrue(json.contains("\"lf1\":\"lf1 val\"")); + assertTrue(json.contains("\"lf1ab\":\"lf1ab val\"")); + assertTrue(json.contains("\"lf1a\":121")); + } + + /** + * Test when element in case is list. + */ + @Test + public void nodeSchemaAsList() throws Exception { + final String json = getJson("/nn-to-json/choice/xml/data_list.xml"); + + assertTrue(json.contains("cont")); + assertTrue(json.contains("\"lst1b\":[")); + assertTrue(json.contains("{\"lf11b\":\"lf11b_1 val\"}")); + assertTrue(json.contains("{\"lf11b\":\"lf11b_2 val\"}")); + } + + /** + * Test when element in case is container. + */ + @Test + public void nodeSchemaAsContainer() throws Exception { + final String json = getJson("/nn-to-json/choice/xml/data_container.xml"); + + assertTrue(json.contains("cont")); + assertTrue(json.contains("\"cont1c\":{")); + assertTrue(json.contains("\"lf11c\":\"lf11c val\"")); + } + + /** + * Test when element in case is leaflist. + */ + @Test + public void nodeSchemaAsLeafList() throws Exception { + final String json = getJson("/nn-to-json/choice/xml/data_leaflist.xml"); + + assertTrue(json.contains("cont")); + assertTrue(json.contains("\"lflst1d\":[")); + assertTrue(json.contains("\"lflst1d_1 val\"")); + assertTrue(json.contains("\"lflst1d_2 val\"")); + } + + @Test + public void nodeSchemasInMultipleChoicesTest() throws Exception { + final String json = getJson("/nn-to-json/choice/xml/data_more_choices_same_level.xml"); + + assertTrue(json.contains("cont")); + assertTrue(json.contains("\"lf2b\":\"lf2b value\"")); + assertTrue(json.contains("\"cont1c\":{")); + assertTrue(json.contains("\"lf11c\":\"lf11c val\"")); + } + + /** + * Test whether is possible to find data schema for node which is specified + * as dirrect subnode of choice (case without CASE key word). + */ + @Test + public void nodeSchemasInCaseNotDefinedWithCaseKeyword() throws Exception { + final String json = getJson("/nn-to-json/choice/xml/data_case_defined_without_case.xml"); + + assertTrue(json.contains("cont")); + assertTrue(json.contains("\"lf2b\":\"lf2b val\"")); + assertTrue(json.contains("\"e1\":45")); + } + + /** + * Test of multiple use of choices. + */ + @Test + public void nodeSchemasInThreeChoicesAtSameLevel() throws Exception { + final String json = getJson("/nn-to-json/choice/xml/data_three_choices_same_level.xml"); + + assertTrue(json.contains("cont")); + assertTrue(json.contains("lf2b\":\"lf2b value")); + assertTrue(json.contains("lst4a\":[{")); + assertTrue(json.contains("{\"lf4ab\":33}")); + assertTrue(json.contains("{\"lf4ab\":37}")); + assertTrue(json.contains("\"lf1aaa\":\"lf1aaa value\"")); + } + + private String getJson(final String xmlPath) throws Exception { + final String uri = "choice-case-test:cont"; + final NormalizedNodeContext testNN = TestRestconfUtils + .loadNormalizedContextFromXmlFile(xmlPath, uri, controllerContext); + + final OutputStream output = new ByteArrayOutputStream(); + jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null, + output); + + return output.toString(); + } + + @Override + protected MediaType getMediaType() { + return null; + } +} diff --git a/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnToJsonLeafrefType.java b/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnToJsonLeafrefType.java new file mode 100644 index 0000000..4ea6130 --- /dev/null +++ b/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnToJsonLeafrefType.java @@ -0,0 +1,107 @@ +/* + * 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.json.test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.ws.rs.core.MediaType; +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; +import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest; +import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext; +import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; + +public class NnToJsonLeafrefType extends AbstractBodyReaderTest { + + private static EffectiveModelContext schemaContext; + private final NormalizedNodeJsonBodyWriter jsonBodyWriter; + + public NnToJsonLeafrefType() { + super(schemaContext, null); + jsonBodyWriter = new NormalizedNodeJsonBodyWriter(); + } + + @BeforeClass + public static void initialization() { + schemaContext = schemaContextLoader("/nn-to-json/leafref", schemaContext); + } + + @Test + public void leafrefAbsolutePathToExistingLeafTest() throws Exception { + final String json = toJson("/nn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml"); + validateJson(".*\"lf3\":\\p{Blank}*\"true\".*", json); + } + + @Test + public void leafrefRelativePathToExistingLeafTest() throws Exception { + final String json = toJson("/nn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml"); + validateJson(".*\"lf2\":\\p{Blank}*\"121\".*", json); + } + + @Test(expected = NullPointerException.class) + public void leafrefToNonExistingLeafTest() throws Exception { + toJson("/nn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml"); + } + + @Test + public void leafrefToNotLeafTest() throws Exception { + final String json = toJson("/nn-to-json/leafref/xml/data_ref_to_not_leaf.xml"); + validateJson( + ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44\".*", + json); + } + + @Test + public void leafrefFromLeafListToLeafTest() throws Exception { + final String json = toJson("/nn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml"); + validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"34[5|6|7]\",*\"34[5|6|7]\"," + + "*\"34[5|6|7]\".*", json); + } + + @Test + public void leafrefFromLeafrefToLeafrefTest() throws Exception { + final String json = toJson("/nn-to-json/leafref/xml/data_from_leafref_to_leafref.xml"); + validateJson( + ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*", + json); + } + + private static void validateJson(final String regex, final String value) { + assertNotNull(value); + final Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL); + final Matcher mtch = ptrn.matcher(value); + assertTrue(mtch.matches()); + } + + private String toJson(final String xmlDataPath) throws Exception { + final String uri = "main-module:cont"; + final String pathToInputFile = xmlDataPath; + + final NormalizedNodeContext testNN = TestRestconfUtils + .loadNormalizedContextFromXmlFile(pathToInputFile, uri, controllerContext); + + final OutputStream output = new ByteArrayOutputStream(); + jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null, + output); + final String jsonOutput = output.toString(); + + return jsonOutput; + } + + @Override + protected MediaType getMediaType() { + return new MediaType(MediaType.APPLICATION_XML, null); + } +} diff --git a/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnToJsonWithAugmentTest.java b/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnToJsonWithAugmentTest.java new file mode 100644 index 0000000..d97b246 --- /dev/null +++ b/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnToJsonWithAugmentTest.java @@ -0,0 +1,70 @@ +/* + * 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.json.test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; +import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest; +import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext; +import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; + +@Deprecated +public class NnToJsonWithAugmentTest extends AbstractBodyReaderTest { + + private static EffectiveModelContext schemaContext; + private final NormalizedNodeJsonBodyWriter xmlBodyWriter; + + public NnToJsonWithAugmentTest() { + super(schemaContext, null); + xmlBodyWriter = new NormalizedNodeJsonBodyWriter(); + } + + @BeforeClass + public static void initialize() { + schemaContext = schemaContextLoader("/nn-to-json/augmentation", schemaContext); + } + + @Test + public void augmentedElementsToJson() throws WebApplicationException, + IOException { + final String uri = "yang:cont"; + final String pathToInputFile = "/nn-to-json/augmentation/xml/data.xml"; + + final NormalizedNodeContext testNN = TestRestconfUtils + .loadNormalizedContextFromXmlFile(pathToInputFile, uri, controllerContext); + + final OutputStream output = new ByteArrayOutputStream(); + xmlBodyWriter + .writeTo(testNN, null, null, null, mediaType, null, output); + final String jsonOutput = output.toString(); + + assertNotNull(jsonOutput); + assertTrue(jsonOutput.contains("\"cont1\"" + ":" + '{')); + assertTrue(jsonOutput.contains("\"lf11\"" + ":" + "\"lf11\"")); + assertTrue(jsonOutput.contains("\"lst1\"" + ":" + '[')); + assertTrue(jsonOutput.contains("\"lf11\"" + ":" + "\"lf1_1\"")); + assertTrue(jsonOutput.contains("\"lf11\"" + ":" + "\"lf1_2\"")); + assertTrue(jsonOutput.contains("\"lflst1\"" + ":" + "[")); + assertTrue(jsonOutput.contains("\"lf2\"" + ":" + "\"lf2\"")); + } + + @Override + protected MediaType getMediaType() { + return new MediaType(MediaType.APPLICATION_XML, null); + } +} diff --git a/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnInstanceIdentifierToXmlTest.java b/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnInstanceIdentifierToXmlTest.java new file mode 100644 index 0000000..2ab6476 --- /dev/null +++ b/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnInstanceIdentifierToXmlTest.java @@ -0,0 +1,250 @@ +/* + * 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 java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.net.URISyntaxException; +import javax.ws.rs.core.MediaType; +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest; +import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext; +import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter; +import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; +import org.opendaylight.restconf.common.context.InstanceIdentifierContext; +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.MapEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode; +import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode; +import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder; +import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder; +import org.opendaylight.yangtools.yang.data.api.schema.builder.ListNodeBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders; +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.EffectiveModelContext; +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.util.SchemaInferenceStack; + +public class NnInstanceIdentifierToXmlTest extends AbstractBodyReaderTest { + private static EffectiveModelContext schemaContext; + + private final NormalizedNodeXmlBodyWriter xmlBodyWriter = new NormalizedNodeXmlBodyWriter(); + + public NnInstanceIdentifierToXmlTest() { + super(schemaContext, null); + } + + @BeforeClass + public static void initialization() throws URISyntaxException { + schemaContext = schemaContextLoader("/instanceidentifier/yang", 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("<cont xmlns=")); + assertTrue(outputJson.contains( + '"' + "instance:identifier:module" + '"')); + assertTrue(outputJson.contains(">")); + + assertTrue(outputJson.contains("<cont1>")); + + assertTrue(outputJson.contains("<lf11 xmlns=")); + assertTrue(outputJson.contains( + '"' + "augment:module:leaf:list" + '"')); + assertTrue(outputJson.contains(">")); + assertTrue(outputJson.contains("/instanceidentifier/")); + assertTrue(outputJson.contains("</lf11>")); + + assertTrue(outputJson.contains("<lflst11 xmlns=")); + assertTrue(outputJson.contains( + '"' + "augment:module:leaf:list" + '"')); + assertTrue(outputJson.contains(">")); + assertTrue(outputJson.contains("lflst11 value")); + assertTrue(outputJson.contains("</lflst11>")); + + assertTrue(outputJson.contains("</cont1>")); + assertTrue(outputJson.contains("</cont>")); + } + + private static 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 DataContainerNodeBuilder<NodeIdentifier, ContainerNode> dataCont = SchemaAwareBuilders + .containerBuilder((ContainerSchemaNode) schemaCont); + + final DataSchemaNode schemaCont1 = ((ContainerSchemaNode) schemaCont).getDataChildByName(cont1); + + final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> dataCont1 = SchemaAwareBuilders + .containerBuilder((ContainerSchemaNode) schemaCont1); + + final var instanceLfLst11 = ControllerContext.findInstanceDataChildrenByName( + (DataNodeContainer) schemaCont1, lflst11.getLocalName()); + + final DataSchemaNode lfLst11Schema = instanceLfLst11.get(0).child; + final ListNodeBuilder<Object, SystemLeafSetNode<Object>> lfLst11Data = SchemaAwareBuilders + .leafSetBuilder((LeafListSchemaNode) lfLst11Schema); + + lfLst11Data.withChild(SchemaAwareBuilders.leafSetEntryBuilder((LeafListSchemaNode) lfLst11Schema) + .withValue("lflst11 value").build()); + dataCont1.withChild(lfLst11Data.build()); + + final var instanceLf11 = ControllerContext.findInstanceDataChildrenByName( + (DataNodeContainer) schemaCont1, lf11.getLocalName()); + final DataSchemaNode lf11Schema = instanceLf11.get(0).child; + + dataCont1.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) lf11Schema) + .withValue("/instanceidentifier/").build()); + dataCont.withChild(dataCont1.build()); + + return new NormalizedNodeContext( + InstanceIdentifierContext.ofStack(SchemaInferenceStack.ofDataTreePath(schemaContext, cont)), + dataCont.build()); + } + + @Test + public void nnAsYangInstanceIdentifierAugment() throws Exception { + + final NormalizedNodeContext normalizedNodeContext = preparNNC(); + final OutputStream output = new ByteArrayOutputStream(); + + xmlBodyWriter.writeTo(normalizedNodeContext, null, null, null, + mediaType, null, output); + + assertNotNull(output); + + final String outputJson = output.toString(); + + assertTrue(outputJson.contains("<cont xmlns=")); + assertTrue(outputJson.contains( + '"' + "instance:identifier:module" + '"')); + assertTrue(outputJson.contains(">")); + + assertTrue(outputJson.contains("<cont1>")); + + assertTrue(outputJson.contains("<lst11 xmlns=")); + assertTrue(outputJson.contains('"' + "augment:module" + '"')); + assertTrue(outputJson.contains(">")); + + assertTrue(outputJson.contains( + "<keyvalue111>keyvalue111</keyvalue111>")); + assertTrue(outputJson.contains( + "<keyvalue112>keyvalue112</keyvalue112>")); + + assertTrue(outputJson.contains("<lf111 xmlns=")); + assertTrue(outputJson.contains( + '"' + "augment:augment:module" + '"')); + assertTrue(outputJson.contains(">/cont/cont1/lf12</lf111>")); + + assertTrue(outputJson.contains("<lf112 xmlns=")); + assertTrue(outputJson.contains( + '"' + "augment:augment:module" + '"')); + assertTrue(outputJson.contains(">lf12 value</lf112>")); + + assertTrue(outputJson.contains("</lst11></cont1></cont>")); + } + + private static 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 DataContainerNodeBuilder<NodeIdentifier, ContainerNode> dataCont = SchemaAwareBuilders + .containerBuilder((ContainerSchemaNode) schemaCont); + + final DataSchemaNode schemaCont1 = ((ContainerSchemaNode) schemaCont) + .getDataChildByName(cont1); + + final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> dataCont1 = SchemaAwareBuilders + .containerBuilder((ContainerSchemaNode) schemaCont1); + + final var instanceLst11 = ControllerContext.findInstanceDataChildrenByName( + (DataNodeContainer) schemaCont1, lst11.getLocalName()); + final DataSchemaNode lst11Schema = instanceLst11.get(0).child; + + final CollectionNodeBuilder<MapEntryNode, SystemMapNode> dataLst11 = SchemaAwareBuilders + .mapBuilder((ListSchemaNode) lst11Schema); + + final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> dataLst11Vaule = SchemaAwareBuilders + .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()); + + return new NormalizedNodeContext( + InstanceIdentifierContext.ofStack(SchemaInferenceStack.ofDataTreePath(schemaContext, cont)), + dataCont.build()); + } + + private static DataContainerChild buildLeaf(final DataSchemaNode lst11Schema, final QName qname, + final CollectionNodeBuilder<MapEntryNode, SystemMapNode> dataLst11, final Object value) { + + final var instanceLf = ControllerContext.findInstanceDataChildrenByName( + (DataNodeContainer) lst11Schema, qname.getLocalName()); + final DataSchemaNode schemaLf = instanceLf.get(0).child; + + return SchemaAwareBuilders.leafBuilder((LeafSchemaNode) schemaLf).withValue(value).build(); + } + + @Override + protected MediaType getMediaType() { + return null; + } +} diff --git a/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlTest.java b/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlTest.java new file mode 100644 index 0000000..b1385d9 --- /dev/null +++ b/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlTest.java @@ -0,0 +1,407 @@ +/* + * 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.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import com.google.common.base.Throwables; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import javax.ws.rs.core.MediaType; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; +import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest; +import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext; +import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter; +import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; +import org.opendaylight.restconf.common.context.InstanceIdentifierContext; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.Uint32; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder; +import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec; +import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders; +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.EffectiveModelContext; +import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; +import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition; +import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes; +import org.opendaylight.yangtools.yang.model.ri.type.BitsTypeBuilder; +import org.opendaylight.yangtools.yang.model.ri.type.EnumerationTypeBuilder; +import org.opendaylight.yangtools.yang.model.ri.type.UnionTypeBuilder; +import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack; + +public class NnToXmlTest extends AbstractBodyReaderTest { + private static EffectiveModelContext schemaContext; + + private final NormalizedNodeXmlBodyWriter xmlBodyWriter; + + public NnToXmlTest() { + super(schemaContext, null); + xmlBodyWriter = new NormalizedNodeXmlBodyWriter(); + } + + @BeforeClass + public static void initialization() { + schemaContext = schemaContextLoader("/nn-to-xml/yang", schemaContext); + } + + @Test + public void nnAsYangIdentityrefToXMLTest() throws Exception { + final NormalizedNodeContext normalizedNodeContext = prepareIdrefData(null, true); + nnToXml(normalizedNodeContext, "<lf11 xmlns:x=\"referenced:module\">x:iden</lf11>"); + } + + @Test + public void nnAsYangIdentityrefWithQNamePrefixToXMLTest() throws Exception { + final NormalizedNodeContext normalizedNodeContext = prepareIdrefData("prefix", true); + nnToXml(normalizedNodeContext, "<lf11 xmlns", "=\"referenced:module\">", ":iden</lf11>"); + } + + @Test + public void nnAsYangIdentityrefWithPrefixToXMLTest() throws Exception { + final NormalizedNodeContext normalizedNodeContext = prepareIdrefData("prefix", false); + nnToXml(normalizedNodeContext, "<lf11>no qname value</lf11>"); + } + + @Test + public void nnAsYangLeafrefWithPrefixToXMLTest() throws Exception { + nnToXml(prepareLeafrefData(), "<lfBoolean>true</lfBoolean>", "<lfLfref>true</lfLfref>"); + } + + /** + * Negative test when leaf of type leafref references to not-existing leaf. + * {@code VerifyException} is expected. + */ + @Test + public void nnAsYangLeafrefWithPrefixToXMLNegativeTest() throws Exception { + final NormalizedNodeContext normalizedNodeContext = prepareLeafrefNegativeData(); + + final IOException ex = assertThrows(IOException.class, () -> nnToXml(normalizedNodeContext, + "<not-existing>value</not-existing>", "<lfLfrefNegative>value</lfLfrefnegative>")); + final Throwable rootCause = Throwables.getRootCause(ex); + assertThat(rootCause, instanceOf(IllegalArgumentException.class)); + assertEquals("Data tree child (basic:module?revision=2013-12-02)not-existing not present in schema parent " + + "(basic:module?revision=2013-12-02)cont", rootCause.getMessage()); + } + + @Test + public void nnAsYangStringToXmlTest() throws Exception { + final NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(BaseTypes.stringType()).deserialize("lfStr value"), "lfStr"); + nnToXml(normalizedNodeContext, "<lfStr>lfStr value</lfStr>"); + } + + @Test + public void nnAsYangInt8ToXmlTest() throws Exception { + final String elName = "lfInt8"; + + final NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(BaseTypes.int8Type()).deserialize("14"), elName); + nnToXml(normalizedNodeContext, "<" + elName + ">14</" + elName + ">"); + } + + @Test + public void nnAsYangInt16ToXmlTest() throws Exception { + final String elName = "lfInt16"; + + final NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(BaseTypes.int16Type()).deserialize("3000"), elName); + nnToXml(normalizedNodeContext, "<" + elName + ">3000</" + elName + ">"); + } + + @Test + public void nnAsYangInt32ToXmlTest() throws Exception { + final String elName = "lfInt32"; + + final NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(BaseTypes.int32Type()).deserialize("201234"), elName); + nnToXml(normalizedNodeContext, "<" + elName + ">201234</" + elName + ">"); + } + + @Test + public void nnAsYangInt64ToXmlTest() throws Exception { + final String elName = "lfInt64"; + + final NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(BaseTypes.int64Type()).deserialize("5123456789"), elName); + nnToXml(normalizedNodeContext, "<" + elName + ">5123456789</" + elName + ">"); + } + + @Test + public void nnAsYangUint8ToXmlTest() throws Exception { + final String elName = "lfUint8"; + + final NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(BaseTypes.uint8Type()).deserialize("200"), elName); + nnToXml(normalizedNodeContext, "<" + elName + ">200</" + elName + ">"); + } + + @Test + public void snAsYangUint16ToXmlTest() throws Exception { + final String elName = "lfUint16"; + + final NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(BaseTypes.uint16Type()).deserialize("4000"), elName); + nnToXml(normalizedNodeContext, "<" + elName + ">4000</" + elName + ">"); + } + + @Test + public void nnAsYangUint32ToXmlTest() throws Exception { + final String elName = "lfUint32"; + + final NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(BaseTypes.uint32Type()).deserialize("4123456789"), elName); + nnToXml(normalizedNodeContext, "<" + elName + ">4123456789</" + elName + ">"); + } + + @Test + public void snAsYangUint64ToXmlTest() throws Exception { + final String elName = "lfUint64"; + final NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(BaseTypes.uint64Type()).deserialize("5123456789"), elName); + nnToXml(normalizedNodeContext, "<" + elName + ">5123456789</" + elName + ">"); + } + + @Test + public void nnAsYangBinaryToXmlTest() throws Exception { + final String elName = "lfBinary"; + final NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(BaseTypes.binaryType()) + .deserialize("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567"), + elName); + nnToXml(normalizedNodeContext, + "<" + elName + ">ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567</" + elName + ">"); + } + + @Test + public void nnAsYangBitsToXmlTest() throws Exception { + final BitsTypeDefinition.Bit mockBit1 = Mockito.mock(BitsTypeDefinition.Bit.class); + Mockito.when(mockBit1.getName()).thenReturn("one"); + Mockito.when(mockBit1.getPosition()).thenReturn(Uint32.ONE); + final BitsTypeDefinition.Bit mockBit2 = Mockito.mock(BitsTypeDefinition.Bit.class); + Mockito.when(mockBit2.getName()).thenReturn("two"); + Mockito.when(mockBit2.getPosition()).thenReturn(Uint32.TWO); + final BitsTypeBuilder bitsTypeBuilder = BaseTypes.bitsTypeBuilder(QName.create("foo", "foo")); + bitsTypeBuilder.addBit(mockBit1); + bitsTypeBuilder.addBit(mockBit2); + + final String elName = "lfBits"; + final NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(bitsTypeBuilder.build()).deserialize("one two"), elName); + nnToXml(normalizedNodeContext, "<" + elName + ">one two</" + elName + ">"); + } + + @Test + public void nnAsYangEnumerationToXmlTest() throws Exception { + final EnumTypeDefinition.EnumPair mockEnum = Mockito.mock(EnumTypeDefinition.EnumPair.class); + Mockito.when(mockEnum.getName()).thenReturn("enum2"); + + final EnumerationTypeBuilder enumerationTypeBuilder = BaseTypes + .enumerationTypeBuilder(QName.create("foo", "foo")); + enumerationTypeBuilder.addEnum(mockEnum); + + final String elName = "lfEnumeration"; + final NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(enumerationTypeBuilder.build()).deserialize("enum2"), elName); + nnToXml(normalizedNodeContext, "<" + elName + ">enum2</" + elName + ">"); + } + + @Test + public void nnAsYangEmptyToXmlTest() throws Exception { + final String elName = "lfEmpty"; + final NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(BaseTypes.emptyType()).deserialize(""), elName); + nnToXml(normalizedNodeContext, "<" + elName + "/>"); + } + + @Test + public void nnAsYangBooleanToXmlTest() throws Exception { + final String elName = "lfBoolean"; + NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(BaseTypes.booleanType()).deserialize("false"), elName); + nnToXml(normalizedNodeContext, "<" + elName + ">false</" + elName + ">"); + + normalizedNodeContext = prepareNNC(TypeDefinitionAwareCodec.from(BaseTypes.booleanType()).deserialize("true"), + elName); + nnToXml(normalizedNodeContext, "<" + elName + ">true</" + elName + ">"); + } + + @Test + public void nnAsYangUnionToXmlTest() throws Exception { + final BitsTypeDefinition.Bit mockBit1 = Mockito.mock(BitsTypeDefinition.Bit.class); + Mockito.when(mockBit1.getName()).thenReturn("first"); + Mockito.when(mockBit1.getPosition()).thenReturn(Uint32.ONE); + final BitsTypeDefinition.Bit mockBit2 = Mockito.mock(BitsTypeDefinition.Bit.class); + Mockito.when(mockBit2.getName()).thenReturn("second"); + Mockito.when(mockBit2.getPosition()).thenReturn(Uint32.TWO); + + final BitsTypeBuilder bitsTypeBuilder = BaseTypes.bitsTypeBuilder(QName.create("foo", "foo")); + bitsTypeBuilder.addBit(mockBit1); + bitsTypeBuilder.addBit(mockBit2); + + final UnionTypeBuilder unionTypeBuilder = BaseTypes.unionTypeBuilder(QName.create("foo", "foo")); + unionTypeBuilder.addType(BaseTypes.int8Type()); + unionTypeBuilder.addType(bitsTypeBuilder.build()); + unionTypeBuilder.addType(BaseTypes.booleanType()); + unionTypeBuilder.addType(BaseTypes.stringType()); + + final String elName = "lfUnion"; + + // test int8 + final String int8 = "15"; + NormalizedNodeContext normalizedNodeContext = prepareNNC( + TypeDefinitionAwareCodec.from(unionTypeBuilder.build()).deserialize(int8), elName); + nnToXml(normalizedNodeContext, "<" + elName + ">15</" + elName + ">"); + + // test bits + final String bits = "first second"; + normalizedNodeContext = prepareNNC(TypeDefinitionAwareCodec.from(unionTypeBuilder.build()).deserialize(bits), + elName); + nnToXml(normalizedNodeContext, "<" + elName + ">[first, second]</" + elName + ">"); + + // test boolean + final String bool = "true"; + normalizedNodeContext = prepareNNC(TypeDefinitionAwareCodec.from(unionTypeBuilder.build()).deserialize(bool), + elName); + nnToXml(normalizedNodeContext, "<" + elName + ">true</" + elName + ">"); + + // test string + final String s = "Hi!"; + normalizedNodeContext = prepareNNC(TypeDefinitionAwareCodec.from(unionTypeBuilder.build()).deserialize(s), + elName); + nnToXml(normalizedNodeContext, "<" + elName + ">Hi!</" + elName + ">"); + } + + private static NormalizedNodeContext prepareNNC(final Object object, final String name) { + final QName cont = QName.create("basic:module", "2013-12-02", "cont"); + final QName lf = QName.create("basic:module", "2013-12-02", name); + + final DataSchemaNode contSchema = schemaContext.getDataChildByName(cont); + + final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> contData = SchemaAwareBuilders + .containerBuilder((ContainerSchemaNode) contSchema); + + final var instanceLf = ControllerContext + .findInstanceDataChildrenByName((DataNodeContainer) contSchema, lf.getLocalName()); + final DataSchemaNode schemaLf = instanceLf.get(0).child; + + contData.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) schemaLf).withValue(object).build()); + + return new NormalizedNodeContext( + InstanceIdentifierContext.ofStack(SchemaInferenceStack.ofDataTreePath(schemaContext, cont)), + contData.build()); + } + + private void nnToXml(final NormalizedNodeContext normalizedNodeContext, final String... xmlRepresentation) + throws Exception { + final OutputStream output = new ByteArrayOutputStream(); + xmlBodyWriter.writeTo(normalizedNodeContext, null, null, null, mediaType, null, output); + + for (String element : xmlRepresentation) { + assertTrue(output.toString().contains(element)); + } + } + + private static NormalizedNodeContext prepareLeafrefData() { + final QName cont = QName.create("basic:module", "2013-12-02", "cont"); + final QName lfBoolean = QName.create("basic:module", "2013-12-02", "lfBoolean"); + final QName lfLfref = QName.create("basic:module", "2013-12-02", "lfLfref"); + + final DataSchemaNode contSchema = schemaContext.getDataChildByName(cont); + + final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> contData = SchemaAwareBuilders + .containerBuilder((ContainerSchemaNode) contSchema); + + var instanceLf = ControllerContext + .findInstanceDataChildrenByName((DataNodeContainer) contSchema, lfBoolean.getLocalName()); + DataSchemaNode schemaLf = instanceLf.get(0).child; + + contData.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) schemaLf).withValue(Boolean.TRUE).build()); + + instanceLf = ControllerContext.findInstanceDataChildrenByName((DataNodeContainer) contSchema, + lfLfref.getLocalName()); + schemaLf = instanceLf.get(0).child; + + contData.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) schemaLf).withValue("true").build()); + + return new NormalizedNodeContext( + InstanceIdentifierContext.ofStack(SchemaInferenceStack.ofDataTreePath(schemaContext, cont)), + contData.build()); + } + + private static NormalizedNodeContext prepareLeafrefNegativeData() { + final QName cont = QName.create("basic:module", "2013-12-02", "cont"); + final QName lfLfref = QName.create("basic:module", "2013-12-02", "lfLfrefNegative"); + + final DataSchemaNode contSchema = schemaContext.getDataChildByName(cont); + final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> contData = SchemaAwareBuilders + .containerBuilder((ContainerSchemaNode) contSchema); + + final var instanceLf = ControllerContext.findInstanceDataChildrenByName((DataNodeContainer) + contSchema, lfLfref.getLocalName()); + final DataSchemaNode schemaLf = instanceLf.get(0).child; + + contData.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) schemaLf).withValue("value").build()); + + return new NormalizedNodeContext( + InstanceIdentifierContext.ofStack(SchemaInferenceStack.ofDataTreePath(schemaContext, cont)), + contData.build()); + } + + private static NormalizedNodeContext prepareIdrefData(final String prefix, final boolean valueAsQName) { + final QName cont = QName.create("basic:module", "2013-12-02", "cont"); + final QName cont1 = QName.create("basic:module", "2013-12-02", "cont1"); + final QName lf11 = QName.create("basic:module", "2013-12-02", "lf11"); + + final DataSchemaNode contSchema = schemaContext.getDataChildByName(cont); + + final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> contData = SchemaAwareBuilders + .containerBuilder((ContainerSchemaNode) contSchema); + + final DataSchemaNode cont1Schema = ((ContainerSchemaNode) contSchema).getDataChildByName(cont1); + + final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> cont1Data = SchemaAwareBuilders + .containerBuilder((ContainerSchemaNode) cont1Schema); + + Object value = null; + if (valueAsQName) { + value = QName.create("referenced:module", "2013-12-02", "iden"); + } else { + value = "no qname value"; + } + + final var instanceLf = ControllerContext + .findInstanceDataChildrenByName((DataNodeContainer) cont1Schema, lf11.getLocalName()); + final DataSchemaNode schemaLf = instanceLf.get(0).child; + + cont1Data.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) schemaLf).withValue(value).build()); + + contData.withChild(cont1Data.build()); + + final NormalizedNodeContext testNormalizedNodeContext = new NormalizedNodeContext( + InstanceIdentifierContext.ofStack(SchemaInferenceStack.ofDataTreePath(schemaContext, cont)), + contData.build()); + return testNormalizedNodeContext; + } + + @Override + protected MediaType getMediaType() { + return null; + } +} diff --git a/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithChoiceTest.java b/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithChoiceTest.java new file mode 100644 index 0000000..9d2954d --- /dev/null +++ b/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithChoiceTest.java @@ -0,0 +1,108 @@ +/* + * 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.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import javax.ws.rs.core.MediaType; +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest; +import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext; +import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter; +import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; +import org.opendaylight.restconf.common.context.InstanceIdentifierContext; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders; +import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; +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.EffectiveModelContext; +import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; +import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack; + +public class NnToXmlWithChoiceTest extends AbstractBodyReaderTest { + + private final NormalizedNodeXmlBodyWriter xmlBodyWriter; + private static EffectiveModelContext schemaContext; + + public NnToXmlWithChoiceTest() { + super(schemaContext, null); + xmlBodyWriter = new NormalizedNodeXmlBodyWriter(); + } + + @BeforeClass + public static void initialization() { + schemaContext = schemaContextLoader("/nn-to-xml/choice", schemaContext); + } + + @Test + public void cnSnToXmlWithYangChoice() throws Exception { + NormalizedNodeContext normalizedNodeContext = prepareNNC("lf1", + "String data1"); + OutputStream output = new ByteArrayOutputStream(); + xmlBodyWriter.writeTo(normalizedNodeContext, null, null, null, + mediaType, null, output); + assertTrue(output.toString().contains("<lf1>String data1</lf1>")); + + normalizedNodeContext = prepareNNC("lf2", "String data2"); + output = new ByteArrayOutputStream(); + + xmlBodyWriter.writeTo(normalizedNodeContext, null, null, null, + mediaType, null, output); + assertTrue(output.toString().contains("<lf2>String data2</lf2>")); + } + + private static NormalizedNodeContext prepareNNC(final String name, final Object value) { + + final QName contQname = QName.create("module:with:choice", "2013-12-18", + "cont"); + final QName lf = QName.create("module:with:choice", "2013-12-18", name); + final QName choA = QName.create("module:with:choice", "2013-12-18", "choA"); + + final DataSchemaNode contSchemaNode = schemaContext + .getDataChildByName(contQname); + final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> dataContainerNodeAttrBuilder = SchemaAwareBuilders + .containerBuilder((ContainerSchemaNode) contSchemaNode); + + final DataSchemaNode choiceSchemaNode = ((ContainerSchemaNode) contSchemaNode) + .getDataChildByName(choA); + assertTrue(choiceSchemaNode instanceof ChoiceSchemaNode); + + final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> dataChoice = SchemaAwareBuilders + .choiceBuilder((ChoiceSchemaNode) choiceSchemaNode); + + final var instanceLf = ControllerContext + .findInstanceDataChildrenByName( + (DataNodeContainer) contSchemaNode, lf.getLocalName()); + final DataSchemaNode schemaLf = instanceLf.get(0).child; + + dataChoice.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) schemaLf) + .withValue(value).build()); + + dataContainerNodeAttrBuilder.withChild(dataChoice.build()); + + final NormalizedNodeContext testNormalizedNodeContext = new NormalizedNodeContext( + InstanceIdentifierContext.ofStack(SchemaInferenceStack.ofDataTreePath(schemaContext, contQname)), + dataContainerNodeAttrBuilder.build()); + + return testNormalizedNodeContext; + } + + @Override + protected MediaType getMediaType() { + return null; + } +} diff --git a/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithDataFromSeveralModulesTest.java b/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithDataFromSeveralModulesTest.java new file mode 100644 index 0000000..a9cf7a6 --- /dev/null +++ b/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithDataFromSeveralModulesTest.java @@ -0,0 +1,144 @@ +/* + * 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.assertTrue; + +import com.google.common.base.Preconditions; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.URISyntaxException; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest; +import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext; +import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter; +import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; +import org.opendaylight.restconf.common.context.InstanceIdentifierContext; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders; +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.EffectiveModelContext; +import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; + +public class NnToXmlWithDataFromSeveralModulesTest extends + AbstractBodyReaderTest { + + private final NormalizedNodeXmlBodyWriter xmlBodyWriter; + private static EffectiveModelContext schemaContext; + + public NnToXmlWithDataFromSeveralModulesTest() { + super(schemaContext, null); + xmlBodyWriter = new NormalizedNodeXmlBodyWriter(); + } + + @BeforeClass + public static void initialize() { + schemaContext = schemaContextLoader("/nn-to-xml/data-of-several-modules/yang", schemaContext); + } + + @Test + public void dataFromSeveralModulesToXmlTest() + throws WebApplicationException, IOException, URISyntaxException { + final NormalizedNodeContext normalizedNodeContext = prepareNormalizedNodeContext(); + final OutputStream output = new ByteArrayOutputStream(); + xmlBodyWriter.writeTo(normalizedNodeContext, null, null, null, + mediaType, null, output); + + final String outputString = output.toString(); + // data + assertTrue(outputString + .contains( + "<data xmlns=" + '"' + + "urn:ietf:params:xml:ns:netconf:base:1.0" + + '"' + '>')); + // cont m2 + assertTrue(outputString.contains( + "<cont_m2 xmlns=" + '"' + "module:two" + '"' + '>')); + assertTrue(outputString.contains("<lf1_m2>lf1 m2 value</lf1_m2>")); + assertTrue(outputString.contains("<contB_m2/>")); + assertTrue(outputString.contains("</cont_m2>")); + + // cont m1 + assertTrue(outputString.contains( + "<cont_m1 xmlns=" + '"' + "module:one" + '"' + '>')); + assertTrue(outputString.contains("<contB_m1/>")); + assertTrue(outputString.contains("<lf1_m1>lf1 m1 value</lf1_m1>")); + assertTrue(outputString.contains("</cont_m1>")); + + // end + assertTrue(output.toString().contains("</data>")); + } + + @Override + protected MediaType getMediaType() { + // TODO Auto-generated method stub + return null; + } + + private static NormalizedNodeContext prepareNormalizedNodeContext() { + final String rev = "2014-01-17"; + + final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> dataContSchemaContNode = SchemaAwareBuilders + .containerBuilder(schemaContext); + + final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> modul1 = buildContBuilderMod1( + "module:one", rev, "cont_m1", "contB_m1", "lf1_m1", + "lf1 m1 value"); + dataContSchemaContNode.withChild(modul1.build()); + + final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> modul2 = buildContBuilderMod1( + "module:two", rev, "cont_m2", "contB_m2", "lf1_m2", + "lf1 m2 value"); + dataContSchemaContNode.withChild(modul2.build()); + + final NormalizedNodeContext testNormalizedNodeContext = new NormalizedNodeContext( + InstanceIdentifierContext.ofLocalRoot(schemaContext), + dataContSchemaContNode.build()); + + return testNormalizedNodeContext; + } + + private static DataContainerNodeBuilder<NodeIdentifier, ContainerNode> buildContBuilderMod1( + final String uri, final String rev, final String cont, final String contB, final String lf1, + final String lf1Value) { + final QName contQname = QName.create(uri, rev, cont); + final QName contBQname = QName.create(uri, rev, contB); + final QName lf1Qname = QName.create(contQname, lf1); + + final DataSchemaNode contSchemaNode = schemaContext + .getDataChildByName(contQname); + final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> dataContainerNodeAttrBuilder = SchemaAwareBuilders + .containerBuilder((ContainerSchemaNode) contSchemaNode); + + Preconditions.checkState(contSchemaNode instanceof ContainerSchemaNode); + final var instanceLf1_m1 = ControllerContext.findInstanceDataChildrenByName( + (DataNodeContainer) contSchemaNode, lf1Qname.getLocalName()); + final DataSchemaNode schemaLf1_m1 = instanceLf1_m1.get(0).child; + + dataContainerNodeAttrBuilder.withChild(SchemaAwareBuilders + .leafBuilder((LeafSchemaNode) schemaLf1_m1) + .withValue(lf1Value).build()); + + final DataSchemaNode contBSchemaNode = ((ContainerSchemaNode) contSchemaNode) + .getDataChildByName(contBQname); + + final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> dataContainerB = SchemaAwareBuilders + .containerBuilder((ContainerSchemaNode) contBSchemaNode); + + return dataContainerNodeAttrBuilder.withChild(dataContainerB.build()); + } +} |