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