HONEYCOMB-116: utility for stubbing jvpp methods
[honeycomb.git] / lisp / lisp2vpp / src / test / java / io / fd / honeycomb / lisp / translate / write / RemoteMappingCustomizerTest.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.junit.Assert.assertNotNull;
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.lisp.context.util.EidMappingContext;
27 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
30 import java.util.concurrent.ExecutionException;
31 import org.junit.Test;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Captor;
34 import org.mockito.Mockito;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Builder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.Lisp;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.MapReplyAction;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.MappingId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.EidTable;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTable;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTableKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.RemoteMappings;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.RemoteMapping;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.RemoteMappingBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.RemoteMappingKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.Eid;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.EidBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.locator.list.NegativeMappingBuilder;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.openvpp.jvpp.core.dto.LispAddDelRemoteMapping;
52 import org.openvpp.jvpp.core.dto.LispAddDelRemoteMappingReply;
53
54
55 public class RemoteMappingCustomizerTest extends WriterCustomizerTest {
56
57     @Captor
58     private ArgumentCaptor<LispAddDelRemoteMapping> mappingCaptor;
59
60     private MappingId mappingId;
61     private RemoteMappingCustomizer customizer;
62     private RemoteMapping intf;
63     private InstanceIdentifier<RemoteMapping> id;
64     private EidMappingContext remoteMappingContext;
65
66     @Override
67     public void setUp() {
68         final Eid eid = new EidBuilder().setAddress(
69                 new Ipv4Builder().setIpv4(
70                         new Ipv4Address("192.168.2.1"))
71                         .build())
72                 .build();
73
74         mappingId = new MappingId("REMOTE");
75         final RemoteMappingKey key = new RemoteMappingKey(mappingId);
76         remoteMappingContext = new EidMappingContext("remote");
77
78         intf = new RemoteMappingBuilder()
79                 .setEid(
80                         eid)
81                 .setLocatorList(new NegativeMappingBuilder().setMapReplyAction(MapReplyAction.Drop).build())
82                 .build();
83
84         id = InstanceIdentifier.builder(Lisp.class).child(EidTable.class)
85                 .child(VniTable.class, new VniTableKey(25L))
86                 .child(RemoteMappings.class)
87                 .child(RemoteMapping.class, key).build();
88
89         customizer = new RemoteMappingCustomizer(api, remoteMappingContext);
90
91         when(api.lispAddDelRemoteMapping(Mockito.any())).thenReturn(future(new LispAddDelRemoteMappingReply()));
92         when(mappingContext.read(Mockito.any())).thenReturn(com.google.common.base.Optional
93                 .of(new RemoteMappingBuilder().setKey(key).setId(mappingId).setEid(eid).build()));
94     }
95
96     @Test(expected = NullPointerException.class)
97     public void testWriteCurrentAttributesNullData() throws WriteFailedException {
98         customizer.writeCurrentAttributes(null, null, writeContext);
99     }
100
101     @Test(expected = NullPointerException.class)
102     public void testWriteCurrentAttributesBadData() throws WriteFailedException {
103         customizer
104                 .writeCurrentAttributes(null, mock(RemoteMapping.class), writeContext);
105     }
106
107     @Test
108     public void testWriteCurrentAttributes() throws WriteFailedException, InterruptedException, ExecutionException {
109         //to simulate no mapping
110         when(mappingContext.read(Mockito.any())).thenReturn(com.google.common.base.Optional.absent());
111
112         customizer.writeCurrentAttributes(id, intf, writeContext);
113
114         verify(api, times(1)).lispAddDelRemoteMapping(mappingCaptor.capture());
115
116         LispAddDelRemoteMapping request = mappingCaptor.getValue();
117
118         assertNotNull(request);
119         assertEquals(1, request.isAdd);
120         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.eid).getValue());
121         assertEquals(25, request.vni);
122     }
123
124     @Test(expected = UnsupportedOperationException.class)
125     public void testUpdateCurrentAttributes() throws WriteFailedException {
126         customizer.updateCurrentAttributes(null, null, null, writeContext);
127     }
128
129     @Test(expected = NullPointerException.class)
130     public void testDeleteCurrentAttributesNullData() throws WriteFailedException {
131         customizer.deleteCurrentAttributes(null, null, writeContext);
132     }
133
134     @Test
135     public void testDeleteCurrentAttributes() throws WriteFailedException, InterruptedException, ExecutionException {
136         customizer.deleteCurrentAttributes(id, intf, writeContext);
137
138         verify(api, times(1)).lispAddDelRemoteMapping(mappingCaptor.capture());
139
140         LispAddDelRemoteMapping request = mappingCaptor.getValue();
141
142         assertNotNull(request);
143         assertEquals(0, request.isAdd);
144         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.eid).getValue());
145         assertEquals(25, request.vni);
146     }
147
148 }