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