BUG 1440 - additional tests for data-codec-gson
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / StreamToNormalizedNodeTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.codec.gson;
9
10 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
11 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
12
13 import com.google.gson.stream.JsonReader;
14 import java.io.File;
15 import java.io.IOException;
16 import java.io.StringReader;
17 import java.io.StringWriter;
18 import java.net.URISyntaxException;
19 import org.junit.BeforeClass;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.stream.LoggingNormalizedNodeStreamWriter;
26 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
27 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
29 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class StreamToNormalizedNodeTest {
35     private static final Logger LOG = LoggerFactory.getLogger(StreamToNormalizedNodeTest.class);
36     private static SchemaContext schemaContext;
37     private static String streamAsString;
38
39     @BeforeClass
40     public static void initialization() throws IOException, URISyntaxException {
41         schemaContext = loadModules("/complexjson/yang");
42         streamAsString = loadTextFile(new File(StreamToNormalizedNodeTest.class.getResource(
43                 "/complexjson/complex-json.json").toURI()));
44     }
45
46     /**
47      * Demonstrates how to log events produced by a {@link JsonReader}.
48      *
49      * @throws IOException
50      */
51     @Test
52     public void ownStreamWriterImplementationDemonstration() throws IOException {
53         // GSON's JsonReader reading from the loaded string (our event source)
54         final JsonReader reader = new JsonReader(new StringReader(streamAsString));
55
56         // StreamWriter which outputs SLF4J events
57         final LoggingNormalizedNodeStreamWriter logWriter = new LoggingNormalizedNodeStreamWriter();
58
59         // JSON -> StreamWriter parser
60         try (final JsonParserStream jsonHandler = JsonParserStream.create(logWriter, schemaContext)) {
61             // Process multiple readers, flush()/close() as needed
62             jsonHandler.parse(reader);
63         }
64     }
65
66     /**
67      * Demonstrates how to create an immutable NormalizedNode tree from a {@link JsonReader} and
68      * then writes the data back into string representation.
69      *
70      * @throws IOException
71      */
72     @Ignore
73     @Test
74     public void immutableNormalizedNodeStreamWriterDemonstration() throws IOException {
75         /*
76          * This is the parsing part
77          */
78         // This is where we will output the nodes
79         NormalizedNodeResult result = new NormalizedNodeResult();
80
81         // StreamWriter which attaches NormalizedNode under parent
82         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
83
84         // JSON -> StreamWriter parser
85         try (JsonParserStream handler = JsonParserStream.create(streamWriter, schemaContext)) {
86             handler.parse(new JsonReader(new StringReader(streamAsString)));
87         }
88
89         // Finally build the node
90         final NormalizedNode<?, ?> parsedData = result.getResult();
91         LOG.debug("Parsed NormalizedNodes: {}", parsedData);
92
93         /*
94          * This is the serialization part.
95          */
96         // We want to write the first child out
97         final DataContainerChild<? extends PathArgument, ?> firstChild = (DataContainerChild<? extends PathArgument, ?>) parsedData;
98         LOG.debug("Serializing first child: {}", firstChild);
99
100         // String holder
101         final StringWriter writer = new StringWriter();
102
103         // StreamWriter which outputs JSON strings
104         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.create(schemaContext, writer, 2);
105
106         // NormalizedNode -> StreamWriter
107         final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
108
109         // Write multiple NormalizedNodes fluently, flush()/close() as needed
110         nodeWriter.write(firstChild).close();
111
112         // Just to put it somewhere
113         LOG.debug("Serialized JSON: {}", writer.toString());
114     }
115
116 }