HONEYCOMB-154: update revison of models that changed since 16.09
[honeycomb.git] / lisp / lisp2vpp / src / test / java / io / fd / honeycomb / lisp / translate / write / ItrRemoteLocatorSetCustomizerTest.java
1 package io.fd.honeycomb.lisp.translate.write;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.junit.Assert.fail;
7 import static org.mockito.Matchers.any;
8 import static org.mockito.Mockito.times;
9 import static org.mockito.Mockito.verify;
10 import static org.mockito.Mockito.when;
11 import static org.mockito.MockitoAnnotations.initMocks;
12
13 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
14 import io.fd.honeycomb.translate.write.WriteFailedException;
15 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
16 import io.fd.vpp.jvpp.VppCallbackException;
17 import io.fd.vpp.jvpp.core.dto.LispAddDelMapRequestItrRlocs;
18 import io.fd.vpp.jvpp.core.dto.LispAddDelMapRequestItrRlocsReply;
19 import java.util.concurrent.CompletableFuture;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Captor;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.itr.remote.locator.sets.grouping.ItrRemoteLocatorSet;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.itr.remote.locator.sets.grouping.ItrRemoteLocatorSetBuilder;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30
31 public class ItrRemoteLocatorSetCustomizerTest extends WriterCustomizerTest implements ByteDataTranslator {
32
33     private static final String VALID_NAME = "loc-set";
34
35     @Captor
36     private ArgumentCaptor<LispAddDelMapRequestItrRlocs> requestCaptor;
37
38     private ItrRemoteLocatorSetCustomizer customizer;
39     private InstanceIdentifier<ItrRemoteLocatorSet> validId;
40     private ItrRemoteLocatorSet validData;
41
42     @Before
43     public void setUp() throws Exception {
44         initMocks(this);
45         customizer = new ItrRemoteLocatorSetCustomizer(api);
46         validId = InstanceIdentifier.create(ItrRemoteLocatorSet.class);
47         validData = new ItrRemoteLocatorSetBuilder().setRemoteLocatorSetName(VALID_NAME).build();
48     }
49
50     @Test
51     public void writeCurrentAttributesSuccess() throws Exception {
52         onWriteSuccess();
53         customizer.writeCurrentAttributes(validId, validData, writeContext);
54         verifyWriteInvoked(true, VALID_NAME);
55     }
56
57     @Test
58     public void writeCurrentAttributesFailed() {
59         onWriteThrow();
60
61         try {
62             customizer.writeCurrentAttributes(validId, validData, writeContext);
63         } catch (WriteFailedException e) {
64             assertTrue(e.getCause() instanceof VppCallbackException);
65             verifyWriteInvoked(true, VALID_NAME);
66             return;
67         }
68
69         fail("Test should have thrown exception");
70     }
71
72     @Test
73     public void updateCurrentAttributes() {
74         try {
75             customizer.updateCurrentAttributes(validId, validData, validData, writeContext);
76         } catch (WriteFailedException e) {
77             assertTrue(e.getCause() instanceof UnsupportedOperationException);
78             return;
79         }
80
81         fail("Test should have thrown exception");
82     }
83
84     @Test
85     public void deleteCurrentAttributesSuccess() throws Exception {
86         onWriteSuccess();
87         customizer.deleteCurrentAttributes(validId, validData, writeContext);
88         verifyWriteInvoked(false, VALID_NAME);
89     }
90
91     @Test
92     public void deleteCurrentAttributesFailed() throws Exception {
93         onWriteThrow();
94
95         try {
96             customizer.writeCurrentAttributes(validId, validData, writeContext);
97         } catch (WriteFailedException e) {
98             assertTrue(e.getCause() instanceof VppCallbackException);
99             verifyWriteInvoked(true, VALID_NAME);
100             return;
101         }
102
103         fail("Test should have thrown exception");
104     }
105
106     private void onWriteSuccess() {
107         when(api.lispAddDelMapRequestItrRlocs(any(LispAddDelMapRequestItrRlocs.class)))
108                 .thenReturn(CompletableFuture.completedFuture(new LispAddDelMapRequestItrRlocsReply()));
109     }
110
111     private void onWriteThrow() {
112         when(api.lispAddDelMapRequestItrRlocs(any(LispAddDelMapRequestItrRlocs.class)))
113                 .thenReturn(new CompletableFuture<LispAddDelMapRequestItrRlocsReply>() {
114                     @Override
115                     public LispAddDelMapRequestItrRlocsReply get(final long l, final TimeUnit timeUnit)
116                             throws InterruptedException, ExecutionException, TimeoutException {
117                         throw new ExecutionException(new VppCallbackException("lispAddDelMapRequestItrRlocs", 1, -2));
118                     }
119                 });
120     }
121
122     private void verifyWriteInvoked(final boolean add, final String name) {
123         verify(api, times(1)).lispAddDelMapRequestItrRlocs(requestCaptor.capture());
124
125         final LispAddDelMapRequestItrRlocs request = requestCaptor.getValue();
126         assertNotNull(request);
127         assertEquals(booleanToByte(add), request.isAdd);
128         assertEquals(name, toString(request.locatorSetName));
129     }
130 }