Bug 2358 - Remove tests cnsn to xml and add tests nn to xml 09/22609/4
authorJakub Toth <jatoth@cisco.com>
Mon, 1 Jun 2015 08:33:58 +0000 (10:33 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 8 Jul 2015 14:20:15 +0000 (14:20 +0000)
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 CnSnToXmlWithChoiceTest test
* add NnToXmlWithChoiceTest to nn/to/xml/test/
  * positive test for choice statement

Change-Id: Ie3fba1c51db57ef7621703812f7a98b6d417feca
Signed-off-by: Jakub Toth <jatoth@cisco.com>
(cherry picked from commit 015b6436357637c6cbd29b72e931735ff2a34007)

opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlWithChoiceTest.java [deleted file]
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithChoiceTest.java [new file with mode: 0644]

diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlWithChoiceTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlWithChoiceTest.java
deleted file mode 100644 (file)
index b5fd49f..0000000
+++ /dev/null
@@ -1,25 +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 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 CnSnToXmlWithChoiceTest extends YangAndXmlAndDataSchemaLoader {
-    @BeforeClass
-    public static void initialization() {
-        dataLoad("/cnsn-to-xml/choice", 1, "module-with-choice", "cont");
-    }
-
-}
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithChoiceTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithChoiceTest.java
new file mode 100644 (file)
index 0000000..943a05c
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * 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.collect.Iterables;
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+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.schema.ChoiceNode;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
+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.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public class NnToXmlWithChoiceTest extends AbstractBodyReaderTest {
+
+    private final NormalizedNodeXmlBodyWriter xmlBodyWriter;
+    private static SchemaContext schemaContext;
+
+    public NnToXmlWithChoiceTest() throws NoSuchFieldException,
+            SecurityException {
+        super();
+        xmlBodyWriter = new NormalizedNodeXmlBodyWriter();
+    }
+
+    @BeforeClass
+    public static void initialization() {
+        schemaContext = schemaContextLoader("/nn-to-xml/choice", schemaContext);
+        controllerContext.setSchemas(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 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 DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> dataContainerNodeAttrBuilder = Builders
+                .containerBuilder((ContainerSchemaNode) contSchemaNode);
+
+        final DataSchemaNode choiceSchemaNode = ((ContainerSchemaNode) contSchemaNode)
+                .getDataChildByName(choA);
+        assertTrue(choiceSchemaNode instanceof ChoiceSchemaNode);
+
+        final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> dataChoice = Builders
+                .choiceBuilder((ChoiceSchemaNode) choiceSchemaNode);
+
+        final List<DataSchemaNode> instanceLf = ControllerContext
+                .findInstanceDataChildrenByName(
+                        (DataNodeContainer) contSchemaNode, lf.getLocalName());
+        final DataSchemaNode schemaLf = Iterables.getFirst(instanceLf, null);
+
+        dataChoice.withChild(Builders.leafBuilder((LeafSchemaNode) schemaLf)
+                .withValue(value).build());
+
+        dataContainerNodeAttrBuilder.withChild(dataChoice.build());
+
+        final NormalizedNodeContext testNormalizedNodeContext = new NormalizedNodeContext(
+                new InstanceIdentifierContext<DataSchemaNode>(null,
+                        contSchemaNode, null, schemaContext),
+                dataContainerNodeAttrBuilder.build());
+
+        return testNormalizedNodeContext;
+    }
+
+    @Override
+    protected MediaType getMediaType() {
+        return null;
+    }
+}