a380cf02aa68e0e4f6bd29d4c31ae806ba56b1b5
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / TestCommitCohort.java
1 /*
2  * Copyright (c) 2015 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.mdsal.dom.broker;
9
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
13
14 public enum TestCommitCohort implements DOMStoreThreePhaseCommitCohort {
15
16
17     ALLWAYS_SUCCESS(true, true, true, true), CAN_COMMIT_FAILED(false, false, false, true), PRE_COMMIT_FAILED(true,
18             false, false, true), COMMIT_FAILED(true, true, false, true);
19
20
21     TestCommitCohort(final boolean canCommit, final boolean preCommit,
22             final boolean commit, final boolean abort) {
23         this.canCommit = Futures.immediateFuture(canCommit);
24         this.preCommit = immediate(canCommit, new IllegalStateException());
25         this.commit = immediate(commit, new IllegalStateException());
26         this.abort = immediate(abort, new IllegalStateException());
27     }
28
29
30     private final ListenableFuture<Boolean> canCommit;
31     private final ListenableFuture<Void> preCommit;
32     private final ListenableFuture<Void> commit;
33     private final ListenableFuture<Void> abort;
34
35     @Override
36     public ListenableFuture<Boolean> canCommit() {
37         return canCommit;
38     }
39
40     @Override
41     public ListenableFuture<Void> preCommit() {
42         return preCommit;
43     }
44
45     @Override
46     public ListenableFuture<Void> abort() {
47         return abort;
48     }
49
50     @Override
51     public ListenableFuture<Void> commit() {
52         return commit;
53     }
54
55     private static ListenableFuture<Void> immediate(final boolean isSuccess, final Exception except) {
56         return isSuccess ? Futures.immediateFuture(null) : Futures.immediateFailedFuture(except);
57     }
58
59
60
61 }