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