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;
12 import static org.mockito.Mockito.any;
13 import static org.mockito.Mockito.anyString;
14 import static org.mockito.Mockito.atMost;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
19 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.rev130715.Ipv4Address;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container
35 .MappingAuthkeyBuilder;
37 public class FlatMapCacheTest {
39 private static ILispDAO daoMock;
40 private static FlatMapCache flatMapCache;
41 private static final Eid EID_MOCK = mock(Eid.class);
42 private static final Object DUMMY_OBJECT = "dummy_object";
44 private static final Eid EID_TEST = new EidBuilder().setAddress(new Ipv4Builder()
45 .setIpv4(new Ipv4Address("127.0.0.1")).build()).build();
46 private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder().setKeyString("auth_string_test")
48 private static final Eid NORMALIZED_EID = MaskUtil.normalize(EID_TEST);
52 daoMock = mock(ILispDAO.class);
53 flatMapCache = new FlatMapCache(daoMock);
57 * Tests {@link FlatMapCache#addMapping}.
60 public void addMappingTest() {
61 flatMapCache.addMapping(EID_TEST, DUMMY_OBJECT, true, false);
62 verify(daoMock, atMost(2)).put(NORMALIZED_EID, new MappingEntry<>(anyString(), any(Date.class)));
63 verify(daoMock).put(NORMALIZED_EID, new MappingEntry<>(SubKeys.RECORD, DUMMY_OBJECT));
67 * Tests {@link FlatMapCache#getMapping}.
70 public void getMappingTest() {
71 when(daoMock.getSpecific(NORMALIZED_EID, SubKeys.RECORD)).thenReturn(DUMMY_OBJECT);
72 assertEquals(DUMMY_OBJECT, flatMapCache.getMapping(EID_MOCK, EID_TEST));
76 * Tests {@link FlatMapCache#removeMapping}.
79 public void removeMappingTest() {
80 flatMapCache.removeMapping(EID_TEST, true);
81 verify(daoMock).removeSpecific(NORMALIZED_EID, SubKeys.RECORD);
85 * Tests {@link FlatMapCache#addAuthenticationKey}.
88 public void addAuthenticationKeyTest() {
89 flatMapCache.addAuthenticationKey(EID_TEST, MAPPING_AUTHKEY);
90 verify(daoMock, atMost(1)).put(NORMALIZED_EID, new MappingEntry<>(SubKeys.AUTH_KEY, MAPPING_AUTHKEY));
94 * Tests {@link FlatMapCache#getAuthenticationKey}.
97 public void getAuthenticationKeyTest() {
98 when(daoMock.getSpecific(NORMALIZED_EID, SubKeys.AUTH_KEY)).thenReturn(MAPPING_AUTHKEY);
99 assertEquals(MAPPING_AUTHKEY, flatMapCache.getAuthenticationKey(EID_TEST));
103 * Tests {@link FlatMapCache#getAuthenticationKey} with other than MappingAuthkey object.
106 public void getAuthenticationKeyTest_withNull() {
107 when(daoMock.getSpecific(NORMALIZED_EID, SubKeys.AUTH_KEY)).thenReturn(new Object());
108 assertNull(flatMapCache.getAuthenticationKey(EID_TEST));
112 * Tests {@link FlatMapCache#removeAuthenticationKey}.
115 public void removeAuthenticationKeyTest() {
116 flatMapCache.removeAuthenticationKey(EID_TEST);
117 verify(daoMock).removeSpecific(NORMALIZED_EID, SubKeys.AUTH_KEY);
121 * Tests {@link FlatMapCache#updateMappingRegistration}.
124 public void updateMappingRegistrationTest() {
125 flatMapCache.updateMappingRegistration(EID_TEST, null);
126 verify(daoMock).put(NORMALIZED_EID, new MappingEntry<>(anyString(), any(Date.class)));
130 * Tests {@link FlatMapCache#addData}.
133 public void addDataTest() {
134 flatMapCache.addData(EID_TEST, SubKeys.RECORD, DUMMY_OBJECT);
135 verify(daoMock).put(NORMALIZED_EID, new MappingEntry<>(SubKeys.RECORD, DUMMY_OBJECT));
139 * Tests {@link FlatMapCache#getData}.
142 public void getDataTest() {
143 when(daoMock.getSpecific(NORMALIZED_EID, SubKeys.RECORD)).thenReturn(DUMMY_OBJECT);
144 assertEquals(DUMMY_OBJECT, flatMapCache.getData(EID_TEST, SubKeys.RECORD));
148 * Tests {@link FlatMapCache#removeData}.
151 public void removeDataTest() {
152 flatMapCache.removeData(EID_TEST, SubKeys.RECORD);
153 verify(daoMock).removeSpecific(NORMALIZED_EID, SubKeys.RECORD);