ea4c2ef27ccffaaa3470c7c2a1050f4080bee711
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / test / AbstractBaseDataBrokerTest.java
1 /*
2  * Copyright (c) 2017 Red Hat, 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.mdsal.binding.dom.adapter.test;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.TimeUnit;
13 import java.util.concurrent.TimeoutException;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
16 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
17
18 public abstract class AbstractBaseDataBrokerTest extends AbstractSchemaAwareTest {
19     private static final int ASSERT_COMMIT_DEFAULT_TIMEOUT = 5000;
20
21     private AbstractDataBrokerTestCustomizer testCustomizer;
22     private DataBroker dataBroker;
23     private DOMDataBroker domBroker;
24
25     protected abstract AbstractDataBrokerTestCustomizer createDataBrokerTestCustomizer();
26
27     public AbstractDataBrokerTestCustomizer getDataBrokerTestCustomizer() {
28         if (testCustomizer == null) {
29             throw new IllegalStateException("testCustomizer not yet set by call to createDataBrokerTestCustomizer()");
30         }
31         return testCustomizer;
32     }
33
34     @Override
35     protected void setupWithSchema(final EffectiveModelContext context) {
36         testCustomizer = createDataBrokerTestCustomizer();
37         dataBroker = testCustomizer.createDataBroker();
38         domBroker = testCustomizer.getDOMDataBroker();
39         testCustomizer.updateSchema(context);
40     }
41
42     public DataBroker getDataBroker() {
43         return dataBroker;
44     }
45
46     public DOMDataBroker getDomBroker() {
47         return domBroker;
48     }
49
50     protected static final void assertCommit(final ListenableFuture<?> commit) {
51         assertCommit(commit, ASSERT_COMMIT_DEFAULT_TIMEOUT);
52     }
53
54     protected static final void assertCommit(final ListenableFuture<?> commit, final long timeoutInMS) {
55         try {
56             commit.get(timeoutInMS, TimeUnit.MILLISECONDS);
57         } catch (InterruptedException | ExecutionException | TimeoutException e) {
58             throw new IllegalStateException(e);
59         }
60     }
61 }