Hide BindingReflections.getModuleInfo()
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / Bug1418AugmentationTest.java
1 /*
2  * Copyright (c) 2014, 2015 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.adapter;
9
10 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
11 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.complexUsesAugment;
12 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.leafOnlyUsesAugment;
13 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.path;
14 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.top;
15 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.topLevelList;
16 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
17
18 import java.util.Set;
19 import org.junit.Test;
20 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataTreeChangeListenerTest;
21 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyUsesAugment;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.complex.from.grouping.ListViaUsesKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
30
31 public class Bug1418AugmentationTest extends AbstractDataTreeChangeListenerTest {
32     private static final InstanceIdentifier<Top> TOP = InstanceIdentifier.create(Top.class);
33     private static final InstanceIdentifier<TopLevelList> TOP_FOO = TOP.child(TopLevelList.class, TOP_FOO_KEY);
34     private static final InstanceIdentifier<TreeLeafOnlyUsesAugment> SIMPLE_AUGMENT =
35             TOP.child(TopLevelList.class, TOP_FOO_KEY).augmentation(TreeLeafOnlyUsesAugment.class);
36     private static final InstanceIdentifier<TreeComplexUsesAugment> COMPLEX_AUGMENT =
37             TOP.child(TopLevelList.class, TOP_FOO_KEY).augmentation(TreeComplexUsesAugment.class);
38     private static final ListViaUsesKey LIST_VIA_USES_KEY =
39             new ListViaUsesKey("list key");
40     private static final ListViaUsesKey LIST_VIA_USES_KEY_MOD =
41             new ListViaUsesKey("list key modified");
42
43     @Override
44     protected Set<YangModuleInfo> getModuleInfos() {
45         return Set.of(BindingRuntimeHelpers.getYangModuleInfo(Top.class),
46             BindingRuntimeHelpers.getYangModuleInfo(TreeComplexUsesAugment.class));
47     }
48
49     @Test
50     public void leafOnlyAugmentationCreatedTest() {
51         final var leafOnlyUsesAugment = leafOnlyUsesAugment("test leaf");
52         try (var collector = createCollector(CONFIGURATION, SIMPLE_AUGMENT)) {
53             collector.verifyModifications();
54
55             final var writeTx = getDataBroker().newWriteOnlyTransaction();
56             writeTx.put(CONFIGURATION, TOP, top());
57             writeTx.put(CONFIGURATION, TOP_FOO, topLevelList(new TopLevelListKey(TOP_FOO_KEY)));
58             writeTx.put(CONFIGURATION, SIMPLE_AUGMENT, leafOnlyUsesAugment);
59             assertCommit(writeTx.commit());
60
61             collector.verifyModifications(
62                 added(path(TOP_FOO_KEY, TreeLeafOnlyUsesAugment.class), leafOnlyUsesAugment));
63         }
64     }
65
66     @Test
67     public void leafOnlyAugmentationUpdatedTest() {
68         var writeTx = getDataBroker().newWriteOnlyTransaction();
69         writeTx.put(CONFIGURATION, TOP, top());
70         writeTx.put(CONFIGURATION, TOP_FOO, topLevelList(new TopLevelListKey(TOP_FOO_KEY)));
71         final var leafOnlyUsesAugmentBefore = leafOnlyUsesAugment("test leaf");
72         writeTx.put(CONFIGURATION, SIMPLE_AUGMENT, leafOnlyUsesAugmentBefore);
73         assertCommit(writeTx.commit());
74
75         final var leafOnlyUsesAugmentAfter = leafOnlyUsesAugment("test leaf changed");
76         try (var collector = createCollector(CONFIGURATION, SIMPLE_AUGMENT)) {
77             collector.verifyModifications(
78                 added(path(TOP_FOO_KEY, TreeLeafOnlyUsesAugment.class), leafOnlyUsesAugmentBefore));
79
80             writeTx = getDataBroker().newWriteOnlyTransaction();
81             writeTx.put(CONFIGURATION, SIMPLE_AUGMENT, leafOnlyUsesAugmentAfter);
82             assertCommit(writeTx.commit());
83
84             collector.verifyModifications(
85                 replaced(path(TOP_FOO_KEY, TreeLeafOnlyUsesAugment.class), leafOnlyUsesAugmentBefore,
86                     leafOnlyUsesAugmentAfter));
87         }
88     }
89
90     @Test
91     public void leafOnlyAugmentationDeletedTest() {
92         var writeTx = getDataBroker().newWriteOnlyTransaction();
93         writeTx.put(CONFIGURATION, TOP, top());
94         writeTx.put(CONFIGURATION, TOP_FOO, topLevelList(new TopLevelListKey(TOP_FOO_KEY)));
95         final var leafOnlyUsesAugment = leafOnlyUsesAugment("test leaf");
96         writeTx.put(CONFIGURATION, SIMPLE_AUGMENT, leafOnlyUsesAugment);
97         assertCommit(writeTx.commit());
98
99         try (var collector = createCollector(CONFIGURATION, SIMPLE_AUGMENT)) {
100             collector.verifyModifications(
101                 added(path(TOP_FOO_KEY, TreeLeafOnlyUsesAugment.class), leafOnlyUsesAugment));
102
103             writeTx = getDataBroker().newWriteOnlyTransaction();
104             writeTx.delete(CONFIGURATION, SIMPLE_AUGMENT);
105             assertCommit(writeTx.commit());
106
107             collector.verifyModifications(
108                 deleted(path(TOP_FOO_KEY, TreeLeafOnlyUsesAugment.class), leafOnlyUsesAugment));
109         }
110     }
111
112     @Test
113     public void complexAugmentationCreatedTest() {
114         try (var collector = createCollector(CONFIGURATION, COMPLEX_AUGMENT)) {
115             final var complexUsesAugment = complexUsesAugment(LIST_VIA_USES_KEY);
116             final var writeTx = getDataBroker().newWriteOnlyTransaction();
117             writeTx.put(CONFIGURATION, TOP, top());
118             writeTx.put(CONFIGURATION, TOP_FOO, topLevelList(new TopLevelListKey(TOP_FOO_KEY)));
119             writeTx.put(CONFIGURATION, COMPLEX_AUGMENT, complexUsesAugment);
120             assertCommit(writeTx.commit());
121
122             collector.verifyModifications(
123                 added(path(TOP_FOO_KEY, TreeComplexUsesAugment.class), complexUsesAugment));
124         }
125     }
126
127     @Test
128     public void complexAugmentationUpdatedTest() {
129         var writeTx = getDataBroker().newWriteOnlyTransaction();
130         writeTx.put(CONFIGURATION, TOP, top());
131         writeTx.put(CONFIGURATION, TOP_FOO, topLevelList(new TopLevelListKey(TOP_FOO_KEY)));
132         final var complexUsesAugmentBefore = complexUsesAugment(LIST_VIA_USES_KEY);
133         writeTx.put(CONFIGURATION, COMPLEX_AUGMENT, complexUsesAugmentBefore);
134         assertCommit(writeTx.commit());
135
136         try (var collector = createCollector(CONFIGURATION, COMPLEX_AUGMENT)) {
137             collector.verifyModifications(
138                 added(path(TOP_FOO_KEY, TreeComplexUsesAugment.class), complexUsesAugmentBefore));
139
140             final var complexUsesAugmentAfter = complexUsesAugment(LIST_VIA_USES_KEY_MOD);
141             writeTx = getDataBroker().newWriteOnlyTransaction();
142             writeTx.put(CONFIGURATION, COMPLEX_AUGMENT, complexUsesAugmentAfter);
143             assertCommit(writeTx.commit());
144
145             collector.verifyModifications(
146                 // While we are overwriting the augment, at the transaction ends up replacing one child with another,
147                 // so the Augmentation ends up not being overwritten, but modified
148                 subtreeModified(path(TOP_FOO_KEY, TreeComplexUsesAugment.class), complexUsesAugmentBefore,
149                         complexUsesAugmentAfter));
150         }
151     }
152 }