Do not emit empty lists to NormalizedNodes
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / mdsal / binding / dom / codec / test / 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.test;
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 = registry.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 = registry.fromNormalizedNode(BI_TOP_PATH, topNormalized);
134         assertEquals(top(), entry.getValue());
135     }
136
137     private static ContainerNode getEmptyTop() {
138         return ImmutableContainerNodeBuilder.create()
139                     .withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
140                     .build();
141     }
142
143     private static final QName AGUMENT_STRING_Q = QName.create(TOP_QNAME, "augmented-string");
144     private static final String AUGMENT_STRING_VALUE = "testingEquals";
145     private static final QName AUGMENT_INT_Q = QName.create(TOP_QNAME, "augmented-int");
146     private static final int AUGMENT_INT_VALUE = 44;
147
148     @Test
149     public void equalsWithAugment() {
150         final ContainerNode topNormalizedWithAugments = getNormalizedTopWithAugments(
151             augmentationBuilder()
152                 .withNodeIdentifier(new AugmentationIdentifier(singleton(AGUMENT_STRING_Q)))
153                 .withChild(ImmutableNodes.leafNode(AGUMENT_STRING_Q, AUGMENT_STRING_VALUE))
154                 .build());
155         final ContainerNode topNormalized = getEmptyTop();
156
157         final Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_PATH, topNormalized);
158         final Entry<InstanceIdentifier<?>, DataObject> entryWithAugments = registry.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 = registry.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 (Entry<Class<? extends Augmentation<Top>>, ? extends Augmentation<Top>> augment : augments.entrySet()) {
231             topBuilder.addAugmentation(augment.getKey(), augment.getValue());
232         }
233         return topBuilder.setTopLevelList(Collections.emptyList()).build();
234     }
235
236     @Test
237     public void listWithKeysToNormalized() {
238         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = registry.toNormalizedNode(BA_TOP_LEVEL_LIST,
239             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 = registry.fromNormalizedNode(BI_TOP_LEVEL_LIST_FOO_PATH,
256             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 = registry.toNormalizedNode(BA_TREE_LEAF_ONLY,
263             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 = registry.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 = registry.toNormalizedNode(
294             InstanceIdentifier.create(Top.class), top);
295         ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
296                 .withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
297                 .withChild(ImmutableOrderedLeafSetNodeBuilder.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 = registry.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 = registry.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(ImmutableOrderedLeafSetNodeBuilder.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 = registry.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 = registry.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 = registry.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             registry.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             registry.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 = registry.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 = registry.toNormalizedNode(ii, topLevelList);
489         final MapEntryNode foo = mapEntryBuilder().withNodeIdentifier(NodeIdentifierWithPredicates.of(
490                 TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
491                 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
492                 .withChild(ImmutableOrderedMapNodeBuilder.create()
493                     .withNodeIdentifier(new NodeIdentifier(NESTED_LIST_QNAME))
494                     .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "foo"))
495                     .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "bar")).build()).build();
496         assertEquals(foo, entry.getValue());
497     }
498
499     @Test
500     public void orderedLisFromNormalized() {
501         final MapEntryNode foo = mapEntryBuilder().withNodeIdentifier(NodeIdentifierWithPredicates.of(
502                 TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
503                 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
504                 .withChild(ImmutableOrderedMapNodeBuilder.create()
505                     .withNodeIdentifier(new NodeIdentifier(NESTED_LIST_QNAME))
506                     .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "foo"))
507                     .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "bar")).build()).build();
508         final Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_LEVEL_LIST_FOO_PATH,
509             foo);
510         final List<NestedList> nestedLists = new ArrayList<>();
511         nestedLists.add(new NestedListBuilder().withKey(new NestedListKey("foo")).build());
512         nestedLists.add(new NestedListBuilder().withKey(new NestedListKey("bar")).build());
513         final TopLevelList topLevelList = new TopLevelListBuilder().withKey(TOP_LEVEL_LIST_FOO_KEY).setNestedList(
514             nestedLists).build();
515         assertEquals(topLevelList, entry.getValue());
516     }
517
518     @Test
519     public void augmentMultipleChoices() {
520         final QName augmentChoice1QName = AugmentChoice1.QNAME;
521         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
522         final QName containerQName = QName.create(augmentChoice1QName, "case11-choice-case-container");
523         final QName leafQName = QName.create(augmentChoice1QName, "case11-choice-case-leaf");
524
525         final AugmentationIdentifier aug1Id = new AugmentationIdentifier(Collections.singleton(augmentChoice1QName));
526         final AugmentationIdentifier aug2Id = new AugmentationIdentifier(Collections.singleton(augmentChoice2QName));
527         final NodeIdentifier augmentChoice1Id = new NodeIdentifier(augmentChoice1QName);
528         final NodeIdentifier augmentChoice2Id = new NodeIdentifier(augmentChoice2QName);
529         final NodeIdentifier containerId = new NodeIdentifier(containerQName);
530
531         final TopBuilder tBuilder = new TopBuilder();
532         final TopChoiceAugment1Builder tca1Builder = new TopChoiceAugment1Builder();
533         final Case1Builder c1Builder = new Case1Builder();
534         final TopChoiceAugment2Builder tca2Builder = new TopChoiceAugment2Builder();
535         final Case11Builder c11Builder = new Case11Builder();
536         final Case11ChoiceCaseContainerBuilder cccc1Builder = new Case11ChoiceCaseContainerBuilder();
537         cccc1Builder.setCase11ChoiceCaseLeaf("leaf-value");
538         c11Builder.setCase11ChoiceCaseContainer(cccc1Builder.build());
539         tca2Builder.setAugmentChoice2(c11Builder.build());
540         c1Builder.addAugmentation(TopChoiceAugment2.class, tca2Builder.build());
541         tca1Builder.setAugmentChoice1(c1Builder.build());
542         tBuilder.addAugmentation(TopChoiceAugment1.class, tca1Builder.build());
543         final Top top = tBuilder.build();
544
545         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> biResult = registry.toNormalizedNode(
546             InstanceIdentifier.create(Top.class), top);
547
548         final NormalizedNode<?, ?> topNormalized =
549                 containerBuilder().withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
550                 .withChild(augmentationBuilder().withNodeIdentifier(aug1Id)
551                         .withChild(choiceBuilder().withNodeIdentifier(augmentChoice1Id)
552                                 .withChild(augmentationBuilder().withNodeIdentifier(aug2Id)
553                                         .withChild(choiceBuilder().withNodeIdentifier(augmentChoice2Id)
554                                                 .withChild(containerBuilder().withNodeIdentifier(containerId)
555                                                         .withChild(leafNode(leafQName, "leaf-value"))
556                                                         .build())
557                                                 .build())
558                                         .build())
559                                 .build())
560                         .build()).build();
561
562         assertEquals(BI_TOP_PATH, biResult.getKey());
563         assertEquals(topNormalized, biResult.getValue());
564
565         final Entry<InstanceIdentifier<?>, DataObject> baResult = registry.fromNormalizedNode(BI_TOP_PATH,
566             topNormalized);
567
568         assertEquals(InstanceIdentifier.create(Top.class), baResult.getKey());
569         assertEquals(top, baResult.getValue());
570     }
571 }