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