Refactor AbstractDataTreeChangeListenerTest
[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.spec.reflect.BindingReflections;
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(BindingReflections.getModuleInfo(Top.class),
46                 BindingReflections.getModuleInfo(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             final var writeTx = getDataBroker().newWriteOnlyTransaction();
54             writeTx.put(CONFIGURATION, TOP, top());
55             writeTx.put(CONFIGURATION, TOP_FOO, topLevelList(new TopLevelListKey(TOP_FOO_KEY)));
56             writeTx.put(CONFIGURATION, SIMPLE_AUGMENT, leafOnlyUsesAugment);
57             assertCommit(writeTx.commit());
58
59             collector.assertModifications(added(path(TOP_FOO_KEY, TreeLeafOnlyUsesAugment.class), leafOnlyUsesAugment));
60         }
61     }
62
63     @Test
64     public void leafOnlyAugmentationUpdatedTest() {
65         var writeTx = getDataBroker().newWriteOnlyTransaction();
66         writeTx.put(CONFIGURATION, TOP, top());
67         writeTx.put(CONFIGURATION, TOP_FOO, topLevelList(new TopLevelListKey(TOP_FOO_KEY)));
68         final var leafOnlyUsesAugmentBefore = leafOnlyUsesAugment("test leaf");
69         writeTx.put(CONFIGURATION, SIMPLE_AUGMENT, leafOnlyUsesAugmentBefore);
70         assertCommit(writeTx.commit());
71
72         final var leafOnlyUsesAugmentAfter = leafOnlyUsesAugment("test leaf changed");
73         try (var collector = createCollector(CONFIGURATION, SIMPLE_AUGMENT)) {
74             writeTx = getDataBroker().newWriteOnlyTransaction();
75             writeTx.put(CONFIGURATION, SIMPLE_AUGMENT, leafOnlyUsesAugmentAfter);
76             assertCommit(writeTx.commit());
77
78             collector.assertModifications(
79                 added(path(TOP_FOO_KEY, TreeLeafOnlyUsesAugment.class), leafOnlyUsesAugmentBefore),
80                 replaced(path(TOP_FOO_KEY, TreeLeafOnlyUsesAugment.class), leafOnlyUsesAugmentBefore,
81                     leafOnlyUsesAugmentAfter));
82         }
83     }
84
85     @Test
86     public void leafOnlyAugmentationDeletedTest() {
87         var writeTx = getDataBroker().newWriteOnlyTransaction();
88         writeTx.put(CONFIGURATION, TOP, top());
89         writeTx.put(CONFIGURATION, TOP_FOO, topLevelList(new TopLevelListKey(TOP_FOO_KEY)));
90         final var leafOnlyUsesAugment = leafOnlyUsesAugment("test leaf");
91         writeTx.put(CONFIGURATION, SIMPLE_AUGMENT, leafOnlyUsesAugment);
92         assertCommit(writeTx.commit());
93
94         try (var collector = createCollector(CONFIGURATION, SIMPLE_AUGMENT)) {
95             writeTx = getDataBroker().newWriteOnlyTransaction();
96             writeTx.delete(CONFIGURATION, SIMPLE_AUGMENT);
97             assertCommit(writeTx.commit());
98
99             collector.assertModifications(
100                 added(path(TOP_FOO_KEY, TreeLeafOnlyUsesAugment.class), leafOnlyUsesAugment),
101                 deleted(path(TOP_FOO_KEY, TreeLeafOnlyUsesAugment.class), leafOnlyUsesAugment));
102         }
103     }
104
105     @Test
106     public void complexAugmentationCreatedTest() {
107         try (var collector = createCollector(CONFIGURATION, COMPLEX_AUGMENT)) {
108             final var complexUsesAugment = complexUsesAugment(LIST_VIA_USES_KEY);
109             final var writeTx = getDataBroker().newWriteOnlyTransaction();
110             writeTx.put(CONFIGURATION, TOP, top());
111             writeTx.put(CONFIGURATION, TOP_FOO, topLevelList(new TopLevelListKey(TOP_FOO_KEY)));
112             writeTx.put(CONFIGURATION, COMPLEX_AUGMENT, complexUsesAugment);
113             assertCommit(writeTx.commit());
114
115             collector.assertModifications(added(path(TOP_FOO_KEY, TreeComplexUsesAugment.class), complexUsesAugment));
116         }
117     }
118
119     @Test
120     public void complexAugmentationUpdatedTest() {
121         var writeTx = getDataBroker().newWriteOnlyTransaction();
122         writeTx.put(CONFIGURATION, TOP, top());
123         writeTx.put(CONFIGURATION, TOP_FOO, topLevelList(new TopLevelListKey(TOP_FOO_KEY)));
124         final var complexUsesAugmentBefore = complexUsesAugment(LIST_VIA_USES_KEY);
125         writeTx.put(CONFIGURATION, COMPLEX_AUGMENT, complexUsesAugmentBefore);
126         assertCommit(writeTx.commit());
127
128         try (var collector = createCollector(CONFIGURATION, COMPLEX_AUGMENT)) {
129             final var complexUsesAugmentAfter = complexUsesAugment(LIST_VIA_USES_KEY_MOD);
130             writeTx = getDataBroker().newWriteOnlyTransaction();
131             writeTx.put(CONFIGURATION, COMPLEX_AUGMENT, complexUsesAugmentAfter);
132             assertCommit(writeTx.commit());
133
134             collector.assertModifications(
135                 added(path(TOP_FOO_KEY, TreeComplexUsesAugment.class), complexUsesAugmentBefore),
136                 // While we are overwriting the augment, at the transaction ends up replacing one child with another,
137                 // so the Augmentation ends up not being overwritten, but modified
138                 subtreeModified(path(TOP_FOO_KEY, TreeComplexUsesAugment.class), complexUsesAugmentBefore,
139                         complexUsesAugmentAfter));
140         }
141     }
142 }