Merge "Restoring authentication key from MDSAL."
[lispflowmapping.git] / mappingservice / mapcache / src / test / java / org / opendaylight / lispflowmapping / mapcache / MappingMergeUtilTest.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.mapcache;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15
16 import com.google.common.collect.Lists;
17 import com.google.common.collect.Sets;
18
19 import java.util.ArrayList;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Set;
23
24 import org.junit.Test;
25 import org.mockito.Mockito;
26 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container
36         .MappingRecordBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
38
39 public class MappingMergeUtilTest {
40
41     private static final String IPV4_STRING_1 =         "1.2.3.0";
42     private static final String IPV4_STRING_2 =         "1.2.4.0";
43     private static final String IPV4_RLOC_STRING_1 =    "101.101.101.101";
44     private static final String IPV4_RLOC_STRING_2 =    "102.102.102.102";
45     private static final String IPV4_RLOC_STRING_3 =    "103.103.103.103";
46     private static final String IPV4_RLOC_STRING_4 =    "104.104.104.104";
47     private static final String IPV4_STRING_PREFIX_24 = "/24";
48     private static final String IPV4_STRING_PREFIX_16 = "/16";
49     private static final Eid IPV4_PREFIX_EID_1 = LispAddressUtil.asIpv4PrefixEid(IPV4_STRING_1 + IPV4_STRING_PREFIX_24);
50     private static final Eid IPV4_PREFIX_EID_2 = LispAddressUtil.asIpv4PrefixEid(IPV4_STRING_2 + IPV4_STRING_PREFIX_16);
51     private static final Rloc IPV4_RLOC_1 = LispAddressUtil.asIpv4Rloc(IPV4_RLOC_STRING_1);
52     private static final Rloc IPV4_RLOC_2 = LispAddressUtil.asIpv4Rloc(IPV4_RLOC_STRING_2);
53     private static final Rloc IPV4_RLOC_3 = LispAddressUtil.asIpv4Rloc(IPV4_RLOC_STRING_3);
54     private static final Rloc IPV4_RLOC_4 = LispAddressUtil.asIpv4Rloc(IPV4_RLOC_STRING_4);
55     private static final IpAddressBinary IPV4_SOURCE_RLOC_1 = new IpAddressBinary(
56             new Ipv4AddressBinary(new byte[] {1, 1, 1, 1}));
57     private static final IpAddressBinary IPV4_SOURCE_RLOC_2 = new IpAddressBinary(
58             new Ipv4AddressBinary(new byte[] {2, 2, 2, 2}));
59     private static final IpAddressBinary IPV4_SOURCE_RLOC_3 = new IpAddressBinary(
60             new Ipv4AddressBinary(new byte[] {3, 3, 3, 3}));
61
62     private static final String LOCATOR_ID_STRING = "locator-id";
63
64     private static final XtrId XTR_ID_1 = new XtrId(new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
65     private static final XtrId XTR_ID_2 = new XtrId(new byte[] {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2});
66     private static final XtrId XTR_ID_3 = new XtrId(new byte[] {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3});
67
68     private static final SiteId SITE_ID_1 = new SiteId(new byte[]{1, 1, 1, 1, 1, 1, 1, 1});
69     private static final SiteId SITE_ID_2 = new SiteId(new byte[]{2, 2, 2, 2, 2, 2, 2, 2});
70
71     // TODO Change when configuration infra is ready
72     private static final long REGISTRATION_VALIDITY = 200000L;
73
74     /**
75      * Tests {@link MappingMergeUtil#mergeLocatorRecords} method with a new LocatorRecord equal to current
76      * LocatorRecord and null XtrId.
77      */
78     @Test
79     public void mergeLocatorRecordsTest_withSingleEqualLocator() {
80         // current locator record
81         final LocatorRecord currentLocatorRecord_1 = new LocatorRecordBuilder() // locator equals to newLocatorRecord_1
82                 .setRloc(IPV4_RLOC_1)
83                 .setLocatorId(LOCATOR_ID_STRING).build();
84
85         final MappingRecordBuilder currentMergedMappingBuilder = getDefaultMappingRecordBuilder();
86         currentMergedMappingBuilder.getLocatorRecord().add(currentLocatorRecord_1);
87
88         // new locator record
89         final LocatorRecord newLocatorRecord_1 = new LocatorRecordBuilder()
90                 .setRloc(IPV4_RLOC_1)
91                 .setLocatorId(LOCATOR_ID_STRING).build();
92
93         final MappingRecordBuilder newMappingRecordBuilder = getDefaultMappingRecordBuilder().setEid(IPV4_PREFIX_EID_2)
94                 .setLocatorRecord(Lists.newArrayList())
95                 .setXtrId(XTR_ID_2)
96                 .setRecordTtl(1)
97                 .setSiteId(SITE_ID_2);
98         newMappingRecordBuilder.getLocatorRecord().add(newLocatorRecord_1);
99
100         // expected result
101         MappingRecordBuilder mrb = new MappingRecordBuilder(currentMergedMappingBuilder.build())
102                 .setXtrId(XTR_ID_2)
103                 .setRecordTtl(1)
104                 .setSiteId(SITE_ID_2);
105
106         MappingRecord result = MappingMergeUtil.mergeMappings
107                 (currentMergedMappingBuilder.build(), newMappingRecordBuilder.build(), null, null);
108         assertEquals(mrb.build(),result);
109     }
110
111     /**
112      * Tests {@link MappingMergeUtil#mergeLocatorRecords} method with a new LocatorRecord not equal to current
113      * LocatorRecord but same Rloc, where current locator is local. XtrId is null.
114      */
115     @Test
116     public void mergeLocatorRecordsTest_withSingleSameRloc_currentIsLocal() {
117         // current locator record
118         final LocatorRecord currentLocatorRecord_1 = new LocatorRecordBuilder() // Rloc equals to Rloc in
119                 .setRloc(IPV4_RLOC_1)                                           // newLocatorRecord_1
120                 .setLocatorId(LOCATOR_ID_STRING)
121                 .setLocalLocator(true).build();                                 // local locator set to true
122
123         final MappingRecordBuilder currentMergedMappingBuilder = getDefaultMappingRecordBuilder();
124         currentMergedMappingBuilder.getLocatorRecord().add(currentLocatorRecord_1);
125
126         // new locator record
127         final LocatorRecord newLocatorRecord_1 = new LocatorRecordBuilder()
128                 .setRloc(IPV4_RLOC_1)
129                 .setLocatorId("locator-id-1").build();                          // different locator id
130
131         final MappingRecordBuilder newMappingRecordBuilder = getDefaultMappingRecordBuilder().setEid(IPV4_PREFIX_EID_2)
132                 .setLocatorRecord(Lists.newArrayList())
133                 .setXtrId(XTR_ID_2)
134                 .setRecordTtl(1)
135                 .setSiteId(SITE_ID_2);
136         newMappingRecordBuilder.getLocatorRecord().add(newLocatorRecord_1);
137
138         // expected result
139         MappingRecordBuilder mrb = new MappingRecordBuilder(getDefaultMappingRecordBuilder()
140                     .setTimestamp(currentMergedMappingBuilder.getTimestamp()).build())
141                 .setXtrId(XTR_ID_2)
142                 .setRecordTtl(1)
143                 .setSiteId(SITE_ID_2);
144         mrb.getLocatorRecord().add(currentLocatorRecord_1);
145
146         MappingRecord result = MappingMergeUtil.mergeMappings
147                 (currentMergedMappingBuilder.build(), newMappingRecordBuilder.build(), null, null);
148         assertEquals(mrb.build(),result);
149     }
150
151     /**
152      * Tests {@link MappingMergeUtil#mergeLocatorRecords} method with a new LocatorRecord not equal to current
153      * LocatorRecord but same Rloc, where current locator is not local. XtrId is null.
154      */
155     @Test
156     public void mergeLocatorRecordsTest_withSingleSameRloc_currentIsNotLocal() {
157         // current locator record
158         final long timestamp = 1L;
159         final LocatorRecord currentLocatorRecord_1 = new LocatorRecordBuilder() // Rloc equals to Rloc in
160                 .setRloc(IPV4_RLOC_1)                                           // newLocatorRecord_1
161                 .setLocatorId(LOCATOR_ID_STRING)
162                 .setLocalLocator(false).build();                                // local locator set to false
163
164         final MappingRecordBuilder currentMergedMappingBuilder = getDefaultMappingRecordBuilder()
165                 .setTimestamp(timestamp);
166         currentMergedMappingBuilder.getLocatorRecord().add(currentLocatorRecord_1);
167
168         // new locator record
169         final LocatorRecord newLocatorRecord_1 = new LocatorRecordBuilder()
170                 .setRloc(IPV4_RLOC_1)
171                 .setLocatorId("locator-id-1").build();                          // different locator id
172
173         final MappingRecordBuilder newMappingRecordBuilder = getDefaultMappingRecordBuilder().setEid(IPV4_PREFIX_EID_2)
174                 .setLocatorRecord(Lists.newArrayList())
175                 .setXtrId(XTR_ID_2)
176                 .setRecordTtl(1)
177                 .setSiteId(SITE_ID_2);
178         newMappingRecordBuilder.getLocatorRecord().add(newLocatorRecord_1);
179
180         // expected result
181         MappingRecordBuilder mrb = new MappingRecordBuilder(getDefaultMappingRecordBuilder().build())
182                 .setXtrId(XTR_ID_2)
183                 .setRecordTtl(1)
184                 .setSiteId(SITE_ID_2).setTimestamp(timestamp);
185         mrb.getLocatorRecord().add(newLocatorRecord_1);
186
187         MappingRecord result = MappingMergeUtil.mergeMappings
188                 (currentMergedMappingBuilder.build(), newMappingRecordBuilder.build(), null, null);
189         assertEquals(mrb.build(),result);
190     }
191
192     /**
193      * Tests {@link MappingMergeUtil#mergeLocatorRecords} method with different Rlocs. XtrId is null.
194      */
195     @Test
196     public void mergeLocatorRecordsTest_withSingleDifferentRloc() {
197         // current locator record
198         final LocatorRecord currentLocatorRecord_1 = new LocatorRecordBuilder() // different locator record
199                 .setRloc(IPV4_RLOC_1)
200                 .setLocatorId(LOCATOR_ID_STRING).build();
201
202         final MappingRecordBuilder currentMergedMappingBuilder = getDefaultMappingRecordBuilder();
203         currentMergedMappingBuilder.getLocatorRecord().add(currentLocatorRecord_1);
204
205         // new locator record
206         final LocatorRecord newLocatorRecord_1 = new LocatorRecordBuilder() // different locator record
207                 .setRloc(IPV4_RLOC_2)
208                 .setLocatorId("locator-id-1").build();
209
210         final MappingRecordBuilder newMappingRecordBuilder = getDefaultMappingRecordBuilder().setEid(IPV4_PREFIX_EID_2)
211                 .setLocatorRecord(Lists.newArrayList())
212                 .setXtrId(XTR_ID_2)
213                 .setRecordTtl(1)
214                 .setSiteId(SITE_ID_2);
215         newMappingRecordBuilder.getLocatorRecord().add(newLocatorRecord_1);
216
217         // expected result
218         MappingRecordBuilder mrb = new MappingRecordBuilder(getDefaultMappingRecordBuilder().build())
219                 .setXtrId(XTR_ID_2)
220                 .setRecordTtl(1)
221                 .setSiteId(SITE_ID_2)
222                 .setTimestamp(currentMergedMappingBuilder.getTimestamp());
223         mrb.getLocatorRecord().add(currentLocatorRecord_1);
224         mrb.getLocatorRecord().add(newLocatorRecord_1);
225
226         MappingRecord result = MappingMergeUtil.mergeMappings
227                 (currentMergedMappingBuilder.build(), newMappingRecordBuilder.build(), null, null);
228         assertEquals(mrb.build(),result);
229     }
230
231     /**
232      * Tests {@link MappingMergeUtil#mergeMappings} method with multiple locator records. We assume that
233      * provided lists of locator records are pre-ordered.
234      */
235     @Test
236     public void mergeMappingsTest_withMultipleSortedLocatorRecords() {
237         // current locator records
238         final LocatorRecord currentLocatorRecord_1 = new LocatorRecordBuilder()
239                 .setRloc(IPV4_RLOC_1)
240                 .setLocatorId(LOCATOR_ID_STRING).build();
241
242         final LocatorRecord currentLocatorRecord_3 = new LocatorRecordBuilder()
243                 .setRloc(IPV4_RLOC_3)
244                 .setLocatorId(LOCATOR_ID_STRING).build();
245
246         final MappingRecordBuilder currentMergedMappingBuilder = getDefaultMappingRecordBuilder();
247         currentMergedMappingBuilder.getLocatorRecord().add(currentLocatorRecord_1);
248         currentMergedMappingBuilder.getLocatorRecord().add(currentLocatorRecord_3);
249
250         // new locator records
251         final LocatorRecord newLocatorRecord_2 = new LocatorRecordBuilder()
252                 .setRloc(IPV4_RLOC_2)
253                 .setLocatorId(LOCATOR_ID_STRING).build();
254
255         final LocatorRecord newLocatorRecord_4 = new LocatorRecordBuilder()
256                 .setRloc(IPV4_RLOC_4)
257                 .setLocatorId(LOCATOR_ID_STRING).build();
258
259         final MappingRecordBuilder newMappingRecordBuilder = getDefaultMappingRecordBuilder().setEid(IPV4_PREFIX_EID_2)
260                 .setLocatorRecord(Lists.newArrayList())
261                 .setXtrId(XTR_ID_2)
262                 .setRecordTtl(1)
263                 .setSiteId(SITE_ID_2);
264         newMappingRecordBuilder.getLocatorRecord().add(newLocatorRecord_2);
265         newMappingRecordBuilder.getLocatorRecord().add(newLocatorRecord_4);
266         final Date timestamp = new Date();
267
268         // expected result
269         MappingRecordBuilder mrb = new MappingRecordBuilder(getDefaultMappingRecordBuilder().build())
270                 .setXtrId(XTR_ID_3)
271                 .setTimestamp(timestamp.getTime())
272                 .setRecordTtl(1)
273                 .setSiteId(SITE_ID_2);
274         mrb.getLocatorRecord().add(currentLocatorRecord_1);
275         mrb.getLocatorRecord().add(newLocatorRecord_2);
276         mrb.getLocatorRecord().add(currentLocatorRecord_3);
277         mrb.getLocatorRecord().add(newLocatorRecord_4);
278
279         MappingRecord result = MappingMergeUtil.mergeMappings
280                 (currentMergedMappingBuilder.build(), newMappingRecordBuilder.build(), XTR_ID_3, timestamp);
281         assertEquals(mrb.build(),result);
282     }
283
284     /**
285      * Tests {@link MappingMergeUtil#mergeMappings} method with multiple locator records. We assume that
286      * provided lists of locator records are not sorted. Test returns inconsistent results based on the order of
287      * the items.
288      */
289     @Test
290     public void mergeLocatorRecordsTest_withMultipleUnsortedLocatorRecords() {
291         // current locator records
292         final LocatorRecord currentLocatorRecord_1 = new LocatorRecordBuilder() // locator equals to newLocatorRecord_1
293                 .setRloc(IPV4_RLOC_1)
294                 .setLocatorId(LOCATOR_ID_STRING).build();
295
296         final LocatorRecord currentLocatorRecord_2 = new LocatorRecordBuilder() // Rloc equals to Rloc in
297                 .setRloc(IPV4_RLOC_2)                                           // newLocatorRecord_2
298                 .setLocatorId(LOCATOR_ID_STRING)
299                 .setLocalLocator(false).build();                                // local locator set to false
300
301         final LocatorRecord currentLocatorRecord_3 = new LocatorRecordBuilder()
302                 .setRloc(IPV4_RLOC_4)
303                 .setLocatorId(LOCATOR_ID_STRING).build();
304
305         final MappingRecordBuilder currentMergedMappingBuilder = getDefaultMappingRecordBuilder();
306         currentMergedMappingBuilder.getLocatorRecord().add(currentLocatorRecord_3);
307         currentMergedMappingBuilder.getLocatorRecord().add(currentLocatorRecord_2);
308         currentMergedMappingBuilder.getLocatorRecord().add(currentLocatorRecord_1);
309
310         // new locator records
311         final LocatorRecord newLocatorRecord_1 = new LocatorRecordBuilder()
312                 .setRloc(IPV4_RLOC_1)
313                 .setLocatorId(LOCATOR_ID_STRING).build();
314
315         final LocatorRecord newLocatorRecord_2 = new LocatorRecordBuilder()
316                 .setRloc(IPV4_RLOC_2)
317                 .setLocatorId(LOCATOR_ID_STRING + "-1").build();
318
319         final LocatorRecord newLocatorRecord_3 = new LocatorRecordBuilder() // new Rloc
320                 .setRloc(IPV4_RLOC_3)
321                 .setLocatorId(LOCATOR_ID_STRING).build();
322
323         final MappingRecordBuilder newMappingRecordBuilder = getDefaultMappingRecordBuilder().setEid(IPV4_PREFIX_EID_2)
324                 .setLocatorRecord(Lists.newArrayList())
325                 .setXtrId(XTR_ID_2)
326                 .setRecordTtl(1)
327                 .setSiteId(SITE_ID_2);
328         newMappingRecordBuilder.getLocatorRecord().add(newLocatorRecord_3);
329         newMappingRecordBuilder.getLocatorRecord().add(newLocatorRecord_2);
330         newMappingRecordBuilder.getLocatorRecord().add(newLocatorRecord_1);
331         final Date timestamp = new Date();
332
333         // expected result
334         MappingRecordBuilder mrb = new MappingRecordBuilder(getDefaultMappingRecordBuilder().build())
335                 .setXtrId(XTR_ID_3)
336                 .setTimestamp(timestamp.getTime())
337                 .setRecordTtl(1)
338                 .setSiteId(SITE_ID_2);
339         mrb.getLocatorRecord().add(currentLocatorRecord_1);
340         mrb.getLocatorRecord().add(newLocatorRecord_2);
341         mrb.getLocatorRecord().add(newLocatorRecord_3);
342         mrb.getLocatorRecord().add(currentLocatorRecord_3);
343
344         MappingRecord result = MappingMergeUtil.mergeMappings
345                 (currentMergedMappingBuilder.build(), newMappingRecordBuilder.build(), XTR_ID_3, timestamp);
346         assertNotEquals(mrb.build(),result);
347     }
348
349     /**
350      * Tests {@link MappingMergeUtil#mergeMappings} method for setting the correct XtrId.
351      */
352     @Test
353     public void mergeMappingsTest_withSingleEqualLocator_XtrIdNotNull() {
354         // current locator record
355         final LocatorRecord currentLocatorRecord_1 = new LocatorRecordBuilder() // locator equals to newLocatorRecord_1
356                 .setRloc(IPV4_RLOC_1)
357                 .setLocatorId(LOCATOR_ID_STRING).build();
358
359         final MappingRecordBuilder currentMergedMappingBuilder = getDefaultMappingRecordBuilder();
360         currentMergedMappingBuilder.getLocatorRecord().add(currentLocatorRecord_1);
361
362         // new locator record
363         final LocatorRecord newLocatorRecord_1 = new LocatorRecordBuilder()
364                 .setRloc(IPV4_RLOC_1)
365                 .setLocatorId(LOCATOR_ID_STRING).build();
366
367         final MappingRecordBuilder newMappingRecordBuilder = getDefaultMappingRecordBuilder().setEid(IPV4_PREFIX_EID_2)
368                 .setLocatorRecord(Lists.newArrayList())
369                 .setXtrId(XTR_ID_2)
370                 .setRecordTtl(1)
371                 .setSiteId(SITE_ID_2);
372         newMappingRecordBuilder.getLocatorRecord().add(newLocatorRecord_1);
373
374         // result
375         MappingRecord result = MappingMergeUtil.mergeMappings
376                 (currentMergedMappingBuilder.build(), newMappingRecordBuilder.build(), XTR_ID_3, new Date(100L));
377         assertEquals(XTR_ID_3, result.getXtrId());
378         assertEquals((Long) 100L, result.getTimestamp());
379     }
380
381     /**
382      * Tests {@link MappingMergeUtil#mergeMappings} method with newMapping == null. Should throw an NPE.
383      */
384     @Test(expected = NullPointerException.class)
385     public void mergeMappingsTest_withNewMappingNull() {
386         // current locator record
387         final LocatorRecord currentLocatorRecord_1 = new LocatorRecordBuilder()             // same locator
388                 .setRloc(IPV4_RLOC_1)
389                 .setLocatorId(LOCATOR_ID_STRING).build();
390
391         final MappingRecordBuilder currentMergedMappingBuilder = getDefaultMappingRecordBuilder();
392         currentMergedMappingBuilder.getLocatorRecord().add(currentLocatorRecord_1);
393
394         MappingMergeUtil.mergeMappings(currentMergedMappingBuilder.build(), null, null, null);
395     }
396
397     /**
398      * Tests {@link MappingMergeUtil#mergeMappings} method with currentMergedMapping == null.
399      */
400     @Test
401     public void mergeMappingsTest_withNullCurrentMergedMapping() {
402         MappingRecord newMapping = Mockito.mock(MappingRecord.class);
403
404         assertEquals(newMapping, MappingMergeUtil.mergeMappings(null, newMapping, null, null));
405     }
406
407     /**
408      * Tests {@link MappingMergeUtil#mergeXtrIdMappings} method with two expired mappings.
409      */
410     @Test
411     public void mergeXtrIdMappingsTest_verifyExpiredMappings() {
412         MappingRecord expiredMappingRecord_1 = getDefaultMappingRecordBuilder().setTimestamp(1L).build();
413         MappingRecord expiredMappingRecord_2 = getDefaultMappingRecordBuilder()
414                 .setXtrId(XTR_ID_2)
415                 .setTimestamp(1L).build();
416         List<XtrId> expiredMappings = Lists.newArrayList();
417         List<Object> mappingRecords =
418                 Lists.newArrayList(expiredMappingRecord_1, expiredMappingRecord_2);
419
420         assertNull(MappingMergeUtil.mergeXtrIdMappings(mappingRecords, expiredMappings, null));
421         assertEquals(2, expiredMappings.size());
422         assertTrue(expiredMappings.contains(XTR_ID_1));
423         assertTrue(expiredMappings.contains(XTR_ID_2));
424     }
425
426     /**
427      * Tests {@link MappingMergeUtil#mergeXtrIdMappings} method, verifies the oldest timestamp.
428      */
429     @Test
430     public void mergeXtrIdMappingsTest_verifyOldestTimestamp() {
431         final long timestamp = new Date().getTime();
432         final long timestamp_1 = timestamp - 200L;
433         final long timestamp_2 = timestamp - 300L; // oldest mapping
434         final long timestamp_3 = timestamp - 100L;
435
436         final MappingRecord expiredMappingRecord_1 = getDefaultMappingRecordBuilder()
437                 .setTimestamp(timestamp_1).build();
438         final MappingRecord expiredMappingRecord_2 = getDefaultMappingRecordBuilder()
439                 .setSourceRloc(IPV4_SOURCE_RLOC_2)
440                 .setXtrId(XTR_ID_2)
441                 .setTimestamp(timestamp_2).build();
442         final MappingRecord expiredMappingRecord_3 = getDefaultMappingRecordBuilder()
443                 .setSourceRloc(IPV4_SOURCE_RLOC_3)
444                 .setXtrId(XTR_ID_3)
445                 .setTimestamp(timestamp_3).build();
446
447         Set<IpAddressBinary> sourceRlocs = Sets.newHashSet();
448         final List<XtrId> expiredMappings = Lists.newArrayList();
449         final List<Object> mappingRecords =
450                 Lists.newArrayList(expiredMappingRecord_1, expiredMappingRecord_2, expiredMappingRecord_3);
451
452         // result
453         MappingRecord result = MappingMergeUtil.mergeXtrIdMappings(mappingRecords, expiredMappings, sourceRlocs);
454         assertEquals(timestamp_2, (long) result.getTimestamp());
455         assertEquals(XTR_ID_2, result.getXtrId());
456
457         assertTrue(sourceRlocs.size() == 3);
458         assertTrue(sourceRlocs.contains(IPV4_SOURCE_RLOC_1));
459         assertTrue(sourceRlocs.contains(IPV4_SOURCE_RLOC_2));
460         assertTrue(sourceRlocs.contains(IPV4_SOURCE_RLOC_3));
461
462         assertTrue(expiredMappings.isEmpty());
463     }
464
465     /**
466      * Tests {@link MappingMergeUtil#mappingIsExpired} method.
467      */
468     @Test
469     public void mappingIsExpiredTest() {
470         long timestamp = new Date().getTime();
471         MappingRecordBuilder mappingRecordBuilder = getDefaultMappingRecordBuilder();
472         assertTrue(MappingMergeUtil.mappingIsExpired(mappingRecordBuilder
473                 .setTimestamp(timestamp - (REGISTRATION_VALIDITY + 1L)).build()));
474         assertFalse(MappingMergeUtil.mappingIsExpired(mappingRecordBuilder.setTimestamp(timestamp).build()));
475         assertFalse(MappingMergeUtil.mappingIsExpired(mappingRecordBuilder.setTimestamp(null).build()));
476     }
477
478     /**
479      * Tests {@link MappingMergeUtil#mappingIsExpired} method, throws NPE.
480      */
481     @Test(expected = NullPointerException.class)
482     public void mappingIsExpiredTest_throwsNPE() {
483         MappingMergeUtil.mappingIsExpired(null);
484     }
485
486     /**
487      * Tests {@link MappingMergeUtil#timestampIsExpired} method, throws NPE.
488      */
489     @Test(expected = NullPointerException.class)
490     public void timestampIsExpiredTest_withDate_throwsNPE() {
491         Date date = null;
492         MappingMergeUtil.timestampIsExpired(date);
493     }
494
495     /**
496      * Tests {@link MappingMergeUtil#timestampIsExpired} method, throws NPE.
497      */
498     @Test
499     public void timestampIsExpiredTest_withDate() {
500         long timestamp = new Date().getTime();
501         assertTrue(MappingMergeUtil.timestampIsExpired(new Date(timestamp - (REGISTRATION_VALIDITY + 1L))));
502         assertFalse(MappingMergeUtil.timestampIsExpired(new Date(timestamp)));
503     }
504
505     private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
506         return new MappingRecordBuilder()
507                 .setEid(IPV4_PREFIX_EID_1)
508                 .setLocatorRecord(new ArrayList<>())
509                 .setRecordTtl(2)
510                 .setAction(MappingRecord.Action.NativelyForward)
511                 .setAuthoritative(true)
512                 .setMapVersion((short) 1)
513                 .setSiteId(SITE_ID_1)
514                 .setSourceRloc(IPV4_SOURCE_RLOC_1)
515                 .setTimestamp(new Date().getTime())
516                 .setXtrId(XTR_ID_1);
517     }
518 }