X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=yang%2Fyang-data-codec-gson%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fcodec%2Fgson%2FJsonStreamToNormalizedNodeTest.java;h=5ab681dc290ed29a6282ebf4edf2bb9ce7f17d00;hb=21b3fb1ea12d1e28115f6c216bbbfd9c80b2216f;hp=1a71142be5553ecee451b42edb5fce7d02bf2612;hpb=8fe36ef87f721d0e8052ddec466b36bb54dd9f64;p=yangtools.git diff --git a/yang/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/JsonStreamToNormalizedNodeTest.java b/yang/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/JsonStreamToNormalizedNodeTest.java index 1a71142be5..5ab681dc29 100644 --- a/yang/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/JsonStreamToNormalizedNodeTest.java +++ b/yang/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/JsonStreamToNormalizedNodeTest.java @@ -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, @@ -8,22 +8,33 @@ 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.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules; +import static org.junit.Assert.fail; 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 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.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; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; +import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; /** * @@ -31,63 +42,45 @@ 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 - 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() throws IOException, URISyntaxException, ReactorException { + schemaContext = YangParserTestUtils.parseYangSources("/complexjson/yang"); } @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()); } @@ -100,7 +93,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()); } @@ -113,7 +106,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()); } @@ -123,14 +116,14 @@ 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()); } @@ -142,7 +135,7 @@ public class JsonStreamToNormalizedNodeTest { */ @Test public void missingModuleInfoInTopLevelElement() throws IOException, URISyntaxException { - String inputJson = loadTextFile("/complexjson/missing-module-in-top-level.json"); + final String inputJson = loadTextFile("/complexjson/missing-module-in-top-level.json"); verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.topLevelContainer()); } @@ -155,11 +148,12 @@ public class JsonStreamToNormalizedNodeTest { */ @Test public void leafNamesakes() throws IOException, URISyntaxException { - String inputJson = loadTextFile("/complexjson/namesakes.json"); + 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); - } catch (IllegalStateException e) { + 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")); @@ -167,6 +161,17 @@ public class JsonStreamToNormalizedNodeTest { } } + @Test + 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)) + .build(); + + verifyTransformationToNormalizedNode(inputJson, awaitedStructure); + } + /** * * Exception expected. @@ -175,23 +180,94 @@ public class JsonStreamToNormalizedNodeTest { */ @Test public void parsingNotExistingElement() throws IOException, URISyntaxException { - String inputJson = loadTextFile("/complexjson/not-existing-element.json"); + 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 (IllegalStateException e) { - assertTrue(e.getMessage().contains("Schema node with name dummy-element wasn't found.")); + } catch (final IllegalStateException e) { + assertTrue(e.getMessage().contains("Schema node with name dummy-element wasn't found")); } } - private void verifyTransformationToNormalizedNode(final String inputJson, + @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(CONT_1); + 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(CONT_1); + 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 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(CONT_1); + + 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 NormalizedNode cont1Normalized = + containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(parentNode.getQName())) + .withChild(augmentationBuilder().withNodeIdentifier(aug1Id) + .withChild(choiceBuilder().withNodeIdentifier(augmentChoice1Id) + .withChild(augmentationBuilder().withNodeIdentifier(aug2Id) + .withChild(choiceBuilder().withNodeIdentifier(augmentChoice2Id) + .withChild(containerBuilder().withNodeIdentifier(containerId) + .withChild(leafNode(leafQName, "leaf-value")) + .build()) + .build()) + .build()) + .build()) + .build()).build(); + + final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext); + jsonParser.parse(new JsonReader(new StringReader(inputJson))); + final NormalizedNode transformedInput = result.getResult(); + assertNotNull(transformedInput); + assertEquals(cont1Normalized, transformedInput); + } + + private static 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); }