a961bbf44472197e5571f947c436c63bb239ad25
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / lisp / MappingServiceTest.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.lisp;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12
13 import java.util.ArrayList;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Future;
16
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.InjectMocks;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.runners.MockitoJUnitRunner;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
25 import org.opendaylight.lispflowmapping.implementation.MappingService;
26 import org.opendaylight.lispflowmapping.implementation.MappingSystem;
27 import org.opendaylight.lispflowmapping.implementation.mdsal.AuthenticationKeyDataListener;
28 import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
29 import org.opendaylight.lispflowmapping.implementation.mdsal.MappingDataListener;
30 import org.opendaylight.lispflowmapping.implementation.util.DSBEInputUtil;
31 import org.opendaylight.lispflowmapping.implementation.util.RPCInputConvertorUtil;
32 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
33 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
34 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
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.opendaylight.lfm.lisp.proto.rev151105.SiteId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container
41         .MappingRecordBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeyInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeyInputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeysInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddMappingInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddMappingInputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddMappingsInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeyInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeyInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeyOutput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeyOutputBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeysInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingInput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingInputBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingOutput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingOutputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeyInput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeyInputBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeysInput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveMappingInput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveMappingInputBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveMappingsInput;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateKeyInput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateKeyInputBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateKeysInput;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingInput;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingInputBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingsInput;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container
71         .MappingAuthkey;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container
73         .MappingAuthkeyBuilder;
74 import org.opendaylight.yangtools.yang.common.RpcError;
75 import org.opendaylight.yangtools.yang.common.RpcResult;
76 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
77
78 @RunWith(MockitoJUnitRunner.class)
79 public class MappingServiceTest {
80
81     @Mock(name = "mappingSystem") private static MappingSystem mappingSystem;
82     @Mock(name = "dsbe") private static DataStoreBackEnd dsbe;
83     @Mock(name = "keyListener") private static AuthenticationKeyDataListener keyListener;
84     @Mock(name = "mappingListener") private static MappingDataListener mappingListener;
85
86     private final DataBroker dataBroker = Mockito.mock(DataBroker.class);
87     private final NotificationPublishService notificationPublishService = Mockito
88             .mock(NotificationPublishService.class);
89     private final ILispDAO lispDAO = Mockito.mock(ILispDAO.class);
90
91     @InjectMocks
92     MappingService mappingService = new MappingService(dataBroker,
93             notificationPublishService, lispDAO);
94
95     private static final String IPV4_STRING = "1.2.3.0";
96     private static final Eid IPV4_EID = LispAddressUtil.asIpv4Eid(IPV4_STRING);
97
98     private static final RpcResult<Object> RPC_RESULT_SUCCESS = RpcResultBuilder.success().build();
99     private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder()
100             .setKeyString("password")
101             .setKeyType(1).build();
102     private static final SiteId SITE_ID = new SiteId(new byte[] {0, 1, 2, 3, 4, 5, 6, 7});
103     private static final XtrId XTR_ID = new XtrId(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
104     private static final Object DUMMY_OBJECT = "dummy_object";
105
106     /**
107      * Tests {@link MappingService#addKey} method.
108      *
109      * @throws ExecutionException
110      * @throws InterruptedException
111      */
112     @Test
113     public void addKeyTest() throws ExecutionException, InterruptedException {
114         final MappingAuthkey mappingAuthkey = new MappingAuthkeyBuilder()
115                 .setKeyString("dummy-password")
116                 .setKeyType(2).build();
117         final AddKeyInput addKeyInput = new AddKeyInputBuilder()
118                 .setMappingAuthkey(mappingAuthkey)
119                 .setEid(IPV4_EID).build();
120         Mockito.when(mappingSystem.getAuthenticationKey(IPV4_EID)).thenReturn(MAPPING_AUTHKEY);
121
122         // input
123         final RpcResult<Object> rpc = RpcResultBuilder.failed().withError(RpcError.ErrorType.PROTOCOL, "data-exists",
124                 "Key already exists! Please use update-key if you want to change it.").build();
125         final RpcError error = rpc.getErrors().iterator().next(); // equals() not implemented int RpcError
126
127         // result
128         final Future<RpcResult<Void>> result = mappingService.addKey(addKeyInput);
129         final RpcError errorResult = result.get().getErrors().iterator().next();
130
131         assertEquals(1, result.get().getErrors().size());
132         assertEquals(error.getMessage(), errorResult.getMessage());
133         assertEquals(error.getApplicationTag(), errorResult.getApplicationTag());
134         assertEquals(error.getCause(), errorResult.getCause());
135         assertEquals(error.getErrorType(), errorResult.getErrorType());
136         assertEquals(error.getInfo(), errorResult.getInfo());
137         assertEquals(error.getTag(), errorResult.getTag());
138         assertEquals(error.getSeverity(), errorResult.getSeverity());
139         assertEquals(rpc.getResult(), result.get().getResult());
140         assertEquals(rpc.isSuccessful(), result.get().isSuccessful());
141     }
142
143     /**
144      * Tests {@link MappingService#addKey} method with null MappingAuthkey.
145      *
146      * @throws ExecutionException
147      * @throws InterruptedException
148      */
149     @Test
150     public void addKeyTest_withNullMapAuthkey() throws ExecutionException, InterruptedException {
151         final AddKeyInput addKeyInput = new AddKeyInputBuilder().setEid(IPV4_EID).build();
152         Mockito.when(mappingSystem.getAuthenticationKey(IPV4_EID)).thenReturn(null);
153
154         final Future<RpcResult<Void>> result = mappingService.addKey(addKeyInput);
155         Mockito.verify(dsbe).addAuthenticationKey(Mockito.any());
156
157         assertEquals(RPC_RESULT_SUCCESS.getErrors(), result.get().getErrors());
158         assertEquals(RPC_RESULT_SUCCESS.getResult(), result.get().getResult());
159         assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), result.get().isSuccessful());
160     }
161
162     /**
163      * Tests {@link MappingService#addMapping} method.
164      *
165      * @throws ExecutionException
166      * @throws InterruptedException
167      */
168     @Test
169     public void addMappingTest() throws ExecutionException, InterruptedException {
170         final MappingRecordBuilder mappingRecordBuilder = getDefaultMappingRecordBuilder();
171         final AddMappingInput addMappingInput = new AddMappingInputBuilder()
172                 .setMappingRecord(mappingRecordBuilder.build()).build();
173
174         final Future<RpcResult<Void>> result = mappingService.addMapping(addMappingInput);
175         Mockito.verify(dsbe).addMapping(RPCInputConvertorUtil.toMapping(addMappingInput));
176
177         assertEquals(RPC_RESULT_SUCCESS.getErrors(), result.get().getErrors());
178         assertEquals(RPC_RESULT_SUCCESS.getResult(), result.get().getResult());
179         assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), result.get().isSuccessful());
180     }
181
182     /**
183      * Tests {@link MappingService#getKey} method.
184      *
185      * @throws ExecutionException
186      * @throws InterruptedException
187      */
188     @Test
189     public void getKeyTest() throws ExecutionException, InterruptedException {
190         final GetKeyInput getKeyInput = new GetKeyInputBuilder().setEid(IPV4_EID).build();
191         final RpcResult<GetKeyOutput> rpcResult = RpcResultBuilder
192                 .success(new GetKeyOutputBuilder().setMappingAuthkey(MAPPING_AUTHKEY).build()).build();
193
194         Mockito.when(mappingSystem.getAuthenticationKey(getKeyInput.getEid())).thenReturn(MAPPING_AUTHKEY);
195
196         final Future<RpcResult<GetKeyOutput>> result = mappingService.getKey(getKeyInput);
197         assertEquals(rpcResult.getErrors(), result.get().getErrors());
198         assertEquals(rpcResult.getResult(), result.get().getResult());
199         assertEquals(rpcResult.isSuccessful(), result.get().isSuccessful());
200     }
201
202     /**
203      * Tests {@link MappingService#getKey} method with null MappingAuthkey.
204      *
205      * @throws ExecutionException
206      * @throws InterruptedException
207      */
208     @Test
209     public void getKeyTest_withNullMappingAuthkey() throws ExecutionException, InterruptedException {
210         // input
211         final GetKeyInput getKeyInput = new GetKeyInputBuilder().setEid(IPV4_EID).build();
212         Mockito.when(mappingSystem.getAuthenticationKey(getKeyInput.getEid())).thenReturn(null);
213
214         final RpcResult<Object> rpc = RpcResultBuilder.failed()
215                 .withError(RpcError.ErrorType.APPLICATION, "data-missing", "Key was not found in the mapping database")
216                 .build();
217         final RpcError error = rpc.getErrors().iterator().next();
218
219         // result
220         final Future<RpcResult<GetKeyOutput>> result = mappingService.getKey(getKeyInput);
221         final RpcError errorResult = result.get().getErrors().iterator().next();
222
223         assertEquals(1, result.get().getErrors().size());
224         assertEquals(error.getMessage(), errorResult.getMessage());
225         assertEquals(error.getApplicationTag(), errorResult.getApplicationTag());
226         assertEquals(error.getCause(), errorResult.getCause());
227         assertEquals(error.getErrorType(), errorResult.getErrorType());
228         assertEquals(error.getInfo(), errorResult.getInfo());
229         assertEquals(error.getTag(), errorResult.getTag());
230         assertEquals(error.getSeverity(), errorResult.getSeverity());
231         assertEquals(rpc.getResult(), result.get().getResult());
232         assertEquals(rpc.isSuccessful(), result.get().isSuccessful());
233     }
234
235     /**
236      * Tests {@link MappingService#getMapping} method.
237      *
238      * @throws ExecutionException
239      * @throws InterruptedException
240      */
241     @Test
242     public void getMappingTest() throws ExecutionException, InterruptedException {
243         // input
244         final GetMappingInput getMappingInput = new GetMappingInputBuilder().setEid(IPV4_EID).build();
245         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build();
246         final MappingRecord nonBinaryMappingRecord = getDefaultMappingRecordBuilder()
247                 .setEid(LispAddressUtil.toEid(new Ipv4Address(IPV4_STRING), null)).build();
248
249         Mockito.when(mappingSystem.getMapping(getMappingInput.getEid())).thenReturn(mappingRecord);
250
251         final RpcResult<GetMappingOutput> rpc = RpcResultBuilder
252                 .success(new GetMappingOutputBuilder().setMappingRecord(nonBinaryMappingRecord)).build();
253
254         //result
255         final Future<RpcResult<GetMappingOutput>> result = mappingService.getMapping(getMappingInput);
256         final RpcResult<GetMappingOutput> rpcResult = result.get();
257
258         assertEquals(rpc.getResult(), rpcResult.getResult());
259         assertEquals(rpc.getErrors(), rpcResult.getErrors());
260         assertEquals(rpc.isSuccessful(), rpcResult.isSuccessful());
261     }
262
263     /**
264      * Tests {@link MappingService#getMapping} method with null MappingRecord.
265      *
266      * @throws ExecutionException
267      * @throws InterruptedException
268      */
269     @Test
270     public void getMappingTest_withNullMapRecord() throws ExecutionException, InterruptedException {
271         // input
272         final GetMappingInput getMappingInput = new GetMappingInputBuilder().setEid(IPV4_EID).build();
273         Mockito.when(mappingSystem.getMapping(getMappingInput.getEid())).thenReturn(null);
274
275         final RpcResult<Object> rpc = RpcResultBuilder.failed().withError(RpcError.ErrorType.APPLICATION,
276                 "data-missing", "No mapping was found in the mapping database").build();
277         final RpcError error = rpc.getErrors().iterator().next();
278
279         //result
280         final Future<RpcResult<GetMappingOutput>> result = (mappingService.getMapping(getMappingInput));
281         final RpcError errorResult = result.get().getErrors().iterator().next();
282
283         assertEquals(1, result.get().getErrors().size());
284         assertEquals(error.getMessage(), errorResult.getMessage());
285         assertEquals(error.getApplicationTag(), errorResult.getApplicationTag());
286         assertEquals(error.getCause(), errorResult.getCause());
287         assertEquals(error.getErrorType(), errorResult.getErrorType());
288         assertEquals(error.getInfo(), errorResult.getInfo());
289         assertEquals(error.getTag(), errorResult.getTag());
290         assertEquals(error.getSeverity(), errorResult.getSeverity());
291         assertEquals(rpc.getResult(), result.get().getResult());
292         assertEquals(rpc.isSuccessful(), result.get().isSuccessful());
293     }
294
295     /**
296      * Tests {@link MappingService#removeKey} method.
297      *
298      * @throws ExecutionException
299      * @throws InterruptedException
300      */
301     @Test
302     public void removeKeyTest() throws ExecutionException, InterruptedException {
303         final RemoveKeyInput removeKeyInput = new RemoveKeyInputBuilder().setEid(IPV4_EID).build();
304
305         final Future<RpcResult<Void>> result = mappingService.removeKey(removeKeyInput);
306         Mockito.verify(dsbe).removeAuthenticationKey(RPCInputConvertorUtil.toAuthenticationKey(removeKeyInput));
307
308         assertEquals(RPC_RESULT_SUCCESS.getErrors(), result.get().getErrors());
309         assertEquals(RPC_RESULT_SUCCESS.getResult(), result.get().getResult());
310         assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), result.get().isSuccessful());
311     }
312
313     /**
314      * Tests {@link MappingService#removeMapping} method.
315      *
316      * @throws ExecutionException
317      * @throws InterruptedException
318      */
319     @Test
320     public void removeMappingTest() throws ExecutionException, InterruptedException {
321         final RemoveMappingInput removeMappingInput = new RemoveMappingInputBuilder().setEid(IPV4_EID).build();
322
323         final Future<RpcResult<Void>> result = mappingService.removeMapping(removeMappingInput);
324         Mockito.verify(dsbe).removeMapping(RPCInputConvertorUtil.toMapping(removeMappingInput));
325
326         assertEquals(RPC_RESULT_SUCCESS.getErrors(), result.get().getErrors());
327         assertEquals(RPC_RESULT_SUCCESS.getResult(), result.get().getResult());
328         assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), result.get().isSuccessful());
329     }
330
331     /**
332      * Tests {@link MappingService#updateKey} method.
333      *
334      * @throws ExecutionException
335      * @throws InterruptedException
336      */
337     @Test
338     public void updateKeyTest() throws ExecutionException, InterruptedException {
339         final UpdateKeyInput updateKeyInput = new UpdateKeyInputBuilder().setEid(IPV4_EID).build();
340         Mockito.when(mappingSystem.getAuthenticationKey(IPV4_EID)).thenReturn(MAPPING_AUTHKEY);
341
342         final Future<RpcResult<Void>> result = mappingService.updateKey(updateKeyInput);
343         Mockito.verify(dsbe).updateAuthenticationKey(RPCInputConvertorUtil.toAuthenticationKey(updateKeyInput));
344
345         assertEquals(RPC_RESULT_SUCCESS.getErrors(), result.get().getErrors());
346         assertEquals(RPC_RESULT_SUCCESS.getResult(), result.get().getResult());
347         assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), result.get().isSuccessful());
348     }
349
350     /**
351      * Tests {@link MappingService#updateKey} method with null MappingAuthkey.
352      *
353      * @throws ExecutionException
354      * @throws InterruptedException
355      */
356     @Test
357     public void updateKeyTest_withNullMapAuthkey() throws ExecutionException, InterruptedException {
358         final UpdateKeyInput updateKeyInput = new UpdateKeyInputBuilder().setEid(IPV4_EID).build();
359         Mockito.when(mappingSystem.getAuthenticationKey(IPV4_EID)).thenReturn(null);
360
361         // input
362         final RpcResult<Object> rpc = RpcResultBuilder.failed().withError(RpcError.ErrorType.PROTOCOL, "data-missing",
363                 "Key doesn't exist! Please use add-key if you want to create a new authentication key.").build();
364         final RpcError error = rpc.getErrors().iterator().next();
365
366         // result
367         final Future<RpcResult<Void>> result = mappingService.updateKey(updateKeyInput);
368         final RpcError errorResult = result.get().getErrors().iterator().next();
369
370         assertEquals(1, result.get().getErrors().size());
371         assertEquals(error.getMessage(), errorResult.getMessage());
372         assertEquals(error.getApplicationTag(), errorResult.getApplicationTag());
373         assertEquals(error.getCause(), errorResult.getCause());
374         assertEquals(error.getErrorType(), errorResult.getErrorType());
375         assertEquals(error.getInfo(), errorResult.getInfo());
376         assertEquals(error.getTag(), errorResult.getTag());
377         assertEquals(error.getSeverity(), errorResult.getSeverity());
378         assertEquals(rpc.getResult(), result.get().getResult());
379         assertEquals(rpc.isSuccessful(), result.get().isSuccessful());
380     }
381
382     /**
383      * Tests {@link MappingService#updateMapping} method.
384      *
385      * @throws ExecutionException
386      * @throws InterruptedException
387      */
388     @Test
389     public void updateMappingTest() throws ExecutionException, InterruptedException {
390         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build();
391         final UpdateMappingInput updateMappingInput = new UpdateMappingInputBuilder()
392                 .setMappingRecord(mappingRecord).build();
393
394         final Future<RpcResult<Void>> result = mappingService.updateMapping(updateMappingInput);
395         Mockito.verify(dsbe).updateMapping(RPCInputConvertorUtil.toMapping(updateMappingInput));
396
397         assertEquals(RPC_RESULT_SUCCESS.getErrors(), result.get().getErrors());
398         assertEquals(RPC_RESULT_SUCCESS.getResult(), result.get().getResult());
399         assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), result.get().isSuccessful());
400     }
401
402     /**
403      * Tests {@link MappingService#addMapping} method from southbound.
404      *
405      * @throws ExecutionException
406      * @throws InterruptedException
407      */
408     @Test
409     public void addMappingTest_fromSouthbound() throws ExecutionException, InterruptedException {
410         // input
411         final MappingRecord data = getDefaultMappingRecordBuilder()
412                 .setXtrId(XTR_ID).build();
413
414         mappingService.addMapping(MappingOrigin.Southbound, IPV4_EID, SITE_ID, data, false);
415
416         Mockito.verify(mappingSystem).addMapping(MappingOrigin.Southbound, IPV4_EID, data, false);
417         Mockito.verify(dsbe).addMapping(DSBEInputUtil.toMapping(MappingOrigin.Southbound, IPV4_EID, SITE_ID, data));
418         Mockito.verify(dsbe).addXtrIdMapping(DSBEInputUtil.toXtrIdMapping(data));
419     }
420
421     /**
422      * Tests {@link MappingService#addMapping} method from northbound.
423      *
424      * @throws ExecutionException
425      * @throws InterruptedException
426      */
427     @Test
428     public void addMappingTest_fromNorthbound() throws ExecutionException, InterruptedException {
429         // input
430         final MappingOrigin origin = MappingOrigin.Northbound;
431         final MappingRecord data = getDefaultMappingRecordBuilder()
432                 .setXtrId(XTR_ID).build();
433
434         mappingService.addMapping(origin, IPV4_EID, SITE_ID, data, false);
435         Mockito.verify(dsbe).addMapping(DSBEInputUtil.toMapping(origin, IPV4_EID, SITE_ID, data));
436         Mockito.verifyZeroInteractions(mappingSystem);
437         Mockito.verifyNoMoreInteractions(dsbe);
438     }
439
440     /**
441      * Tests {@link MappingService#getMapping(MappingOrigin, Eid)} method.
442      */
443     @Test
444     public void getMappingTest_withOriginAndEid() {
445         Mockito.when(mappingSystem.getMapping(Mockito.any(MappingOrigin.class), Mockito.any(Eid.class)))
446                 .thenReturn(DUMMY_OBJECT);
447         assertEquals(DUMMY_OBJECT, mappingService.getMapping(MappingOrigin.Northbound, IPV4_EID));
448     }
449
450     /**
451      * Tests {@link MappingService#getMapping(Eid)} method.
452      */
453     @Test
454     public void getMappingTest_withEid() {
455         Mockito.when(mappingSystem.getMapping(Mockito.any(Eid.class))).thenReturn(DUMMY_OBJECT);
456         assertEquals(DUMMY_OBJECT, mappingService.getMapping(IPV4_EID));
457     }
458
459     /**
460      * Tests {@link MappingService#getMapping(Eid)} method.
461      */
462     @Test
463     public void getMappingTest_withSrcAndDstEid() {
464         Mockito.when(mappingSystem.getMapping(Mockito.any(Eid.class), Mockito.any(Eid.class))).thenReturn(DUMMY_OBJECT);
465         assertEquals(DUMMY_OBJECT, mappingService.getMapping(IPV4_EID, IPV4_EID));
466     }
467
468     /**
469      * Tests {@link MappingService#removeMapping} method.
470      */
471     @Test
472     public void removeMappingTest_withMapOrginAndEid() {
473         mappingService.removeMapping(MappingOrigin.Northbound, IPV4_EID);
474         Mockito.verify(dsbe).removeMapping(DSBEInputUtil.toMapping(MappingOrigin.Northbound, IPV4_EID));
475     }
476
477     /**
478      * Tests {@link MappingService#addAuthenticationKey} method.
479      */
480     @Test
481     public void addAuthenticationKeyTest() {
482         mappingService.addAuthenticationKey(IPV4_EID, MAPPING_AUTHKEY);
483         Mockito.verify(dsbe).addAuthenticationKey(DSBEInputUtil.toAuthenticationKey(IPV4_EID, MAPPING_AUTHKEY));
484     }
485
486     /**
487      * Tests {@link MappingService#getAuthenticationKey(Eid)} method.
488      */
489     @Test
490     public void getAuthenticationKeyTest() {
491         Mockito.when(mappingSystem.getAuthenticationKey(IPV4_EID)).thenReturn(MAPPING_AUTHKEY);
492         assertEquals(MAPPING_AUTHKEY, mappingService.getAuthenticationKey(IPV4_EID));
493     }
494
495     /**
496      * Tests {@link MappingService#removeAuthenticationKey} method.
497      */
498     @Test
499     public void removeAuthenticationKeyTest() {
500         mappingService.removeAuthenticationKey(IPV4_EID);
501         Mockito.verify(dsbe).removeAuthenticationKey(DSBEInputUtil.toAuthenticationKey(IPV4_EID, null));
502     }
503
504     /**
505      * Tests {@link MappingService#addData} method.
506      */
507     @Test
508     public void addDataTest() {
509         mappingService.addData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD, DUMMY_OBJECT);
510         Mockito.verify(mappingSystem).addData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD, DUMMY_OBJECT);
511     }
512
513     /**
514      * Tests {@link MappingService#getData} method.
515      */
516     @Test
517     public void getDataTest() {
518         Mockito.when(mappingSystem.getData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD))
519                 .thenReturn(DUMMY_OBJECT);
520         assertEquals(DUMMY_OBJECT, mappingService.getData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD));
521     }
522
523     /**
524      * Tests {@link MappingService#removeData} method.
525      */
526     @Test
527     public void removeDataTest() {
528         mappingService.removeData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD);
529         Mockito.verify(mappingSystem).removeData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD);
530     }
531
532     /**
533      * Tests {@link MappingService#printMappings} method.
534      */
535     @Test
536     public void printMappingsTest() {
537         mappingService.printMappings();
538         Mockito.verify(mappingSystem).printMappings();
539     }
540
541     /**
542      * Tests {@link MappingService#close} method.
543      */
544     @Test
545     public void close() throws Exception {
546         mappingService.close();
547         Mockito.verify(keyListener).closeDataChangeListener();
548         Mockito.verify(mappingListener).closeDataChangeListener();
549     }
550
551     /**
552      * Tests {@link MappingService#cleanCachedMappings} method.
553      */
554     @Test
555     public void cleanCachedMappingsTest() throws Exception {
556         mappingService.cleanCachedMappings();
557         Mockito.verify(mappingSystem).cleanCaches();
558         Mockito.verify(dsbe).removeAllDatastoreContent();
559     }
560
561     /**
562      * Tests {@link MappingService} not implemented methods.
563      */
564     @Test
565     public void nullReturnMethodTest() throws ExecutionException, InterruptedException {
566         assertNull(mappingService.removeKeys(Mockito.mock(RemoveKeysInput.class)));
567         assertNull(mappingService.removeMappings(Mockito.mock(RemoveMappingsInput.class)));
568         assertNull(mappingService.getKeys(Mockito.mock(GetKeysInput.class)));
569         assertNull(mappingService.addMappings(Mockito.mock(AddMappingsInput.class)));
570         assertNull(mappingService.updateKeys(Mockito.mock(UpdateKeysInput.class)));
571         assertNull(mappingService.removeAllMappings());
572         assertNull(mappingService.getAllKeys());
573         assertNull(mappingService.updateMappings(Mockito.mock(UpdateMappingsInput.class)));
574         assertNull(mappingService.addKeys(Mockito.mock(AddKeysInput.class)));
575         assertNull(mappingService.getAllMappings());
576     }
577
578     private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
579         return new MappingRecordBuilder()
580                 .setAction(MappingRecord.Action.NoAction)
581                 .setAuthoritative(false)
582                 .setLocatorRecord(new ArrayList<>())
583                 .setMapVersion((short) 0)
584                 .setRecordTtl(60)
585                 .setEid(IPV4_EID);
586     }
587 }