Refactor AbstractDataTreeChangeListenerTest
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / Bug1333DataChangeListenerTest.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.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.USES_ONE_KEY;
12 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.USES_TWO_KEY;
13 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.complexUsesAugment;
14 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.path;
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
18 import com.google.common.collect.ImmutableSet;
19 import java.util.Set;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataTreeChangeListenerTest;
22 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
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.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
29
30 /**
31  * This testsuite tries to replicate bug 1333 and tests regresion of it  using test-model with similar construction as
32  * one reported.
33  *
34  * <p>
35  * See  https://bugs.opendaylight.org/show_bug.cgi?id=1333 for Bug Description
36  */
37 public class Bug1333DataChangeListenerTest extends AbstractDataTreeChangeListenerTest {
38     private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.create(Top.class);
39     private static final InstanceIdentifier<TreeComplexUsesAugment> AUGMENT_WILDCARD =
40             TOP_PATH.child(TopLevelList.class).augmentation(TreeComplexUsesAugment.class);
41
42     @Override
43     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
44         return ImmutableSet.of(BindingReflections.getModuleInfo(Top.class),
45                 BindingReflections.getModuleInfo(TreeComplexUsesAugment.class));
46     }
47
48     private static Top topWithListItem() {
49         return top(topLevelList(TOP_FOO_KEY, complexUsesAugment(USES_ONE_KEY, USES_TWO_KEY)));
50     }
51
52     public Top writeTopWithListItem(final LogicalDatastoreType store) {
53         var tx = getDataBroker().newWriteOnlyTransaction();
54         var topItem = topWithListItem();
55         tx.put(store, TOP_PATH, topItem);
56         assertCommit(tx.commit());
57         return topItem;
58     }
59
60     public void deleteItem(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
61         var tx = getDataBroker().newWriteOnlyTransaction();
62         tx.delete(store, path);
63         assertCommit(tx.commit());
64     }
65
66     @Test
67     public void writeTopWithListItemAugmentedListenTopSubtree() {
68         try (var collector = createCollector(LogicalDatastoreType.CONFIGURATION, TOP_PATH)) {
69             writeTopWithListItem(LogicalDatastoreType.CONFIGURATION);
70
71             collector.assertModifications(added(TOP_PATH, topWithListItem()));
72         }
73     }
74
75     @Test
76     public void writeTopWithListItemAugmentedListenAugmentSubtreeWildcarded() {
77         try (var collector = createCollector(LogicalDatastoreType.CONFIGURATION, AUGMENT_WILDCARD)) {
78             writeTopWithListItem(LogicalDatastoreType.CONFIGURATION);
79
80             collector.assertModifications(
81                 added(path(TOP_FOO_KEY, TreeComplexUsesAugment.class), complexUsesAugment(USES_ONE_KEY, USES_TWO_KEY)));
82         }
83     }
84
85     @Test
86     public void deleteAugmentChildListenTopSubtree() {
87         final var top = writeTopWithListItem(LogicalDatastoreType.CONFIGURATION);
88
89         try (var collector = createCollector(LogicalDatastoreType.CONFIGURATION, TOP_PATH)) {
90             deleteItem(LogicalDatastoreType.CONFIGURATION, path(TOP_FOO_KEY, USES_ONE_KEY));
91
92             collector.assertModifications(
93                 added(TOP_PATH, top),
94                 subtreeModified(TOP_PATH, top, top(topLevelList(TOP_FOO_KEY, complexUsesAugment(USES_TWO_KEY)))));
95         }
96     }
97
98     @Test
99     public void deleteAugmentChildListenAugmentSubtreeWildcarded() {
100         writeTopWithListItem(LogicalDatastoreType.CONFIGURATION);
101
102         try (var collector = createCollector(LogicalDatastoreType.CONFIGURATION, AUGMENT_WILDCARD)) {
103             deleteItem(LogicalDatastoreType.CONFIGURATION, path(TOP_FOO_KEY, USES_ONE_KEY));
104
105             collector.assertModifications(
106                 added(path(TOP_FOO_KEY, TreeComplexUsesAugment.class), complexUsesAugment(USES_ONE_KEY, USES_TWO_KEY)),
107                 subtreeModified(path(TOP_FOO_KEY, TreeComplexUsesAugment.class),
108                     complexUsesAugment(USES_ONE_KEY, USES_TWO_KEY), complexUsesAugment(USES_TWO_KEY)));
109         }
110     }
111 }