Merge branch 'blueprint' from controller
[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 com.google.common.collect.Maps;
26 import java.util.ArrayList;
27 import java.util.Collections;
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.DataContainerNodeAttrBuilder;
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(new YangInstanceIdentifier.NodeIdentifierWithPredicates(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                     .withChild(mapNodeBuilder(TOP_LEVEL_LIST_QNAME).build()).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 = Maps.newHashMap();
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 = Maps.newHashMap();
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 DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder =
219                 ImmutableContainerNodeBuilder.create();
220
221         for (AugmentationNode augmentationNode : augChild) {
222             builder.withChild(augmentationNode);
223         }
224         return builder.withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
225                     .withChild(mapNodeBuilder(TOP_LEVEL_LIST_QNAME).build()).build();
226     }
227
228     private static Top topWithAugments(
229             final Map<Class<? extends Augmentation<Top>>, ? extends Augmentation<Top>> augments) {
230         final TopBuilder topBuilder = new TopBuilder();
231         for (Entry<Class<? extends Augmentation<Top>>, ? extends Augmentation<Top>> augment : augments.entrySet()) {
232             topBuilder.addAugmentation(augment.getKey(), augment.getValue());
233         }
234         return topBuilder.setTopLevelList(Collections.<TopLevelList>emptyList()).build();
235     }
236
237     @Test
238     public void listWithKeysToNormalized() {
239         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = registry.toNormalizedNode(BA_TOP_LEVEL_LIST,
240             topLevelList(TOP_LEVEL_LIST_FOO_KEY));
241         final MapEntryNode topLevelListNormalized = ImmutableMapEntryNodeBuilder.create()
242                 .withNodeIdentifier(new NodeIdentifierWithPredicates(TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME,
243                     TOP_LEVEL_LIST_FOO_KEY_VALUE))
244                 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
245                 .build();
246         assertEquals(topLevelListNormalized, entry.getValue());
247     }
248
249     @Test
250     public void listWithKeysFromNormalized() {
251         final MapEntryNode topLevelListNormalized = ImmutableMapEntryNodeBuilder.create()
252                 .withNodeIdentifier(new NodeIdentifierWithPredicates(TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME,
253                     TOP_LEVEL_LIST_FOO_KEY_VALUE))
254                 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
255                 .build();
256         final Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_LEVEL_LIST_FOO_PATH,
257             topLevelListNormalized);
258         assertEquals(topLevelList(TOP_LEVEL_LIST_FOO_KEY), entry.getValue());
259     }
260
261     @Test
262     public void leafOnlyAugmentationToNormalized() {
263         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = registry.toNormalizedNode(BA_TREE_LEAF_ONLY,
264             new TreeLeafOnlyAugmentBuilder().setSimpleValue("simpleValue").build());
265         final Set<QName> augmentationChildren = new HashSet<>();
266         augmentationChildren.add(SIMPLE_VALUE_QNAME);
267         final AugmentationNode augmentationNode = ImmutableAugmentationNodeBuilder.create()
268                 .withNodeIdentifier(new AugmentationIdentifier(augmentationChildren))
269                 .withChild(leafNode(SIMPLE_VALUE_QNAME, "simpleValue"))
270                 .build();
271         assertEquals(augmentationNode, entry.getValue());
272     }
273
274     @Test
275     public void leafOnlyAugmentationFromNormalized() {
276         final Set<QName> augmentationChildren = new HashSet<>();
277         augmentationChildren.add(SIMPLE_VALUE_QNAME);
278         final AugmentationNode augmentationNode = ImmutableAugmentationNodeBuilder.create()
279                 .withNodeIdentifier(new AugmentationIdentifier(augmentationChildren))
280                 .withChild(leafNode(SIMPLE_VALUE_QNAME, "simpleValue"))
281                 .build();
282         final Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(
283             BI_TOP_LEVEL_LIST_FOO_PATH.node(new AugmentationIdentifier(augmentationChildren)),
284             augmentationNode);
285         assertEquals(new TreeLeafOnlyAugmentBuilder().setSimpleValue("simpleValue").build(), entry.getValue());
286     }
287
288     @Test
289     public void orderedleafListToNormalized() {
290         List<String> topLevelLeafList = new ArrayList<>();
291         topLevelLeafList.add("foo");
292         Top top = new TopBuilder().setTopLevelOrderedLeafList(topLevelLeafList).build();
293
294         Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = registry.toNormalizedNode(
295             InstanceIdentifier.create(Top.class), top);
296         ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
297                 .withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
298                 .withChild(ImmutableOrderedLeafSetNodeBuilder.create()
299                         .withNodeIdentifier(new NodeIdentifier(TOP_LEVEL_ORDERED_LEAF_LIST_QNAME))
300                         .withChild(
301                                 ImmutableLeafSetEntryNodeBuilder.create()
302                                         .withNodeIdentifier(new NodeWithValue<>(TOP_LEVEL_ORDERED_LEAF_LIST_QNAME,
303                                                 "foo"))
304                                         .withValue("foo")
305                                         .build())
306                         .build())
307                 .build();
308         assertEquals(containerNode, entry.getValue());
309     }
310
311     @Test
312     public void leafListToNormalized() {
313         final List<String> topLevelLeafList = new ArrayList<>();
314         topLevelLeafList.add("foo");
315         final Top top = new TopBuilder().setTopLevelLeafList(topLevelLeafList).build();
316
317         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = registry.toNormalizedNode(
318             InstanceIdentifier.create(Top.class), top);
319         final ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
320                 .withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
321                 .withChild(ImmutableLeafSetNodeBuilder.create()
322                         .withNodeIdentifier(new NodeIdentifier(TOP_LEVEL_LEAF_LIST_QNAME))
323                         .withChild(
324                                 ImmutableLeafSetEntryNodeBuilder.create()
325                                         .withNodeIdentifier(new NodeWithValue<>(TOP_LEVEL_LEAF_LIST_QNAME, "foo"))
326                                         .withValue("foo")
327                                         .build())
328                         .build())
329                 .build();
330         assertEquals(containerNode, entry.getValue());
331     }
332
333     @Test
334     public void leafListFromNormalized() {
335         final ContainerNode topWithLeafList = ImmutableContainerNodeBuilder.create()
336                 .withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
337                 .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(
338                     TOP_LEVEL_LEAF_LIST_QNAME))
339                     .withChild(ImmutableLeafSetEntryNodeBuilder.create()
340                         .withNodeIdentifier(new NodeWithValue<>(TOP_LEVEL_LEAF_LIST_QNAME, "foo"))
341                         .withValue("foo").build()).build())
342                 .build();
343         final Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_PATH,
344             topWithLeafList);
345         final List<String> topLevelLeafList = new ArrayList<>();
346         topLevelLeafList.add("foo");
347         final Top top = new TopBuilder().setTopLevelLeafList(topLevelLeafList).build();
348         assertEquals(top, entry.getValue());
349     }
350
351     @Test
352     public void orderedLeafListFromNormalized() {
353         ContainerNode topWithLeafList = ImmutableContainerNodeBuilder.create()
354                 .withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
355                 .withChild(ImmutableOrderedLeafSetNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(
356                     TOP_LEVEL_ORDERED_LEAF_LIST_QNAME))
357                     .withChild(ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
358                         new NodeWithValue<>(TOP_LEVEL_ORDERED_LEAF_LIST_QNAME, "foo")).withValue("foo").build())
359                     .build())
360                 .build();
361         Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_PATH, topWithLeafList);
362         List<String> topLevelLeafList = new ArrayList<>();
363         topLevelLeafList.add("foo");
364         Top top = new TopBuilder().setTopLevelOrderedLeafList(topLevelLeafList).build();
365         assertEquals(top, entry.getValue());
366     }
367
368     @Test
369     public void choiceToNormalized() {
370         final ChoiceContainer choiceContainerBA = new ChoiceContainerBuilder().setIdentifier(new ExtendedBuilder()
371             .setExtendedId(new ExtendedIdBuilder().setId("identifier_value").build()).build()).build();
372         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = registry.toNormalizedNode(
373             InstanceIdentifier.create(ChoiceContainer.class), choiceContainerBA);
374         final ContainerNode choiceContainer = ImmutableContainerNodeBuilder.create()
375                 .withNodeIdentifier(new NodeIdentifier(CHOICE_CONTAINER_QNAME))
376                 .withChild(ImmutableChoiceNodeBuilder.create()
377                     .withNodeIdentifier(new NodeIdentifier(CHOICE_IDENTIFIER_QNAME))
378                         .withChild(ImmutableContainerNodeBuilder.create()
379                             .withNodeIdentifier(new NodeIdentifier(EXTENDED_ID_QNAME))
380                             .withChild(leafNode(CHOICE_IDENTIFIER_ID_QNAME, "identifier_value")).build()).build())
381                 .build();
382         assertEquals(choiceContainer, entry.getValue());
383     }
384
385     @Test
386     public void test4798() {
387         final QName containerIdentifierQname4798 = Root.QNAME;
388         final QName choiceIdentifierQname4798 = QName.create(containerIdentifierQname4798, "bug4798-choice");
389         final QName nestedListQname4798 = QName.create(containerIdentifierQname4798, "list-in-case");
390         final QName nestedListKeyQname4798 = QName.create(containerIdentifierQname4798, "test-leaf");
391         final QName nestedContainerValidQname = QName.create(containerIdentifierQname4798, "case-b-container");
392         final QName nestedContainerOuterQname = QName.create(containerIdentifierQname4798, "outer-container");
393         final QName nestedContainerLeafOuterQname = QName.create(containerIdentifierQname4798,
394                 "leaf-in-outer-container");
395
396         final YangInstanceIdentifier yangInstanceIdentifierOuter = YangInstanceIdentifier.of(
397             containerIdentifierQname4798);
398         final ContainerNode containerNodeOuter = ImmutableContainerNodeBuilder.create()
399                 .withNodeIdentifier(new NodeIdentifier(containerIdentifierQname4798))
400                 .withChild(ImmutableContainerNodeBuilder.create()
401                         .withNodeIdentifier(new NodeIdentifier(nestedContainerOuterQname))
402                         .withChild(leafNode(nestedContainerLeafOuterQname, "bar"))
403                         .build())
404                 .build();
405         final Entry<InstanceIdentifier<?>, DataObject> entryContainer = registry.fromNormalizedNode(
406             yangInstanceIdentifierOuter, containerNodeOuter);
407         assertNotNull(entryContainer.getValue());
408         assertNotNull(entryContainer.getKey());
409
410         final NodeIdentifierWithPredicates nodeIdentifierWithPredicates4798 =
411                 new NodeIdentifierWithPredicates(nestedListQname4798, nestedListKeyQname4798, "foo");
412         final YangInstanceIdentifier yangInstanceIdentifier4798 = YangInstanceIdentifier.of(
413             containerIdentifierQname4798)
414                 .node(choiceIdentifierQname4798)
415                 .node(nestedListQname4798)
416                 .node(nodeIdentifierWithPredicates4798);
417
418         final YangInstanceIdentifier yangInstanceIdentifierValid = YangInstanceIdentifier.of(
419             containerIdentifierQname4798)
420                 .node(choiceIdentifierQname4798)
421                 .node(nestedContainerValidQname)
422                 .node(nestedListQname4798)
423                 .node(nodeIdentifierWithPredicates4798);
424         final ContainerNode containerNodeValid = ImmutableContainerNodeBuilder.create()
425                 .withNodeIdentifier(new NodeIdentifier(containerIdentifierQname4798))
426                 .withChild(ImmutableChoiceNodeBuilder.create()
427                         .withNodeIdentifier(new NodeIdentifier(choiceIdentifierQname4798))
428                         .withChild(ImmutableContainerNodeBuilder.create()
429                                 .withNodeIdentifier(new NodeIdentifier(nestedContainerValidQname))
430                                 .withChild(ImmutableMapNodeBuilder.create()
431                                     .withNodeIdentifier(new NodeIdentifier(nestedListQname4798))
432                                         .withChild(mapEntry(nestedListQname4798, nestedListKeyQname4798, "foo"))
433                                         .withChild(mapEntry(nestedListQname4798, nestedListKeyQname4798, "bar"))
434                                         .build())
435                                 .build())
436                         .build())
437                 .build();
438         try {
439             registry.fromNormalizedNode(yangInstanceIdentifierValid, containerNodeValid);
440             fail("Incorect YangInstanceIdentifier should fail");
441         } catch (IllegalStateException e) {
442             // Expected
443         }
444
445         final ContainerNode containerNode4798 = ImmutableContainerNodeBuilder.create()
446                 .withNodeIdentifier(new NodeIdentifier(containerIdentifierQname4798))
447                 .withChild(ImmutableChoiceNodeBuilder.create()
448                         .withNodeIdentifier(new NodeIdentifier(choiceIdentifierQname4798))
449                         .withChild(ImmutableMapNodeBuilder.create()
450                             .withNodeIdentifier(new NodeIdentifier(nestedListQname4798))
451                             .withChild(mapEntry(nestedListQname4798, nestedListKeyQname4798, "foo"))
452                             .withChild(mapEntry(nestedListQname4798, nestedListKeyQname4798, "bar"))
453                             .build())
454                         .build())
455                 .build();
456         try {
457             registry.fromNormalizedNode(yangInstanceIdentifier4798, containerNode4798);
458             fail("Incorect YangInstanceIdentifier should fail");
459         } catch (IllegalStateException e) {
460             // Expected
461         }
462     }
463
464     @Test
465     public void choiceFromNormalized() {
466         final ContainerNode choiceContainerBI = ImmutableContainerNodeBuilder.create()
467                 .withNodeIdentifier(new NodeIdentifier(CHOICE_CONTAINER_QNAME))
468                 .withChild(ImmutableChoiceNodeBuilder.create()
469                     .withNodeIdentifier(new NodeIdentifier(CHOICE_IDENTIFIER_QNAME))
470                     .withChild(ImmutableContainerNodeBuilder.create()
471                         .withNodeIdentifier(new NodeIdentifier(EXTENDED_ID_QNAME))
472                         .withChild(leafNode(CHOICE_IDENTIFIER_ID_QNAME, "identifier_value")).build()).build())
473                 .build();
474         final Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_CHOICE_CONTAINER_PATH,
475             choiceContainerBI);
476         final ChoiceContainer choiceContainerBA = new ChoiceContainerBuilder().setIdentifier(new ExtendedBuilder()
477             .setExtendedId(new ExtendedIdBuilder().setId("identifier_value").build()).build()).build();
478         assertEquals(choiceContainerBA, entry.getValue());
479     }
480
481     @Test
482     public void orderedLisToNormalized() {
483         final InstanceIdentifier<TopLevelList> ii = BA_TOP_LEVEL_LIST;
484         final List<NestedList> nestedLists = new ArrayList<>();
485         nestedLists.add(new NestedListBuilder().withKey(new NestedListKey("foo")).build());
486         nestedLists.add(new NestedListBuilder().withKey(new NestedListKey("bar")).build());
487         final TopLevelList topLevelList = new TopLevelListBuilder().withKey(TOP_LEVEL_LIST_FOO_KEY).setNestedList(
488             nestedLists).build();
489         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = registry.toNormalizedNode(ii, topLevelList);
490         final MapEntryNode foo = mapEntryBuilder().withNodeIdentifier(new NodeIdentifierWithPredicates(
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(ImmutableOrderedMapNodeBuilder.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(new NodeIdentifierWithPredicates(
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(ImmutableOrderedMapNodeBuilder.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 = registry.fromNormalizedNode(BI_TOP_LEVEL_LIST_FOO_PATH,
510             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(TopChoiceAugment2.class, tca2Builder.build());
542         tca1Builder.setAugmentChoice1(c1Builder.build());
543         tBuilder.addAugmentation(TopChoiceAugment1.class, tca1Builder.build());
544         final Top top = tBuilder.build();
545
546         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> biResult = registry.toNormalizedNode(
547             InstanceIdentifier.create(Top.class), top);
548
549         final NormalizedNode<?, ?> topNormalized =
550                 containerBuilder().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 = registry.fromNormalizedNode(BI_TOP_PATH,
567             topNormalized);
568
569         assertEquals(InstanceIdentifier.create(Top.class), baResult.getKey());
570         assertEquals(top, baResult.getValue());
571     }
572 }