500c313870e807f6a9d6ecee298ccd99ed0ab2bc
[honeycomb.git] / vpp-common / vpp-translate-test / src / main / java / io / fd / honeycomb / vpp / test / read / JvppDumpExecutorTest.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.vpp.test.read;
18
19 import static org.mockito.Matchers.anyLong;
20 import static org.mockito.Matchers.eq;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.when;
24
25 import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
26 import io.fd.honeycomb.vpp.test.util.FutureProducer;
27 import io.fd.vpp.jvpp.VppInvocationException;
28 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
29 import java.util.concurrent.CompletableFuture;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.TimeoutException;
33 import org.junit.Before;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36
37 /**
38  * Generic test for implementation of {@link EntityDumpExecutor}
39  *
40  * @param <T> implementation of {@link EntityDumpExecutor}
41  */
42 public abstract class JvppDumpExecutorTest<T extends EntityDumpExecutor<?, ?>> implements FutureProducer {
43
44     @Mock
45     protected FutureJVppCore api;
46
47     private T executor;
48
49     @Before
50     public void setUpParent() {
51         MockitoAnnotations.initMocks(this);
52         this.executor = initExecutor();
53     }
54
55     protected abstract T initExecutor();
56
57     protected T getExecutor() {
58         return this.executor;
59     }
60
61     /**
62      * Return pre-stubed mock that will throw {@link TimeoutException},
63      * while performing desired method
64      */
65     protected FutureJVppCore doThrowTimeoutExceptionWhen()
66             throws InterruptedException, ExecutionException, TimeoutException {
67
68         CompletableFuture failedFuture = mock(CompletableFuture.class);
69         when(failedFuture.toCompletableFuture()).thenReturn(failedFuture);
70         when(failedFuture.get(anyLong(), eq(TimeUnit.SECONDS)))
71                 .thenThrow(new TimeoutException("Exception invoked by " + JvppDumpExecutorTest.class.getName()));
72
73         return doReturn(failedFuture).when(api);
74     }
75
76     /**
77      * Return pre-stubed mock that will throw {@link VppInvocationException},
78      * while performing desired method
79      */
80     protected FutureJVppCore doThrowFailExceptionWhen() {
81         return doReturn(failedFuture()).when(api);
82     }
83
84     /**
85      * Return pre-stubed mock that will return specified result
86      * while performing desired method
87      */
88     protected <U> FutureJVppCore doReturnResponseWhen(U replyDump) {
89         return doReturn(future(replyDump)).when(api);
90     }
91 }