Speed up AbstractModificationTest
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / modification / AbstractModificationTest.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
9 package org.opendaylight.controller.cluster.datastore.modification;
10
11 import com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import java.util.Optional;
14 import org.junit.AfterClass;
15 import org.junit.Before;
16 import org.junit.BeforeClass;
17 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
18 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
19 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
20 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
21 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25
26 public abstract class AbstractModificationTest {
27     private static SchemaContext TEST_SCHEMA_CONTEXT;
28
29     protected InMemoryDOMDataStore store;
30
31     @BeforeClass
32     public static void beforeClass() {
33         TEST_SCHEMA_CONTEXT = TestModel.createTestContext();
34     }
35
36     @AfterClass
37     public static void afterClass() {
38         TEST_SCHEMA_CONTEXT = null;
39     }
40
41     @Before
42     public void setUp() {
43         store = new InMemoryDOMDataStore("test", MoreExecutors.newDirectExecutorService());
44         store.onGlobalContextUpdated(TEST_SCHEMA_CONTEXT);
45     }
46
47     protected void commitTransaction(final DOMStoreWriteTransaction transaction) {
48         DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
49         cohort.preCommit();
50         cohort.commit();
51     }
52
53     protected Optional<NormalizedNode<?, ?>> readData(final YangInstanceIdentifier path) throws Exception {
54         DOMStoreReadTransaction transaction = store.newReadOnlyTransaction();
55         ListenableFuture<Optional<NormalizedNode<?, ?>>> future = transaction.read(path);
56         return future.get();
57     }
58 }