2 * Copyright (c) 2016 Cisco Systems, Inc. and others. 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
8 package org.opendaylight.lispflowmapping.mapcache;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.Mockito;
16 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
17 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
18 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
19 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
20 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
26 public class AuthKeyDbTest {
28 private static ILispDAO tableMock;
29 private static ILispDAO daoMock;
30 private static AuthKeyDb authKeyDb;
32 private static final String IPV4_STRING_1 = "1.2.3.0";
33 private static final String IPV4_PREFIX_STRING = "/24";
34 private static final short MASK = 24;
35 private static final long VNI_0 = 0L;
36 private static final long VNI_100 = 100L;
38 private static final Eid EID_IPV4_PREFIX_1_VNI = LispAddressUtil
39 .asIpv4PrefixEid(IPV4_STRING_1 + IPV4_PREFIX_STRING, new InstanceIdType(VNI_100));
40 private static final Eid EID_IPV4 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
41 private static final Eid NORMALIZED_EID_IPV4 = MaskUtil.normalize(EID_IPV4);
42 private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder()
44 .setKeyType(1).build();
48 daoMock = Mockito.mock(ILispDAO.class, "dao");
49 tableMock = Mockito.mock(ILispDAO.class);
50 authKeyDb = new AuthKeyDb(daoMock);
54 * Tests {@link AuthKeyDb#addAuthenticationKey} method.
57 public void addAuthenticationKeyTest() {
58 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
60 authKeyDb.addAuthenticationKey(EID_IPV4, MAPPING_AUTHKEY);
61 Mockito.verify(tableMock)
62 .put(MaskUtil.normalize(EID_IPV4), new MappingEntry<>(SubKeys.AUTH_KEY, MAPPING_AUTHKEY));
66 * Tests {@link AuthKeyDb#getAuthenticationKey} method with maskable address.
69 public void getAuthenticationKeyTest_withMaskableAddress() {
70 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
71 Mockito.when(tableMock.getSpecific(MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI, MASK), SubKeys.AUTH_KEY))
72 .thenReturn(MAPPING_AUTHKEY);
74 assertEquals(MAPPING_AUTHKEY, authKeyDb.getAuthenticationKey(EID_IPV4_PREFIX_1_VNI));
78 * Tests {@link AuthKeyDb#getAuthenticationKey} method with non maskable address.
81 public void addAuthenticationKeyTest_withNonMaskableAddress() {
82 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
83 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY)).thenReturn(MAPPING_AUTHKEY);
85 assertEquals(MAPPING_AUTHKEY, authKeyDb.getAuthenticationKey(EID_IPV4));
86 Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
90 * Tests {@link AuthKeyDb#getAuthenticationKey} method with no MappingAuthkey.
93 public void addAuthenticationKeyTest_withNoMappingAuthkey() {
94 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
95 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY)).thenReturn(null);
97 assertNull(authKeyDb.getAuthenticationKey(EID_IPV4));
101 * Tests {@link AuthKeyDb#getAuthenticationKey} method with no VNI_100 table.
104 public void addAuthenticationKeyTest_withNullVniTable() {
105 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
107 assertNull(authKeyDb.getAuthenticationKey(EID_IPV4));
111 * Tests {@link AuthKeyDb#removeAuthenticationKey} method.
114 public void removeAuthenticationKeyTest() {
115 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
117 authKeyDb.removeAuthenticationKey(EID_IPV4);
118 Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
122 * Tests {@link AuthKeyDb#removeAuthenticationKey} method with no VNI_100 table.
125 public void removeAuthenticationKeyTest_withNoVniTable() {
126 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
128 authKeyDb.removeAuthenticationKey(EID_IPV4);
129 Mockito.verify(tableMock, Mockito.never()).removeSpecific(Mockito.any(Eid.class), Mockito.anyString());