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