diff options
Diffstat (limited to 'netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json')
3 files changed, 358 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); + } +} |