Bump to odlparent-6.0.1/yangtools-4.0.2/mdsal-5.0.4
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / test / java / org / opendaylight / controller / md / sal / dom / store / impl / AbstractDataTreeChangeListenerTest.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.controller.md.sal.dom.store.impl;
9
10 import java.util.Collection;
11 import java.util.Map;
12 import org.junit.After;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.opendaylight.controller.md.sal.dom.store.impl.DatastoreTestTask.WriteTransactionCustomizer;
16 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
17 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelList;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.top.level.list.NestedList;
22 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
23 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
32 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37
38 public abstract class AbstractDataTreeChangeListenerTest {
39
40     protected static final YangInstanceIdentifier TOP_LEVEL = YangInstanceIdentifier
41             .of(Top.QNAME);
42     private static final QName NAME_QNAME = QName.create(Top.QNAME, "name");
43     protected static final String FOO = "foo";
44     protected static final String BAR = "bar";
45     protected static final String BAZ = "baz";
46
47     private InMemoryDOMDataStore datastore;
48     private SchemaContext schemaContext;
49     private TestDCLExecutorService dclExecutorService;
50
51     @Before
52     public final void setup() throws Exception {
53         final YangModuleInfo moduleInfo = BindingReflections
54                 .getModuleInfo(TwoLevelList.class);
55         final ModuleInfoBackedContext context = ModuleInfoBackedContext.create();
56         context.registerModuleInfo(moduleInfo);
57         this.schemaContext = context.tryToCreateSchemaContext().get();
58
59         this.dclExecutorService = new TestDCLExecutorService(
60                 SpecialExecutors.newBlockingBoundedFastThreadPool(1, 10, "DCL",
61                     AbstractDataTreeChangeListenerTest.class));
62
63         this.datastore = new InMemoryDOMDataStore("TEST", this.dclExecutorService);
64         this.datastore.onGlobalContextUpdated(this.schemaContext);
65     }
66
67     @After
68     public void tearDown() {
69         if (this.dclExecutorService != null) {
70             this.dclExecutorService.shutdownNow();
71         }
72     }
73
74     /**
75      * Create a new test task. The task will operate on the backed database,
76      * and will use the proper background executor service.
77      *
78      * @return Test task initialized to clean up {@value #TOP_LEVEL} and its
79      *         children.
80      */
81     public final DatastoreTestTask newTestTask() {
82         return new DatastoreTestTask(this.datastore, this.dclExecutorService).cleanup(DatastoreTestTask
83                 .simpleDelete(TOP_LEVEL));
84     }
85
86
87     public static final YangInstanceIdentifier path(final String topName,
88             final String nestedName) {
89         return path(topName).node(NestedList.QNAME).node(
90                 NodeIdentifierWithPredicates.of(NestedList.QNAME, NAME_QNAME,
91                         nestedName));
92     }
93
94     public static final YangInstanceIdentifier path(final String topName) {
95         return TOP_LEVEL.node(TopLevelList.QNAME).node(
96                 NodeIdentifierWithPredicates.of(TopLevelList.QNAME,
97                         NAME_QNAME, topName));
98     }
99
100     protected static DataContainerNodeBuilder<NodeIdentifier, ContainerNode> top() {
101         return Builders.containerBuilder().withNodeIdentifier(
102                 new NodeIdentifier(Top.QNAME));
103     }
104
105
106
107     protected static void assertEmpty(final Collection<?> set) {
108         Assert.assertTrue(set.isEmpty());
109     }
110
111     protected static void assertEmpty(final Map<?,?> set) {
112         Assert.assertTrue(set.isEmpty());
113     }
114
115     protected static <K> void assertContains(final Collection<K> set, final K... values) {
116         for (final K key : values) {
117             Assert.assertTrue(set.contains(key));
118         }
119
120     }
121
122     protected static <K> void assertContains(final Map<K,?> map, final K... values) {
123         for (final K key : values) {
124             Assert.assertTrue(map.containsKey(key));
125         }
126     }
127
128     protected static <K> void assertNotContains(final Collection<K> set, final K... values) {
129         for (final K key : values) {
130             Assert.assertFalse(set.contains(key));
131         }
132     }
133
134     protected static <K> void assertNotContains(final Map<K,?> map, final K... values) {
135         for (final K key : values) {
136             Assert.assertFalse(map.containsKey(key));
137         }
138     }
139
140     protected static CollectionNodeBuilder<MapEntryNode, MapNode> topLevelMap() {
141         return ImmutableNodes.mapNodeBuilder(TopLevelList.QNAME);
142     }
143
144     protected static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> nestedMap() {
145         return Builders.orderedMapBuilder().withNodeIdentifier(new NodeIdentifier(NestedList.QNAME));
146     }
147
148     public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> topLevelList(
149             final String key) {
150         return ImmutableNodes.mapEntryBuilder(TopLevelList.QNAME, NAME_QNAME,
151                 key);
152     }
153
154     public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> nestedList(
155             final String key) {
156         return ImmutableNodes
157                 .mapEntryBuilder(NestedList.QNAME, NAME_QNAME, key);
158     }
159
160     public static final WriteTransactionCustomizer writeOneTopMultipleNested(
161             final String topName, final String... nestedName) {
162         final CollectionNodeBuilder<MapEntryNode, OrderedMapNode> nestedMapBuilder = nestedMap();
163         for (final String nestedItem : nestedName) {
164             nestedMapBuilder.addChild(nestedList(nestedItem).build());
165         }
166
167         final ContainerNode data = top().addChild(
168                 topLevelMap().addChild(
169                         topLevelList(topName)
170                                 .addChild(nestedMapBuilder.build()).build())
171                         .build()).build();
172
173         return DatastoreTestTask.simpleWrite(TOP_LEVEL, data);
174     }
175
176     public static final  WriteTransactionCustomizer deleteNested(final String topName,
177             final String nestedName) {
178         return DatastoreTestTask.simpleDelete(path(topName, nestedName));
179     }
180 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.