Bug 3325 - RPC returns 500.
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / JsonStreamToNormalizedNodeTest.java
index 56d12104bd8b86e27888762767bca7f4b5b88d81..058c2747ad1aaadacd6af8f1baa3e62a946aafcf 100644 (file)
@@ -8,6 +8,9 @@
 package org.opendaylight.yangtools.yang.data.codec.gson;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
 
@@ -18,11 +21,17 @@ import java.net.URISyntaxException;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 
 /**
  *
@@ -30,6 +39,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext;
  */
 public class JsonStreamToNormalizedNodeTest {
 
+    private static final QName CONT_1 = QName.create("ns:complex:json", "2014-08-11", "cont1");
+    private static final QName EMPTY_LEAF = QName.create(CONT_1,"empty");
     private static SchemaContext schemaContext;
 
     @BeforeClass
@@ -59,34 +70,34 @@ public class JsonStreamToNormalizedNodeTest {
 
     @Test
     public void leafNodeInContainer() throws IOException, URISyntaxException {
-        String inputJson = loadTextFile("/complexjson/leaf-node-in-container.json");
+        final String inputJson = loadTextFile("/complexjson/leaf-node-in-container.json");
         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.leafNodeInContainer());
     }
 
     @Test
     public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
-        String inputJson = loadTextFile("/complexjson/leaf-node-via-augmentation-in-container.json");
+        final String inputJson = loadTextFile("/complexjson/leaf-node-via-augmentation-in-container.json");
         verifyTransformationToNormalizedNode(inputJson,
                 TestingNormalizedNodeStructuresCreator.leafNodeViaAugmentationInContainer());
     }
 
     @Test
     public void leafListNodeInContainer() throws IOException, URISyntaxException {
-        String inputJson = loadTextFile("/complexjson/leaflist-node-in-container.json");
+        final String inputJson = loadTextFile("/complexjson/leaflist-node-in-container.json");
         verifyTransformationToNormalizedNode(inputJson,
                 TestingNormalizedNodeStructuresCreator.leafListNodeInContainer());
     }
 
     @Test
     public void keyedListNodeInContainer() throws IOException, URISyntaxException {
-        String inputJson = loadTextFile("/complexjson/keyed-list-node-in-container.json");
+        final String inputJson = loadTextFile("/complexjson/keyed-list-node-in-container.json");
         verifyTransformationToNormalizedNode(inputJson,
                 TestingNormalizedNodeStructuresCreator.keyedListNodeInContainer());
     }
 
     @Test
     public void choiceNodeInContainer() throws IOException, URISyntaxException {
-        String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
+        final String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.choiceNodeInContainer());
     }
 
@@ -99,7 +110,7 @@ public class JsonStreamToNormalizedNodeTest {
      */
     @Test
     public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
-        String inputJson = loadTextFile("/complexjson/case-node-augmentation-in-choice-in-container.json");
+        final String inputJson = loadTextFile("/complexjson/case-node-augmentation-in-choice-in-container.json");
         verifyTransformationToNormalizedNode(inputJson,
                 TestingNormalizedNodeStructuresCreator.caseNodeAugmentationInChoiceInContainer());
     }
@@ -112,7 +123,7 @@ public class JsonStreamToNormalizedNodeTest {
      */
     @Test
     public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
-        String inputJson = loadTextFile("/complexjson/case-node-external-augmentation-in-choice-in-container.json");
+        final String inputJson = loadTextFile("/complexjson/case-node-external-augmentation-in-choice-in-container.json");
         verifyTransformationToNormalizedNode(inputJson,
                 TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer());
     }
@@ -122,24 +133,113 @@ public class JsonStreamToNormalizedNodeTest {
      */
     @Test
     public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
-        String inputJson = loadTextFile("/complexjson/choice-node-augmentation-in-container.json");
+        final String inputJson = loadTextFile("/complexjson/choice-node-augmentation-in-container.json");
         verifyTransformationToNormalizedNode(inputJson,
                 TestingNormalizedNodeStructuresCreator.choiceNodeAugmentationInContainer());
     }
 
     @Test
     public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
-        String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
+        final String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
     }
 
+    /**
+     * Top level JSON element contains no information about module name.
+     *
+     * It should be possible to find out potential module name from available schema context.
+     *
+     */
+    @Test
+    public void missingModuleInfoInTopLevelElement() throws IOException, URISyntaxException {
+        final String inputJson = loadTextFile("/complexjson/missing-module-in-top-level.json");
+        verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.topLevelContainer());
+    }
+
+    /**
+     *
+     * Exception expected.
+     *
+     * It tests case when several elements with the same name and various namespaces exists and are in JSON specified
+     * without module name prefix.
+     */
+    @Test
+    public void leafNamesakes() throws IOException, URISyntaxException {
+        final String inputJson = loadTextFile("/complexjson/namesakes.json");
+        try {
+            //second parameter isn't necessary because error will be raised before it is used.
+            verifyTransformationToNormalizedNode(inputJson, null);
+            fail("Expected exception not raised");
+        } catch (final IllegalStateException e) {
+            final String errorMessage = e.getMessage();
+            assertTrue(errorMessage.contains("Choose suitable module name for element lf11-namesake:"));
+            assertTrue(errorMessage.contains("complexjson-augmentation"));
+            assertTrue(errorMessage.contains("complexjson-augmentation-namesake"));
+        }
+    }
+
+    @Test
+    public void emptyTypeTest() throws IOException, URISyntaxException {
+        final String inputJson = loadTextFile("/complexjson/type-empty.json");
+        final ContainerNode awaitedStructure = Builders.containerBuilder()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONT_1))
+                .addChild(ImmutableNodes.leafNode(EMPTY_LEAF, null))
+                .build();
+
+        verifyTransformationToNormalizedNode(inputJson, awaitedStructure);
+    }
+
+    /**
+     *
+     * Exception expected.
+     *
+     * Json input contains element which doesn't exist in YANG schema
+     */
+    @Test
+    public void parsingNotExistingElement() throws IOException, URISyntaxException {
+        final String inputJson = loadTextFile("/complexjson/not-existing-element.json");
+        try {
+            //second parameter isn't necessary because error will be raised before it is used.
+            verifyTransformationToNormalizedNode(inputJson, null);
+        } catch (final IllegalStateException e) {
+            assertTrue(e.getMessage().contains("Schema node with name dummy-element wasn't found."));
+        }
+    }
+
+
+    @Test
+    public void listItemWithoutArray() throws IOException, URISyntaxException {
+        final String inputJson = loadTextFile("/complexjson/keyed-list-restconf-behaviour.json");
+
+        final NormalizedNodeResult result = new NormalizedNodeResult();
+        final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
+        final SchemaNode parentNode = schemaContext.getDataChildByName("cont1");
+        final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext, parentNode);
+        jsonParser.parse(new JsonReader(new StringReader(inputJson)));
+        final NormalizedNode<?, ?> transformedInput = result.getResult();
+        assertNotNull(transformedInput);
+    }
+
+    @Test
+    public void listItemWithArray() throws IOException, URISyntaxException {
+        final String inputJson = loadTextFile("/complexjson/keyed-list-yang-json-behaviour.json");
+
+        final NormalizedNodeResult result = new NormalizedNodeResult();
+        final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
+        final SchemaNode parentNode = schemaContext.getDataChildByName("cont1");
+        final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext, parentNode);
+        jsonParser.parse(new JsonReader(new StringReader(inputJson)));
+        final NormalizedNode<?, ?> transformedInput = result.getResult();
+        assertNotNull(transformedInput);
+    }
+
     private void verifyTransformationToNormalizedNode(final String inputJson,
             final NormalizedNode<?, ?> awaitedStructure) {
-        NormalizedNodeResult result = new NormalizedNodeResult();
+        final NormalizedNodeResult result = new NormalizedNodeResult();
         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
-        JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
+        final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
-        NormalizedNode<?, ?> transformedInput = result.getResult();
+        final NormalizedNode<?, ?> transformedInput = result.getResult();
         assertEquals("Transformation of json input to normalized node wasn't successful.", awaitedStructure,
                 transformedInput);
     }