Re-enable tests on mappingservice.implementation
[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.Ignore;
18 import org.junit.Test;
19 import org.opendaylight.lispflowmapping.implementation.MappingService;
20 import org.opendaylight.lispflowmapping.implementation.MappingSystem;
21 import org.opendaylight.lispflowmapping.implementation.mdsal.DataStoreBackEnd;
22 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
23 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
24 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
25 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
26 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
27 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkeyBuilder;
33
34 /**
35  *
36  * @author Florin Coras
37  *
38  */
39 public class MappingServiceTest extends BaseTestCase {
40
41     private ILispDAO dao;
42     private MappingService mapService;
43     private DataStoreBackEnd dsbe;
44     private MappingSystem mapSystem;
45
46     private Eid eid;
47     private String eidIpv4String = "10.31.0.5";
48
49     @Override
50     @Before
51     public void before() throws Exception {
52         super.before();
53         dao = context.mock(ILispDAO.class);
54         dsbe = context.mock(DataStoreBackEnd.class);
55
56         // map-caches init and table creation
57         allowing(dao).putTable(with(MappingOrigin.Northbound.toString()));will(returnValue(dao));
58         allowing(dao).putTable(with(MappingOrigin.Southbound.toString()));will(returnValue(dao));
59
60         mapSystem = new MappingSystem(dao, true, true, true);
61
62         mapService = new MappingService();
63         mapService.setDaoService(dao);
64         inject(mapService, "dsbe", dsbe);
65         inject(mapService, "mappingSystem", mapSystem);
66
67         eid = LispAddressUtil.asIpv4PrefixEid(eidIpv4String + "/32");
68
69     }
70
71     @SuppressWarnings("unchecked")
72     @Test
73     @Ignore
74     public void handleAddAuthenticationKey() throws Exception {
75         MappingAuthkey authKey = new MappingAuthkeyBuilder().setKeyType(1).setKeyString("pass").build();
76         MappingEntry<MappingAuthkey> keyMappingEntry = new MappingEntry<>(SubKeys.AUTH_KEY, authKey);
77         Eid key = getDefaultKey();
78         MappingEntry<MappingAuthkey>[] authKeys =(MappingEntry<MappingAuthkey>[])
79                 (Arrays.asList(keyMappingEntry).toArray());
80         addDsbeAddKeyExpectation();
81         oneOf(dao).put(weq(key), weq(authKeys));
82         mapService.addAuthenticationKey(eid, authKey);
83     }
84
85     @Test
86     @Ignore
87     public void handleGetAuthenticationKey() throws Exception {
88         Eid key = getDefaultKey();
89         oneOf(dao).getSpecific(weq(key), with(SubKeys.AUTH_KEY));
90         ret("password");
91         assertEquals("password", mapService.getAuthenticationKey(eid));
92     }
93
94     @Test
95     public void handleGetAuthenticationKeyNoIteration() throws Exception {
96         mapSystem.setIterateMask(false);
97         Eid key = getDefaultKey();
98         Eid passKey = getKey(30);
99         oneOf(dao).getSpecific(weq(key), with(SubKeys.AUTH_KEY));
100         allowing(dao).getSpecific(weq(passKey), with(SubKeys.AUTH_KEY));
101         ret("password");
102         assertEquals(null, mapService.getAuthenticationKey(eid));
103     }
104
105     @Test
106     @Ignore
107     public void handleRemoveAuthenticationKey() throws Exception {
108         Eid key = getDefaultKey();
109         addDsbeRemoveKeyExpectation();
110         oneOf(dao).removeSpecific(weq(key), with(SubKeys.AUTH_KEY));
111         mapService.removeAuthenticationKey(eid);
112     }
113
114     private Eid getDefaultKey() {
115         return getKey(32);
116     }
117
118     private Eid getKey(int mask) {
119         Eid key = LispAddressUtil.asIpv4PrefixEid(eidIpv4String + "/" + Integer.toString(mask));
120         return MaskUtil.normalize(key, (short) mask);
121     }
122
123     private void addDsbeAddKeyExpectation() {
124         ValueSaverAction<AuthenticationKey> dsbeAddKeySaverAction = new ValueSaverAction<AuthenticationKey>() {
125             @Override
126             public Object invoke(Invocation invocation) throws Throwable {
127                 mapSystem.addAuthenticationKey(lastValue.getEid(), lastValue.getMappingAuthkey());
128                 return null;
129             }
130         };
131         oneOf(dsbe).addAuthenticationKey(with(dsbeAddKeySaverAction)); will(dsbeAddKeySaverAction);
132     }
133
134     private void addDsbeRemoveKeyExpectation() {
135         ValueSaverAction<AuthenticationKey> dsbeRemoveKeySaverAction = new ValueSaverAction<AuthenticationKey>() {
136             @Override
137             public Object invoke(Invocation invocation) throws Throwable {
138                 mapSystem.removeAuthenticationKey(lastValue.getEid());
139                 return null;
140             }
141         };
142         oneOf(dsbe).removeAuthenticationKey(with(dsbeRemoveKeySaverAction)); will(dsbeRemoveKeySaverAction);
143     }
144
145 }