5855894708f0a81db4d0a4765817c811ab8fd5b1
[honeycomb.git] / lisp / lisp2vpp / src / test / java / io / fd / honeycomb / lisp / translate / write / 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.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.honeycomb.translate.vpp.util.ByteDataTranslator;
30 import io.fd.honeycomb.translate.write.WriteFailedException;
31 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
32 import io.fd.vpp.jvpp.VppCallbackException;
33 import io.fd.vpp.jvpp.core.dto.LispAddDelMapRequestItrRlocs;
34 import io.fd.vpp.jvpp.core.dto.LispAddDelMapRequestItrRlocsReply;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.concurrent.ExecutionException;
37 import java.util.concurrent.TimeUnit;
38 import java.util.concurrent.TimeoutException;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.Captor;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.itr.remote.locator.sets.grouping.ItrRemoteLocatorSet;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.itr.remote.locator.sets.grouping.ItrRemoteLocatorSetBuilder;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46
47 public class ItrRemoteLocatorSetCustomizerTest extends WriterCustomizerTest implements ByteDataTranslator {
48
49     private static final String VALID_NAME = "loc-set";
50
51     @Captor
52     private ArgumentCaptor<LispAddDelMapRequestItrRlocs> requestCaptor;
53
54     private ItrRemoteLocatorSetCustomizer customizer;
55     private InstanceIdentifier<ItrRemoteLocatorSet> validId;
56     private ItrRemoteLocatorSet validData;
57
58     @Before
59     public void setUp() throws Exception {
60         initMocks(this);
61         customizer = new ItrRemoteLocatorSetCustomizer(api);
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
89     public void updateCurrentAttributes() {
90         try {
91             customizer.updateCurrentAttributes(validId, validData, validData, writeContext);
92         } catch (WriteFailedException e) {
93             assertTrue(e.getCause() instanceof UnsupportedOperationException);
94             return;
95         }
96
97         fail("Test should have thrown exception");
98     }
99
100     @Test
101     public void deleteCurrentAttributesSuccess() throws Exception {
102         onWriteSuccess();
103         customizer.deleteCurrentAttributes(validId, validData, writeContext);
104         verifyWriteInvoked(false, VALID_NAME);
105     }
106
107     @Test
108     public void deleteCurrentAttributesFailed() throws Exception {
109         onWriteThrow();
110
111         try {
112             customizer.writeCurrentAttributes(validId, validData, writeContext);
113         } catch (WriteFailedException e) {
114             assertTrue(e.getCause() instanceof VppCallbackException);
115             verifyWriteInvoked(true, VALID_NAME);
116             return;
117         }
118
119         fail("Test should have thrown exception");
120     }
121
122     private void onWriteSuccess() {
123         when(api.lispAddDelMapRequestItrRlocs(any(LispAddDelMapRequestItrRlocs.class)))
124                 .thenReturn(CompletableFuture.completedFuture(new LispAddDelMapRequestItrRlocsReply()));
125     }
126
127     private void onWriteThrow() {
128         when(api.lispAddDelMapRequestItrRlocs(any(LispAddDelMapRequestItrRlocs.class)))
129                 .thenReturn(new CompletableFuture<LispAddDelMapRequestItrRlocsReply>() {
130                     @Override
131                     public LispAddDelMapRequestItrRlocsReply get(final long l, final TimeUnit timeUnit)
132                             throws InterruptedException, ExecutionException, TimeoutException {
133                         throw new ExecutionException(new VppCallbackException("lispAddDelMapRequestItrRlocs", 1, -2));
134                     }
135                 });
136     }
137
138     private void verifyWriteInvoked(final boolean add, final String name) {
139         verify(api, times(1)).lispAddDelMapRequestItrRlocs(requestCaptor.capture());
140
141         final LispAddDelMapRequestItrRlocs request = requestCaptor.getValue();
142         assertNotNull(request);
143         assertEquals(booleanToByte(add), request.isAdd);
144         assertEquals(name, toString(request.locatorSetName));
145     }
146 }