314fc5a463888f26c4baa0020ea623fd1fd90a6d
[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.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer.NodeResult;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.RpcComplexUsesAugment;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.RpcComplexUsesAugmentBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugmentBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.complex.from.grouping.ContainerWithUsesBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23
24 public class AugmentationSubstitutionTest extends AbstractBindingCodecTest {
25     private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
26     private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier.builder(Top.class)
27             .child(TopLevelList.class, TOP_FOO_KEY).build();
28
29     @Test
30     public void augmentationInGroupingSubstituted() {
31         final TopLevelList baRpc = new TopLevelListBuilder()
32             .withKey(TOP_FOO_KEY)
33             .addAugmentation(new RpcComplexUsesAugmentBuilder(createComplexData()).build())
34             .build();
35         final TopLevelList baTree = new TopLevelListBuilder()
36             .withKey(TOP_FOO_KEY)
37             .addAugmentation(new TreeComplexUsesAugmentBuilder(createComplexData()).build())
38             .build();
39         final var domTreeEntry = ((NodeResult) codecContext.toNormalizedNode(BA_TOP_LEVEL_LIST, baTree)).node();
40         final var domRpcEntry = ((NodeResult) codecContext.toNormalizedNode(BA_TOP_LEVEL_LIST, baRpc)).node();
41         assertEquals(domTreeEntry, domRpcEntry);
42     }
43
44     @Test
45     public void copyBuilderWithAugmenationsTest() {
46         final TopLevelList manuallyConstructed = new TopLevelListBuilder()
47             .withKey(TOP_FOO_KEY)
48             .addAugmentation(new TreeComplexUsesAugmentBuilder(createComplexData()).build())
49             .build();
50
51         final var result = (NodeResult) codecContext.toNormalizedNode(BA_TOP_LEVEL_LIST, manuallyConstructed);
52         final TopLevelList deserialized =
53             (TopLevelList) codecContext.fromNormalizedNode(result.path(), result.node()).getValue();
54         assertEquals(manuallyConstructed, deserialized);
55         final TopLevelList copiedFromDeserialized = new TopLevelListBuilder(deserialized).build();
56         assertEquals(manuallyConstructed, copiedFromDeserialized);
57     }
58
59     private static RpcComplexUsesAugment createComplexData() {
60         return new RpcComplexUsesAugmentBuilder()
61                 .setContainerWithUses(new ContainerWithUsesBuilder().setLeafFromGrouping("foo").build())
62                 .build();
63     }
64 }