Validate parsed QName to identity
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / JsonStreamToNormalizedNodeTest.java
1 /*
2  * Copyright (c) 2016 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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
15 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.augmentationBuilder;
16 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.choiceBuilder;
17 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.containerBuilder;
18 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
19
20 import com.google.common.collect.Sets;
21 import com.google.gson.stream.JsonReader;
22 import java.io.IOException;
23 import java.io.StringReader;
24 import java.net.URISyntaxException;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
33 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
37 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
38
39 /**
40  *
41  * Each test tests whether json input is correctly transformed to normalized node structure
42  */
43 public class JsonStreamToNormalizedNodeTest {
44
45     private static final QName CONT_1 = QName.create("ns:complex:json", "2014-08-11", "cont1");
46     private static final QName EMPTY_LEAF = QName.create(CONT_1,"empty");
47     private static SchemaContext schemaContext;
48
49     @BeforeClass
50     public static void initialization() throws IOException, URISyntaxException, ReactorException {
51         schemaContext = YangParserTestUtils.parseYangSources("/complexjson/yang");
52     }
53
54     @Test
55     public void leafNodeInContainer() throws IOException, URISyntaxException {
56         final String inputJson = loadTextFile("/complexjson/leaf-node-in-container.json");
57         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.leafNodeInContainer());
58     }
59
60     @Test
61     public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
62         final String inputJson = loadTextFile("/complexjson/leaf-node-via-augmentation-in-container.json");
63         verifyTransformationToNormalizedNode(inputJson,
64                 TestingNormalizedNodeStructuresCreator.leafNodeViaAugmentationInContainer());
65     }
66
67     @Test
68     public void leafListNodeInContainer() throws IOException, URISyntaxException {
69         final String inputJson = loadTextFile("/complexjson/leaflist-node-in-container.json");
70         verifyTransformationToNormalizedNode(inputJson,
71                 TestingNormalizedNodeStructuresCreator.leafListNodeInContainer());
72     }
73
74     @Test
75     public void keyedListNodeInContainer() throws IOException, URISyntaxException {
76         final String inputJson = loadTextFile("/complexjson/keyed-list-node-in-container.json");
77         verifyTransformationToNormalizedNode(inputJson,
78                 TestingNormalizedNodeStructuresCreator.keyedListNodeInContainer());
79     }
80
81     @Test
82     public void choiceNodeInContainer() throws IOException, URISyntaxException {
83         final String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
84         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.choiceNodeInContainer());
85     }
86
87     /**
88      * Test of translating internal augmentations to normalized nodes structure
89      *
90      * 2 nodes are added via internal augmentation A, 1 node via internal augmentation B and one node is originally
91      * member of case.
92      *
93      */
94     @Test
95     public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
96         final String inputJson = loadTextFile("/complexjson/case-node-augmentation-in-choice-in-container.json");
97         verifyTransformationToNormalizedNode(inputJson,
98                 TestingNormalizedNodeStructuresCreator.caseNodeAugmentationInChoiceInContainer());
99     }
100
101     /**
102      * also test using of namesakes (equal local names with different
103      *
104      * @throws IOException
105      * @throws URISyntaxException
106      */
107     @Test
108     public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
109         final String inputJson = loadTextFile("/complexjson/case-node-external-augmentation-in-choice-in-container.json");
110         verifyTransformationToNormalizedNode(inputJson,
111                 TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer());
112     }
113
114     /**
115      * augmentation of choice - adding new case
116      */
117     @Test
118     public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
119         final String inputJson = loadTextFile("/complexjson/choice-node-augmentation-in-container.json");
120         verifyTransformationToNormalizedNode(inputJson,
121                 TestingNormalizedNodeStructuresCreator.choiceNodeAugmentationInContainer());
122     }
123
124     @Test
125     public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
126         final String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
127         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
128     }
129
130     /**
131      * Top level JSON element contains no information about module name.
132      *
133      * It should be possible to find out potential module name from available schema context.
134      *
135      */
136     @Test
137     public void missingModuleInfoInTopLevelElement() throws IOException, URISyntaxException {
138         final String inputJson = loadTextFile("/complexjson/missing-module-in-top-level.json");
139         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.topLevelContainer());
140     }
141
142     /**
143      *
144      * Exception expected.
145      *
146      * It tests case when several elements with the same name and various namespaces exists and are in JSON specified
147      * without module name prefix.
148      */
149     @Test
150     public void leafNamesakes() throws IOException, URISyntaxException {
151         final String inputJson = loadTextFile("/complexjson/namesakes.json");
152         try {
153             //second parameter isn't necessary because error will be raised before it is used.
154             verifyTransformationToNormalizedNode(inputJson, null);
155             fail("Expected exception not raised");
156         } catch (final IllegalStateException e) {
157             final String errorMessage = e.getMessage();
158             assertTrue(errorMessage.contains("Choose suitable module name for element lf11-namesake:"));
159             assertTrue(errorMessage.contains("complexjson-augmentation"));
160             assertTrue(errorMessage.contains("complexjson-augmentation-namesake"));
161         }
162     }
163
164     @Test
165     public void emptyTypeTest() throws IOException, URISyntaxException {
166         final String inputJson = loadTextFile("/complexjson/type-empty.json");
167         final ContainerNode awaitedStructure = containerBuilder()
168                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONT_1))
169                 .addChild(leafNode(EMPTY_LEAF, null))
170                 .build();
171
172         verifyTransformationToNormalizedNode(inputJson, awaitedStructure);
173     }
174
175     /**
176      *
177      * Exception expected.
178      *
179      * Json input contains element which doesn't exist in YANG schema
180      */
181     @Test
182     public void parsingNotExistingElement() throws IOException, URISyntaxException {
183         final String inputJson = loadTextFile("/complexjson/not-existing-element.json");
184         try {
185             //second parameter isn't necessary because error will be raised before it is used.
186             verifyTransformationToNormalizedNode(inputJson, null);
187         } catch (final IllegalStateException e) {
188             assertTrue(e.getMessage().contains("Schema node with name dummy-element wasn't found"));
189         }
190     }
191
192
193     @Test
194     public void listItemWithoutArray() throws IOException, URISyntaxException {
195         final String inputJson = loadTextFile("/complexjson/keyed-list-restconf-behaviour.json");
196
197         final NormalizedNodeResult result = new NormalizedNodeResult();
198         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
199         final SchemaNode parentNode = schemaContext.getDataChildByName(CONT_1);
200         final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext, parentNode);
201         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
202         final NormalizedNode<?, ?> transformedInput = result.getResult();
203         assertNotNull(transformedInput);
204     }
205
206     @Test
207     public void listItemWithArray() throws IOException, URISyntaxException {
208         final String inputJson = loadTextFile("/complexjson/keyed-list-yang-json-behaviour.json");
209
210         final NormalizedNodeResult result = new NormalizedNodeResult();
211         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
212         final SchemaNode parentNode = schemaContext.getDataChildByName(CONT_1);
213         final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext, parentNode);
214         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
215         final NormalizedNode<?, ?> transformedInput = result.getResult();
216         assertNotNull(transformedInput);
217     }
218
219    @Test
220     public void multipleChoiceAugmentation() throws IOException, URISyntaxException {
221         final String inputJson = loadTextFile("/complexjson/multiple-choice-augmentation-in-container.json");
222
223         final NormalizedNodeResult result = new NormalizedNodeResult();
224         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
225         final SchemaNode parentNode = schemaContext.getDataChildByName(CONT_1);
226
227         final QName augmentChoice1QName = QName.create(parentNode.getQName(), "augment-choice1");
228         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
229         final QName containerQName = QName.create(augmentChoice1QName, "case11-choice-case-container");
230         final QName leafQName = QName.create(augmentChoice1QName, "case11-choice-case-leaf");
231
232         final YangInstanceIdentifier.AugmentationIdentifier aug1Id =
233                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
234         final YangInstanceIdentifier.AugmentationIdentifier aug2Id =
235                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
236         final YangInstanceIdentifier.NodeIdentifier augmentChoice1Id =
237                 new YangInstanceIdentifier.NodeIdentifier(augmentChoice1QName);
238         final YangInstanceIdentifier.NodeIdentifier augmentChoice2Id =
239                 new YangInstanceIdentifier.NodeIdentifier(augmentChoice2QName);
240         final YangInstanceIdentifier.NodeIdentifier containerId =
241                 new YangInstanceIdentifier.NodeIdentifier(containerQName);
242
243         final NormalizedNode<?, ?> cont1Normalized =
244                 containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(parentNode.getQName()))
245                         .withChild(augmentationBuilder().withNodeIdentifier(aug1Id)
246                                 .withChild(choiceBuilder().withNodeIdentifier(augmentChoice1Id)
247                                         .withChild(augmentationBuilder().withNodeIdentifier(aug2Id)
248                                                 .withChild(choiceBuilder().withNodeIdentifier(augmentChoice2Id)
249                                                         .withChild(containerBuilder().withNodeIdentifier(containerId)
250                                                                 .withChild(leafNode(leafQName, "leaf-value"))
251                                                                 .build())
252                                                         .build())
253                                                 .build())
254                                         .build())
255                                 .build()).build();
256
257         final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
258         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
259         final NormalizedNode<?, ?> transformedInput = result.getResult();
260         assertNotNull(transformedInput);
261         assertEquals(cont1Normalized, transformedInput);
262     }
263
264     private static void verifyTransformationToNormalizedNode(final String inputJson,
265             final NormalizedNode<?, ?> awaitedStructure) {
266         final NormalizedNodeResult result = new NormalizedNodeResult();
267         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
268         final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
269         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
270         final NormalizedNode<?, ?> transformedInput = result.getResult();
271         assertEquals("Transformation of json input to normalized node wasn't successful.", awaitedStructure,
272                 transformedInput);
273     }
274
275 }