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.hc2vpp.common.translate.util;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 import static org.mockito.Matchers.anyLong;
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
27 import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
28 import io.fd.hc2vpp.common.translate.util.ReadTimeoutException;
29 import io.fd.hc2vpp.common.translate.util.WriteTimeoutException;
30 import java.util.concurrent.Future;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.TimeoutException;
33 import org.junit.Test;
34 import org.opendaylight.yangtools.yang.binding.DataContainer;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import io.fd.vpp.jvpp.dto.JVppReply;
39 public class JvppReplyConsumerTest implements JvppReplyConsumer {
41 private static class AnDataObject implements DataObject {
43 public Class<? extends DataContainer> getImplementedInterface() {
49 public void testGetReplyForWriteTimeout() throws Exception {
50 final Future<JVppReply<?>> future = mock(Future.class);
51 when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class);
52 final InstanceIdentifier<AnDataObject>
53 replyType = InstanceIdentifier.create(AnDataObject.class);
55 getReplyForWrite(future, replyType);
56 } catch (WriteTimeoutException e) {
57 assertTrue(e.getCause() instanceof TimeoutException);
58 assertEquals(replyType, e.getFailedId());
61 fail("WriteTimeoutException was expected");
65 public void testGetReplyForReadTimeout() throws Exception {
66 final Future<JVppReply<?>> future = mock(Future.class);
67 final InstanceIdentifier<AnDataObject> replyType =
68 InstanceIdentifier.create(AnDataObject.class);
69 when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class);
71 getReplyForRead(future, replyType);
72 } catch (ReadTimeoutException e) {
73 assertTrue(e.getCause() instanceof TimeoutException);
74 assertEquals(replyType, e.getFailedId());
77 fail("ReadTimeoutException was expected");