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