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.southbound.lisp;
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.SimpleMapCache;
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;
33 @RunWith(MockitoJUnitRunner.class)
34 public class AuthenticationKeyDataListenerTest {
36 @Mock(name = "smc") private static SimpleMapCache smcMock;
37 @Mock(name = "broker") private static DataBroker brokerMock;
38 @Mock(name = "registration") private static ListenerRegistration<AuthenticationKeyDataListener>
40 @InjectMocks private static AuthenticationKeyDataListener authenticationKeyDataListener;
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;
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";
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);
60 private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder().setKeyType(1)
61 .setKeyString("key").build();
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);
69 @SuppressWarnings("unchecked")
71 final InstanceIdentifier<AuthenticationKey> instanceIdentifierMock = Mockito.mock(InstanceIdentifier.class);
72 final DataTreeIdentifier<AuthenticationKey> dataTreeIdentifier =
73 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceIdentifierMock);
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);
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);
93 Mockito.when(brokerMock.registerDataTreeChangeListener(Mockito.any(DataTreeIdentifier.class),
94 Mockito.any(AuthenticationKeyDataListener.class))).thenReturn(registrationMock);
95 authenticationKeyDataListener = new AuthenticationKeyDataListener(brokerMock, smcMock);
99 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE mod type, binary eid.
102 @SuppressWarnings("unchecked")
103 public void onDataTreeChangedTest_delete_BinaryEid() {
104 Mockito.when(mod_del.getDataBefore()).thenReturn(AUTHENTICATION_KEY_1);
106 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_del));
107 Mockito.verify(smcMock).removeAuthenticationKey(IPV4_BINARY_EID_1);
111 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE mod type, Ipv4Prefix eid.
114 @SuppressWarnings("unchecked")
115 public void onDataTreeChangedTest_delete_Ipv4PrefixEid() {
116 Mockito.when(mod_del.getDataBefore()).thenReturn(AUTHENTICATION_KEY_PREFIX);
118 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_del));
119 Mockito.verify(smcMock).removeAuthenticationKey(IPV4_PREFIX_BINARY_EID);
123 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with SUBTREE_MODIFIED mod type.
126 @SuppressWarnings("unchecked")
127 public void onDataTreeChangedTest_subtreeModified() {
128 Mockito.when(mod_subtreeModified.getDataAfter()).thenReturn(AUTHENTICATION_KEY_2);
130 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_subtreeModified));
131 Mockito.verify(smcMock).addAuthenticationKey(IPV4_BINARY_EID_2, MAPPING_AUTHKEY);
135 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with WRITE mod type.
138 @SuppressWarnings("unchecked")
139 public void onDataTreeChangedTest_write() {
140 Mockito.when(mod_write.getDataAfter()).thenReturn(AUTHENTICATION_KEY_3);
142 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_write));
143 Mockito.verify(smcMock).addAuthenticationKey(IPV4_BINARY_EID_3, MAPPING_AUTHKEY);
147 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with null mod type.
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);
157 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_nullModType));
158 Mockito.verifyZeroInteractions(smcMock);
162 * Tests {@link AuthenticationKeyDataListener#closeDataChangeListener} method.
165 public void closeDataChangeListenerTest() {
166 authenticationKeyDataListener.closeDataChangeListener();
167 Mockito.verify(registrationMock).close();
170 private static AuthenticationKey getAuthenticationKey(Eid eid) {
171 return new AuthenticationKeyBuilder()
173 .setMappingAuthkey(MAPPING_AUTHKEY)