JUnit test - MultiTableMapCacheTest
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / mapcache / FlatMapCacheTest.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.implementation.mapcache;
9
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;
18
19 import java.util.Date;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
23 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
24 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
25 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address
28         .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
32         .MappingAuthkey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container
34         .MappingAuthkeyBuilder;
35
36 public class FlatMapCacheTest {
37
38     private static ILispDAO daoMock;
39     private static FlatMapCache flatMapCache;
40     private static final Eid EID_MOCK = mock(Eid.class);
41     private static final Object DUMMY_OBJECT = "dummy_object";
42
43     private static final Eid EID_TEST = new EidBuilder().setAddress(new Ipv4Builder()
44             .setIpv4(new Ipv4Address("127.0.0.1")).build()).build();
45     private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder().setKeyString("auth_string_test")
46             .build();
47     private static final Eid NORMALIZED_EID = MaskUtil.normalize(EID_TEST);
48
49     @Before
50     public void init() {
51         daoMock = mock(ILispDAO.class);
52         flatMapCache = new FlatMapCache(daoMock);
53     }
54
55     /**
56      * Tests {@link FlatMapCache#addMapping}.
57      */
58     @Test
59     public void addMappingTest() {
60         flatMapCache.addMapping(EID_TEST, DUMMY_OBJECT, false);
61         verify(daoMock, atMost(2)).put(NORMALIZED_EID, new MappingEntry<>(anyString(), any(Date.class)));
62         verify(daoMock).put(NORMALIZED_EID, new MappingEntry<>(SubKeys.RECORD, DUMMY_OBJECT));
63     }
64
65     /**
66      * Tests {@link FlatMapCache#getMapping}.
67      */
68     @Test
69     public void getMappingTest() {
70         when(daoMock.getSpecific(NORMALIZED_EID, SubKeys.RECORD)).thenReturn(DUMMY_OBJECT);
71         assertEquals(DUMMY_OBJECT, flatMapCache.getMapping(EID_MOCK, EID_TEST));
72     }
73
74     /**
75      * Tests {@link FlatMapCache#removeMapping}.
76      */
77     @Test
78     public void removeMappingTest() {
79         flatMapCache.removeMapping(EID_TEST, true);
80         verify(daoMock).removeSpecific(NORMALIZED_EID, SubKeys.RECORD);
81     }
82
83     /**
84      * Tests {@link FlatMapCache#addAuthenticationKey}.
85      */
86     @Test
87     public void addAuthenticationKeyTest() {
88         flatMapCache.addAuthenticationKey(EID_TEST, MAPPING_AUTHKEY);
89         verify(daoMock, atMost(1)).put(NORMALIZED_EID, new MappingEntry<>(SubKeys.AUTH_KEY, MAPPING_AUTHKEY));
90     }
91
92     /**
93      * Tests {@link FlatMapCache#getAuthenticationKey}.
94      */
95     @Test
96     public void getAuthenticationKeyTest() {
97         when(daoMock.getSpecific(NORMALIZED_EID, SubKeys.AUTH_KEY)).thenReturn(MAPPING_AUTHKEY);
98         assertEquals(MAPPING_AUTHKEY, flatMapCache.getAuthenticationKey(EID_TEST));
99     }
100
101     /**
102      * Tests {@link FlatMapCache#getAuthenticationKey} with other than MappingAuthkey object.
103      */
104     @Test
105     public void getAuthenticationKeyTest_withNull() {
106         when(daoMock.getSpecific(NORMALIZED_EID, SubKeys.AUTH_KEY)).thenReturn(new Object());
107         assertNull(flatMapCache.getAuthenticationKey(EID_TEST));
108     }
109
110     /**
111      * Tests {@link FlatMapCache#removeAuthenticationKey}.
112      */
113     @Test
114     public void removeAuthenticationKeyTest() {
115         flatMapCache.removeAuthenticationKey(EID_TEST);
116         verify(daoMock).removeSpecific(NORMALIZED_EID, SubKeys.AUTH_KEY);
117     }
118
119     /**
120      * Tests {@link FlatMapCache#updateMappingRegistration}.
121      */
122     @Test
123     public void updateMappingRegistrationTest() {
124         flatMapCache.updateMappingRegistration(EID_TEST);
125         verify(daoMock).put(NORMALIZED_EID, new MappingEntry<>(anyString(), any(Date.class)));
126     }
127
128     /**
129      * Tests {@link FlatMapCache#addData}.
130      */
131     @Test
132     public void addDataTest() {
133         flatMapCache.addData(EID_TEST, SubKeys.RECORD, DUMMY_OBJECT);
134         verify(daoMock).put(NORMALIZED_EID, new MappingEntry<>(SubKeys.RECORD, DUMMY_OBJECT));
135     }
136
137     /**
138      * Tests {@link FlatMapCache#getData}.
139      */
140     @Test
141     public void getDataTest() {
142         when(daoMock.getSpecific(NORMALIZED_EID, SubKeys.RECORD)).thenReturn(DUMMY_OBJECT);
143         assertEquals(DUMMY_OBJECT, flatMapCache.getData(EID_TEST, SubKeys.RECORD));
144     }
145
146     /**
147      * Tests {@link FlatMapCache#removeData}.
148      */
149     @Test
150     public void removeDataTest() {
151         flatMapCache.removeData(EID_TEST, SubKeys.RECORD);
152         verify(daoMock).removeSpecific(NORMALIZED_EID, SubKeys.RECORD);
153     }
154 }