--- /dev/null
+/*
+ * 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.yangtools.yang.data.codec.gson;
+
+import static org.junit.Assert.assertEquals;
+import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
+import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
+
+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.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;
+
+/**
+ *
+ * Each test tests whether json input is correctly transformed to normalized node structure
+ */
+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 {
+
+ }
+
+ @Test
+ public void leafNodeInContainer() throws IOException, URISyntaxException {
+ 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");
+ verifyTransformationToNormalizedNode(inputJson,
+ TestingNormalizedNodeStructuresCreator.leafNodeViaAugmentationInContainer());
+ }
+
+ @Test
+ public void leafListNodeInContainer() throws IOException, URISyntaxException {
+ 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");
+ verifyTransformationToNormalizedNode(inputJson,
+ TestingNormalizedNodeStructuresCreator.keyedListNodeInContainer());
+ }
+
+ @Test
+ public void choiceNodeInContainer() throws IOException, URISyntaxException {
+ String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
+ verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.choiceNodeInContainer());
+ }
+
+ /**
+ * Test of translating internal augmentations to normalized nodes structure
+ *
+ * 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 {
+ String inputJson = loadTextFile("/complexjson/case-node-augmentation-in-choice-in-container.json");
+ verifyTransformationToNormalizedNode(inputJson,
+ TestingNormalizedNodeStructuresCreator.caseNodeAugmentationInChoiceInContainer());
+ }
+
+ /**
+ * also test using of namesakes (equal local names with different
+ *
+ * @throws IOException
+ * @throws URISyntaxException
+ */
+ @Test
+ public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
+ String inputJson = loadTextFile("/complexjson/case-node-external-augmentation-in-choice-in-container.json");
+ verifyTransformationToNormalizedNode(inputJson,
+ TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer());
+ }
+
+ /**
+ * augmentation of choice - adding new case
+ */
+ @Test
+ public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
+ 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");
+ verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
+ }
+
+ private void verifyTransformationToNormalizedNode(final String inputJson,
+ final NormalizedNode<?, ?> awaitedStructure) {
+ NormalizedNodeResult result = new NormalizedNodeResult();
+ final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
+ JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
+ jsonParser.parse(new JsonReader(new StringReader(inputJson)));
+ NormalizedNode<?, ?> transformedInput = result.getResult();
+ assertEquals("Transformation of json input to normalized node wasn't successful.", awaitedStructure,
+ transformedInput);
+ }
+
+}
--- /dev/null
+/*
+ * 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.yangtools.yang.data.codec.gson;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.childArray;
+import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.childPrimitive;
+import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
+import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.resolveCont1;
+
+import com.google.common.collect.Sets;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonPrimitive;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.net.URISyntaxException;
+import java.util.HashSet;
+import java.util.Iterator;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+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.api.schema.stream.NormalizedNodeWriter;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+/**
+ * Each test tests whether json output obtained after transformation contains is corect. The transformation takes
+ * normalized node data structure and transform it to json output. To make it easier validate json output it is loaded
+ * via gson as structure of json elements which are walked and compared with awaited values.
+ *
+ */
+public class NormalizedNodeToJsonStreamTest {
+
+ private static SchemaContext schemaContext;
+
+ public interface JsonValidator {
+ void validate(final String jsonOutput);
+ }
+
+ @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 {
+
+ }
+
+ @Test
+ public void leafNodeInContainer() throws IOException, URISyntaxException {
+ Writer writer = new StringWriter();
+ NormalizedNode<?, ?> leafNodeInContainer = TestingNormalizedNodeStructuresCreator.leafNodeInContainer();
+ String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeInContainer);
+ new JsonValidator() {
+
+ @Override
+ public void validate(String jsonOutput) {
+ JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+
+ JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11");
+ assertNotNull(lf11);
+ int asInt = lf11.getAsInt();
+ assertEquals(453, asInt);
+ }
+ }.validate(jsonOutput);
+
+ }
+
+ @Test
+ public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
+ Writer writer = new StringWriter();
+ NormalizedNode<?, ?> leafNodeViaAugmentationInContainer = TestingNormalizedNodeStructuresCreator
+ .leafNodeViaAugmentationInContainer();
+ String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeViaAugmentationInContainer);
+ new JsonValidator() {
+
+ @Override
+ public void validate(String jsonOutput) {
+ JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+
+ JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
+ assertNotNull(lf12_1);
+ String asString = lf12_1.getAsString();
+ assertEquals("lf12 value", asString);
+ }
+ }.validate(jsonOutput);
+
+ }
+
+ @Test
+ public void leafListNodeInContainer() throws IOException, URISyntaxException {
+ Writer writer = new StringWriter();
+ NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator.leafListNodeInContainer();
+ String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
+ new JsonValidator() {
+
+ @Override
+ public void validate(String jsonOutput) {
+ JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+ JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
+ assertNotNull(lflst11);
+
+ HashSet<Object> lflst11Values = Sets.newHashSet();
+ for (JsonElement jsonElement : lflst11) {
+ assertTrue(jsonElement instanceof JsonPrimitive);
+ lflst11Values.add(((JsonPrimitive) jsonElement).getAsString());
+ }
+
+ assertEquals(Sets.newHashSet("lflst11 value2", "lflst11 value1"), lflst11Values);
+ }
+ }.validate(jsonOutput);
+ }
+
+ @Test
+ public void keyedListNodeInContainer() throws IOException, URISyntaxException {
+ Writer writer = new StringWriter();
+ NormalizedNode<?, ?> keyedListNodeInContainer = TestingNormalizedNodeStructuresCreator
+ .keyedListNodeInContainer();
+ String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, keyedListNodeInContainer);
+ new JsonValidator() {
+
+ @Override
+ public void validate(String jsonOutput) {
+ JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+ JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
+ assertNotNull(lst11);
+
+ Iterator<JsonElement> iterator = lst11.iterator();
+ assertTrue(iterator.hasNext());
+ JsonElement lst11Entry1Raw = iterator.next();
+ assertFalse(iterator.hasNext());
+ assertTrue(lst11Entry1Raw instanceof JsonObject);
+ JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
+
+ JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
+ assertNotNull(key111);
+ JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
+ assertNotNull(lf112);
+ JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
+ assertNotNull(lf113);
+ JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
+ assertNotNull(lf111);
+
+ assertEquals("key111 value", key111.getAsString());
+ assertEquals("/complexjson:cont1/complexjson:lflst11", lf112.getAsString());
+ assertEquals("lf113 value", lf113.getAsString());
+ assertEquals("lf111 value", lf111.getAsString());
+ }
+ }.validate(jsonOutput);
+ }
+
+ @Test
+ public void choiceNodeInContainer() throws IOException, URISyntaxException {
+ Writer writer = new StringWriter();
+ NormalizedNode<?, ?> choiceNodeInContainer = TestingNormalizedNodeStructuresCreator.choiceNodeInContainer();
+ String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeInContainer);
+ new JsonValidator() {
+
+ @Override
+ public void validate(String jsonOutput) {
+ JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+ JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
+ assertNotNull(lf13);
+
+ assertEquals("lf13 value", lf13.getAsString());
+ }
+ }.validate(jsonOutput);
+ }
+
+ /**
+ * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
+ * added)
+ *
+ * after running this test following exception is raised
+ *
+ * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
+ * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
+ *
+ */
+// @Ignore
+ @Test
+ public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
+ Writer writer = new StringWriter();
+ NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
+ .caseNodeAugmentationInChoiceInContainer();
+ String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, caseNodeAugmentationInChoiceInContainer);
+ new JsonValidator() {
+
+ @Override
+ public void validate(String jsonOutput) {
+ JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+
+ JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
+ assertNotNull(lf15_21);
+ JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
+ assertNotNull(lf13);
+ JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
+ assertNotNull(lf15_11);
+ JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
+ assertNotNull(lf15_12);
+
+ assertEquals("lf15_21 value", lf15_21.getAsString());
+ assertEquals("lf13 value", lf13.getAsString());
+ assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
+ assertEquals("complexjson:lf11", lf15_12.getAsString());
+
+ }
+ }.validate(jsonOutput);
+ }
+
+ /**
+ * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
+ * with the same names externally (augment B) are added)
+ *
+ * after running this test following exception is raised
+ *
+ * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
+ * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
+ *
+ */
+// @Ignore
+ @Test
+ public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
+ Writer writer = new StringWriter();
+ NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
+ .caseNodeExternalAugmentationInChoiceInContainer();
+ String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
+ caseNodeExternalAugmentationInChoiceInContainer);
+ new JsonValidator() {
+
+ @Override
+ public void validate(String jsonOutput) {
+ JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+
+ JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
+ assertNotNull(lf15_11Augment);
+ JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
+ assertNotNull(lf15_12Augment);
+ JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
+ assertNotNull(lf13);
+ JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
+ assertNotNull(lf15_11);
+ JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
+ assertNotNull(lf15_12);
+
+ assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
+ assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
+ assertEquals("lf13 value", lf13.getAsString());
+ assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
+ assertEquals("complexjson:lf11", lf15_12.getAsString());
+
+ }
+ }.validate(jsonOutput);
+ }
+
+ /**
+ * augmentation of choice - adding new case
+ *
+ * after running this test following exception is raised
+ *
+ * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
+ * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
+ *
+ */
+// @Ignore
+ @Test
+ public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
+ Writer writer = new StringWriter();
+ NormalizedNode<?, ?> choiceNodeAugmentationInContainer = TestingNormalizedNodeStructuresCreator
+ .choiceNodeAugmentationInContainer();
+ String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
+ choiceNodeAugmentationInContainer);
+ new JsonValidator() {
+
+ @Override
+ public void validate(String jsonOutput) {
+ JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+
+ JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17","lf17");
+ assertNotNull(lf17);
+ assertEquals("lf17 value",lf17.getAsString());
+ }
+ }.validate(jsonOutput);
+ }
+
+ @Test
+ public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
+ Writer writer = new StringWriter();
+ NormalizedNode<?, ?> unkeyedNodeInContainer = TestingNormalizedNodeStructuresCreator
+ .unkeyedNodeInContainer();
+ String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
+ unkeyedNodeInContainer);
+ new JsonValidator() {
+
+ @Override
+ public void validate(String jsonOutput) {
+ JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+
+ JsonArray lst12 = childArray(cont1, "complexjson:lst12","lst12");
+ assertNotNull(lst12);
+
+ Iterator<JsonElement> iterator = lst12.iterator();
+ assertTrue(iterator.hasNext());
+ JsonElement lst12Entry1Raw = iterator.next();
+ assertFalse(iterator.hasNext());
+
+ assertTrue(lst12Entry1Raw instanceof JsonObject);
+ JsonObject lst12Entry1 = (JsonObject)lst12Entry1Raw;
+ JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
+ assertNotNull(lf121);
+
+ assertEquals("lf121 value",lf121.getAsString());
+
+ }
+ }.validate(jsonOutput);
+
+ }
+
+ private String normalizedNodeToJsonStreamTransformation(final Writer writer,
+ final NormalizedNode<?, ?> inputStructure) throws IOException {
+ writer.write("{\n");
+ final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.create(schemaContext, writer, 2);
+ final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
+ nodeWriter.write(inputStructure);
+ writer.write("\n}");
+ nodeWriter.close();
+ return writer.toString();
+ }
+
+}
*/
package org.opendaylight.yangtools.yang.data.codec.gson;
-import com.google.gson.stream.JsonReader;
+import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
+import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
-import java.io.BufferedReader;
+import com.google.gson.stream.JsonReader;
import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
-import java.net.URI;
import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.List;
-
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
-import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.stream.LoggingNormalizedNodeStreamWriter;
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.parser.api.YangContextParser;
-import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
LOG.debug("Serialized JSON: {}", writer.toString());
}
- private static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException {
- YangContextParser parser = new YangParserImpl();
- URI path = StreamToNormalizedNodeTest.class.getResource(resourceDirectory).toURI();
- final File testDir = new File(path);
- final String[] fileList = testDir.list();
- final List<File> testFiles = new ArrayList<File>();
- if (fileList == null) {
- throw new FileNotFoundException(resourceDirectory);
- }
- for (String fileName : fileList) {
- if (new File(testDir, fileName).isDirectory() == false) {
- testFiles.add(new File(testDir, fileName));
- }
- }
- return parser.parseFiles(testFiles);
- }
-
- private static String loadTextFile(final File file) throws IOException {
- FileReader fileReader = new FileReader(file);
- BufferedReader bufReader = new BufferedReader(fileReader);
-
- String line = null;
- StringBuilder result = new StringBuilder();
- while ((line = bufReader.readLine()) != null) {
- result.append(line);
- }
- bufReader.close();
- return result.toString();
- }
}
--- /dev/null
+/*
+ * 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.yangtools.yang.data.codec.gson;
+
+import static org.junit.Assert.assertTrue;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonPrimitive;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+
+public class TestUtils {
+
+ private TestUtils() {
+ }
+
+ static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException {
+ YangContextParser parser = new YangParserImpl();
+ URI path = StreamToNormalizedNodeTest.class.getResource(resourceDirectory).toURI();
+ final File testDir = new File(path);
+ final String[] fileList = testDir.list();
+ final List<File> testFiles = new ArrayList<File>();
+ if (fileList == null) {
+ throw new FileNotFoundException(resourceDirectory);
+ }
+ for (String fileName : fileList) {
+ if (new File(testDir, fileName).isDirectory() == false) {
+ testFiles.add(new File(testDir, fileName));
+ }
+ }
+ return parser.parseFiles(testFiles);
+ }
+
+ static String loadTextFile(final File file) throws IOException {
+ FileReader fileReader = new FileReader(file);
+ BufferedReader bufReader = new BufferedReader(fileReader);
+
+ String line = null;
+ StringBuilder result = new StringBuilder();
+ while ((line = bufReader.readLine()) != null) {
+ result.append(line);
+ }
+ bufReader.close();
+ return result.toString();
+ }
+
+ static String loadTextFile(final String relativePath) throws IOException, URISyntaxException {
+ return loadTextFile(new File(TestUtils.class.getResource(relativePath).toURI()));
+ }
+
+ static JsonObject childObject(final JsonObject jsonObject,final String ... names) {
+ for (String name : names) {
+ JsonObject childJsonObject = jsonObject.getAsJsonObject(name);
+ if (childJsonObject != null) {
+ return childJsonObject;
+ }
+ }
+ return null;
+ }
+
+ static JsonPrimitive childPrimitive(final JsonObject jsonObject,final String ... names) {
+ for (String name : names) {
+ JsonPrimitive childJsonPrimitive = jsonObject.getAsJsonPrimitive(name);
+ if (childJsonPrimitive != null) {
+ return childJsonPrimitive;
+ }
+ }
+ return null;
+ }
+
+ static JsonArray childArray(final JsonObject jsonObject,final String ... names) {
+ for (String name : names) {
+ JsonArray childJsonArray = jsonObject.getAsJsonArray(name);
+ if (childJsonArray != null) {
+ return childJsonArray;
+ }
+ }
+ return null;
+ }
+
+ static JsonObject resolveCont1(String jsonOutput) {
+ JsonParser parser = new JsonParser();
+ JsonElement rootElement = parser.parse(jsonOutput);
+ assertTrue(rootElement.isJsonObject());
+ JsonObject rootObject = rootElement.getAsJsonObject();
+ JsonObject cont1 = childObject(rootObject, "complexjson:cont1", "cont1");
+ return cont1;
+ }
+
+
+}
--- /dev/null
+/*
+ * 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.yangtools.yang.data.codec.gson;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import java.util.HashMap;
+import java.util.Map;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.SimpleNode;
+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.InstanceIdentifierBuilder;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
+import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
+import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
+import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
+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.data.impl.schema.builder.api.ListNodeBuilder;
+
+public class TestingNormalizedNodeStructuresCreator {
+
+ static NormalizedNode<?, ?> cont1Node(
+ DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>... children) {
+ DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> cont1 = Builders.containerBuilder();
+ cont1.withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "cont1")));
+
+ cont1.withValue(Lists.newArrayList(children));
+ return cont1.build();
+ }
+
+ private static DataContainerChild<? extends PathArgument, ?> lst12Node() {
+ CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> lst12Builder = Builders.unkeyedListBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lst12")));
+ lst12Builder.withChild(lst12Entry1Node());
+ return lst12Builder.build();
+ }
+
+ private static UnkeyedListEntryNode lst12Entry1Node() {
+ DataContainerNodeAttrBuilder<NodeIdentifier, UnkeyedListEntryNode> lst12Entry1Builder = Builders
+ .unkeyedListEntryBuilder();
+ lst12Entry1Builder
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lst12")));
+ lst12Entry1Builder.withChild(Builders.leafBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf121")))
+ .withValue("lf121 value").build());
+ return lst12Entry1Builder.build();
+ }
+
+ private static DataContainerChild<? extends PathArgument, ?> choc12Node() {
+ DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choc12Builder = Builders.choiceBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "choc12")));
+
+// DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> choc12Augmentation = Builders
+// .augmentationBuilder();
+// choc12Augmentation.withNodeIdentifier(new AugmentationIdentifier(Sets.newHashSet(QName.create(
+// "ns:complex:json", "2014-08-11", "c12B"))));
+// choc12Augmentation.withChild(lf17Node());
+
+ choc12Builder.withChild(lf17Node());
+ return choc12Builder.build();
+ }
+
+ protected static LeafNode<Object> lf17Node() {
+ return Builders.leafBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf17")))
+ .withValue("lf17 value").build();
+ }
+
+ private static DataContainerChild<? extends PathArgument, ?> externalAugmentC11AWithLf15_11AndLf15_12Node() {
+ DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> augmentationBuilder = Builders
+ .augmentationBuilder();
+ augmentationBuilder.withNodeIdentifier(new AugmentationIdentifier(Sets.newHashSet(
+ QName.create("ns:complex:json:augmentation", "2014-08-14", "lf15_11"),
+ QName.create("ns:complex:json:augmentation", "2014-08-14", "lf15_12"))));
+ augmentationBuilder.withChild(lf15_11NodeExternal());
+ augmentationBuilder.withChild(lf15_12NodeExternal());
+ return augmentationBuilder.build();
+ }
+
+ private static LeafNode<Object> lf15_12NodeExternal() {
+ return Builders
+ .leafBuilder()
+ .withNodeIdentifier(
+ new NodeIdentifier(QName.create("ns:complex:json:augmentation", "2014-08-14", "lf15_12")))
+ .withValue("lf15_12 value from augmentation").build();
+ }
+
+ private static LeafNode<Object> lf15_11NodeExternal() {
+ return Builders
+ .leafBuilder()
+ .withNodeIdentifier(
+ new NodeIdentifier(QName.create("ns:complex:json:augmentation", "2014-08-14", "lf15_11")))
+ .withValue("lf15_11 value from augmentation").build();
+ }
+
+ private static DataContainerChild<? extends PathArgument, ?> choc11Node(
+ DataContainerChild<? extends PathArgument, ?>... children) {
+ DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choc11Builder = Builders.choiceBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "choc11")));
+ choc11Builder.withValue(Lists.newArrayList(children));
+ // choc11Builder.addChild(lf13Node());
+ // choc11Builder.addChild(augmentChoc11_c11A_lf1511AndLf1512Children());
+ // choc11Builder.addChild(augmentChoc11_c11_lf1521Children());
+ return choc11Builder.build();
+ }
+
+ private static LeafNode<Object> lf13Node() {
+ return Builders.leafBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf13")))
+ .withValue("lf13 value").build();
+ }
+
+ private static DataContainerChild<? extends PathArgument, ?> augmentC11AWithLf15_21Node() {
+ DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> choc11_c11AugmentBuilder = Builders
+ .augmentationBuilder();
+ choc11_c11AugmentBuilder.withNodeIdentifier(new AugmentationIdentifier(Sets.newHashSet(QName.create(
+ "ns:complex:json", "2014-08-11", "lf15_21"))));
+
+ choc11_c11AugmentBuilder.withChild(lf15_21Node());
+ return choc11_c11AugmentBuilder.build();
+ }
+
+ private static LeafNode<Object> lf15_21Node() {
+ return Builders.leafBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf15_21")))
+ .withValue("lf15_21 value").build();
+ }
+
+ private static DataContainerChild<? extends PathArgument, ?> augmentC11AWithLf15_11AndLf15_12Node() {
+ DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> choc11_c11AugmentBuilder = Builders
+ .augmentationBuilder();
+ choc11_c11AugmentBuilder.withNodeIdentifier(new AugmentationIdentifier(Sets.newHashSet(
+ QName.create("ns:complex:json", "2014-08-11", "lf15_11"),
+ QName.create("ns:complex:json", "2014-08-11", "lf15_12"))));
+ choc11_c11AugmentBuilder.withChild(lf15_11Node());
+ choc11_c11AugmentBuilder.withChild(lf15_12Node());
+ return choc11_c11AugmentBuilder.build();
+ }
+
+ private static LeafNode<Object> lf15_12Node() {
+ return Builders.leafBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf15_12")))
+ .withValue(QName.create("ns:complex:json", "2014-08-11", "lf11")).build();
+ }
+
+ private static LeafNode<Object> lf15_11Node() {
+ return Builders.leafBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf15_11")))
+ .withValue(Sets.newHashSet("one", "two")).build();
+ }
+
+ private static DataContainerChild<? extends PathArgument, ?> lf12_1Node() {
+ DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> augmentBuilder = Builders
+ .augmentationBuilder().withNodeIdentifier(
+ new AugmentationIdentifier(Sets.newHashSet(
+ QName.create("ns:complex:json", "2014-08-11", "lf12_1"),
+ QName.create("ns:complex:json", "2014-08-11", "lf12_2"))));
+ augmentBuilder.withChild(Builders.leafBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf12_1")))
+ .withValue("lf12 value").build());
+ return augmentBuilder.build();
+ }
+
+ private static DataContainerChild<? extends PathArgument, ?> childLst11() {
+ CollectionNodeBuilder<MapEntryNode, MapNode> lst11 = Builders.mapBuilder().withNodeIdentifier(
+ new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lst11")));
+
+ DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> lst11Entry1Builder = Builders
+ .mapEntryBuilder();
+
+ Map<QName, Object> key = new HashMap<>();
+ key.put(QName.create("ns:complex:json", "2014-08-11", "key111"), "key111 value");
+ key.put(QName.create("ns:complex:json", "2014-08-11", "lf111"), "lf111 value");
+
+ lst11Entry1Builder.withNodeIdentifier(new NodeIdentifierWithPredicates(QName.create("ns:complex:json",
+ "2014-08-11", "lst11"), key));
+ lst11Entry1Builder.withChild(Builders.leafBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "key111")))
+ .withValue("key111 value").build());
+ lst11Entry1Builder.withChild(Builders.leafBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf112")))
+ .withValue(lf112Value()).build());
+ lst11Entry1Builder.withChild(Builders.leafBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf113")))
+ .withValue("lf113 value").build());
+ lst11Entry1Builder.withChild(Builders.leafBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf111")))
+ .withValue("lf111 value").build());
+ lst11.withChild(lst11Entry1Builder.build());
+ return lst11.build();
+ }
+
+ private static Object lf112Value() {
+ InstanceIdentifierBuilder builder = YangInstanceIdentifier.builder();
+ builder.node(QName.create("ns:complex:json", "2014-08-11", "cont1"));
+ builder.node(QName.create("ns:complex:json", "2014-08-11", "lflst11"));
+ return builder.build();
+ }
+
+ private static DataContainerChild<? extends PathArgument, ?> childLflst11() {
+ ListNodeBuilder<Object, LeafSetEntryNode<Object>> lflst11 = Builders.leafSetBuilder().withNodeIdentifier(
+ new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lflst11")));
+ lflst11.withChild(Builders
+ .leafSetEntryBuilder()
+ .withNodeIdentifier(
+ new NodeWithValue(QName.create("ns:complex:json", "2014-08-11", "lflst11"), "lflst11 value1"))
+ .withValue("lflst11 value1").build());
+ lflst11.withChild(Builders
+ .leafSetEntryBuilder()
+ .withNodeIdentifier(
+ new NodeWithValue(QName.create("ns:complex:json", "2014-08-11", "lflst11"), "lflst11 value2"))
+ .withValue("lflst11 value2").build());
+ return lflst11.build();
+ }
+
+ private static CompositeNode prepareLf12Value() {
+ SimpleNode<?> anyxmlInData = NodeFactory.createImmutableSimpleNode(
+ QName.create("ns:complex:json", "2014-08-11", "anyxml-in-data"), null, "foo");
+ return ImmutableCompositeNode.builder().add(anyxmlInData)
+ .setQName(QName.create("ns:complex:json", "2014-08-11", "lf12-any")).toInstance();
+ }
+
+ private static CompositeNode prepareLf13Value() {
+ SimpleNode<?> anyxmlInData = NodeFactory.createImmutableSimpleNode(
+ QName.create("ns:complex:json", "2014-08-11", "anyxml-in-data"), null, "foo");
+ return ImmutableCompositeNode.builder().add(anyxmlInData)
+ .setQName(QName.create("ns:complex:json", "2014-08-11", "lf13-any")).toInstance();
+ }
+
+ public static NormalizedNode<?, ?> leafNodeInContainer() {
+ LeafNode<Object> lf11 = Builders.leafBuilder()
+ .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf11")))
+ .withValue((int) 453).build();
+ return cont1Node(lf11);
+ }
+
+ public static NormalizedNode<?, ?> leafListNodeInContainer() {
+ return cont1Node(childLflst11());
+ }
+
+ public static NormalizedNode<?, ?> keyedListNodeInContainer() {
+ return cont1Node(childLst11());
+ }
+
+ public static NormalizedNode<?, ?> leafNodeViaAugmentationInContainer() {
+ return cont1Node(lf12_1Node());
+ }
+
+ public static NormalizedNode<?, ?> choiceNodeInContainer() {
+ return cont1Node(choc11Node(lf13Node()));
+ }
+
+ /**
+ * choc11 contains lf13, lf15_11 and lf15_12 are added via external augmentation
+ *
+ * @return
+ */
+ public static NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer() {
+ return cont1Node(choc11Node(lf13Node(), lf15_11Node(), lf15_12Node(), lf15_21Node()));
+ }
+
+ public static NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer() {
+ return cont1Node(choc11Node(lf13Node(), lf15_11Node(), lf15_12Node(), lf15_11NodeExternal(), lf15_12NodeExternal()));
+ }
+
+ public static NormalizedNode<?, ?> choiceNodeAugmentationInContainer() {
+ return cont1Node(choc12Node());
+ }
+
+ public static NormalizedNode<?, ?> unkeyedNodeInContainer() {
+ return cont1Node(lst12Node());
+ }
+
+}
--- /dev/null
+{
+ "complexjson:cont1": {
+ "lf15_11" : "one two",
+ "lf13" : "lf13 value",
+ "lf15_21" : "lf15_21 value",
+ "lf15_12" : "complexjson:lf11"
+ }
+}
--- /dev/null
+{
+ "complexjson:cont1": {
+ "complexjson-augmentation:lf15_11" : "lf15_11 value from augmentation",
+ "lf15_11" : "one two",
+ "lf13" : "lf13 value",
+ "complexjson-augmentation:lf15_12" : "lf15_12 value from augmentation",
+ "lf15_12" : "complexjson:lf11"
+ }
+}
--- /dev/null
+{
+ "complexjson:cont1": {
+ "lf17" : "lf17 value"
+ }
+}
--- /dev/null
+{
+ "complexjson:cont1": {
+ "lf13" : "lf13 value"
+ }
+}
--- /dev/null
+{
+ "complexjson:cont1": {
+ "lst11":[
+ {
+ "key111":"key111 value",
+ "lf112":"/complexjson:cont1/complexjson:lflst11",
+ "lf113":"lf113 value",
+ "lf111":"lf111 value"
+ }
+ ]
+ }
+}
--- /dev/null
+{
+ "complexjson:cont1": {
+ "lf11" : "453"
+ }
+}
--- /dev/null
+{
+ "complexjson:cont1": {
+ "lf12_1" : "lf12 value"
+ }
+}
--- /dev/null
+{
+ "complexjson:cont1": {
+ "lflst11":["lflst11 value1","lflst11 value2"]
+ }
+}
--- /dev/null
+{
+ "complexjson:cont1": {
+ "lst12":[
+ {
+ "lf121":"lf121 value"
+ }
+ ]
+ }
+}