2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.store.impl;
10 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
12 import java.util.Collection;
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;
34 public abstract class AbstractDataChangeListenerTest {
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";
43 private InMemoryDOMDataStore datastore;
44 private SchemaContext schemaContext;
45 private TestDCLExecutorService dclExecutorService;
48 public final void setup() throws Exception {
49 schemaContext = TestModel.createTestContext();
51 dclExecutorService = new TestDCLExecutorService(
52 SpecialExecutors.newBlockingBoundedFastThreadPool(1, 10, "DCL" ));
54 datastore = new InMemoryDOMDataStore("TEST", dclExecutorService);
55 datastore.onGlobalContextUpdated(schemaContext);
59 public void tearDown() {
60 if( dclExecutorService != null ) {
61 dclExecutorService.shutdownNow();
66 * Create a new test task. The task will operate on the backed database,
67 * and will use the proper background executor service.
69 * @return Test task initialized to clean up {@value #TOP_LEVEL} and its
72 public final DatastoreTestTask newTestTask() {
73 return new DatastoreTestTask(datastore, dclExecutorService).cleanup(DatastoreTestTask
74 .simpleDelete(TOP_LEVEL));
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,
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));
91 protected static DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> top() {
92 return Builders.containerBuilder().withNodeIdentifier(
93 new NodeIdentifier(TestModel.TEST_QNAME));
98 protected static void assertEmpty(final Collection<?> set) {
99 Assert.assertTrue(set.isEmpty());
102 protected static void assertEmpty(final Map<?,?> set) {
103 Assert.assertTrue(set.isEmpty());
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));
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));
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));
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));
131 protected static CollectionNodeBuilder<MapEntryNode, MapNode> topLevelMap() {
132 return ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME);
135 protected static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> nestedMap() {
136 return Builders.orderedMapBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME));
139 public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> topLevelList(
141 return ImmutableNodes.mapEntryBuilder(TestModel.OUTER_LIST_QNAME, NAME_QNAME,
145 public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> nestedList(
147 return ImmutableNodes
148 .mapEntryBuilder(TestModel.INNER_LIST_QNAME, NAME_QNAME, key);
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());
158 final ContainerNode data = top().addChild(
159 topLevelMap().addChild(
160 topLevelList(topName)
161 .addChild(nestedMapBuilder.build()).build())
164 return DatastoreTestTask.simpleWrite(TOP_LEVEL, data);
167 public static final WriteTransactionCustomizer deleteNested(final String topName,
168 final String nestedName) {
169 return DatastoreTestTask.simpleDelete(path(topName, nestedName));