63be37a2371b9088b8a0a36f12a3376a7061edcb
[honeycomb.git] / lisp / lisp2vpp / src / test / java / io / fd / honeycomb / lisp / translate / write / MapResolverCustomizerTest.java
1 /*
2  * Copyright (c) 2015 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.mockito.Matchers.any;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.times;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25
26 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import java.util.concurrent.CompletableFuture;
29 import java.util.concurrent.ExecutionException;
30 import org.junit.Test;
31 import org.mockito.ArgumentCaptor;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.map.resolvers.grouping.map.resolvers.MapResolver;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.map.resolvers.grouping.map.resolvers.MapResolverBuilder;
36 import org.openvpp.jvpp.core.dto.LispAddDelMapResolver;
37 import org.openvpp.jvpp.core.dto.LispAddDelMapResolverReply;
38 import org.openvpp.jvpp.core.future.FutureJVppCore;
39
40
41 public class MapResolverCustomizerTest {
42
43     @Test(expected = NullPointerException.class)
44     public void testWriteCurrentAttributesNullData() throws WriteFailedException {
45         new MapResolverCustomizer(mock(FutureJVppCore.class)).writeCurrentAttributes(null, null, null);
46     }
47
48     @Test(expected = NullPointerException.class)
49     public void testWriteCurrentAttributesBadData() throws WriteFailedException {
50         new MapResolverCustomizer(mock(FutureJVppCore.class))
51                 .writeCurrentAttributes(null, new MapResolverBuilder().build(), null);
52     }
53
54     @Test
55     public void testWriteCurrentAttributes() throws WriteFailedException, InterruptedException, ExecutionException {
56         FutureJVppCore fakeJvpp = mock(FutureJVppCore.class);
57
58         MapResolverCustomizer customizer = new MapResolverCustomizer(fakeJvpp);
59         Ipv4Address address = new Ipv4Address("192.168.2.1");
60         MapResolver resolver = new MapResolverBuilder().setIpAddress(new IpAddress(address)).build();
61
62         ArgumentCaptor<LispAddDelMapResolver> resolverCaptor = ArgumentCaptor.forClass(LispAddDelMapResolver.class);
63
64         LispAddDelMapResolverReply fakeReply = new LispAddDelMapResolverReply();
65
66         CompletableFuture<LispAddDelMapResolverReply> finalStage = new CompletableFuture<>();
67         finalStage.complete(fakeReply);
68
69         when(fakeJvpp.lispAddDelMapResolver(any(LispAddDelMapResolver.class))).thenReturn(finalStage);
70
71         customizer.writeCurrentAttributes(null, resolver, null);
72         verify(fakeJvpp, times(1)).lispAddDelMapResolver(resolverCaptor.capture());
73
74         LispAddDelMapResolver request = resolverCaptor.getValue();
75         assertEquals(1, request.isAdd);
76         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.ipAddress).getValue());
77     }
78
79
80     @Test(expected = UnsupportedOperationException.class)
81     public void testUpdateCurrentAttributes() throws WriteFailedException {
82         new MapResolverCustomizer(mock(FutureJVppCore.class)).updateCurrentAttributes(null, null, null, null);
83     }
84
85     @Test
86     public void testDeleteCurrentAttributes() throws InterruptedException, ExecutionException, WriteFailedException {
87
88         FutureJVppCore fakeJvpp = mock(FutureJVppCore.class);
89
90         MapResolverCustomizer customizer = new MapResolverCustomizer(fakeJvpp);
91         Ipv4Address address = new Ipv4Address("192.168.2.1");
92         MapResolver resolver = new MapResolverBuilder().setIpAddress(new IpAddress(address)).build();
93
94         ArgumentCaptor<LispAddDelMapResolver> resolverCaptor = ArgumentCaptor.forClass(LispAddDelMapResolver.class);
95
96         LispAddDelMapResolverReply fakeReply = new LispAddDelMapResolverReply();
97
98         CompletableFuture<LispAddDelMapResolverReply> finalStage = new CompletableFuture<>();
99         finalStage.complete(fakeReply);
100
101         when(fakeJvpp.lispAddDelMapResolver(any(LispAddDelMapResolver.class))).thenReturn(finalStage);
102
103         customizer.deleteCurrentAttributes(null, resolver, null);
104         verify(fakeJvpp, times(1)).lispAddDelMapResolver(resolverCaptor.capture());
105
106         LispAddDelMapResolver request = resolverCaptor.getValue();
107         assertEquals(0, request.isAdd);
108         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.ipAddress).getValue());
109     }
110
111 }