a1688ae356b2e553f0367c4e47c63e03bdda8cc9
[hc2vpp.git] /
1 package io.fd.honeycomb.lisp.translate.read;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertNull;
6 import static org.junit.Assert.assertTrue;
7 import static org.junit.Assert.fail;
8 import static org.mockito.Matchers.any;
9 import static org.mockito.Mockito.times;
10 import static org.mockito.Mockito.verify;
11 import static org.mockito.Mockito.when;
12
13 import io.fd.honeycomb.translate.read.ReadFailedException;
14 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
15 import io.fd.honeycomb.translate.util.read.cache.exceptions.execution.i.DumpCallFailedException;
16 import io.fd.honeycomb.vpp.test.read.ReaderCustomizerTest;
17 import io.fd.vpp.jvpp.VppCallbackException;
18 import io.fd.vpp.jvpp.core.dto.LispGetMapRequestItrRlocs;
19 import io.fd.vpp.jvpp.core.dto.LispGetMapRequestItrRlocsReply;
20 import java.nio.charset.StandardCharsets;
21 import java.util.concurrent.CompletableFuture;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.itr.remote.locator.sets.grouping.ItrRemoteLocatorSet;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.itr.remote.locator.sets.grouping.ItrRemoteLocatorSetBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.lisp.feature.data.grouping.LispFeatureDataBuilder;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31
32
33 public class ItrRemoteLocatorSetCustomizerTest
34         extends ReaderCustomizerTest<ItrRemoteLocatorSet, ItrRemoteLocatorSetBuilder> {
35
36     private static final String EXPECTED_LOCATOR_SET_NAME = "loc-set";
37
38     private InstanceIdentifier<ItrRemoteLocatorSet> validId;
39     private ItrRemoteLocatorSetBuilder builder;
40
41     public ItrRemoteLocatorSetCustomizerTest() {
42         super(ItrRemoteLocatorSet.class, LispFeatureDataBuilder.class);
43     }
44
45     @Before
46     public void setUp() throws Exception {
47         validId = InstanceIdentifier.create(ItrRemoteLocatorSet.class);
48         builder = new ItrRemoteLocatorSetBuilder();
49     }
50
51     @Override
52     protected ReaderCustomizer<ItrRemoteLocatorSet, ItrRemoteLocatorSetBuilder> initCustomizer() {
53         return new ItrRemoteLocatorSetCustomizer(api);
54     }
55
56     @Test
57     public void getBuilder() throws Exception {
58         final ItrRemoteLocatorSetBuilder itrRemoteLocatorSetBuilder = getCustomizer().getBuilder(validId);
59
60         assertNotNull(itrRemoteLocatorSetBuilder);
61         assertNull(itrRemoteLocatorSetBuilder.getRemoteLocatorSetName());
62     }
63
64     @Test
65     public void readCurrentAttributesSuccess() throws Exception {
66         doReturnValidDataOnDump();
67
68         getCustomizer().readCurrentAttributes(validId, builder, ctx);
69
70         assertNotNull(builder);
71         assertEquals(EXPECTED_LOCATOR_SET_NAME, builder.getRemoteLocatorSetName());
72         verifyLispGetMapRequestItrRlocsInvokedOnce();
73     }
74
75     @Test
76     public void readCurrentAttributesEmptyData() throws Exception {
77         doReturnEmptyDataOnDump();
78         getCustomizer().readCurrentAttributes(validId, builder, ctx);
79         verifyInvalidDataCase(builder);
80     }
81
82     @Test
83     public void readCurrentAttributesFailedCallHalted() {
84         doThrowExceptionOnDump();
85         try {
86             getCustomizer().readCurrentAttributes(validId, builder, ctx);
87         } catch (ReadFailedException e) {
88             assertTrue(e.getCause() instanceof DumpCallFailedException);
89             assertTrue(e.getCause().getCause() instanceof VppCallbackException);
90             assertNotNull(builder);
91             assertNull(builder.getRemoteLocatorSetName());
92
93             verifyLispGetMapRequestItrRlocsInvokedOnce();
94             return;
95         }
96
97         fail("Test should have thrown exception");
98     }
99
100     @Test
101     public void merge() throws Exception {
102         LispFeatureDataBuilder builder = new LispFeatureDataBuilder();
103         ItrRemoteLocatorSet set = new ItrRemoteLocatorSetBuilder().setRemoteLocatorSetName("loc-set").build();
104         getCustomizer().merge(builder, set);
105
106         assertNotNull(builder);
107         assertEquals(set, builder.getItrRemoteLocatorSet());
108     }
109
110
111     private void doReturnValidDataOnDump() {
112         LispGetMapRequestItrRlocsReply reply = new LispGetMapRequestItrRlocsReply();
113         reply.locatorSetName = EXPECTED_LOCATOR_SET_NAME.getBytes(StandardCharsets.UTF_8);
114
115         when(api.lispGetMapRequestItrRlocs(any(LispGetMapRequestItrRlocs.class)))
116                 .thenReturn(CompletableFuture.completedFuture(reply));
117     }
118
119     private void doReturnNullDataOnDump() {
120         when(api.lispGetMapRequestItrRlocs(any(LispGetMapRequestItrRlocs.class)))
121                 .thenReturn(CompletableFuture.completedFuture(null));
122     }
123
124     private void doReturnEmptyDataOnDump() {
125         when(api.lispGetMapRequestItrRlocs(any(LispGetMapRequestItrRlocs.class)))
126                 .thenReturn(CompletableFuture.completedFuture(new LispGetMapRequestItrRlocsReply()));
127     }
128
129     private void doThrowExceptionOnDump() {
130         when(api.lispGetMapRequestItrRlocs(any(LispGetMapRequestItrRlocs.class))).
131                 thenReturn(new CompletableFuture<LispGetMapRequestItrRlocsReply>() {
132                     @Override
133                     public LispGetMapRequestItrRlocsReply get(final long l, final TimeUnit timeUnit)
134                             throws InterruptedException, ExecutionException, TimeoutException {
135                         throw new ExecutionException(new VppCallbackException("lispGetMapRequestItrRlocs", 1, -2));
136                     }
137                 });
138     }
139
140     private void verifyLispGetMapRequestItrRlocsInvokedOnce() {
141         verify(api, times(1)).lispGetMapRequestItrRlocs(any(LispGetMapRequestItrRlocs.class));
142     }
143
144     private void verifyInvalidDataCase(final ItrRemoteLocatorSetBuilder builder) {
145         assertNotNull(builder);
146         assertNull(builder.getRemoteLocatorSetName());
147
148         verifyLispGetMapRequestItrRlocsInvokedOnce();
149     }
150 }