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