e5db74c40b0769eb00bc02e7886f6deff92adc9a
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / lisp / MappingServiceTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc.  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
9 package org.opendaylight.lispflowmapping.implementation.lisp;
10
11 import static org.junit.Assert.assertEquals;
12
13 import java.util.Arrays;
14
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;
30
31 /**
32  *
33  * @author Florin Coras
34  *
35  */
36 public class MappingServiceTest extends BaseTestCase {
37
38     private ILispDAO dao;
39     private MappingService mapService;
40     private DataStoreBackEnd dsbe;
41     private MappingSystem mapSystem;
42
43     private LispAddressContainer eid;
44     private String eidIpv4String = "10.31.0.5";
45
46     @Override
47     @Before
48     public void before() throws Exception {
49         super.before();
50         dao = context.mock(ILispDAO.class);
51         dsbe = context.mock(DataStoreBackEnd.class);
52
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));
56
57         mapSystem = new MappingSystem(dao, true, true, true);
58
59         mapService = new MappingService();
60         mapService.setDaoService(dao);
61         inject(mapService, "dsbe", dsbe);
62         inject(mapService, "mappingSystem", mapSystem);
63
64         eid = LispAFIConvertor.asIPv4Address(eidIpv4String);
65
66     }
67
68     @SuppressWarnings("unchecked")
69     @Test
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);
78     }
79
80     @Test
81     public void handleGetAuthenticationKey() throws Exception {
82         LispAddressContainer key = getDefaultKey();
83         oneOf(dao).getSpecific(weq(key), with(SubKeys.AUTH_KEY));
84         ret("password");
85         assertEquals("password", mapService.getAuthenticationKey(eid));
86     }
87
88     @Test
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));
95         ret("password");
96         assertEquals(null, mapService.getAuthenticationKey(eid));
97     }
98
99     @Test
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);
105     }
106
107     private LispAddressContainer getDefaultKey() {
108         return getKey(32);
109     }
110
111     private LispAddressContainer getKey(int mask) {
112         return MaskUtil.normalize(eid, (short)mask);
113     }
114
115     private void addDsbeAddKeyExpectation() {
116         ValueSaverAction<AuthenticationKey> dsbeAddKeySaverAction = new ValueSaverAction<AuthenticationKey>() {
117             @Override
118             public Object invoke(Invocation invocation) throws Throwable {
119                 mapSystem.addAuthenticationKey(lastValue.getLispAddressContainer(), lastValue.getAuthkey());
120                 return null;
121             }
122         };
123         oneOf(dsbe).addAuthenticationKey(with(dsbeAddKeySaverAction)); will(dsbeAddKeySaverAction);
124     }
125
126     private void addDsbeRemoveKeyExpectation() {
127         ValueSaverAction<AuthenticationKey> dsbeRemoveKeySaverAction = new ValueSaverAction<AuthenticationKey>() {
128             @Override
129             public Object invoke(Invocation invocation) throws Throwable {
130                 mapSystem.removeAuthenticationKey(lastValue.getLispAddressContainer());
131                 return null;
132             }
133         };
134         oneOf(dsbe).removeAuthenticationKey(with(dsbeRemoveKeySaverAction)); will(dsbeRemoveKeySaverAction);
135     }
136
137 }