2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.vpp.test.read;
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;
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;
38 * Generic test for implementation of {@link EntityDumpExecutor}
40 * @param <T> implementation of {@link EntityDumpExecutor}
42 public abstract class JvppDumpExecutorTest<T extends EntityDumpExecutor<?, ?>> implements FutureProducer {
45 protected FutureJVppCore api;
50 public void setUpParent() {
51 MockitoAnnotations.initMocks(this);
52 this.executor = initExecutor();
55 protected abstract T initExecutor();
57 protected T getExecutor() {
62 * Return pre-stubed mock that will throw {@link TimeoutException},
63 * while performing desired method
65 protected FutureJVppCore doThrowTimeoutExceptionWhen()
66 throws InterruptedException, ExecutionException, TimeoutException {
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()));
73 return doReturn(failedFuture).when(api);
77 * Return pre-stubed mock that will throw {@link VppInvocationException},
78 * while performing desired method
80 protected FutureJVppCore doThrowFailExceptionWhen() {
81 return doReturn(failedFuture()).when(api);
85 * Return pre-stubed mock that will return specified result
86 * while performing desired method
88 protected <U> FutureJVppCore doReturnResponseWhen(U replyDump) {
89 return doReturn(future(replyDump)).when(api);