YANGTOOLS-766: introduce JSONCodecFactorySupplier
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / JsonStreamToNormalizedNodeTest.java
index 5582d508c49d8a845e159734656065ff9897b467..822bda7f6f505f3c1408193b8d619b1e5cb09f04 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ * 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,
@@ -11,22 +11,23 @@ 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;
 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.augmentationBuilder;
 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.choiceBuilder;
 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.containerBuilder;
 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
-import com.google.common.collect.Sets;
+
 import com.google.gson.stream.JsonReader;
 import java.io.IOException;
 import java.io.StringReader;
 import java.net.URISyntaxException;
+import java.util.Collections;
 import org.junit.BeforeClass;
-import org.junit.Ignore;
 import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.Empty;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
+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.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
@@ -34,10 +35,10 @@ import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeS
 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;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 /**
- *
- * Each test tests whether json input is correctly transformed to normalized node structure
+ * Each test tests whether json input is correctly transformed to normalized node structure.
  */
 public class JsonStreamToNormalizedNodeTest {
 
@@ -46,28 +47,8 @@ public class JsonStreamToNormalizedNodeTest {
     private static SchemaContext schemaContext;
 
     @BeforeClass
-    public static void initialization() throws IOException, URISyntaxException {
-        schemaContext = loadModules("/complexjson/yang");
-    }
-
-    /**
-     * case when anyxml contains simple value will be implemented when anyxml normalized node reprezentation will be
-     * specified
-     */
-    @Ignore
-    @Test
-    public void anyXmlNodeWithSimpleValueInContainer() throws IOException, URISyntaxException {
-
-    }
-
-    /**
-     * case when anyxml contains complex xml will be implemented when anyxml normalized node reprezentation will be
-     * specified
-     */
-    @Ignore
-    @Test
-    public void anyXmlNodeWithCompositeValueInContainer() throws IOException, URISyntaxException {
-
+    public static void initialization() {
+        schemaContext = YangParserTestUtils.parseYangResourceDirectory("/complexjson/yang");
     }
 
     @Test
@@ -104,11 +85,11 @@ public class JsonStreamToNormalizedNodeTest {
     }
 
     /**
-     * Test of translating internal augmentations to normalized nodes structure
+     * Test of translating internal augmentations to normalized nodes structure.
      *
+     * <p>
      * 2 nodes are added via internal augmentation A, 1 node via internal augmentation B and one node is originally
      * member of case.
-     *
      */
     @Test
     public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
@@ -118,20 +99,18 @@ public class JsonStreamToNormalizedNodeTest {
     }
 
     /**
-     * also test using of namesakes (equal local names with different
-     *
-     * @throws IOException
-     * @throws URISyntaxException
+     * also test using of namesakes (equal local names with different.
      */
     @Test
     public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
-        final 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());
     }
 
     /**
-     * augmentation of choice - adding new case
+     * augmentation of choice - adding new case.
      */
     @Test
     public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
@@ -143,14 +122,15 @@ public class JsonStreamToNormalizedNodeTest {
     @Test
     public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
         final String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
-        verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
+        verifyTransformationToNormalizedNode(inputJson,
+            TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
     }
 
     /**
      * Top level JSON element contains no information about module name.
      *
+     * <p>
      * It should be possible to find out potential module name from available schema context.
-     *
      */
     @Test
     public void missingModuleInfoInTopLevelElement() throws IOException, URISyntaxException {
@@ -159,9 +139,9 @@ public class JsonStreamToNormalizedNodeTest {
     }
 
     /**
-     *
      * Exception expected.
      *
+     * <p>
      * It tests case when several elements with the same name and various namespaces exists and are in JSON specified
      * without module name prefix.
      */
@@ -184,17 +164,17 @@ public class JsonStreamToNormalizedNodeTest {
     public void emptyTypeTest() throws IOException, URISyntaxException {
         final String inputJson = loadTextFile("/complexjson/type-empty.json");
         final ContainerNode awaitedStructure = containerBuilder()
-                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONT_1))
-                .addChild(leafNode(EMPTY_LEAF, null))
+                .withNodeIdentifier(new NodeIdentifier(CONT_1))
+                .addChild(leafNode(EMPTY_LEAF, Empty.getInstance()))
                 .build();
 
         verifyTransformationToNormalizedNode(inputJson, awaitedStructure);
     }
 
     /**
-     *
      * Exception expected.
      *
+     * <p>
      * Json input contains element which doesn't exist in YANG schema
      */
     @Test
@@ -204,19 +184,19 @@ public class JsonStreamToNormalizedNodeTest {
             //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"));
+            assertTrue(e.getMessage().contains("Schema node with name dummy-element was not 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);
+        final SchemaNode parentNode = schemaContext.getDataChildByName(CONT_1);
+        final JsonParserStream jsonParser = JsonParserStream.create(streamWriter,
+            JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), parentNode);
         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
         final NormalizedNode<?, ?> transformedInput = result.getResult();
         assertNotNull(transformedInput);
@@ -228,39 +208,35 @@ public class JsonStreamToNormalizedNodeTest {
 
         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);
+        final SchemaNode parentNode = schemaContext.getDataChildByName(CONT_1);
+        final JsonParserStream jsonParser = JsonParserStream.create(streamWriter,
+            JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), parentNode);
         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
         final NormalizedNode<?, ?> transformedInput = result.getResult();
         assertNotNull(transformedInput);
     }
 
-   @Test
+    @Test
     public void multipleChoiceAugmentation() throws IOException, URISyntaxException {
         final String inputJson = loadTextFile("/complexjson/multiple-choice-augmentation-in-container.json");
 
         final NormalizedNodeResult result = new NormalizedNodeResult();
         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
-        final SchemaNode parentNode = schemaContext.getDataChildByName("cont1");
+        final SchemaNode parentNode = schemaContext.getDataChildByName(CONT_1);
 
-        QName augmentChoice1QName = QName.create(parentNode.getQName(), "augment-choice1");
-        QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
+        final QName augmentChoice1QName = QName.create(parentNode.getQName(), "augment-choice1");
+        final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
         final QName containerQName = QName.create(augmentChoice1QName, "case11-choice-case-container");
         final QName leafQName = QName.create(augmentChoice1QName, "case11-choice-case-leaf");
 
-        final YangInstanceIdentifier.AugmentationIdentifier aug1Id =
-                new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
-        final YangInstanceIdentifier.AugmentationIdentifier aug2Id =
-                new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
-        final YangInstanceIdentifier.NodeIdentifier augmentChoice1Id =
-                new YangInstanceIdentifier.NodeIdentifier(augmentChoice1QName);
-        final YangInstanceIdentifier.NodeIdentifier augmentChoice2Id =
-                new YangInstanceIdentifier.NodeIdentifier(augmentChoice2QName);
-        final YangInstanceIdentifier.NodeIdentifier containerId =
-                new YangInstanceIdentifier.NodeIdentifier(containerQName);
+        final AugmentationIdentifier aug1Id = new AugmentationIdentifier(Collections.singleton(augmentChoice1QName));
+        final AugmentationIdentifier aug2Id = new AugmentationIdentifier(Collections.singleton(augmentChoice2QName));
+        final NodeIdentifier augmentChoice1Id = new NodeIdentifier(augmentChoice1QName);
+        final NodeIdentifier augmentChoice2Id = new NodeIdentifier(augmentChoice2QName);
+        final NodeIdentifier containerId = new NodeIdentifier(containerQName);
 
         final NormalizedNode<?, ?> cont1Normalized =
-                containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(parentNode.getQName()))
+                containerBuilder().withNodeIdentifier(new NodeIdentifier(parentNode.getQName()))
                         .withChild(augmentationBuilder().withNodeIdentifier(aug1Id)
                                 .withChild(choiceBuilder().withNodeIdentifier(augmentChoice1Id)
                                         .withChild(augmentationBuilder().withNodeIdentifier(aug2Id)
@@ -273,7 +249,8 @@ public class JsonStreamToNormalizedNodeTest {
                                         .build())
                                 .build()).build();
 
-        final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
+        final JsonParserStream jsonParser = JsonParserStream.create(streamWriter,
+            JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext));
         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
         final NormalizedNode<?, ?> transformedInput = result.getResult();
         assertNotNull(transformedInput);
@@ -284,11 +261,11 @@ public class JsonStreamToNormalizedNodeTest {
             final NormalizedNode<?, ?> awaitedStructure) {
         final NormalizedNodeResult result = new NormalizedNodeResult();
         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
-        final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
+        final JsonParserStream jsonParser = JsonParserStream.create(streamWriter,
+            JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext));
         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
         final NormalizedNode<?, ?> transformedInput = result.getResult();
         assertEquals("Transformation of json input to normalized node wasn't successful.", awaitedStructure,
                 transformedInput);
     }
-
 }