From 29783e67f0d3b7d0b47ba0e2e30684b3424feaad Mon Sep 17 00:00:00 2001 From: Ivan Hrasko Date: Wed, 18 May 2016 11:05:35 +0200 Subject: [PATCH] Bug 5527: Unit testing - serializer Unit tests for YangInstanceIdentifierSerializer Change-Id: Ife737bb38f43e7855a9b8657fa56bd49adf1be18 Signed-off-by: Ivan Hrasko --- .../YangInstanceIdentifierSerializerTest.java | 332 ++++++++++++++++++ .../serializer/serializer-test-included.yang | 22 ++ .../parser/serializer/serializer-test.yang | 86 +++++ 3 files changed, 440 insertions(+) create mode 100644 restconf/sal-rest-connector/src/test/java/org/opendaylight/restconf/parser/builder/YangInstanceIdentifierSerializerTest.java create mode 100644 restconf/sal-rest-connector/src/test/resources/restconf/parser/serializer/serializer-test-included.yang create mode 100644 restconf/sal-rest-connector/src/test/resources/restconf/parser/serializer/serializer-test.yang diff --git a/restconf/sal-rest-connector/src/test/java/org/opendaylight/restconf/parser/builder/YangInstanceIdentifierSerializerTest.java b/restconf/sal-rest-connector/src/test/java/org/opendaylight/restconf/parser/builder/YangInstanceIdentifierSerializerTest.java new file mode 100644 index 0000000000..965ce34995 --- /dev/null +++ b/restconf/sal-rest-connector/src/test/java/org/opendaylight/restconf/parser/builder/YangInstanceIdentifierSerializerTest.java @@ -0,0 +1,332 @@ +/* + * Copyright (c) 2016 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.restconf.parser.builder; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; +import java.util.LinkedHashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; +import org.opendaylight.restconf.utils.parser.builder.ParserBuilderConstants; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; + +/** + * Unit tests for {@link YangInstanceIdentifierSerializer} + */ +public class YangInstanceIdentifierSerializerTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + // schema context with test modules + private SchemaContext schemaContext; + + @Before + public void init() throws Exception { + schemaContext = TestRestconfUtils.loadSchemaContext("/restconf/parser/serializer"); + } + + /** + * Positive test of serialization of YangInstanceIdentifier containing container node to + * String. Returned String is compared to have expected value. + */ + @Test + public void serializeContainerTest() { + final YangInstanceIdentifier data = YangInstanceIdentifier.builder() + .node(QName.create("serializer:test", "2016-06-06", "contA")) + .build(); + + final String result = YangInstanceIdentifierSerializer.create(schemaContext, data); + assertEquals("Serialization not successful", + "/serializer-test:contA", result); + } + + /** + * Positive test of serialization of YangInstanceIdentifier containing container with leaf node to + * String. Returned String is compared to have expected value. + */ + @Test + public void serializeContainerWithLeafTest() { + final YangInstanceIdentifier data = YangInstanceIdentifier.builder() + .node(QName.create("serializer:test", "2016-06-06", "contA")) + .node(QName.create("serializer:test", "2016-06-06", "leaf-A")) + .build(); + + final String result = YangInstanceIdentifierSerializer.create(schemaContext, data); + assertEquals("Serialization not successful", "/serializer-test:contA/leaf-A", result); + } + + /** + * Positive test of serialization of YangInstanceIdentifier containing container with list with leaf + * list node to String. Returned String is compared to have expected value. + */ + @Test + public void serializeContainerWithListWithLeafListTest() { + final QName list = QName.create("serializer:test", "2016-06-06", "list-A"); + final QName leafList = QName.create("serializer:test", "2016-06-06", "leaf-list-AA"); + + final YangInstanceIdentifier data = YangInstanceIdentifier.builder() + .node(QName.create("serializer:test", "2016-06-06", "contA")) + .node(list) + .node(new YangInstanceIdentifier.NodeIdentifierWithPredicates( + list, QName.create(list, "list-key"), 100)) + .node(leafList) + .node(new NodeWithValue(leafList, "instance")) + .build(); + + final String result = YangInstanceIdentifierSerializer.create(schemaContext, data); + assertEquals("Serialization not successful", + "/serializer-test:contA/list-A=100/leaf-list-AA=instance", + result); + } + + /** + * Positive test of serialization of YangInstanceIdentifier to String when serialized + * YangInstanceIdentifier contains list with no keys. Returned String is compared to have + * expected value. + */ + @Test + public void serializeListWithNoKeysTest() { + final YangInstanceIdentifier data = YangInstanceIdentifier.builder() + .node(QName.create("serializer:test", "2016-06-06", "list-no-key")) + .nodeWithKey(QName.create("serializer:test", "2016-06-06", "list-no-key"), Maps.newHashMap()) + .build(); + + final String result = YangInstanceIdentifierSerializer.create(schemaContext, data); + assertEquals("Serialization not successful", "/serializer-test:list-no-key", result); + } + + /** + * Positive test of serialization of YangInstanceIdentifier to String when serialized + * YangInstanceIdentifier contains list with one key. Returned String is compared to have + * expected value. + */ + @Test + public void serializeListWithOneKeyTest() { + final YangInstanceIdentifier data = YangInstanceIdentifier.builder() + .node(QName.create("serializer:test", "2016-06-06", "list-one-key")) + .nodeWithKey(QName.create("serializer:test", "2016-06-06", "list-one-key"), + QName.create("serializer:test", "2016-06-06", "list-one-key"), "value") + .build(); + + final String result = YangInstanceIdentifierSerializer.create(schemaContext, data); + assertEquals("Serialization not successful", "/serializer-test:list-one-key=value", result); + } + + /** + * Positive test of serialization of YangInstanceIdentifier to String when serialized + * YangInstanceIdentifier contains list with multiple keys. Returned String is compared + * to have expected value. + */ + @Test + public void serializeListWithMultipleKeysTest() { + final QName list = QName.create("serializer:test", "2016-06-06", "list-multiple-keys"); + final Map values = new LinkedHashMap<>(); + values.put(QName.create(list, "name"), "value-1"); + values.put(QName.create(list, "number"), "2"); + values.put(QName.create(list, "enabled"), "true"); + + final YangInstanceIdentifier data = YangInstanceIdentifier.builder() + .node(list).nodeWithKey(list, values).build(); + + final String result = YangInstanceIdentifierSerializer.create(schemaContext, data); + assertEquals("Serialization not successful", "/serializer-test:list-multiple-keys=value-1,2,true", result); + } + + /** + * Positive test of serialization of YangInstanceIdentifier to String when serialized + * YangInstanceIdentifier contains leaf node. Returned String is compared to have + * expected value. + */ + @Test + public void serializeLeafTest() { + final YangInstanceIdentifier data = YangInstanceIdentifier.builder() + .node(QName.create("serializer:test", "2016-06-06", "leaf-0")) + .build(); + + final String result = YangInstanceIdentifierSerializer.create(schemaContext, data); + assertEquals("Serialization not successful", "/serializer-test:leaf-0", result); + } + + /** + * Positive test of serialization of YangInstanceIdentifier to String when serialized + * YangInstanceIdentifier contains leaf list node. Returned String is compared to have + * expected value. + */ + @Test + public void serializeLeafListTest() { + final YangInstanceIdentifier data = YangInstanceIdentifier.builder() + .node(QName.create("serializer:test", "2016-06-06", "leaf-list-0")) + .node(new NodeWithValue(QName.create("serializer:test", "2016-06-06", "leaf-list-0"), "instance")) + .build(); + + final String result = YangInstanceIdentifierSerializer.create(schemaContext, data); + assertEquals("Serialization not successful", "/serializer-test:leaf-list-0=instance", result); + } + + /** + * Negative test of serialization YangInstanceIdentifier to String when + * SchemaContext is null. Test is expected to fail with + * NullPointerException. + */ + @Test + public void serializeNullSchemaContextNegativeTest() { + thrown.expect(NullPointerException.class); + YangInstanceIdentifierSerializer.create(null, YangInstanceIdentifier.EMPTY); + } + + /** + * Negative test of serialization YangInstanceIdentifier to String when supplied + * YangInstanceIdentifier is null. Test is expected to fail with + * NullPointerException. + */ + @Test + public void serializeNullDataNegativeTest() { + thrown.expect(NullPointerException.class); + YangInstanceIdentifierSerializer.create(schemaContext, null); + } + + /** + * Test of serialization YangInstanceIdentifier to String when supplied + * YangInstanceIdentifier is YangInstanceIdentifier.EMPTY. + * Single slash is expected as a return value. + */ + @Test + public void serializeEmptyDataTest() { + final String result = YangInstanceIdentifierSerializer.create(schemaContext, YangInstanceIdentifier.EMPTY); + assertEquals("Empty identifier is expected", "/", result); + } + + /** + * Negative test when it is not possible to find child node of current node. Test is expected to fail with + * IllegalArgumentException and error message is compared to expected error message. + */ + @Test + public void serializeChildNodeNotFoundNegativeTest() { + final YangInstanceIdentifier data = YangInstanceIdentifier.builder() + .node(QName.create("serializer:test", "2016-06-06", "contA")) + .node(QName.create("serializer:test", "2016-06-06", "not-existing-leaf")) + .build(); + + thrown.expect(IllegalArgumentException.class); + YangInstanceIdentifierSerializer.create(schemaContext, data); + } + + /** + * Test to verify if all reserved characters according to rfc3986 are considered by serializer implementation to + * be percent encoded. + */ + @Test + public void verifyReservedCharactersTest() { + final char[] genDelims = { ':', '/', '?', '#', '[', ']', '@' }; + final char[] subDelims = { '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=' }; + + for (final char c : genDelims) { + assertTrue("Current character is reserved and should be percent encoded", + ParserBuilderConstants.Serializer.PERCENT_ENCODE_CHARS.matches(c)); + } + + for (final char c : subDelims) { + assertTrue("Current character is reserved and should be percent encoded", + ParserBuilderConstants.Serializer.PERCENT_ENCODE_CHARS.matches(c)); + } + } + + /** + * Test if URIs with percent encoded characters are all correctly serialized. + */ + @Test + public void serializePercentEncodingTest() { + final String value = "foo" + ":foo bar/" + "foo,bar/" + "'bar'"; + final String encoded = "foo%3Afoo%20bar%2Ffoo%2Cbar%2F%27bar%27"; + + final YangInstanceIdentifier data = YangInstanceIdentifier.builder() + .node(QName.create("serializer:test", "2016-06-06", "list-one-key")) + .nodeWithKey(QName.create("serializer:test", "2016-06-06", "list-one-key"), + QName.create("serializer:test", "2016-06-06", "list-one-key"), value) + .build(); + + final String result = YangInstanceIdentifierSerializer.create(schemaContext, data); + assertEquals("Serialization not successful", "/serializer-test:list-one-key=" + encoded, result); + } + + /** + * Test if URIs with no percent encoded characters are correctly serialized. Input should be untouched. + */ + @Test + public void serializeNoPercentEncodingTest() { + final String value = "foo\"b\"bar"; + + final YangInstanceIdentifier data = YangInstanceIdentifier.builder() + .node(QName.create("serializer:test", "2016-06-06", "list-one-key")) + .nodeWithKey(QName.create("serializer:test", "2016-06-06", "list-one-key"), + QName.create("serializer:test", "2016-06-06", "list-one-key"), value) + .build(); + + final String result = YangInstanceIdentifierSerializer.create(schemaContext, data); + assertEquals("Serialization not successful", "/serializer-test:list-one-key=" + value, result); + } + + /** + * Test of serialization when nodes in input YangInstanceIdentifier are defined in two different + * modules by using augmentation. + */ + @Test + public void serializeIncludedNodesTest() { + final QName list = QName.create("serializer:test:included", "2016-06-06", "augmented-list"); + final QName child = QName.create("serializer:test", "2016-06-06", "augmented-leaf"); + + final YangInstanceIdentifier data = YangInstanceIdentifier.builder() + .node(list) + .node(new YangInstanceIdentifier.NodeIdentifierWithPredicates( + list, QName.create(list, "list-key"), 100)) + .node(new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(child))) + .node(child) + .build(); + + final String result = YangInstanceIdentifierSerializer.create(schemaContext, data); + + assertEquals("Serialization not successful", + "/serializer-test-included:augmented-list=100/serializer-test:augmented-leaf", result); + } + + /** + * Test of serialization when nodes in input YangInstanceIdentifier are defined in two different + * modules by using augmentation. Augmented node in data supplied for serialization has wrong namespace. + * IllegalArgumentException is expected because augmented node is defined in other module than its + * parent and will not be found. + */ + @Test + public void serializeIncludedNodesSerializationTest() { + final QName list = QName.create("serializer:test:included", "2016-06-06", "augmented-list"); + // child should has different namespace + final QName child = QName.create("serializer:test:included", "2016-06-06", "augmented-leaf"); + + final YangInstanceIdentifier data = YangInstanceIdentifier.builder() + .node(list) + .node(new YangInstanceIdentifier.NodeIdentifierWithPredicates( + list, QName.create(list, "list-key"), 100)) + .node(new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(child))) + .node(child) + .build(); + + thrown.expect(IllegalArgumentException.class); + YangInstanceIdentifierSerializer.create(schemaContext, data); + } +} diff --git a/restconf/sal-rest-connector/src/test/resources/restconf/parser/serializer/serializer-test-included.yang b/restconf/sal-rest-connector/src/test/resources/restconf/parser/serializer/serializer-test-included.yang new file mode 100644 index 0000000000..c404aeb8af --- /dev/null +++ b/restconf/sal-rest-connector/src/test/resources/restconf/parser/serializer/serializer-test-included.yang @@ -0,0 +1,22 @@ +module serializer-test-included { + namespace "serializer:test:included"; + prefix "sti"; + yang-version 1; + + revision 2016-06-06 { + description + "Initial revision."; + } + + list augmented-list { + key list-key; + + leaf list-key { + type uint16; + } + + leaf list-value { + type string; + } + } +} \ No newline at end of file diff --git a/restconf/sal-rest-connector/src/test/resources/restconf/parser/serializer/serializer-test.yang b/restconf/sal-rest-connector/src/test/resources/restconf/parser/serializer/serializer-test.yang new file mode 100644 index 0000000000..691e4dc8e2 --- /dev/null +++ b/restconf/sal-rest-connector/src/test/resources/restconf/parser/serializer/serializer-test.yang @@ -0,0 +1,86 @@ +module serializer-test { + namespace "serializer:test"; + prefix "st"; + yang-version 1; + + import serializer-test-included { prefix sti; revision-date 2016-06-06; } + + revision 2016-06-06 { + description + "Initial revision."; + } + + container contA { + leaf-list leaf-list-A { + type string; + } + + leaf leaf-A { + type string; + } + + list list-A { + key list-key; + + leaf list-key { + type uint8; + } + + leaf-list leaf-list-AA { + type string; + } + } + } + + leaf-list leaf-list-0 { + type boolean; + } + + leaf leaf-0 { + type string; + } + + list list-no-key { + leaf name { + type string; + } + + leaf number { + type uint8; + } + } + + list list-one-key { + key name; + + leaf name { + type string; + } + + leaf number { + type uint8; + } + } + + list list-multiple-keys { + key "name number enabled"; + + leaf name { + type string; + } + + leaf number { + type uint8; + } + + leaf enabled { + type boolean; + } + } + + augment "/sti:augmented-list" { + leaf augmented-leaf { + type string; + } + } +} \ No newline at end of file -- 2.36.6