Remove hc2vpp codebase
[honeycomb.git] / infra / translate-utils / src / test / java / io / fd / honeycomb / translate / util / read / KeepaliveReaderWrapperTest.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.util.read;
18
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25
26 import io.fd.honeycomb.translate.read.ReadContext;
27 import io.fd.honeycomb.translate.read.Reader;
28 import java.util.concurrent.CompletableFuture;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.Future;
31 import java.util.concurrent.ScheduledExecutorService;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.opendaylight.yangtools.concepts.Builder;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40
41 public class KeepaliveReaderWrapperTest {
42
43     @Mock
44     private ReadContext ctx;
45     private InstanceIdentifier<DataObject> iid = InstanceIdentifier.create(DataObject.class);
46     @Mock
47     private Reader<DataObject, Builder<DataObject>> delegate;
48     @Mock
49     private Builder<DataObject> builder;
50
51     private InstanceIdentifier<DataObject> id = InstanceIdentifier.create(DataObject.class);
52     private ScheduledExecutorService exec;
53
54     @Before
55     public void setUp() throws Exception {
56         MockitoAnnotations.initMocks(this);
57         exec = Executors.newScheduledThreadPool(1);
58         when(delegate.getManagedDataObjectType()).thenReturn(iid);
59         when(delegate.read(eq(iid), any(ReadContext.class))).thenThrow(TestingException.class);
60     }
61
62     @After
63     public void tearDown() throws Exception {
64         exec.shutdownNow();
65     }
66
67     @Test(timeout = 10000)
68     public void testKeepalive() throws Exception {
69         final CapturingFailListener listener = new CapturingFailListener();
70
71         final KeepaliveReaderWrapper<DataObject, Builder<DataObject>> keepaliveWrapper =
72                 new KeepaliveReaderWrapper<>(delegate, exec, TestingException.class, 1, listener);
73
74         keepaliveWrapper.readCurrentAttributes(id, builder, ctx);
75         verify(delegate).readCurrentAttributes(id, builder, ctx);
76
77         keepaliveWrapper.getBuilder(id);
78         verify(delegate).getBuilder(id);
79
80
81         assertTrue(listener.getTriggerFuture().get());
82         verify(delegate).read(any(InstanceIdentifier.class), any(ReadContext.class));
83
84         keepaliveWrapper.close();
85     }
86
87     @Test(timeout = 10000)
88     public void testKeepaliveCancel() throws Exception {
89         final CapturingFailListener listener = new CapturingFailListener();
90
91         final KeepaliveReaderWrapper<DataObject, Builder<DataObject>> keepaliveWrapper =
92                 new KeepaliveReaderWrapper<>(delegate, exec, TestingException.class, 100000, listener);
93         keepaliveWrapper.close();
94         assertFalse(listener.getTriggerFuture().isDone());
95     }
96
97     private static final class TestingException extends RuntimeException {}
98
99     private static class CapturingFailListener implements KeepaliveReaderWrapper.KeepaliveFailureListener {
100         private CompletableFuture<Boolean> booleanCompletableFuture = new CompletableFuture<>();
101
102         @Override
103         public synchronized void onKeepaliveFailure() {
104             booleanCompletableFuture.complete(true);
105         }
106
107         synchronized Future<Boolean> getTriggerFuture() {
108             return booleanCompletableFuture;
109         }
110     }
111 }