Deprecate all MD-SAL APIs
[controller.git] / opendaylight / md-sal / sal-common-api / src / test / java / org / opendaylight / controller / md / sal / common / api / MappingCheckedFutureTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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.controller.md.sal.common.api;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16
17 import com.google.common.util.concurrent.CheckedFuture;
18 import com.google.common.util.concurrent.SettableFuture;
19 import java.util.concurrent.CancellationException;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.atomic.AtomicReference;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
26
27 /**
28  * Unit tests for MappingCheckedFuture.
29  *
30  * @author Thomas Pantelis
31  */
32 @Deprecated
33 public class MappingCheckedFutureTest {
34
35     interface FutureInvoker {
36         void invokeGet(CheckedFuture<?,?> future) throws Exception;
37
38         Throwable extractWrappedTestEx(Exception from);
39     }
40
41     static class TestException extends Exception {
42         private static final long serialVersionUID = 1L;
43
44         TestException(final String message, final Throwable cause) {
45             super(message, cause);
46         }
47     }
48
49     static final ExceptionMapper<TestException> MAPPER = new ExceptionMapper<TestException>(
50                                                                       "Test", TestException.class) {
51
52         @Override
53         protected TestException newWithCause(final String message, final Throwable cause) {
54             return new TestException(message, cause);
55         }
56     };
57
58     static final FutureInvoker GET = new FutureInvoker() {
59         @Override
60         public void invokeGet(final CheckedFuture<?, ?> future) throws Exception {
61             future.get();
62         }
63
64         @Override
65         public Throwable extractWrappedTestEx(final Exception from) {
66             if (from instanceof ExecutionException) {
67                 return from.getCause();
68             }
69
70             return from;
71         }
72     };
73
74     static final FutureInvoker TIMED_GET = new FutureInvoker() {
75         @Override
76         public void invokeGet(final CheckedFuture<?, ?> future) throws Exception {
77             future.get(1, TimeUnit.HOURS);
78         }
79
80         @Override
81         public Throwable extractWrappedTestEx(final Exception from) {
82             if (from instanceof ExecutionException) {
83                 return from.getCause();
84             }
85
86             return from;
87         }
88     };
89
90     static final FutureInvoker CHECKED_GET = new FutureInvoker() {
91         @Override
92         public void invokeGet(final CheckedFuture<?,?> future) throws Exception {
93             future.checkedGet();
94         }
95
96         @Override
97         public Throwable extractWrappedTestEx(final Exception from) {
98             return from;
99         }
100     };
101
102     static final FutureInvoker TIMED_CHECKED_GET = new FutureInvoker() {
103         @Override
104         public void invokeGet(final CheckedFuture<?,?> future) throws Exception {
105             future.checkedGet(50, TimeUnit.MILLISECONDS);
106         }
107
108         @Override
109         public Throwable extractWrappedTestEx(final Exception from) {
110             return from;
111         }
112     };
113
114     @Test
115     public void testGet() throws Exception {
116         SettableFuture<String> delegate = SettableFuture.create();
117         MappingCheckedFuture<String,TestException> future = MappingCheckedFuture.create(delegate, MAPPER);
118         delegate.set("test");
119         assertEquals("get", "test", future.get());
120     }
121
122     @Test
123     public void testGetWithExceptions() throws Exception {
124         testExecutionException(GET, new RuntimeException());
125         testExecutionException(GET, new TestException("mock", null));
126         testCancellationException(GET);
127         testInterruptedException(GET);
128     }
129
130     @Test
131     public void testTimedGet() throws Exception {
132         SettableFuture<String> delegate = SettableFuture.create();
133         MappingCheckedFuture<String,TestException> future = MappingCheckedFuture.create(delegate, MAPPER);
134         delegate.set("test");
135         assertEquals("get", "test", future.get(50, TimeUnit.MILLISECONDS));
136     }
137
138     @Test
139     public void testTimedGetWithExceptions() throws Exception {
140         testExecutionException(TIMED_GET, new RuntimeException());
141         testCancellationException(TIMED_GET);
142         testInterruptedException(TIMED_GET);
143     }
144
145     @Test
146     public void testCheckedGetWithExceptions() throws Exception {
147         testExecutionException(CHECKED_GET, new RuntimeException());
148         testCancellationException(CHECKED_GET);
149         testInterruptedException(CHECKED_GET);
150     }
151
152     @Test
153     public void testTimedCheckedWithExceptions() throws Exception {
154         testExecutionException(TIMED_CHECKED_GET, new RuntimeException());
155         testCancellationException(TIMED_CHECKED_GET);
156         testInterruptedException(TIMED_CHECKED_GET);
157     }
158
159     @SuppressWarnings("checkstyle:illegalCatch")
160     private static void testExecutionException(final FutureInvoker invoker, final Throwable cause) {
161         SettableFuture<String> delegate = SettableFuture.create();
162         MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
163
164         delegate.setException(cause);
165
166         try {
167             invoker.invokeGet(mappingFuture);
168             fail("Expected exception thrown");
169         } catch (Exception e) {
170             Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
171             assertNotNull("Expected returned exception is null", expectedTestEx);
172             assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
173
174             if (cause instanceof TestException) {
175                 assertNull("Expected null cause", expectedTestEx.getCause());
176             } else {
177                 assertSame("TestException cause", cause, expectedTestEx.getCause());
178             }
179         }
180     }
181
182     @SuppressWarnings("checkstyle:illegalCatch")
183     private static void testCancellationException(final FutureInvoker invoker) {
184         SettableFuture<String> delegate = SettableFuture.create();
185         MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
186
187         mappingFuture.cancel(false);
188
189         try {
190             invoker.invokeGet(mappingFuture);
191             fail("Expected exception thrown");
192         } catch (Exception e) {
193             Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
194             assertNotNull("Expected returned exception is null", expectedTestEx);
195             assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
196             assertEquals("TestException cause type", CancellationException.class, expectedTestEx.getCause().getClass());
197         }
198     }
199
200     @SuppressWarnings("checkstyle:illegalCatch")
201     private static void testInterruptedException(final FutureInvoker invoker) throws Exception {
202         SettableFuture<String> delegate = SettableFuture.create();
203         final MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
204
205         final AtomicReference<AssertionError> assertError = new AtomicReference<>();
206         final CountDownLatch doneLatch = new CountDownLatch(1);
207         Thread thread = new Thread() {
208             @Override
209             public void run() {
210                 try {
211                     doInvoke();
212                 } catch (AssertionError e) {
213                     assertError.set(e);
214                 } finally {
215                     doneLatch.countDown();
216                 }
217             }
218
219             void doInvoke() {
220                 try {
221                     invoker.invokeGet(mappingFuture);
222                     fail("Expected exception thrown");
223                 } catch (Exception e) {
224                     Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
225                     assertNotNull("Expected returned exception is null", expectedTestEx);
226                     assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
227                     assertEquals("TestException cause type", InterruptedException.class,
228                                   expectedTestEx.getCause().getClass());
229                 }
230             }
231         };
232         thread.start();
233
234         thread.interrupt();
235         assertTrue("get call completed", doneLatch.await(5, TimeUnit.SECONDS));
236
237         if (assertError.get() != null) {
238             throw assertError.get();
239         }
240     }
241 }