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.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;
39 public abstract class AbstractDataTreeChangeListenerTest {
41 protected static final YangInstanceIdentifier TOP_LEVEL = YangInstanceIdentifier
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";
48 private InMemoryDOMDataStore datastore;
49 private SchemaContext schemaContext;
50 private TestDCLExecutorService dclExecutorService;
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();
60 this.dclExecutorService = new TestDCLExecutorService(
61 SpecialExecutors.newBlockingBoundedFastThreadPool(1, 10, "DCL",
62 AbstractDataTreeChangeListenerTest.class));
64 this.datastore = new InMemoryDOMDataStore("TEST", this.dclExecutorService);
65 this.datastore.onGlobalContextUpdated(this.schemaContext);
69 public void tearDown() {
70 if (this.dclExecutorService != null) {
71 this.dclExecutorService.shutdownNow();
76 * Create a new test task. The task will operate on the backed database,
77 * and will use the proper background executor service.
79 * @return Test task initialized to clean up {@value #TOP_LEVEL} and its
82 public final DatastoreTestTask newTestTask() {
83 return new DatastoreTestTask(this.datastore, this.dclExecutorService).cleanup(DatastoreTestTask
84 .simpleDelete(TOP_LEVEL));
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,
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));
101 protected static DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> top() {
102 return Builders.containerBuilder().withNodeIdentifier(
103 new NodeIdentifier(Top.QNAME));
108 protected static void assertEmpty(final Collection<?> set) {
109 Assert.assertTrue(set.isEmpty());
112 protected static void assertEmpty(final Map<?,?> set) {
113 Assert.assertTrue(set.isEmpty());
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));
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));
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));
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));
141 protected static CollectionNodeBuilder<MapEntryNode, MapNode> topLevelMap() {
142 return ImmutableNodes.mapNodeBuilder(TopLevelList.QNAME);
145 protected static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> nestedMap() {
146 return Builders.orderedMapBuilder().withNodeIdentifier(new NodeIdentifier(NestedList.QNAME));
149 public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> topLevelList(
151 return ImmutableNodes.mapEntryBuilder(TopLevelList.QNAME, NAME_QNAME,
155 public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> nestedList(
157 return ImmutableNodes
158 .mapEntryBuilder(NestedList.QNAME, NAME_QNAME, key);
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());
168 final ContainerNode data = top().addChild(
169 topLevelMap().addChild(
170 topLevelList(topName)
171 .addChild(nestedMapBuilder.build()).build())
174 return DatastoreTestTask.simpleWrite(TOP_LEVEL, data);
177 public static final WriteTransactionCustomizer deleteNested(final String topName,
178 final String nestedName) {
179 return DatastoreTestTask.simpleDelete(path(topName, nestedName));