Bug 6102: Upgrade ietf-{inet,yang}-types to 2013-07-15
[lispflowmapping.git] / mappingservice / mapcache / src / test / java / org / opendaylight / lispflowmapping / 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.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
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
29         .Ipv4Builder;
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
33         .MappingAuthkey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container
35         .MappingAuthkeyBuilder;
36
37 public class FlatMapCacheTest {
38
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";
43
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")
47             .build();
48     private static final Eid NORMALIZED_EID = MaskUtil.normalize(EID_TEST);
49
50     @Before
51     public void init() {
52         daoMock = mock(ILispDAO.class);
53         flatMapCache = new FlatMapCache(daoMock);
54     }
55
56     /**
57      * Tests {@link FlatMapCache#addMapping}.
58      */
59     @Test
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));
64     }
65
66     /**
67      * Tests {@link FlatMapCache#getMapping}.
68      */
69     @Test
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));
73     }
74
75     /**
76      * Tests {@link FlatMapCache#removeMapping}.
77      */
78     @Test
79     public void removeMappingTest() {
80         flatMapCache.removeMapping(EID_TEST, true);
81         verify(daoMock).removeSpecific(NORMALIZED_EID, SubKeys.RECORD);
82     }
83
84     /**
85      * Tests {@link FlatMapCache#addAuthenticationKey}.
86      */
87     @Test
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));
91     }
92
93     /**
94      * Tests {@link FlatMapCache#getAuthenticationKey}.
95      */
96     @Test
97     public void getAuthenticationKeyTest() {
98         when(daoMock.getSpecific(NORMALIZED_EID, SubKeys.AUTH_KEY)).thenReturn(MAPPING_AUTHKEY);
99         assertEquals(MAPPING_AUTHKEY, flatMapCache.getAuthenticationKey(EID_TEST));
100     }
101
102     /**
103      * Tests {@link FlatMapCache#getAuthenticationKey} with other than MappingAuthkey object.
104      */
105     @Test
106     public void getAuthenticationKeyTest_withNull() {
107         when(daoMock.getSpecific(NORMALIZED_EID, SubKeys.AUTH_KEY)).thenReturn(new Object());
108         assertNull(flatMapCache.getAuthenticationKey(EID_TEST));
109     }
110
111     /**
112      * Tests {@link FlatMapCache#removeAuthenticationKey}.
113      */
114     @Test
115     public void removeAuthenticationKeyTest() {
116         flatMapCache.removeAuthenticationKey(EID_TEST);
117         verify(daoMock).removeSpecific(NORMALIZED_EID, SubKeys.AUTH_KEY);
118     }
119
120     /**
121      * Tests {@link FlatMapCache#updateMappingRegistration}.
122      */
123     @Test
124     public void updateMappingRegistrationTest() {
125         flatMapCache.updateMappingRegistration(EID_TEST, null);
126         verify(daoMock).put(NORMALIZED_EID, new MappingEntry<>(anyString(), any(Date.class)));
127     }
128
129     /**
130      * Tests {@link FlatMapCache#addData}.
131      */
132     @Test
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));
136     }
137
138     /**
139      * Tests {@link FlatMapCache#getData}.
140      */
141     @Test
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));
145     }
146
147     /**
148      * Tests {@link FlatMapCache#removeData}.
149      */
150     @Test
151     public void removeDataTest() {
152         flatMapCache.removeData(EID_TEST, SubKeys.RECORD);
153         verify(daoMock).removeSpecific(NORMALIZED_EID, SubKeys.RECORD);
154     }
155 }