2 * Copyright (c) 2015 Cisco Systems, Inc. All rights reserved.
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
9 package org.opendaylight.lispflowmapping.implementation.lisp;
11 import static org.junit.Assert.assertEquals;
13 import java.util.Arrays;
15 import org.jmock.api.Invocation;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.lispflowmapping.implementation.MappingService;
19 import org.opendaylight.lispflowmapping.implementation.MappingSystem;
20 import org.opendaylight.lispflowmapping.implementation.mdsal.DataStoreBackEnd;
21 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
22 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
23 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
24 import org.opendaylight.lispflowmapping.lisp.util.LispAFIConvertor;
25 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
26 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispaddress.LispAddressContainer;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
33 * @author Florin Coras
36 public class MappingServiceTest extends BaseTestCase {
39 private MappingService mapService;
40 private DataStoreBackEnd dsbe;
41 private MappingSystem mapSystem;
43 private LispAddressContainer eid;
44 private String eidIpv4String = "10.31.0.5";
48 public void before() throws Exception {
50 dao = context.mock(ILispDAO.class);
51 dsbe = context.mock(DataStoreBackEnd.class);
53 // map-caches init and table creation
54 allowing(dao).putTable(with(MappingOrigin.Northbound.toString()));will(returnValue(dao));
55 allowing(dao).putTable(with(MappingOrigin.Southbound.toString()));will(returnValue(dao));
57 mapSystem = new MappingSystem(dao, true, true, true);
59 mapService = new MappingService();
60 mapService.setDaoService(dao);
61 inject(mapService, "dsbe", dsbe);
62 inject(mapService, "mappingSystem", mapSystem);
64 eid = LispAFIConvertor.asIPv4Address(eidIpv4String);
68 @SuppressWarnings("unchecked")
70 public void handleAddAuthenticationKey() throws Exception {
71 String authKey = "pass";
72 MappingEntry<String> keyMappingEntry = new MappingEntry<String>(SubKeys.AUTH_KEY, authKey);
73 LispAddressContainer key = getDefaultKey();
74 MappingEntry<String>[] authKeys =(MappingEntry<String>[]) (Arrays.asList(keyMappingEntry).toArray());
75 addDsbeAddKeyExpectation();
76 oneOf(dao).put(weq(key), weq(authKeys));
77 mapService.addAuthenticationKey(eid, authKey);
81 public void handleGetAuthenticationKey() throws Exception {
82 LispAddressContainer key = getDefaultKey();
83 oneOf(dao).getSpecific(weq(key), with(SubKeys.AUTH_KEY));
85 assertEquals("password", mapService.getAuthenticationKey(eid));
89 public void handleGetAuthenticationKeyNoIteration() throws Exception {
90 mapSystem.setIterateMask(false);
91 LispAddressContainer key = getDefaultKey();
92 LispAddressContainer passKey = getKey(30);
93 oneOf(dao).getSpecific(weq(key), with(SubKeys.AUTH_KEY));
94 allowing(dao).getSpecific(weq(passKey), with(SubKeys.AUTH_KEY));
96 assertEquals(null, mapService.getAuthenticationKey(eid));
100 public void handleRemoveAuthenticationKey() throws Exception {
101 LispAddressContainer key = getDefaultKey();
102 addDsbeRemoveKeyExpectation();
103 oneOf(dao).removeSpecific(weq(key), with(SubKeys.AUTH_KEY));
104 mapService.removeAuthenticationKey(eid);
107 private LispAddressContainer getDefaultKey() {
111 private LispAddressContainer getKey(int mask) {
112 return MaskUtil.normalize(eid, (short)mask);
115 private void addDsbeAddKeyExpectation() {
116 ValueSaverAction<AuthenticationKey> dsbeAddKeySaverAction = new ValueSaverAction<AuthenticationKey>() {
118 public Object invoke(Invocation invocation) throws Throwable {
119 mapSystem.addAuthenticationKey(lastValue.getLispAddressContainer(), lastValue.getAuthkey());
123 oneOf(dsbe).addAuthenticationKey(with(dsbeAddKeySaverAction)); will(dsbeAddKeySaverAction);
126 private void addDsbeRemoveKeyExpectation() {
127 ValueSaverAction<AuthenticationKey> dsbeRemoveKeySaverAction = new ValueSaverAction<AuthenticationKey>() {
129 public Object invoke(Invocation invocation) throws Throwable {
130 mapSystem.removeAuthenticationKey(lastValue.getLispAddressContainer());
134 oneOf(dsbe).removeAuthenticationKey(with(dsbeRemoveKeySaverAction)); will(dsbeRemoveKeySaverAction);