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