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