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