Introduce binding.EntryObject
[yangtools.git] / codec / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / Bug4969Test.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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13
14 import com.google.gson.stream.JsonReader;
15 import java.io.StringReader;
16 import java.util.Set;
17 import org.junit.jupiter.api.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
23 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizationResultHolder;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26
27 class Bug4969Test {
28     @Test
29     void newParserLeafRefTest() throws Exception {
30         final var context = YangParserTestUtils.parseYang("""
31             module bar {
32               namespace "bar";
33               prefix bar;
34
35               revision "2016-01-22" {
36                 description "Initial version";
37               }
38
39               typedef ref1 {
40                 type ref1-2;
41               }
42
43               typedef ref2 {
44                 type ref2-2;
45               }
46
47               typedef ref3 {
48                 type ref3-2;
49               }
50
51               typedef ref1-2 {
52                 type leafref {
53                   path "/bar:root/bar:l1";
54                 }
55               }
56
57               typedef ref2-2 {
58                 type leafref {
59                   path "/bar:root/bar:l2";
60                 }
61               }
62
63               typedef ref3-2 {
64                 type leafref {
65                   path "/bar:root/bar:l3";
66                 }
67               }
68
69               container root {
70                 leaf l1 {
71                   type bits {
72                     bit a;
73                     bit b;
74                     bit c;
75                     bit d;
76                   }
77                 }
78                 leaf l2 {
79                   type leafref {
80                     path "/root/l1";
81                   }
82                 }
83                 leaf l3 {
84                   type leafref {
85                     path "../l1";
86                   }
87                 }
88               }
89             }""", """
90             module foo {
91               namespace "foo";
92               prefix foo;
93
94               import bar {
95                 prefix bar; revision-date 2016-01-22;
96               }
97
98               revision "2016-01-22" {
99                 description "Initial version";
100               }
101
102               container root {
103                 leaf ref1 {
104                   type bar:ref1;
105                 }
106                 leaf ref2 {
107                   type bar:ref2;
108                 }
109                 leaf ref3 {
110                   type bar:ref3;
111                 }
112                 leaf ref4 {
113                   type leafref {
114                     path "/bar:root/bar:l1";
115                   }
116                 }
117               }
118             }""");
119         assertNotNull(context);
120
121         verifyNormalizedNodeResult(context);
122     }
123
124     private static void verifyNormalizedNodeResult(final EffectiveModelContext context) throws Exception {
125         final var inputJson = TestUtils.loadTextFile("/bug-4969/json/foo.json");
126         final var result = new NormalizationResultHolder();
127         final var streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
128         final var jsonParser = JsonParserStream.create(streamWriter,
129             JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(context));
130         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
131
132         final var root = assertInstanceOf(ContainerNode.class, result.getResult().data());
133         final var ref1 = assertInstanceOf(LeafNode.class, root.childByArg(NodeIdentifier.create(
134             QName.create("foo", "2016-01-22", "ref1"))));
135         final var ref2 = assertInstanceOf(LeafNode.class, root.childByArg(NodeIdentifier.create(
136             QName.create("foo", "2016-01-22", "ref2"))));
137         final var ref3 = assertInstanceOf(LeafNode.class, root.childByArg(NodeIdentifier.create(
138             QName.create("foo", "2016-01-22", "ref3"))));
139         final var ref4 = assertInstanceOf(LeafNode.class, root.childByArg(NodeIdentifier.create(
140             QName.create("foo", "2016-01-22", "ref4"))));
141
142         assertEquals(Set.of("a"), ref1.body());
143         assertEquals(Set.of("a", "b"), ref2.body());
144         assertEquals(Set.of("a", "b", "c"), ref3.body());
145         assertEquals(Set.of("a", "b", "c", "d"), ref4.body());
146     }
147
148     @Test
149     void newParserLeafRefTest2() throws Exception {
150         final var context = YangParserTestUtils.parseYang("""
151             module augment-leafref-module {
152               namespace "augment:leafref:module";
153               prefix "auglfrfmo";
154               revision 2014-12-16 {
155               }
156               typedef leafreftype {
157                 type leafref {
158                   path "/auglfrfmo:cont/auglfrfmo:lf3";
159                 }
160               }
161               container cont {
162                 leaf lf3 {
163                   type string;
164                 }
165               }
166             }""", """
167             module leafref-module {
168               namespace "leafref:module";
169               prefix "lfrfmo";
170               import augment-leafref-module { prefix augleafref; revision-date 2014-12-16; }
171               revision 2013-11-18 {
172               }
173               container cont {
174                 leaf lf1 {
175                   type int32;
176                 }
177                 leaf lf2 {
178                   type leafref {
179                     path "/cont/lf1";
180                   }
181                 }
182                 leaf lf4 {
183                   type augleafref:leafreftype;
184                 }
185               }
186             }""");
187         assertNotNull(context);
188
189         parseJsonToNormalizedNodes(context);
190     }
191
192     private static void parseJsonToNormalizedNodes(final EffectiveModelContext context) throws Exception {
193         final var inputJson = TestUtils.loadTextFile("/leafref/json/data.json");
194         final var result = new NormalizationResultHolder();
195         final var streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
196         final var jsonParser = JsonParserStream.create(streamWriter,
197             JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(context));
198         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
199         final var transformedInput = result.getResult().data();
200         assertNotNull(transformedInput);
201     }
202 }