daf68d2b6ceb6e28cbc09202a4a291a796f2e469
[hc2vpp.git] /
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.hc2vpp.common.translate.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 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;
38
39 public class JvppReplyConsumerTest implements JvppReplyConsumer {
40
41     private static class AnDataObject implements DataObject {
42         @Override
43         public Class<? extends DataContainer> getImplementedInterface() {
44             return null;
45         }
46     }
47
48     @Test
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);
54         try {
55             getReplyForWrite(future, replyType);
56         } catch (WriteTimeoutException e) {
57             assertTrue(e.getCause() instanceof TimeoutException);
58             assertEquals(replyType, e.getFailedId());
59             return;
60         }
61         fail("WriteTimeoutException was expected");
62     }
63
64     @Test
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);
70         try {
71             getReplyForRead(future, replyType);
72         } catch (ReadTimeoutException e) {
73             assertTrue(e.getCause() instanceof TimeoutException);
74             assertEquals(replyType, e.getFailedId());
75             return;
76         }
77         fail("ReadTimeoutException was expected");
78     }
79 }