8d329624219916de411dec97d0c6e5c3e930a0bb
[controller.git] / opendaylight / md-sal / sal-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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelList;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
19 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;
20 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
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;
38
39 public abstract class AbstractDataChangeListenerTest {
40
41     protected static final YangInstanceIdentifier TOP_LEVEL = YangInstanceIdentifier
42             .of(Top.QNAME);
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";
47
48     private InMemoryDOMDataStore datastore;
49     private SchemaContext schemaContext;
50     private TestDCLExecutorService dclExecutorService;
51
52     @Before
53     public final void setup() throws Exception {
54         YangModuleInfo moduleInfo = BindingReflections
55                 .getModuleInfo(TwoLevelList.class);
56         ModuleInfoBackedContext context = ModuleInfoBackedContext.create();
57         context.registerModuleInfo(moduleInfo);
58         schemaContext = context.tryToCreateSchemaContext().get();
59
60         dclExecutorService = new TestDCLExecutorService(
61                 SpecialExecutors.newBlockingBoundedFastThreadPool(1, 10, "DCL" ));
62
63         datastore = new InMemoryDOMDataStore("TEST", dclExecutorService);
64         datastore.onGlobalContextUpdated(schemaContext);
65     }
66
67     @After
68     public void tearDown() {
69         if( dclExecutorService != null ) {
70             dclExecutorService.shutdownNow();
71         }
72     }
73
74     /**
75      * Create a new test task. The task will operate on the backed database,
76      * and will use the proper background executor service.
77      *
78      * @return Test task initialized to clean up {@value #TOP_LEVEL} and its
79      *         children.
80      */
81     public final DatastoreTestTask newTestTask() {
82         return new DatastoreTestTask(datastore, dclExecutorService).cleanup(DatastoreTestTask
83                 .simpleDelete(TOP_LEVEL));
84     }
85
86
87     public static final YangInstanceIdentifier path(final String topName,
88             final String nestedName) {
89         return path(topName).node(NestedList.QNAME).node(
90                 new NodeIdentifierWithPredicates(NestedList.QNAME, NAME_QNAME,
91                         nestedName));
92     }
93
94     public static final YangInstanceIdentifier path(final String topName) {
95         return TOP_LEVEL.node(TopLevelList.QNAME).node(
96                 new NodeIdentifierWithPredicates(TopLevelList.QNAME,
97                         NAME_QNAME, topName));
98     }
99
100     protected static DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> top() {
101         return Builders.containerBuilder().withNodeIdentifier(
102                 new NodeIdentifier(Top.QNAME));
103     }
104
105
106
107     protected static void assertEmpty(final Collection<?> set) {
108         Assert.assertTrue(set.isEmpty());
109     }
110
111     protected static void assertEmpty(final Map<?,?> set) {
112         Assert.assertTrue(set.isEmpty());
113     }
114
115     protected static <K> void assertContains(final Collection<K> set, final K... values) {
116         for (K key : values) {
117             Assert.assertTrue(set.contains(key));
118         }
119
120     }
121
122     protected static <K> void assertNotContains(final Collection<K> set, final K... values) {
123         for (K key : values) {
124             Assert.assertFalse(set.contains(key));
125         }
126     }
127
128     protected static <K> void assertContains(final Map<K,?> map, final K... values) {
129         for (K key : values) {
130             Assert.assertTrue(map.containsKey(key));
131         }
132     }
133
134     protected static <K> void assertNotContains(final Map<K,?> map, final K... values) {
135         for (K key : values) {
136             Assert.assertFalse(map.containsKey(key));
137         }
138     }
139
140     protected static CollectionNodeBuilder<MapEntryNode, MapNode> topLevelMap() {
141         return ImmutableNodes.mapNodeBuilder(TopLevelList.QNAME);
142     }
143
144     protected static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> nestedMap() {
145         return Builders.orderedMapBuilder().withNodeIdentifier(new NodeIdentifier(NestedList.QNAME));
146     }
147
148     public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> topLevelList(
149             final String key) {
150         return ImmutableNodes.mapEntryBuilder(TopLevelList.QNAME, NAME_QNAME,
151                 key);
152     }
153
154     public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> nestedList(
155             final String key) {
156         return ImmutableNodes
157                 .mapEntryBuilder(NestedList.QNAME, NAME_QNAME, key);
158     }
159
160     public static final WriteTransactionCustomizer writeOneTopMultipleNested(
161             final String topName, final String... nestedName) {
162         CollectionNodeBuilder<MapEntryNode, OrderedMapNode> nestedMapBuilder = nestedMap();
163         for (String nestedItem : nestedName) {
164             nestedMapBuilder.addChild(nestedList(nestedItem).build());
165         }
166
167         final ContainerNode data = top().addChild(
168                 topLevelMap().addChild(
169                         topLevelList(topName)
170                                 .addChild(nestedMapBuilder.build()).build())
171                         .build()).build();
172
173         return DatastoreTestTask.simpleWrite(TOP_LEVEL, data);
174     }
175
176     public static final  WriteTransactionCustomizer deleteNested(final String topName,
177             final String nestedName) {
178         return DatastoreTestTask.simpleDelete(path(topName, nestedName));
179     }
180 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.