JUnit test - FlatMapCacheTest
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / mapcache / FlatMapCacheTest.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.lispflowmapping.implementation.mapcache;
10
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;
19
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;
33
34 public class FlatMapCacheTest {
35
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";
40
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);
44
45     @Before
46     public void init() {
47         DAO_MOCK = mock(ILispDAO.class);
48         FLAT_MAP_CACHE = new FlatMapCache(DAO_MOCK);
49     }
50
51     /**
52      * Tests {@link FlatMapCache#addMapping}.
53      */
54     @Test
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));
59     }
60
61     /**
62      * Tests {@link FlatMapCache#getMapping}.
63      */
64     @Test
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));
68     }
69
70     /**
71      * Tests {@link FlatMapCache#removeMapping}.
72      */
73     @Test
74     public void removeMappingTest() {
75         FLAT_MAP_CACHE.removeMapping(EID_TEST, true);
76         verify(DAO_MOCK).removeSpecific(NORMALIZED_EID, SubKeys.RECORD);
77     }
78
79     /**
80      * Tests {@link FlatMapCache#addAuthenticationKey}.
81      */
82     @Test
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));
86     }
87
88     /**
89      * Tests {@link FlatMapCache#getAuthenticationKey}.
90      */
91     @Test
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));
95     }
96
97     /**
98      * Tests {@link FlatMapCache#getAuthenticationKey} with other than MappingAuthkey object.
99      */
100     @Test
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));
104     }
105
106     /**
107      * Tests {@link FlatMapCache#removeAuthenticationKey}.
108      */
109     @Test
110     public void removeAuthenticationKeyTest() {
111         FLAT_MAP_CACHE.removeAuthenticationKey(EID_TEST);
112         verify(DAO_MOCK).removeSpecific(NORMALIZED_EID, SubKeys.AUTH_KEY);
113     }
114
115     /**
116      * Tests {@link FlatMapCache#updateMappingRegistration}.
117      */
118     @Test
119     public void updateMappingRegistrationTest() {
120         FLAT_MAP_CACHE.updateMappingRegistration(EID_TEST);
121         verify(DAO_MOCK).put(NORMALIZED_EID, new MappingEntry<>(anyString(), any(Date.class)));
122     }
123
124     /**
125      * Tests {@link FlatMapCache#addData}.
126      */
127     @Test
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));
131     }
132
133     /**
134      * Tests {@link FlatMapCache#getData}.
135      */
136     @Test
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));
140     }
141
142     /**
143      * Tests {@link FlatMapCache#removeData}.
144      */
145     @Test
146     public void removeDataTest() {
147         FLAT_MAP_CACHE.removeData(EID_TEST, SubKeys.RECORD);
148         verify(DAO_MOCK).removeSpecific(NORMALIZED_EID, SubKeys.RECORD);
149     }
150 }