Improve BindingNormalizedNodeSerializer API
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / mdsal / binding / dom / codec / impl / AugmentationSubstitutionTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
9
10 import static org.junit.Assert.assertEquals;
11
12 import org.junit.Test;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.RpcComplexUsesAugment;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.RpcComplexUsesAugmentBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugmentBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.complex.from.grouping.ContainerWithUsesBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22
23 public class AugmentationSubstitutionTest extends AbstractBindingCodecTest {
24     private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
25     private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier.builder(Top.class)
26             .child(TopLevelList.class, TOP_FOO_KEY).build();
27
28     @Test
29     public void augmentationInGroupingSubstituted() {
30         final TopLevelList baRpc = new TopLevelListBuilder()
31             .withKey(TOP_FOO_KEY)
32             .addAugmentation(new RpcComplexUsesAugmentBuilder(createComplexData()).build())
33             .build();
34         final TopLevelList baTree = new TopLevelListBuilder()
35             .withKey(TOP_FOO_KEY)
36             .addAugmentation(new TreeComplexUsesAugmentBuilder(createComplexData()).build())
37             .build();
38         final var domTreeEntry = codecContext.toNormalizedDataObject(BA_TOP_LEVEL_LIST, baTree).node();
39         final var domRpcEntry = codecContext.toNormalizedDataObject(BA_TOP_LEVEL_LIST, baRpc).node();
40         assertEquals(domTreeEntry, domRpcEntry);
41     }
42
43     @Test
44     public void copyBuilderWithAugmenationsTest() {
45         final TopLevelList manuallyConstructed = new TopLevelListBuilder()
46             .withKey(TOP_FOO_KEY)
47             .addAugmentation(new TreeComplexUsesAugmentBuilder(createComplexData()).build())
48             .build();
49
50         final var result = codecContext.toNormalizedDataObject(BA_TOP_LEVEL_LIST, manuallyConstructed);
51         final TopLevelList deserialized =
52             (TopLevelList) codecContext.fromNormalizedNode(result.path(), result.node()).getValue();
53         assertEquals(manuallyConstructed, deserialized);
54         final TopLevelList copiedFromDeserialized = new TopLevelListBuilder(deserialized).build();
55         assertEquals(manuallyConstructed, copiedFromDeserialized);
56     }
57
58     private static RpcComplexUsesAugment createComplexData() {
59         return new RpcComplexUsesAugmentBuilder()
60                 .setContainerWithUses(new ContainerWithUsesBuilder().setLeafFromGrouping("foo").build())
61                 .build();
62     }
63 }