df6709ad9a2648870dc6fb42a48bc810a86ddb9f
[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
22
23     private TestCommitCohort(final boolean canCommit, final boolean preCommit, final boolean commit, final boolean abort) {
24         this.canCommit = Futures.immediateFuture(canCommit);
25         this.preCommit = immediate(canCommit, new IllegalStateException());
26         this.commit = immediate(commit, new IllegalStateException());
27         this.abort = immediate(abort, new IllegalStateException());
28     }
29
30
31     private final ListenableFuture<Boolean> canCommit;
32     private final ListenableFuture<Void> preCommit;
33     private final ListenableFuture<Void> commit;
34     private final ListenableFuture<Void> abort;
35
36     @Override
37     public ListenableFuture<Boolean> canCommit() {
38         return canCommit;
39     }
40
41     @Override
42     public ListenableFuture<Void> preCommit() {
43         return preCommit;
44     }
45
46     @Override
47     public ListenableFuture<Void> abort() {
48         return abort;
49     }
50
51     @Override
52     public ListenableFuture<Void> commit() {
53         return commit;
54     }
55
56     private static ListenableFuture<Void> immediate(final boolean isSuccess, final Exception e) {
57         return isSuccess ? Futures.<Void>immediateFuture(null) : Futures.<Void>immediateFailedFuture(e);
58     }
59
60
61
62 }