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