2 * Copyright (c) 2015 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
9 package org.opendaylight.lispflowmapping.implementation.mapcache;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.mockito.Mockito.any;
14 import static org.mockito.Mockito.anyString;
15 import static org.mockito.Mockito.atMost;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
20 import java.util.Date;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
24 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
25 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
26 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Builder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
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;
34 public class FlatMapCacheTest {
36 private static ILispDAO DAO_MOCK;
37 private static FlatMapCache FLAT_MAP_CACHE;
38 private static final Eid EID_MOCK = mock(Eid.class);
39 private static final Object DUMMY_OBJECT = "dummy_object";
41 private static final Eid EID_TEST = new EidBuilder().setAddress(new Ipv4Builder().setIpv4(new Ipv4Address("127.0.0.1")).build()).build();
42 private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder().setKeyString("auth_string_test").build();
43 private static final Eid NORMALIZED_EID = MaskUtil.normalize(EID_TEST);
47 DAO_MOCK = mock(ILispDAO.class);
48 FLAT_MAP_CACHE = new FlatMapCache(DAO_MOCK);
52 * Tests {@link FlatMapCache#addMapping}.
55 public void addMappingTest() {
56 FLAT_MAP_CACHE.addMapping(EID_TEST, DUMMY_OBJECT, false);
57 verify(DAO_MOCK, atMost(2)).put(NORMALIZED_EID, new MappingEntry<>(anyString(), any(Date.class)));
58 verify(DAO_MOCK).put(NORMALIZED_EID, new MappingEntry<>(SubKeys.RECORD, DUMMY_OBJECT));
62 * Tests {@link FlatMapCache#getMapping}.
65 public void getMappingTest() {
66 when(DAO_MOCK.getSpecific(NORMALIZED_EID, SubKeys.RECORD)).thenReturn(DUMMY_OBJECT);
67 assertEquals(DUMMY_OBJECT, FLAT_MAP_CACHE.getMapping(EID_MOCK, EID_TEST));
71 * Tests {@link FlatMapCache#removeMapping}.
74 public void removeMappingTest() {
75 FLAT_MAP_CACHE.removeMapping(EID_TEST, true);
76 verify(DAO_MOCK).removeSpecific(NORMALIZED_EID, SubKeys.RECORD);
80 * Tests {@link FlatMapCache#addAuthenticationKey}.
83 public void addAuthenticationKeyTest() {
84 FLAT_MAP_CACHE.addAuthenticationKey(EID_TEST, MAPPING_AUTHKEY);
85 verify(DAO_MOCK, atMost(1)).put(NORMALIZED_EID, new MappingEntry<>(SubKeys.AUTH_KEY, MAPPING_AUTHKEY));
89 * Tests {@link FlatMapCache#getAuthenticationKey}.
92 public void getAuthenticationKeyTest() {
93 when(DAO_MOCK.getSpecific(NORMALIZED_EID, SubKeys.AUTH_KEY)).thenReturn(MAPPING_AUTHKEY);
94 assertEquals(MAPPING_AUTHKEY, FLAT_MAP_CACHE.getAuthenticationKey(EID_TEST));
98 * Tests {@link FlatMapCache#getAuthenticationKey} with other than MappingAuthkey object.
101 public void getAuthenticationKeyTest_withNull() {
102 when(DAO_MOCK.getSpecific(NORMALIZED_EID, SubKeys.AUTH_KEY)).thenReturn(new Object());
103 assertNull(FLAT_MAP_CACHE.getAuthenticationKey(EID_TEST));
107 * Tests {@link FlatMapCache#removeAuthenticationKey}.
110 public void removeAuthenticationKeyTest() {
111 FLAT_MAP_CACHE.removeAuthenticationKey(EID_TEST);
112 verify(DAO_MOCK).removeSpecific(NORMALIZED_EID, SubKeys.AUTH_KEY);
116 * Tests {@link FlatMapCache#updateMappingRegistration}.
119 public void updateMappingRegistrationTest() {
120 FLAT_MAP_CACHE.updateMappingRegistration(EID_TEST);
121 verify(DAO_MOCK).put(NORMALIZED_EID, new MappingEntry<>(anyString(), any(Date.class)));
125 * Tests {@link FlatMapCache#addData}.
128 public void addDataTest() {
129 FLAT_MAP_CACHE.addData(EID_TEST, SubKeys.RECORD, DUMMY_OBJECT);
130 verify(DAO_MOCK).put(NORMALIZED_EID, new MappingEntry<>(SubKeys.RECORD, DUMMY_OBJECT));
134 * Tests {@link FlatMapCache#getData}.
137 public void getDataTest() {
138 when(DAO_MOCK.getSpecific(NORMALIZED_EID, SubKeys.RECORD)).thenReturn(DUMMY_OBJECT);
139 assertEquals(DUMMY_OBJECT, FLAT_MAP_CACHE.getData(EID_TEST, SubKeys.RECORD));
143 * Tests {@link FlatMapCache#removeData}.
146 public void removeDataTest() {
147 FLAT_MAP_CACHE.removeData(EID_TEST, SubKeys.RECORD);
148 verify(DAO_MOCK).removeSpecific(NORMALIZED_EID, SubKeys.RECORD);