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

©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.