Revert "Bug 7947: Store MappingOrigin in MappingData"
[lispflowmapping.git] / mappingservice / mapcache / src / test / java / org / opendaylight / lispflowmapping / mapcache / AuthKeyDbTest.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.mapcache;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12
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;
25
26 public class AuthKeyDbTest {
27
28     private static ILispDAO tableMock;
29     private static ILispDAO daoMock;
30     private static AuthKeyDb authKeyDb;
31
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;
37
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()
43             .setKeyString("pass")
44             .setKeyType(1).build();
45
46     @Before
47     public void init() {
48         daoMock = Mockito.mock(ILispDAO.class, "dao");
49         tableMock = Mockito.mock(ILispDAO.class);
50         authKeyDb = new AuthKeyDb(daoMock);
51     }
52
53     /**
54      * Tests {@link AuthKeyDb#addAuthenticationKey} method.
55      */
56     @Test
57     public void addAuthenticationKeyTest() {
58         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
59
60         authKeyDb.addAuthenticationKey(EID_IPV4, MAPPING_AUTHKEY);
61         Mockito.verify(tableMock)
62                 .put(MaskUtil.normalize(EID_IPV4), new MappingEntry<>(SubKeys.AUTH_KEY, MAPPING_AUTHKEY));
63     }
64
65     /**
66      * Tests {@link AuthKeyDb#getAuthenticationKey} method with maskable address.
67      */
68     @Test
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);
73
74         assertEquals(MAPPING_AUTHKEY, authKeyDb.getAuthenticationKey(EID_IPV4_PREFIX_1_VNI));
75     }
76
77     /**
78      * Tests {@link AuthKeyDb#getAuthenticationKey} method with non maskable address.
79      */
80     @Test
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);
84
85         assertEquals(MAPPING_AUTHKEY, authKeyDb.getAuthenticationKey(EID_IPV4));
86         Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
87     }
88
89     /**
90      * Tests {@link AuthKeyDb#getAuthenticationKey} method with no MappingAuthkey.
91      */
92     @Test
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);
96
97         assertNull(authKeyDb.getAuthenticationKey(EID_IPV4));
98     }
99
100     /**
101      * Tests {@link AuthKeyDb#getAuthenticationKey} method with no VNI_100 table.
102      */
103     @Test
104     public void addAuthenticationKeyTest_withNullVniTable() {
105         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
106
107         assertNull(authKeyDb.getAuthenticationKey(EID_IPV4));
108     }
109
110     /**
111      * Tests {@link AuthKeyDb#removeAuthenticationKey} method.
112      */
113     @Test
114     public void removeAuthenticationKeyTest() {
115         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
116
117         authKeyDb.removeAuthenticationKey(EID_IPV4);
118         Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
119     }
120
121     /**
122      * Tests {@link AuthKeyDb#removeAuthenticationKey} method with no VNI_100 table.
123      */
124     @Test
125     public void removeAuthenticationKeyTest_withNoVniTable() {
126         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
127
128         authKeyDb.removeAuthenticationKey(EID_IPV4);
129         Mockito.verify(tableMock, Mockito.never()).removeSpecific(Mockito.any(Eid.class), Mockito.anyString());
130     }
131 }