Seal only modified modifications
[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
14 class TestUtils {
15
16     @FunctionalInterface
17     public interface RunnableWithException {
18         void run() throws Exception;
19     }
20
21     private static final long TIMEOUT = 3;
22
23     /**
24      * Asserts, that future result when it completes is equal to given object.
25      * Future must complete in {@link TestUtils#TIMEOUT} seconds.
26      *
27      * @param expected expected result
28      * @param actual   future
29      * @param <T>      type
30      * @throws Exception exception
31      */
32     static <T> void assertFutureEquals(final T expected, final Future<T> actual) throws Exception {
33         Assert.assertEquals(expected, getWithTimeout(actual));
34     }
35
36     /**
37      * Calls {@link Future#get(long, TimeUnit)} with {@link TestUtils#TIMEOUT} in seconds.
38      *
39      * @param future future
40      * @param <T>    type
41      * @return future result
42      * @throws Exception exception
43      */
44     static <T> T getWithTimeout(final Future<T> future) throws Exception {
45         return future.get(TIMEOUT, TimeUnit.SECONDS);
46     }
47
48     /**
49      * Asserts that given operation invocation, will throw an exception of given class.
50      *
51      * @param operation         operation
52      * @param expectedException expected exception class
53      * @param message           message, when expected exception isn't thrown
54      * @return expected exception instance. Can be used for additional assertions.
55      * @throws Exception unexpected exception.
56      */
57     //Throwable is propagated if doesn't match the expected type
58     @SuppressWarnings("checkstyle:IllegalCatch")
59     static Throwable assertOperationThrowsException(final RunnableWithException operation,
60                                                     final Class<? extends Throwable> expectedException,
61                                                     final String message) throws Exception {
62         try {
63             operation.run();
64             throw new AssertionError(message + expectedException);
65         } catch (final Throwable e) {
66             if (!e.getClass().equals(expectedException)) {
67                 throw e;
68             }
69             return e;
70         }
71     }
72
73     /**
74      * Asserts, that when given operation is run, exception of given class is thrown.
75      *
76      * @param operation         operation
77      * @param expectedException expected exception class
78      * @return expected exception instance. Can be used for additional assertions.
79      * @throws Exception unexpected exception.
80      */
81     static Throwable assertOperationThrowsException(final RunnableWithException operation,
82                                                     final Class<? extends Throwable> expectedException)
83             throws Exception {
84         return assertOperationThrowsException(operation, expectedException, "Operation should throw exception: ");
85     }
86 }