Add AbstractTransactionCommitCohort unit tests
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / actors / dds / TestUtils.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.cluster.databroker.actors.dds;
9
10 import java.util.concurrent.Future;
11 import java.util.concurrent.TimeUnit;
12 import org.junit.Assert;
13 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
14 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
15 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
16 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
17 import org.opendaylight.controller.cluster.access.concepts.MemberName;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19
20 class TestUtils {
21
22     static final MemberName MEMBER_NAME = MemberName.forName("member-1");
23     static final FrontendType FRONTEND_TYPE = FrontendType.forName("type-1");
24     static final FrontendIdentifier FRONTEND_ID = FrontendIdentifier.create(MEMBER_NAME, FRONTEND_TYPE);
25     static final ClientIdentifier CLIENT_ID = ClientIdentifier.create(FRONTEND_ID, 0);
26     static final LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_ID, 0L);
27     static final TransactionIdentifier TRANSACTION_ID = new TransactionIdentifier(HISTORY_ID, 0L);
28
29     @FunctionalInterface
30     public interface RunnableWithException {
31         void run() throws Exception;
32     }
33
34     private static final long TIMEOUT = 3;
35
36     /**
37      * Asserts, that future result when it completes is equal to given object.
38      * Future must complete in {@link TestUtils#TIMEOUT} seconds.
39      *
40      * @param expected expected result
41      * @param actual   future
42      * @param <T>      type
43      * @throws Exception exception
44      */
45     static <T> void assertFutureEquals(final T expected, final Future<T> actual) throws Exception {
46         Assert.assertEquals(expected, getWithTimeout(actual));
47     }
48
49     /**
50      * Calls {@link Future#get(long, TimeUnit)} with {@link TestUtils#TIMEOUT} in seconds.
51      *
52      * @param future future
53      * @param <T>    type
54      * @return future result
55      * @throws Exception exception
56      */
57     static <T> T getWithTimeout(final Future<T> future) throws Exception {
58         return future.get(TIMEOUT, TimeUnit.SECONDS);
59     }
60
61     /**
62      * Asserts that given operation invocation, will throw an exception of given class.
63      *
64      * @param operation         operation
65      * @param expectedException expected exception class
66      * @param message           message, when expected exception isn't thrown
67      * @return expected exception instance. Can be used for additional assertions.
68      * @throws Exception unexpected exception.
69      */
70     //Throwable is propagated if doesn't match the expected type
71     @SuppressWarnings("checkstyle:IllegalCatch")
72     static <T extends Throwable> T assertOperationThrowsException(final RunnableWithException operation,
73                                                                   final Class<T> expectedException,
74                                                                   final String message) throws Exception {
75         try {
76             operation.run();
77             throw new AssertionError(message + expectedException);
78         } catch (final Throwable e) {
79             if (!e.getClass().equals(expectedException)) {
80                 throw e;
81             }
82             return (T) e;
83         }
84     }
85
86     /**
87      * Asserts, that when given operation is run, exception of given class is thrown.
88      *
89      * @param operation         operation
90      * @param expectedException expected exception class
91      * @return expected exception instance. Can be used for additional assertions.
92      * @throws Exception unexpected exception.
93      */
94     static <T extends Throwable> T assertOperationThrowsException(final RunnableWithException operation,
95                                                                   final Class<T> expectedException)
96             throws Exception {
97         return assertOperationThrowsException(operation, expectedException, "Operation should throw exception: ");
98     }
99 }

©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.