HONEYCOMB-58 - Routing Api
[honeycomb.git] / vpp-common / vpp-translate-utils / src / test / java / io / fd / honeycomb / translate / vpp / util / JvppReplyConsumerTest.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.translate.vpp.util;
18
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;
26
27 import java.util.concurrent.Future;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.TimeoutException;
30 import org.junit.Test;
31 import org.opendaylight.yangtools.yang.binding.DataContainer;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import io.fd.vpp.jvpp.dto.JVppReply;
35
36 public class JvppReplyConsumerTest implements JvppReplyConsumer {
37
38     private static class AnDataObject implements DataObject {
39         @Override
40         public Class<? extends DataContainer> getImplementedInterface() {
41             return null;
42         }
43     }
44
45     @Test
46     public void testGetReplyForWriteTimeout() throws Exception {
47         final Future<JVppReply<?>> future = mock(Future.class);
48         when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class);
49         final InstanceIdentifier<AnDataObject>
50                 replyType = InstanceIdentifier.create(AnDataObject.class);
51         try {
52             getReplyForWrite(future, replyType);
53         } catch (WriteTimeoutException e) {
54             assertTrue(e.getCause() instanceof TimeoutException);
55             assertEquals(replyType, e.getFailedId());
56             return;
57         }
58         fail("WriteTimeoutException was expected");
59     }
60
61     @Test
62     public void testGetReplyForReadTimeout() throws Exception {
63         final Future<JVppReply<?>> future = mock(Future.class);
64         final InstanceIdentifier<AnDataObject> replyType =
65                 InstanceIdentifier.create(AnDataObject.class);
66         when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class);
67         try {
68             getReplyForRead(future, replyType);
69         } catch (ReadTimeoutException e) {
70             assertTrue(e.getCause() instanceof TimeoutException);
71             assertEquals(replyType, e.getFailedId());
72             return;
73         }
74         fail("ReadTimeoutException was expected");
75     }
76 }