Refactor binding-dom-adapter
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / mdsal / binding / dom / codec / impl / NormalizedNodeSerializeDeserializeTest.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.mdsal.binding.dom.codec.impl;
9
10 import static java.util.Collections.singleton;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.fail;
15 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.top;
16 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.topLevelList;
17 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.augmentationBuilder;
18 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.choiceBuilder;
19 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.containerBuilder;
20 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
21 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
22 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
23 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
24
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.Set;
33 import org.junit.Test;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TopChoiceAugment1;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TopChoiceAugment1Builder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TopChoiceAugment2;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TopChoiceAugment2Builder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugmentBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.top.AugmentChoice1;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.top.augment.choice1.Case1Builder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.top.augment.choice1.case1.augment.choice2.Case11Builder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.top.augment.choice1.case1.augment.choice2.case11.Case11ChoiceCaseContainerBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.ChoiceContainer;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.ChoiceContainerBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top1;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top1Builder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top2;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top2Builder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TopBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.choice.identifier.ExtendedBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.choice.identifier.extended.ExtendedIdBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.top.level.list.NestedList;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.top.level.list.NestedListBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.top.level.list.NestedListKey;
61 import org.opendaylight.yang.gen.v1.urn.test.foo4798.rev160101.Root;
62 import org.opendaylight.yangtools.yang.binding.Augmentation;
63 import org.opendaylight.yangtools.yang.binding.DataObject;
64 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
65 import org.opendaylight.yangtools.yang.common.QName;
66 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
67 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
68 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
69 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
70 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
71 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
72 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
73 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
74 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
75 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
76 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
77 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
78 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
79 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
80 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
81 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
82 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
83 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
84 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
85 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
86
87 public class NormalizedNodeSerializeDeserializeTest extends AbstractBindingCodecTest {
88
89     public static final String TOP_LEVEL_LIST_FOO_KEY_VALUE = "foo";
90     public static final TopLevelListKey TOP_LEVEL_LIST_FOO_KEY = new TopLevelListKey(TOP_LEVEL_LIST_FOO_KEY_VALUE);
91
92     public static final QName TOP_QNAME = Top.QNAME;
93     public static final QName TOP_LEVEL_LIST_QNAME = QName.create(TOP_QNAME, "top-level-list");
94     public static final QName TOP_LEVEL_LIST_KEY_QNAME = QName.create(TOP_QNAME, "name");
95     public static final QName TOP_LEVEL_LEAF_LIST_QNAME = QName.create(TOP_QNAME, "top-level-leaf-list");
96     public static final QName TOP_LEVEL_ORDERED_LEAF_LIST_QNAME = QName.create(TOP_QNAME,
97         "top-level-ordered-leaf-list");
98     public static final QName NESTED_LIST_QNAME = QName.create(TOP_QNAME, "nested-list");
99     public static final QName NESTED_LIST_KEY_QNAME = QName.create(TOP_QNAME, "name");
100     public static final QName CHOICE_CONTAINER_QNAME = ChoiceContainer.QNAME;
101     public static final QName CHOICE_IDENTIFIER_QNAME = QName.create(CHOICE_CONTAINER_QNAME, "identifier");
102     public static final QName CHOICE_IDENTIFIER_ID_QNAME = QName.create(CHOICE_CONTAINER_QNAME, "id");
103     public static final QName SIMPLE_ID_QNAME = QName.create(CHOICE_CONTAINER_QNAME, "simple-id");
104     public static final QName EXTENDED_ID_QNAME = QName.create(CHOICE_CONTAINER_QNAME, "extended-id");
105     private static final QName SIMPLE_VALUE_QNAME = QName.create(TreeComplexUsesAugment.QNAME, "simple-value");
106
107     private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier
108             .builder(Top.class).child(TopLevelList.class, TOP_LEVEL_LIST_FOO_KEY).build();
109     private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY =
110             BA_TOP_LEVEL_LIST.augmentation(TreeLeafOnlyAugment.class);
111     private static final InstanceIdentifier<TreeComplexUsesAugment> BA_TREE_COMPLEX_USES =
112             BA_TOP_LEVEL_LIST.augmentation(TreeComplexUsesAugment.class);
113
114     public static final YangInstanceIdentifier BI_TOP_PATH = YangInstanceIdentifier.of(TOP_QNAME);
115     public static final YangInstanceIdentifier BI_TOP_LEVEL_LIST_PATH = BI_TOP_PATH.node(TOP_LEVEL_LIST_QNAME);
116     public static final YangInstanceIdentifier BI_TOP_LEVEL_LIST_FOO_PATH = BI_TOP_LEVEL_LIST_PATH
117             .node(NodeIdentifierWithPredicates.of(TOP_LEVEL_LIST_QNAME,
118                 TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE));
119     public static final YangInstanceIdentifier BI_CHOICE_CONTAINER_PATH = YangInstanceIdentifier.of(
120         CHOICE_CONTAINER_QNAME);
121
122     @Test
123     public void containerToNormalized() {
124         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = codecContext.toNormalizedNode(
125             InstanceIdentifier.create(Top.class), top());
126         final ContainerNode topNormalized = getEmptyTop();
127         assertEquals(topNormalized, entry.getValue());
128     }
129
130     @Test
131     public void containerFromNormalized() {
132         final ContainerNode topNormalized = getEmptyTop();
133         final Entry<InstanceIdentifier<?>, DataObject> entry = codecContext.fromNormalizedNode(BI_TOP_PATH,
134             topNormalized);
135         assertEquals(top(), entry.getValue());
136     }
137
138     private static ContainerNode getEmptyTop() {
139         return ImmutableContainerNodeBuilder.create()
140                     .withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
141                     .build();
142     }
143
144     private static final QName AGUMENT_STRING_Q = QName.create(TOP_QNAME, "augmented-string");
145     private static final String AUGMENT_STRING_VALUE = "testingEquals";
146     private static final QName AUGMENT_INT_Q = QName.create(TOP_QNAME, "augmented-int");
147     private static final int AUGMENT_INT_VALUE = 44;
148
149     @Test
150     public void equalsWithAugment() {
151         final ContainerNode topNormalizedWithAugments = getNormalizedTopWithAugments(
152             augmentationBuilder()
153                 .withNodeIdentifier(new AugmentationIdentifier(singleton(AGUMENT_STRING_Q)))
154                 .withChild(ImmutableNodes.leafNode(AGUMENT_STRING_Q, AUGMENT_STRING_VALUE))
155                 .build());
156         final ContainerNode topNormalized = getEmptyTop();
157
158         final Entry<InstanceIdentifier<?>, DataObject> entry = codecContext.fromNormalizedNode(BI_TOP_PATH,
159             topNormalized);
160         final Entry<InstanceIdentifier<?>, DataObject> entryWithAugments = codecContext.fromNormalizedNode(BI_TOP_PATH,
161             topNormalizedWithAugments);
162
163         // Equals on other with no augmentation should be false
164         assertNotEquals(top(), entryWithAugments.getValue());
165         // Equals on other(reversed) with no augmentation should be false
166         assertNotEquals(entryWithAugments.getValue(), top());
167         // Equals on other(lazy) with no augmentation should be false
168         assertNotEquals(entry.getValue(), entryWithAugments.getValue());
169         // Equals on other(lazy, reversed) with no augmentation should be false
170         assertNotEquals(entryWithAugments.getValue(), entry.getValue());
171
172         final Top topWithAugments = topWithAugments(Collections.<Class<? extends Augmentation<Top>>, Augmentation<Top>>
173             singletonMap(Top1.class, new Top1Builder().setAugmentedString(AUGMENT_STRING_VALUE).build()));
174         // Equals other with same augment should be true
175         assertEquals(topWithAugments, entryWithAugments.getValue());
176         // Equals other with same augment should be true
177         assertEquals(entryWithAugments.getValue(), topWithAugments);
178         // Equals on self should be true
179         assertEquals(entryWithAugments.getValue(), entryWithAugments.getValue());
180
181         final Top topWithAugmentsDiffValue = topWithAugments(
182             Collections.<Class<? extends Augmentation<Top>>, Augmentation<Top>>singletonMap(Top1.class,
183                 new Top1Builder().setAugmentedString("differentValue").build()));
184         assertNotEquals(topWithAugmentsDiffValue, entryWithAugments.getValue());
185         assertNotEquals(entryWithAugments.getValue(), topWithAugmentsDiffValue);
186     }
187
188     @Test
189     public void equalsWithMultipleAugments() {
190         final ContainerNode topNormalizedWithAugments = getNormalizedTopWithAugments(
191             augmentationBuilder()
192                 .withNodeIdentifier(new AugmentationIdentifier(singleton(AGUMENT_STRING_Q)))
193                 .withChild(ImmutableNodes.leafNode(AGUMENT_STRING_Q, AUGMENT_STRING_VALUE))
194                 .build(),
195             augmentationBuilder()
196                 .withNodeIdentifier(new AugmentationIdentifier(singleton(AUGMENT_INT_Q)))
197                 .withChild(ImmutableNodes.leafNode(AUGMENT_INT_Q, AUGMENT_INT_VALUE))
198                 .build());
199
200         final Entry<InstanceIdentifier<?>, DataObject> entryWithAugments = codecContext.fromNormalizedNode(BI_TOP_PATH,
201             topNormalizedWithAugments);
202         Map<Class<? extends Augmentation<Top>>, Augmentation<Top>> augments = new HashMap<>();
203         augments.put(Top1.class, new Top1Builder().setAugmentedString(AUGMENT_STRING_VALUE).build());
204         augments.put(Top2.class, new Top2Builder().setAugmentedInt(AUGMENT_INT_VALUE).build());
205         Top topWithAugments = topWithAugments(augments);
206
207         assertEquals(topWithAugments, entryWithAugments.getValue());
208         assertEquals(entryWithAugments.getValue(), topWithAugments);
209
210         augments = new HashMap<>();
211         augments.put(Top1.class, new Top1Builder().setAugmentedString(AUGMENT_STRING_VALUE).build());
212         augments.put(Top2.class, new Top2Builder().setAugmentedInt(999).build());
213         topWithAugments = topWithAugments(augments);
214
215         assertNotEquals(topWithAugments, entryWithAugments.getValue());
216         assertNotEquals(entryWithAugments.getValue(), topWithAugments);
217     }
218
219     private static ContainerNode getNormalizedTopWithAugments(final AugmentationNode... augChild) {
220         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = ImmutableContainerNodeBuilder.create();
221
222         for (AugmentationNode augmentationNode : augChild) {
223             builder.withChild(augmentationNode);
224         }
225         return builder.withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
226                     .withChild(mapNodeBuilder(TOP_LEVEL_LIST_QNAME).build()).build();
227     }
228
229     private static Top topWithAugments(
230             final Map<Class<? extends Augmentation<Top>>, ? extends Augmentation<Top>> augments) {
231         final TopBuilder topBuilder = new TopBuilder();
232         for (Entry<Class<? extends Augmentation<Top>>, ? extends Augmentation<Top>> augment : augments.entrySet()) {
233             topBuilder.addAugmentation(augment.getKey(), augment.getValue());
234         }
235         return topBuilder.setTopLevelList(Collections.emptyMap()).build();
236     }
237
238     @Test
239     public void listWithKeysToNormalized() {
240         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = codecContext.toNormalizedNode(
241             BA_TOP_LEVEL_LIST, topLevelList(TOP_LEVEL_LIST_FOO_KEY));
242         final MapEntryNode topLevelListNormalized = ImmutableMapEntryNodeBuilder.create()
243                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME,
244                     TOP_LEVEL_LIST_FOO_KEY_VALUE))
245                 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
246                 .build();
247         assertEquals(topLevelListNormalized, entry.getValue());
248     }
249
250     @Test
251     public void listWithKeysFromNormalized() {
252         final MapEntryNode topLevelListNormalized = ImmutableMapEntryNodeBuilder.create()
253                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME,
254                     TOP_LEVEL_LIST_FOO_KEY_VALUE))
255                 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
256                 .build();
257         final Entry<InstanceIdentifier<?>, DataObject> entry = codecContext.fromNormalizedNode(
258             BI_TOP_LEVEL_LIST_FOO_PATH, topLevelListNormalized);
259         assertEquals(topLevelList(TOP_LEVEL_LIST_FOO_KEY), entry.getValue());
260     }
261
262     @Test
263     public void leafOnlyAugmentationToNormalized() {
264         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = codecContext.toNormalizedNode(
265             BA_TREE_LEAF_ONLY, new TreeLeafOnlyAugmentBuilder().setSimpleValue("simpleValue").build());
266         final Set<QName> augmentationChildren = new HashSet<>();
267         augmentationChildren.add(SIMPLE_VALUE_QNAME);
268         final AugmentationNode augmentationNode = ImmutableAugmentationNodeBuilder.create()
269                 .withNodeIdentifier(new AugmentationIdentifier(augmentationChildren))
270                 .withChild(leafNode(SIMPLE_VALUE_QNAME, "simpleValue"))
271                 .build();
272         assertEquals(augmentationNode, entry.getValue());
273     }
274
275     @Test
276     public void leafOnlyAugmentationFromNormalized() {
277         final Set<QName> augmentationChildren = new HashSet<>();
278         augmentationChildren.add(SIMPLE_VALUE_QNAME);
279         final AugmentationNode augmentationNode = ImmutableAugmentationNodeBuilder.create()
280                 .withNodeIdentifier(new AugmentationIdentifier(augmentationChildren))
281                 .withChild(leafNode(SIMPLE_VALUE_QNAME, "simpleValue"))
282                 .build();
283         final Entry<InstanceIdentifier<?>, DataObject> entry = codecContext.fromNormalizedNode(
284             BI_TOP_LEVEL_LIST_FOO_PATH.node(new AugmentationIdentifier(augmentationChildren)),
285             augmentationNode);
286         assertEquals(new TreeLeafOnlyAugmentBuilder().setSimpleValue("simpleValue").build(), entry.getValue());
287     }
288
289     @Test
290     public void orderedleafListToNormalized() {
291         List<String> topLevelLeafList = new ArrayList<>();
292         topLevelLeafList.add("foo");
293         Top top = new TopBuilder().setTopLevelOrderedLeafList(topLevelLeafList).build();
294
295         Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = codecContext.toNormalizedNode(
296             InstanceIdentifier.create(Top.class), top);
297         ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
298                 .withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
299                 .withChild(ImmutableOrderedLeafSetNodeBuilder.create()
300                         .withNodeIdentifier(new NodeIdentifier(TOP_LEVEL_ORDERED_LEAF_LIST_QNAME))
301                         .withChild(
302                                 ImmutableLeafSetEntryNodeBuilder.create()
303                                         .withNodeIdentifier(new NodeWithValue<>(TOP_LEVEL_ORDERED_LEAF_LIST_QNAME,
304                                                 "foo"))
305                                         .withValue("foo")
306                                         .build())
307                         .build())
308                 .build();
309         assertEquals(containerNode, entry.getValue());
310     }
311
312     @Test
313     public void leafListToNormalized() {
314         final List<String> topLevelLeafList = new ArrayList<>();
315         topLevelLeafList.add("foo");
316         final Top top = new TopBuilder().setTopLevelLeafList(topLevelLeafList).build();
317
318         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = codecContext.toNormalizedNode(
319             InstanceIdentifier.create(Top.class), top);
320         final ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
321                 .withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
322                 .withChild(ImmutableLeafSetNodeBuilder.create()
323                         .withNodeIdentifier(new NodeIdentifier(TOP_LEVEL_LEAF_LIST_QNAME))
324                         .withChild(
325                                 ImmutableLeafSetEntryNodeBuilder.create()
326                                         .withNodeIdentifier(new NodeWithValue<>(TOP_LEVEL_LEAF_LIST_QNAME, "foo"))
327                                         .withValue("foo")
328                                         .build())
329                         .build())
330                 .build();
331         assertEquals(containerNode, entry.getValue());
332     }
333
334     @Test
335     public void leafListFromNormalized() {
336         final ContainerNode topWithLeafList = ImmutableContainerNodeBuilder.create()
337                 .withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
338                 .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(
339                     TOP_LEVEL_LEAF_LIST_QNAME))
340                     .withChild(ImmutableLeafSetEntryNodeBuilder.create()
341                         .withNodeIdentifier(new NodeWithValue<>(TOP_LEVEL_LEAF_LIST_QNAME, "foo"))
342                         .withValue("foo").build()).build())
343                 .build();
344         final Entry<InstanceIdentifier<?>, DataObject> entry = codecContext.fromNormalizedNode(BI_TOP_PATH,
345             topWithLeafList);
346         final List<String> topLevelLeafList = new ArrayList<>();
347         topLevelLeafList.add("foo");
348         final Top top = new TopBuilder().setTopLevelLeafList(topLevelLeafList).build();
349         assertEquals(top, entry.getValue());
350     }
351
352     @Test
353     public void orderedLeafListFromNormalized() {
354         ContainerNode topWithLeafList = ImmutableContainerNodeBuilder.create()
355                 .withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
356                 .withChild(ImmutableOrderedLeafSetNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(
357                     TOP_LEVEL_ORDERED_LEAF_LIST_QNAME))
358                     .withChild(ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
359                         new NodeWithValue<>(TOP_LEVEL_ORDERED_LEAF_LIST_QNAME, "foo")).withValue("foo").build())
360                     .build())
361                 .build();
362         Entry<InstanceIdentifier<?>, DataObject> entry = codecContext.fromNormalizedNode(BI_TOP_PATH, topWithLeafList);
363         List<String> topLevelLeafList = new ArrayList<>();
364         topLevelLeafList.add("foo");
365         Top top = new TopBuilder().setTopLevelOrderedLeafList(topLevelLeafList).build();
366         assertEquals(top, entry.getValue());
367     }
368
369     @Test
370     public void choiceToNormalized() {
371         final ChoiceContainer choiceContainerBA = new ChoiceContainerBuilder().setIdentifier(new ExtendedBuilder()
372             .setExtendedId(new ExtendedIdBuilder().setId("identifier_value").build()).build()).build();
373         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = codecContext.toNormalizedNode(
374             InstanceIdentifier.create(ChoiceContainer.class), choiceContainerBA);
375         final ContainerNode choiceContainer = ImmutableContainerNodeBuilder.create()
376                 .withNodeIdentifier(new NodeIdentifier(CHOICE_CONTAINER_QNAME))
377                 .withChild(ImmutableChoiceNodeBuilder.create()
378                     .withNodeIdentifier(new NodeIdentifier(CHOICE_IDENTIFIER_QNAME))
379                         .withChild(ImmutableContainerNodeBuilder.create()
380                             .withNodeIdentifier(new NodeIdentifier(EXTENDED_ID_QNAME))
381                             .withChild(leafNode(CHOICE_IDENTIFIER_ID_QNAME, "identifier_value")).build()).build())
382                 .build();
383         assertEquals(choiceContainer, entry.getValue());
384     }
385
386     @Test
387     public void test4798() {
388         final QName containerIdentifierQname4798 = Root.QNAME;
389         final QName choiceIdentifierQname4798 = QName.create(containerIdentifierQname4798, "bug4798-choice");
390         final QName nestedListQname4798 = QName.create(containerIdentifierQname4798, "list-in-case");
391         final QName nestedListKeyQname4798 = QName.create(containerIdentifierQname4798, "test-leaf");
392         final QName nestedContainerValidQname = QName.create(containerIdentifierQname4798, "case-b-container");
393         final QName nestedContainerOuterQname = QName.create(containerIdentifierQname4798, "outer-container");
394         final QName nestedContainerLeafOuterQname = QName.create(containerIdentifierQname4798,
395                 "leaf-in-outer-container");
396
397         final YangInstanceIdentifier yangInstanceIdentifierOuter = YangInstanceIdentifier.of(
398             containerIdentifierQname4798);
399         final ContainerNode containerNodeOuter = ImmutableContainerNodeBuilder.create()
400                 .withNodeIdentifier(new NodeIdentifier(containerIdentifierQname4798))
401                 .withChild(ImmutableContainerNodeBuilder.create()
402                         .withNodeIdentifier(new NodeIdentifier(nestedContainerOuterQname))
403                         .withChild(leafNode(nestedContainerLeafOuterQname, "bar"))
404                         .build())
405                 .build();
406         final Entry<InstanceIdentifier<?>, DataObject> entryContainer = codecContext.fromNormalizedNode(
407             yangInstanceIdentifierOuter, containerNodeOuter);
408         assertNotNull(entryContainer.getValue());
409         assertNotNull(entryContainer.getKey());
410
411         final NodeIdentifierWithPredicates nodeIdentifierWithPredicates4798 =
412                 NodeIdentifierWithPredicates.of(nestedListQname4798, nestedListKeyQname4798, "foo");
413         final YangInstanceIdentifier yangInstanceIdentifier4798 = YangInstanceIdentifier.of(
414             containerIdentifierQname4798)
415                 .node(choiceIdentifierQname4798)
416                 .node(nestedListQname4798)
417                 .node(nodeIdentifierWithPredicates4798);
418
419         final YangInstanceIdentifier yangInstanceIdentifierValid = YangInstanceIdentifier.of(
420             containerIdentifierQname4798)
421                 .node(choiceIdentifierQname4798)
422                 .node(nestedContainerValidQname)
423                 .node(nestedListQname4798)
424                 .node(nodeIdentifierWithPredicates4798);
425         final ContainerNode containerNodeValid = ImmutableContainerNodeBuilder.create()
426                 .withNodeIdentifier(new NodeIdentifier(containerIdentifierQname4798))
427                 .withChild(ImmutableChoiceNodeBuilder.create()
428                         .withNodeIdentifier(new NodeIdentifier(choiceIdentifierQname4798))
429                         .withChild(ImmutableContainerNodeBuilder.create()
430                                 .withNodeIdentifier(new NodeIdentifier(nestedContainerValidQname))
431                                 .withChild(ImmutableMapNodeBuilder.create()
432                                     .withNodeIdentifier(new NodeIdentifier(nestedListQname4798))
433                                         .withChild(mapEntry(nestedListQname4798, nestedListKeyQname4798, "foo"))
434                                         .withChild(mapEntry(nestedListQname4798, nestedListKeyQname4798, "bar"))
435                                         .build())
436                                 .build())
437                         .build())
438                 .build();
439         try {
440             codecContext.fromNormalizedNode(yangInstanceIdentifierValid, containerNodeValid);
441             fail("Incorect YangInstanceIdentifier should fail");
442         } catch (IllegalStateException e) {
443             // Expected
444         }
445
446         final ContainerNode containerNode4798 = ImmutableContainerNodeBuilder.create()
447                 .withNodeIdentifier(new NodeIdentifier(containerIdentifierQname4798))
448                 .withChild(ImmutableChoiceNodeBuilder.create()
449                         .withNodeIdentifier(new NodeIdentifier(choiceIdentifierQname4798))
450                         .withChild(ImmutableMapNodeBuilder.create()
451                             .withNodeIdentifier(new NodeIdentifier(nestedListQname4798))
452                             .withChild(mapEntry(nestedListQname4798, nestedListKeyQname4798, "foo"))
453                             .withChild(mapEntry(nestedListQname4798, nestedListKeyQname4798, "bar"))
454                             .build())
455                         .build())
456                 .build();
457         try {
458             codecContext.fromNormalizedNode(yangInstanceIdentifier4798, containerNode4798);
459             fail("Incorect YangInstanceIdentifier should fail");
460         } catch (IllegalStateException e) {
461             // Expected
462         }
463     }
464
465     @Test
466     public void choiceFromNormalized() {
467         final ContainerNode choiceContainerBI = ImmutableContainerNodeBuilder.create()
468                 .withNodeIdentifier(new NodeIdentifier(CHOICE_CONTAINER_QNAME))
469                 .withChild(ImmutableChoiceNodeBuilder.create()
470                     .withNodeIdentifier(new NodeIdentifier(CHOICE_IDENTIFIER_QNAME))
471                     .withChild(ImmutableContainerNodeBuilder.create()
472                         .withNodeIdentifier(new NodeIdentifier(EXTENDED_ID_QNAME))
473                         .withChild(leafNode(CHOICE_IDENTIFIER_ID_QNAME, "identifier_value")).build()).build())
474                 .build();
475         final Entry<InstanceIdentifier<?>, DataObject> entry = codecContext.fromNormalizedNode(BI_CHOICE_CONTAINER_PATH,
476             choiceContainerBI);
477         final ChoiceContainer choiceContainerBA = new ChoiceContainerBuilder().setIdentifier(new ExtendedBuilder()
478             .setExtendedId(new ExtendedIdBuilder().setId("identifier_value").build()).build()).build();
479         assertEquals(choiceContainerBA, entry.getValue());
480     }
481
482     @Test
483     public void orderedLisToNormalized() {
484         final InstanceIdentifier<TopLevelList> ii = BA_TOP_LEVEL_LIST;
485         final List<NestedList> nestedLists = new ArrayList<>();
486         nestedLists.add(new NestedListBuilder().withKey(new NestedListKey("foo")).build());
487         nestedLists.add(new NestedListBuilder().withKey(new NestedListKey("bar")).build());
488         final TopLevelList topLevelList = new TopLevelListBuilder().withKey(TOP_LEVEL_LIST_FOO_KEY).setNestedList(
489             nestedLists).build();
490         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = codecContext.toNormalizedNode(ii,
491             topLevelList);
492         final MapEntryNode foo = mapEntryBuilder().withNodeIdentifier(NodeIdentifierWithPredicates.of(
493                 TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
494                 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
495                 .withChild(ImmutableOrderedMapNodeBuilder.create()
496                     .withNodeIdentifier(new NodeIdentifier(NESTED_LIST_QNAME))
497                     .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "foo"))
498                     .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "bar")).build()).build();
499         assertEquals(foo, entry.getValue());
500     }
501
502     @Test
503     public void orderedLisFromNormalized() {
504         final MapEntryNode foo = mapEntryBuilder().withNodeIdentifier(NodeIdentifierWithPredicates.of(
505                 TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
506                 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
507                 .withChild(ImmutableOrderedMapNodeBuilder.create()
508                     .withNodeIdentifier(new NodeIdentifier(NESTED_LIST_QNAME))
509                     .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "foo"))
510                     .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "bar")).build()).build();
511         final Entry<InstanceIdentifier<?>, DataObject> entry = codecContext.fromNormalizedNode(
512             BI_TOP_LEVEL_LIST_FOO_PATH, foo);
513         final List<NestedList> nestedLists = new ArrayList<>();
514         nestedLists.add(new NestedListBuilder().withKey(new NestedListKey("foo")).build());
515         nestedLists.add(new NestedListBuilder().withKey(new NestedListKey("bar")).build());
516         final TopLevelList topLevelList = new TopLevelListBuilder().withKey(TOP_LEVEL_LIST_FOO_KEY).setNestedList(
517             nestedLists).build();
518         assertEquals(topLevelList, entry.getValue());
519     }
520
521     @Test
522     public void augmentMultipleChoices() {
523         final QName augmentChoice1QName = AugmentChoice1.QNAME;
524         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
525         final QName containerQName = QName.create(augmentChoice1QName, "case11-choice-case-container");
526         final QName leafQName = QName.create(augmentChoice1QName, "case11-choice-case-leaf");
527
528         final AugmentationIdentifier aug1Id = new AugmentationIdentifier(Collections.singleton(augmentChoice1QName));
529         final AugmentationIdentifier aug2Id = new AugmentationIdentifier(Collections.singleton(augmentChoice2QName));
530         final NodeIdentifier augmentChoice1Id = new NodeIdentifier(augmentChoice1QName);
531         final NodeIdentifier augmentChoice2Id = new NodeIdentifier(augmentChoice2QName);
532         final NodeIdentifier containerId = new NodeIdentifier(containerQName);
533
534         final TopBuilder tBuilder = new TopBuilder();
535         final TopChoiceAugment1Builder tca1Builder = new TopChoiceAugment1Builder();
536         final Case1Builder c1Builder = new Case1Builder();
537         final TopChoiceAugment2Builder tca2Builder = new TopChoiceAugment2Builder();
538         final Case11Builder c11Builder = new Case11Builder();
539         final Case11ChoiceCaseContainerBuilder cccc1Builder = new Case11ChoiceCaseContainerBuilder();
540         cccc1Builder.setCase11ChoiceCaseLeaf("leaf-value");
541         c11Builder.setCase11ChoiceCaseContainer(cccc1Builder.build());
542         tca2Builder.setAugmentChoice2(c11Builder.build());
543         c1Builder.addAugmentation(TopChoiceAugment2.class, tca2Builder.build());
544         tca1Builder.setAugmentChoice1(c1Builder.build());
545         tBuilder.addAugmentation(TopChoiceAugment1.class, tca1Builder.build());
546         final Top top = tBuilder.build();
547
548         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> biResult = codecContext.toNormalizedNode(
549             InstanceIdentifier.create(Top.class), top);
550
551         final NormalizedNode<?, ?> topNormalized =
552                 containerBuilder().withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
553                 .withChild(augmentationBuilder().withNodeIdentifier(aug1Id)
554                         .withChild(choiceBuilder().withNodeIdentifier(augmentChoice1Id)
555                                 .withChild(augmentationBuilder().withNodeIdentifier(aug2Id)
556                                         .withChild(choiceBuilder().withNodeIdentifier(augmentChoice2Id)
557                                                 .withChild(containerBuilder().withNodeIdentifier(containerId)
558                                                         .withChild(leafNode(leafQName, "leaf-value"))
559                                                         .build())
560                                                 .build())
561                                         .build())
562                                 .build())
563                         .build()).build();
564
565         assertEquals(BI_TOP_PATH, biResult.getKey());
566         assertEquals(topNormalized, biResult.getValue());
567
568         final Entry<InstanceIdentifier<?>, DataObject> baResult = codecContext.fromNormalizedNode(BI_TOP_PATH,
569             topNormalized);
570
571         assertEquals(InstanceIdentifier.create(Top.class), baResult.getKey());
572         assertEquals(top, baResult.getValue());
573     }
574 }