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 java.util.Collection;
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;
32 public abstract class AbstractDataChangeListenerTest {
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";
41 private InMemoryDOMDataStore datastore;
42 private SchemaContext schemaContext;
43 private TestDCLExecutorService dclExecutorService;
46 public final void setup() throws Exception {
47 schemaContext = TestModel.createTestContext();
49 dclExecutorService = new TestDCLExecutorService(
50 SpecialExecutors.newBlockingBoundedFastThreadPool(1, 10, "DCL" ));
52 datastore = new InMemoryDOMDataStore("TEST", dclExecutorService);
53 datastore.onGlobalContextUpdated(schemaContext);
57 public void tearDown() {
58 if( dclExecutorService != null ) {
59 dclExecutorService.shutdownNow();
64 * Create a new test task. The task will operate on the backed database,
65 * and will use the proper background executor service.
67 * @return Test task initialized to clean up {@value #TOP_LEVEL} and its
70 public final DatastoreTestTask newTestTask() {
71 return new DatastoreTestTask(datastore, dclExecutorService).cleanup(DatastoreTestTask
72 .simpleDelete(TOP_LEVEL));
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,
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));
89 protected static DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> top() {
90 return Builders.containerBuilder().withNodeIdentifier(
91 new NodeIdentifier(TestModel.TEST_QNAME));
96 protected static void assertEmpty(final Collection<?> set) {
97 Assert.assertTrue(set.isEmpty());
100 protected static void assertEmpty(final Map<?,?> set) {
101 Assert.assertTrue(set.isEmpty());
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));
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));
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));
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));
129 protected static CollectionNodeBuilder<MapEntryNode, MapNode> topLevelMap() {
130 return ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME);
133 protected static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> nestedMap() {
134 return Builders.orderedMapBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME));
137 public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> topLevelList(
139 return ImmutableNodes.mapEntryBuilder(TestModel.OUTER_LIST_QNAME, NAME_QNAME,
143 public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> nestedList(
145 return ImmutableNodes
146 .mapEntryBuilder(TestModel.INNER_LIST_QNAME, NAME_QNAME, key);
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());
156 final ContainerNode data = top().addChild(
157 topLevelMap().addChild(
158 topLevelList(topName)
159 .addChild(nestedMapBuilder.build()).build())
162 return DatastoreTestTask.simpleWrite(TOP_LEVEL, data);
165 public static final WriteTransactionCustomizer deleteNested(final String topName,
166 final String nestedName) {
167 return DatastoreTestTask.simpleDelete(path(topName, nestedName));