5140121a66e7756347226c5ef29da2d4e58c812c
[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.lisp.translate.write;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 import static org.mockito.MockitoAnnotations.initMocks;
28
29 import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
30 import io.fd.honeycomb.translate.write.WriteFailedException;
31 import io.fd.vpp.jvpp.VppCallbackException;
32 import io.fd.vpp.jvpp.core.dto.OneAddDelMapRequestItrRlocs;
33 import io.fd.vpp.jvpp.core.dto.OneAddDelMapRequestItrRlocsReply;
34 import java.util.concurrent.CompletableFuture;
35 import java.util.concurrent.ExecutionException;
36 import java.util.concurrent.TimeUnit;
37 import java.util.concurrent.TimeoutException;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.ArgumentCaptor;
41 import org.mockito.Captor;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.itr.remote.locator.sets.grouping.ItrRemoteLocatorSet;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.itr.remote.locator.sets.grouping.ItrRemoteLocatorSetBuilder;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45
46 public class ItrRemoteLocatorSetCustomizerTest extends LispWriterCustomizerTest implements ByteDataTranslator {
47
48     private static final String VALID_NAME = "loc-set";
49
50     @Captor
51     private ArgumentCaptor<OneAddDelMapRequestItrRlocs> requestCaptor;
52
53     private ItrRemoteLocatorSetCustomizer customizer;
54     private InstanceIdentifier<ItrRemoteLocatorSet> validId;
55     private ItrRemoteLocatorSet validData;
56
57     @Override
58     @Before
59     public void setUpTest() throws Exception {
60         initMocks(this);
61         customizer = new ItrRemoteLocatorSetCustomizer(api, lispStateCheckService);
62         validId = InstanceIdentifier.create(ItrRemoteLocatorSet.class);
63         validData = new ItrRemoteLocatorSetBuilder().setRemoteLocatorSetName(VALID_NAME).build();
64     }
65
66     @Test
67     public void writeCurrentAttributesSuccess() throws Exception {
68         onWriteSuccess();
69         customizer.writeCurrentAttributes(validId, validData, writeContext);
70         verifyWriteInvoked(true, VALID_NAME);
71     }
72
73     @Test
74     public void writeCurrentAttributesFailed() {
75         onWriteThrow();
76
77         try {
78             customizer.writeCurrentAttributes(validId, validData, writeContext);
79         } catch (WriteFailedException e) {
80             assertTrue(e.getCause() instanceof VppCallbackException);
81             verifyWriteInvoked(true, VALID_NAME);
82             return;
83         }
84
85         fail("Test should have thrown exception");
86     }
87
88     @Test(expected = UnsupportedOperationException.class)
89     public void updateCurrentAttributes() throws WriteFailedException {
90         customizer.updateCurrentAttributes(validId, validData, validData, writeContext);
91     }
92
93     @Test
94     public void deleteCurrentAttributesSuccess() throws Exception {
95         onWriteSuccess();
96         customizer.deleteCurrentAttributes(validId, validData, writeContext);
97         verifyWriteInvoked(false, VALID_NAME);
98     }
99
100     @Test
101     public void deleteCurrentAttributesFailed() throws Exception {
102         onWriteThrow();
103
104         try {
105             customizer.writeCurrentAttributes(validId, validData, writeContext);
106         } catch (WriteFailedException e) {
107             assertTrue(e.getCause() instanceof VppCallbackException);
108             verifyWriteInvoked(true, VALID_NAME);
109             return;
110         }
111
112         fail("Test should have thrown exception");
113     }
114
115     private void onWriteSuccess() {
116         when(api.oneAddDelMapRequestItrRlocs(any(OneAddDelMapRequestItrRlocs.class)))
117                 .thenReturn(CompletableFuture.completedFuture(new OneAddDelMapRequestItrRlocsReply()));
118     }
119
120     private void onWriteThrow() {
121         when(api.oneAddDelMapRequestItrRlocs(any(OneAddDelMapRequestItrRlocs.class)))
122                 .thenReturn(new CompletableFuture<OneAddDelMapRequestItrRlocsReply>() {
123                     @Override
124                     public OneAddDelMapRequestItrRlocsReply get(final long l, final TimeUnit timeUnit)
125                             throws InterruptedException, ExecutionException, TimeoutException {
126                         throw new ExecutionException(new VppCallbackException("oneAddDelMapRequestItrRlocs",
127                                 "oneAddDelMapRequestItrRlocs failed", 1, -2));
128                     }
129                 });
130     }
131
132     private void verifyWriteInvoked(final boolean add, final String name) {
133         verify(api, times(1)).oneAddDelMapRequestItrRlocs(requestCaptor.capture());
134
135         final OneAddDelMapRequestItrRlocs request = requestCaptor.getValue();
136         assertNotNull(request);
137         assertEquals(booleanToByte(add), request.isAdd);
138         assertEquals(name, toString(request.locatorSetName));
139     }
140 }