Checkstyle: fix issues and enforce on implementation
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / MappingSystemTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.lispflowmapping.implementation;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.collect.Lists;
15 import java.lang.reflect.Field;
16 import java.net.InetAddress;
17 import java.net.UnknownHostException;
18 import java.util.Arrays;
19 import java.util.Date;
20 import java.util.EnumMap;
21 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.runners.MockitoJUnitRunner;
29 import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
30 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
31 import org.opendaylight.lispflowmapping.implementation.util.MappingMergeUtil;
32 import org.opendaylight.lispflowmapping.inmemorydb.HashMapDb;
33 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
34 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
35 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMapCache;
36 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
37 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ExplicitLocatorPathLcaf;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ServicePathIdType;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPathBuilder;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4PrefixBuilder;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePath;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePathBuilder;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.Hop;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.HopBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4Binary;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.EidUri;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.MappingBuilder;
69 import org.powermock.api.mockito.PowerMockito;
70 import org.powermock.core.classloader.annotations.PrepareForTest;
71 import org.powermock.modules.junit4.PowerMockRunner;
72 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
73
74 @RunWith(PowerMockRunner.class)
75 @PowerMockRunnerDelegate(MockitoJUnitRunner.class)
76 @PrepareForTest(MappingMergeUtil.class)
77 public class MappingSystemTest {
78
79     private static ILispDAO daoMock = Mockito.mock(ILispDAO.class);
80     @Mock private static IMapCache smcMock;
81     @Mock private static IMapCache pmcMock;
82     @Mock private static DataStoreBackEnd dsbeMock;
83     @Mock private static EnumMap<MappingOrigin, IMapCache> tableMapMock;
84     @InjectMocks private static MappingSystem mappingSystem = new MappingSystem(daoMock, false, true, true);
85
86     private static final String IPV4_SRC =      "127.0.0.1";
87     private static final String IPV4_DST =      "192.168.0.1";
88     private static final String IPV4_STRING_1 = "1.2.3.0";
89     private static final String IPV4_STRING_2 = "1.2.4.0";
90     private static final String IPV4_STRING_3 = "1.2.5.0";
91     private static final String MASK =          "/20";
92
93     private static final Eid EID_IPV4_SRC = LispAddressUtil.asIpv4Eid(IPV4_SRC);
94     private static final Eid EID_IPV4_DST = LispAddressUtil.asIpv4Eid(IPV4_DST);
95     private static final Eid EID_IPV4_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
96     private static final Eid EID_IPV4_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
97     private static final Eid EID_SERVICE_PATH = new EidBuilder().setAddress(getDefaultServicePath((short) 253)).build();
98     private static final Eid EID_SERVICE_PATH_INDEX_OOB = new EidBuilder()
99             .setAddress(getDefaultServicePath((short) 200)).build(); // with index out of bounds
100
101     public static final SimpleAddress SIMPLE_ADDR_1 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_1)));
102     public static final SimpleAddress SIMPLE_ADDR_2 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_2)));
103     public static final SimpleAddress SIMPLE_ADDR_3 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_3)));
104
105     private static final Hop HOP_1 = new HopBuilder().setHopId("hop-id-1").setAddress(SIMPLE_ADDR_1).build();
106     private static final Hop HOP_2 = new HopBuilder().setHopId("hop-id-2").setAddress(SIMPLE_ADDR_2).build();
107     private static final Hop HOP_3 = new HopBuilder().setHopId("hop-id-3").setAddress(SIMPLE_ADDR_3).build();
108
109     private static final MappingAuthkeyBuilder MAPPING_AUTHKEY_BUILDER = new MappingAuthkeyBuilder()
110             .setKeyType(1).setKeyString("pass-1");
111     private static final Rloc RLOC = LispAddressUtil.toRloc(new Ipv4Address(IPV4_STRING_2));
112     private static final ConfigIni CONFIG_INI = ConfigIni.getInstance();
113     private static final long REGISTRATION_VALIDITY = CONFIG_INI.getRegistrationValiditySb();
114     private static final Date EXPIRED_DATE = new Date(System.currentTimeMillis() - (REGISTRATION_VALIDITY + 1L));
115     private static final Object DUMMY_OBJECT = "dummy-object";
116     private static final SiteId SITE_ID = new SiteId(new byte[]{0, 1, 2, 3, 4, 5, 6, 7});
117
118     @Before
119     public void init() throws Exception {
120         Mockito.when(daoMock.putTable(MappingOrigin.Southbound.toString())).thenReturn(Mockito.mock(HashMapDb.class));
121         Mockito.when(daoMock.putTable(MappingOrigin.Northbound.toString())).thenReturn(Mockito.mock(HashMapDb.class));
122
123         Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
124         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
125
126         injectMocks();
127         setLookupPolicy(IMappingService.LookupPolicy.NB_FIRST);
128     }
129
130     /**
131      * This method injects mocks which can not be injected the usual way using Mockito.
132      */
133     private static void injectMocks() throws NoSuchFieldException, IllegalAccessException {
134         final Field smcField = MappingSystem.class.getDeclaredField("smc");
135         smcField.setAccessible(true);
136         smcField.set(mappingSystem, smcMock);
137
138         final Field pmcField = MappingSystem.class.getDeclaredField("pmc");
139         pmcField.setAccessible(true);
140         pmcField.set(mappingSystem, pmcMock);
141
142         final Field tableMapField = MappingSystem.class.getDeclaredField("tableMap");
143         tableMapField.setAccessible(true);
144         tableMapField.set(mappingSystem, tableMapMock);
145     }
146
147     /**
148      * This method changes the lookup policy.
149      */
150     private static void setLookupPolicy(IMappingService.LookupPolicy policy) throws NoSuchFieldException,
151             IllegalAccessException {
152         final Field lookupPolicy = ConfigIni.class.getDeclaredField("lookupPolicy");
153         lookupPolicy.setAccessible(true);
154         lookupPolicy.set(CONFIG_INI, policy);
155     }
156
157     /**
158      * Tests {@link MappingSystem#getMapping} method with NB mapping == null, expired mapping.
159      */
160     @Test
161     public void getMappingTest_NbFirst_withNullExpiredNbMapping() {
162         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
163                 .setTimestamp(EXPIRED_DATE.getTime()).build();
164         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
165         Mockito.when(smcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(mappingRecord);
166
167         final Mapping mapping = new MappingBuilder()
168                 .setEidUri(new EidUri("ipv4:1.2.3.0"))
169                 .setOrigin(MappingOrigin.Southbound)
170                 .setSiteId(Lists.newArrayList(SITE_ID))
171                 .setMappingRecord(mappingRecord).build();
172
173         assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
174         Mockito.verify(dsbeMock).removeMapping(mapping);
175     }
176
177     /**
178      * Tests {@link MappingSystem#getMapping} method with NB mapping == null, not expired mapping.
179      * Returns the original mapping.
180      */
181     @Test
182     public void getMappingTest_NbFirst_withNullNbMapping() {
183         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build();
184         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
185         Mockito.when(smcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(mappingRecord);
186
187         assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
188     }
189
190     /**
191      * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and multiple locator record.
192      * Returns the original mapping.
193      */
194     @Test
195     public void getMappingTest_NbFirst_withServicePathDestinationAddress_multipleLocatorRecords() {
196         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
197                 .setLocatorRecord(Lists.newArrayList(
198                         getDefaultLocatorRecordBuilder().build(), // set two locators
199                         getDefaultLocatorRecordBuilder().build())).build();
200         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
201
202         assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
203     }
204
205     /**
206      * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single Ipv4 type locator
207      * record. Returns the original mapping.
208      */
209     @Test
210     public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleIpv4LocatorRecord() {
211         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
212                 .setLocatorRecord(Lists.newArrayList(
213                         getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
214         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
215
216         assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
217     }
218
219     /**
220      * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
221      * type locator record. Service index out of bounds. Returns the original mapping.
222      */
223     @Test
224     public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleELPLocatorRecord_IndexOutOfBounds() {
225         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
226                 .setLocatorRecord(Lists.newArrayList(
227                         getDefaultLocatorRecordBuilder()
228                                 .setRloc(getELPTypeRloc()).build())).build();
229         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB)).thenReturn(mappingRecord);
230
231         assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB));
232     }
233
234     /**
235      * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
236      * type locator record.
237      */
238     @Test
239     public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleELPLocatorRecord()
240             throws UnknownHostException {
241         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
242                 .setLocatorRecord(Lists.newArrayList(
243                         getDefaultLocatorRecordBuilder()
244                                 .setRloc(getELPTypeRloc()).build())).build();
245         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
246
247         final MappingRecord result = (MappingRecord) mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH);
248         final Ipv4Binary ipv4Result = (Ipv4Binary) result.getLocatorRecord().get(0).getRloc().getAddress();
249         assertTrue(Arrays
250                 .equals(InetAddress.getByName(IPV4_STRING_3).getAddress(), ipv4Result.getIpv4Binary().getValue()));
251     }
252
253     /**
254      * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single IpPrefix type locator
255      * record. Returns the original mapping.
256      */
257     @Test
258     public void getMappingTest_NbFirst_withServicePathDestinationAddress_IpPrefixLocatorRecord() {
259         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
260                 .setLocatorRecord(Lists.newArrayList(
261                         getDefaultLocatorRecordBuilder()
262                                 .setRloc(getIpPrefixTypeRloc()).build())).build();
263         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
264
265         assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
266     }
267
268     /**
269      * Tests {@link MappingSystem#getMapping} method with Ipv4 type dst address.
270      */
271     @Test
272     public void getMappingTest_NbFirst() {
273         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
274                 .setLocatorRecord(Lists.newArrayList(
275                         getDefaultLocatorRecordBuilder().build())).build();
276         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(mappingRecord);
277
278         assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
279     }
280
281     /**
282      * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection.
283      */
284     @Test
285     public void getMappingTest_NbSbIntersection_withNullNbMapping() throws NoSuchFieldException,
286             IllegalAccessException {
287         setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
288         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
289
290         assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
291     }
292
293     /**
294      * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with ServicePath type dst
295      * address and single Ipv4 type locator record. Returns the original mapping.
296      */
297     @Test
298     public void getMappingTest_NbSbIntersection_withServicePathDestinationAddress() throws NoSuchFieldException,
299             IllegalAccessException {
300         setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
301         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
302                 .setLocatorRecord(Lists.newArrayList(
303                         getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
304         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
305
306         assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
307     }
308
309     /**
310      * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
311      * locator, southbound null. Returns the original mapping.
312      */
313     @Test
314     public void getMappingTest_NbSbIntersection_withSbNull() throws NoSuchFieldException,
315             IllegalAccessException {
316         setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
317
318         final MappingRecord nbMappingRecord = getDefaultMappingRecordBuilder()
319                 .setLocatorRecord(Lists.newArrayList(
320                         getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
321
322         final MappingRecord sbMappingRecord = getDefaultMappingRecordBuilder()
323                 .setTimestamp(EXPIRED_DATE.getTime())
324                 .setLocatorRecord(Lists.newArrayList(
325                         getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
326
327         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(nbMappingRecord);
328         Mockito.when(smcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(sbMappingRecord);
329
330         final Mapping mapping = new MappingBuilder()
331                 .setEidUri(new EidUri("ipv4:1.2.3.0"))
332                 .setOrigin(MappingOrigin.Southbound)
333                 .setSiteId(Lists.newArrayList(SITE_ID))
334                 .setMappingRecord(sbMappingRecord).build();
335
336         assertEquals(nbMappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
337         Mockito.verify(dsbeMock).removeMapping(mapping);
338     }
339
340     /**
341      * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
342      * locator, southbound non-null. Returns the merged mapping.
343      */
344     @Test
345     public void getMappingTest_NbSbIntersection_mergeMappings() throws NoSuchFieldException,
346             IllegalAccessException {
347         setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
348
349         final MappingRecord nbMappingRecord = getDefaultMappingRecordBuilder()
350                 .setLocatorRecord(Lists.newArrayList(
351                         getDefaultLocatorRecordBuilder().build())).build();
352
353         final MappingRecord sbMappingRecord = getDefaultMappingRecordBuilder()
354                 .setLocatorRecord(Lists.newArrayList(
355                         getDefaultLocatorRecordBuilder().build())).build();
356
357         // this mock will be ultimately returned when MappingMergeUtil.computeNbSbIntersection is called
358         final MappingRecord resultMock = Mockito.mock(MappingRecord.class);
359
360         PowerMockito.mockStatic(MappingMergeUtil.class);
361         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(nbMappingRecord);
362         Mockito.when(smcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(sbMappingRecord);
363         PowerMockito.when(MappingMergeUtil.computeNbSbIntersection(nbMappingRecord, sbMappingRecord))
364                 .thenReturn(resultMock);
365
366         assertEquals(resultMock, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
367     }
368
369     /**
370      * Tests {@link MappingSystem#getMapping(Eid dst)} method.
371      */
372     @Test
373     public void getMappingTest_withSrcNull() {
374         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build();
375         Mockito.when(pmcMock.getMapping(null, EID_IPV4_DST)).thenReturn(null);
376         Mockito.when(smcMock.getMapping(null, EID_IPV4_DST)).thenReturn(mappingRecord);
377
378         assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_DST));
379     }
380
381     /**
382      * Tests {@link MappingSystem#getMapping(MappingOrigin, Eid)} method.
383      */
384     @Test
385     public void getMappingTest_withMappingOrigin() {
386         mappingSystem.getMapping(MappingOrigin.Southbound, EID_IPV4_SRC);
387         Mockito.verify(smcMock).getMapping(null, EID_IPV4_SRC);
388
389         mappingSystem.getMapping(MappingOrigin.Northbound, EID_IPV4_SRC);
390         Mockito.verify(pmcMock).getMapping(null, EID_IPV4_SRC);
391     }
392
393     /**
394      * Tests {@link MappingSystem#removeMapping} method.
395      */
396     @Test
397     public void removeMappingTest_nb() {
398         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
399
400         mappingSystem.removeMapping(MappingOrigin.Northbound, EID_IPV4_1);
401         Mockito.verify(pmcMock).removeMapping(EID_IPV4_1, true);
402     }
403
404     /**
405      * Tests {@link MappingSystem#removeMapping} method.
406      */
407     @Test
408     public void removeMappingTest_sb() throws NoSuchFieldException, IllegalAccessException {
409         mappingSystem = new MappingSystem(daoMock, false, true, false);
410         injectMocks();
411         Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
412
413         mappingSystem.removeMapping(MappingOrigin.Southbound, EID_IPV4_1);
414         Mockito.verify(smcMock).removeMapping(EID_IPV4_1, false);
415     }
416
417     /**
418      * Tests {@link MappingSystem#updateMappingRegistration} method.
419      */
420     @Test
421     public void updateMappingRegistrationTest() {
422         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
423
424         mappingSystem.updateMappingRegistration(MappingOrigin.Northbound, EID_IPV4_1, 1L);
425         Mockito.verify(pmcMock).updateMappingRegistration(EID_IPV4_1, 1L);
426     }
427
428     /**
429      * Tests {@link MappingSystem#restoreDaoFromDatastore} method.
430      */
431     @Test
432     public void restoreDaoFromDatastoreTest() {
433         final Mapping mapping_1 = new MappingBuilder()
434                 .setOrigin(MappingOrigin.Northbound)
435                 .setMappingRecord(getDefaultMappingRecordBuilder()
436                         .setEid(EID_IPV4_1)
437                         .setTimestamp(EXPIRED_DATE.getTime()).build()).build();
438         final Mapping mapping_2 = new MappingBuilder()
439                 .setOrigin(MappingOrigin.Northbound)
440                 .setMappingRecord(getDefaultMappingRecordBuilder()
441                         .setEid(EID_IPV4_2).build()).build();
442
443         final MappingAuthkey mappingAuthkey_1 = MAPPING_AUTHKEY_BUILDER.build();
444         final AuthenticationKey authenticationKey_1 = new AuthenticationKeyBuilder()
445                 .setMappingAuthkey(mappingAuthkey_1)
446                 .setEid(EID_IPV4_1).build();
447
448         final MappingAuthkey mappingAuthkey_2 = MAPPING_AUTHKEY_BUILDER.setKeyString("pass-2").build();
449         final AuthenticationKey authenticationKey_2 = new AuthenticationKeyBuilder()
450                 .setMappingAuthkey(mappingAuthkey_2)
451                 .setEid(EID_IPV4_2).build();
452
453         final List<Mapping> mappings = Lists.newArrayList(mapping_1, mapping_2);
454         final List<AuthenticationKey> authenticationKeys = Lists.newArrayList(authenticationKey_1, authenticationKey_2);
455
456         Mockito.when(dsbeMock.getAllMappings()).thenReturn(mappings);
457         Mockito.when(dsbeMock.getAllAuthenticationKeys()).thenReturn(authenticationKeys);
458         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
459
460         mappingSystem.initialize();
461         Mockito.verify(dsbeMock, Mockito.times(1)).removeMapping(mapping_1);
462         Mockito.verify(pmcMock).addMapping(EID_IPV4_2, mapping_2.getMappingRecord(), true, false);
463         Mockito.verify(smcMock).addAuthenticationKey(EID_IPV4_1, mappingAuthkey_1);
464         Mockito.verify(smcMock).addAuthenticationKey(EID_IPV4_2, mappingAuthkey_2);
465     }
466
467     /**
468      * Tests {@link MappingSystem#addAuthenticationKey} method.
469      */
470     @Test
471     public void addAuthenticationKeyTest() {
472         mappingSystem.addAuthenticationKey(EID_IPV4_1, MAPPING_AUTHKEY_BUILDER.build());
473         Mockito.verify(smcMock).addAuthenticationKey(EID_IPV4_1, MAPPING_AUTHKEY_BUILDER.build());
474     }
475
476     /**
477      * Tests {@link MappingSystem#getAuthenticationKey} method.
478      */
479     @Test
480     public void getAuthenticationKeyTest() {
481         Mockito.when(smcMock.getAuthenticationKey(EID_IPV4_1)).thenReturn(MAPPING_AUTHKEY_BUILDER.build());
482         assertEquals(MAPPING_AUTHKEY_BUILDER.build(), mappingSystem.getAuthenticationKey(EID_IPV4_1));
483     }
484
485     /**
486      * Tests {@link MappingSystem#removeAuthenticationKey} method.
487      */
488     @Test
489     public void removeAuthenticationKeyTest() {
490         mappingSystem.removeAuthenticationKey(EID_IPV4_1);
491         Mockito.verify(smcMock).removeAuthenticationKey(EID_IPV4_1);
492     }
493
494     /**
495      * Tests {@link MappingSystem#addData} method.
496      */
497     @Test
498     public void addDataTest() {
499         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
500
501         mappingSystem.addData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
502         Mockito.verify(pmcMock).addData(EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
503     }
504
505     /**
506      * Tests {@link MappingSystem#getData} method.
507      */
508     @Test
509     public void getDataTest() {
510         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
511         Mockito.when(pmcMock.getData(EID_IPV4_1, SubKeys.RECORD)).thenReturn(DUMMY_OBJECT);
512
513         assertEquals(DUMMY_OBJECT, mappingSystem.getData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD));
514     }
515
516     /**
517      * Tests {@link MappingSystem#removeData} method.
518      */
519     @Test
520     public void removeDataTest() {
521         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
522
523         mappingSystem.removeData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD);
524         Mockito.verify(pmcMock).removeData(EID_IPV4_1, SubKeys.RECORD);
525     }
526
527     /**
528      * Tests {@link MappingSystem#cleanCaches} method.
529      */
530     @Test
531     public void cleanCachesTest() {
532         mappingSystem.cleanCaches();
533         Mockito.verify(daoMock).removeAll();
534     }
535
536     /**
537      * Following test are executed for coverage-increase purpose.
538      */
539     @Test
540     public void otherTests() throws NoSuchFieldException, IllegalAccessException {
541         mappingSystem.printMappings();
542         Mockito.verify(pmcMock).printMappings();
543         Mockito.verify(smcMock).printMappings();
544
545         mappingSystem.destroy();
546         mappingSystem = new MappingSystem(daoMock, true, true, true);
547         mappingSystem.setDataStoreBackEnd(dsbeMock);
548         mappingSystem.setOverwritePolicy(true);
549         mappingSystem.setIterateMask(true);
550     }
551
552     private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
553         return new MappingRecordBuilder()
554                 .setTimestamp(System.currentTimeMillis())
555                 .setSiteId(SITE_ID)
556                 .setEid(EID_IPV4_1);
557     }
558
559     private static ServicePath getDefaultServicePath(short index) {
560         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address
561                 .service.path.ServicePathBuilder servicePathBuilder = new org.opendaylight.yang.gen.v1.urn.ietf.params
562                 .xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.service.path.ServicePathBuilder();
563
564         return new ServicePathBuilder().setServicePath(servicePathBuilder
565                 .setServiceIndex(index)
566                 .setServicePathId(new ServicePathIdType(1L)).build())
567                 .build();
568     }
569
570     private static LocatorRecordBuilder getDefaultLocatorRecordBuilder() {
571         return new LocatorRecordBuilder()
572                 .setLocatorId("locator-id")
573                 .setRloc(RLOC);
574     }
575
576     private static Rloc getELPTypeRloc() {
577
578         final ExplicitLocatorPath explicitLocatorPath = new ExplicitLocatorPathBuilder().setExplicitLocatorPath(new org
579                 .opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
580                 .address.explicit.locator.path.ExplicitLocatorPathBuilder()
581                 .setHop(Lists.newArrayList(HOP_1, HOP_2, HOP_3)).build()).build();
582
583         return new RlocBuilder()
584                 .setAddress(explicitLocatorPath)
585                 .setAddressType(ExplicitLocatorPathLcaf.class).build();
586     }
587
588     private static Rloc getIpPrefixTypeRloc() {
589
590         final Address address = new Ipv4PrefixBuilder().setIpv4Prefix(new org.opendaylight.yang.gen.v1.urn.ietf.params
591                 .xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix(IPV4_STRING_1 + MASK)).build();
592
593         return new RlocBuilder()
594                 .setAddress(address)
595                 .setAddressType(Ipv4PrefixAfi.class).build();
596     }
597 }