Merge "Remove SimpleMapCache#getXtrIdTable()"
[lispflowmapping.git] / mappingservice / southbound / src / test / java / org / opendaylight / lispflowmapping / southbound / lisp / AuthenticationKeyDataListenerTest.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.southbound.lisp;
9
10 import com.google.common.collect.Lists;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.mockito.InjectMocks;
15 import org.mockito.Mock;
16 import org.mockito.Mockito;
17 import org.mockito.runners.MockitoJUnitRunner;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
24 import org.opendaylight.lispflowmapping.mapcache.AuthKeyDb;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyBuilder;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32
33 @RunWith(MockitoJUnitRunner.class)
34 public class AuthenticationKeyDataListenerTest {
35
36     @Mock(name = "akdb") private static AuthKeyDb akdbMock;
37     @Mock(name = "broker") private static DataBroker brokerMock;
38     @Mock(name = "registration") private static ListenerRegistration<AuthenticationKeyDataListener>
39             registrationMock;
40     @InjectMocks private static AuthenticationKeyDataListener authenticationKeyDataListener;
41
42     private static DataTreeModification<AuthenticationKey> change_del;
43     private static DataTreeModification<AuthenticationKey> change_subtreeModified;
44     private static DataTreeModification<AuthenticationKey> change_write;
45     private static DataObjectModification<AuthenticationKey> mod_del;
46     private static DataObjectModification<AuthenticationKey> mod_subtreeModified;
47     private static DataObjectModification<AuthenticationKey> mod_write;
48
49     private static final String IPV4_STRING_1 = "192.168.0.1";
50     private static final String IPV4_STRING_2 = "192.168.0.2";
51     private static final String IPV4_STRING_3 = "192.168.0.3";
52     private static final String MASK = "/24";
53
54     private static final Eid IPV4_BINARY_EID_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
55     private static final Eid IPV4_BINARY_EID_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
56     private static final Eid IPV4_BINARY_EID_3 = LispAddressUtil.asIpv4Eid(IPV4_STRING_3);
57     private static final Eid IPV4_PREFIX_EID = LispAddressUtil.asIpv4PrefixEid(IPV4_STRING_1 + MASK);
58     private static final Eid IPV4_PREFIX_BINARY_EID = LispAddressUtil.asIpv4PrefixBinaryEid(IPV4_STRING_1 + MASK);
59
60     private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder().setKeyType(1)
61             .setKeyString("key").build();
62
63     private static final AuthenticationKey AUTHENTICATION_KEY_1 = getAuthenticationKey(IPV4_BINARY_EID_1);
64     private static final AuthenticationKey AUTHENTICATION_KEY_2 = getAuthenticationKey(IPV4_BINARY_EID_2);
65     private static final AuthenticationKey AUTHENTICATION_KEY_3 = getAuthenticationKey(IPV4_BINARY_EID_3);
66     private static final AuthenticationKey AUTHENTICATION_KEY_PREFIX = getAuthenticationKey(IPV4_PREFIX_EID);
67
68     @Before
69     @SuppressWarnings("unchecked")
70     public void init() {
71         final InstanceIdentifier<AuthenticationKey> instanceIdentifierMock = Mockito.mock(InstanceIdentifier.class);
72         final DataTreeIdentifier<AuthenticationKey> dataTreeIdentifier =
73                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceIdentifierMock);
74
75         change_del = Mockito.mock(DataTreeModification.class);
76         change_subtreeModified = Mockito.mock(DataTreeModification.class);
77         change_write = Mockito.mock(DataTreeModification.class);
78         mod_del = Mockito.mock(DataObjectModification.class);
79         mod_subtreeModified = Mockito.mock(DataObjectModification.class);
80         mod_write = Mockito.mock(DataObjectModification.class);
81
82         Mockito.when(change_del.getRootPath()).thenReturn(dataTreeIdentifier);
83         Mockito.when(change_del.getRootNode()).thenReturn(mod_del);
84         Mockito.when(change_subtreeModified.getRootPath()).thenReturn(dataTreeIdentifier);
85         Mockito.when(change_subtreeModified.getRootNode()).thenReturn(mod_subtreeModified);
86         Mockito.when(change_write.getRootPath()).thenReturn(dataTreeIdentifier);
87         Mockito.when(change_write.getRootNode()).thenReturn(mod_write);
88         Mockito.when(mod_del.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
89         Mockito.when(mod_subtreeModified.getModificationType())
90                 .thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
91         Mockito.when(mod_write.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
92
93         Mockito.when(brokerMock.registerDataTreeChangeListener(Mockito.any(DataTreeIdentifier.class),
94                 Mockito.any(AuthenticationKeyDataListener.class))).thenReturn(registrationMock);
95         authenticationKeyDataListener = new AuthenticationKeyDataListener(brokerMock, akdbMock);
96     }
97
98     /**
99      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE mod type, binary eid.
100      */
101     @Test
102     @SuppressWarnings("unchecked")
103     public void onDataTreeChangedTest_delete_BinaryEid() {
104         Mockito.when(mod_del.getDataBefore()).thenReturn(AUTHENTICATION_KEY_1);
105
106         authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_del));
107         Mockito.verify(akdbMock).removeAuthenticationKey(IPV4_BINARY_EID_1);
108     }
109
110     /**
111      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE mod type, Ipv4Prefix eid.
112      */
113     @Test
114     @SuppressWarnings("unchecked")
115     public void onDataTreeChangedTest_delete_Ipv4PrefixEid() {
116         Mockito.when(mod_del.getDataBefore()).thenReturn(AUTHENTICATION_KEY_PREFIX);
117
118         authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_del));
119         Mockito.verify(akdbMock).removeAuthenticationKey(IPV4_PREFIX_BINARY_EID);
120     }
121
122     /**
123      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with SUBTREE_MODIFIED mod type.
124      */
125     @Test
126     @SuppressWarnings("unchecked")
127     public void onDataTreeChangedTest_subtreeModified() {
128         Mockito.when(mod_subtreeModified.getDataAfter()).thenReturn(AUTHENTICATION_KEY_2);
129
130         authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_subtreeModified));
131         Mockito.verify(akdbMock).addAuthenticationKey(IPV4_BINARY_EID_2, MAPPING_AUTHKEY);
132     }
133
134     /**
135      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with WRITE mod type.
136      */
137     @Test
138     @SuppressWarnings("unchecked")
139     public void onDataTreeChangedTest_write() {
140         Mockito.when(mod_write.getDataAfter()).thenReturn(AUTHENTICATION_KEY_3);
141
142         authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_write));
143         Mockito.verify(akdbMock).addAuthenticationKey(IPV4_BINARY_EID_3, MAPPING_AUTHKEY);
144     }
145
146     /**
147      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with null mod type.
148      */
149     @Test
150     @SuppressWarnings("unchecked")
151     public void onDataTreeChangedTest_nullModType() {
152         final DataTreeModification<AuthenticationKey> change_nullModType = Mockito.mock(DataTreeModification.class);
153         final DataObjectModification mod_nullModType = Mockito.mock(DataObjectModification.class);
154         Mockito.when(change_nullModType.getRootNode()).thenReturn(mod_nullModType);
155         Mockito.when(mod_nullModType.getModificationType()).thenReturn(null);
156
157         authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_nullModType));
158         Mockito.verifyZeroInteractions(akdbMock);
159     }
160
161     /**
162      * Tests {@link AuthenticationKeyDataListener#closeDataChangeListener} method.
163      */
164     @Test
165     public void closeDataChangeListenerTest() {
166         authenticationKeyDataListener.closeDataChangeListener();
167         Mockito.verify(registrationMock).close();
168     }
169
170     private static AuthenticationKey getAuthenticationKey(Eid eid) {
171         return new AuthenticationKeyBuilder()
172                 .setEid(eid)
173                 .setMappingAuthkey(MAPPING_AUTHKEY)
174                 .build();
175     }
176 }