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.mappingservice.rev150906.db.instance.AuthenticationKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container
31 .MappingAuthkeyBuilder;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 @RunWith(MockitoJUnitRunner.class)
36 public class AuthenticationKeyDataListenerTest {
38 @Mock(name = "smc") private static SimpleMapCache smcMock;
39 @Mock(name = "broker") private static DataBroker brokerMock;
40 @Mock(name = "registration") private static ListenerRegistration<AuthenticationKeyDataListener>
42 @InjectMocks private static AuthenticationKeyDataListener authenticationKeyDataListener;
44 private static DataTreeModification<AuthenticationKey> change_del;
45 private static DataTreeModification<AuthenticationKey> change_subtreeModified;
46 private static DataTreeModification<AuthenticationKey> change_write;
47 private static DataObjectModification<AuthenticationKey> mod_del;
48 private static DataObjectModification<AuthenticationKey> mod_subtreeModified;
49 private static DataObjectModification<AuthenticationKey> mod_write;
51 private static final String IPV4_STRING_1 = "192.168.0.1";
52 private static final String IPV4_STRING_2 = "192.168.0.2";
53 private static final String IPV4_STRING_3 = "192.168.0.3";
54 private static final String MASK = "/24";
56 private static final Eid IPV4_BINARY_EID_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
57 private static final Eid IPV4_BINARY_EID_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
58 private static final Eid IPV4_BINARY_EID_3 = LispAddressUtil.asIpv4Eid(IPV4_STRING_3);
59 private static final Eid IPV4_PREFIX_EID = LispAddressUtil.asIpv4PrefixEid(IPV4_STRING_1 + MASK);
60 private static final Eid IPV4_PREFIX_BINARY_EID = LispAddressUtil.asIpv4PrefixBinaryEid(IPV4_STRING_1 + MASK);
62 private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder().setKeyType(1)
63 .setKeyString("key").build();
65 private static final AuthenticationKey AUTHENTICATION_KEY_1 = getAuthenticationKey(IPV4_BINARY_EID_1);
66 private static final AuthenticationKey AUTHENTICATION_KEY_2 = getAuthenticationKey(IPV4_BINARY_EID_2);
67 private static final AuthenticationKey AUTHENTICATION_KEY_3 = getAuthenticationKey(IPV4_BINARY_EID_3);
68 private static final AuthenticationKey AUTHENTICATION_KEY_PREFIX = getAuthenticationKey(IPV4_PREFIX_EID);
71 @SuppressWarnings("unchecked")
73 final InstanceIdentifier<AuthenticationKey> instanceIdentifierMock = Mockito.mock(InstanceIdentifier.class);
74 final DataTreeIdentifier<AuthenticationKey> dataTreeIdentifier =
75 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceIdentifierMock);
77 change_del = Mockito.mock(DataTreeModification.class);
78 change_subtreeModified = Mockito.mock(DataTreeModification.class);
79 change_write = Mockito.mock(DataTreeModification.class);
80 mod_del = Mockito.mock(DataObjectModification.class);
81 mod_subtreeModified = Mockito.mock(DataObjectModification.class);
82 mod_write = Mockito.mock(DataObjectModification.class);
84 Mockito.when(change_del.getRootPath()).thenReturn(dataTreeIdentifier);
85 Mockito.when(change_del.getRootNode()).thenReturn(mod_del);
86 Mockito.when(change_subtreeModified.getRootPath()).thenReturn(dataTreeIdentifier);
87 Mockito.when(change_subtreeModified.getRootNode()).thenReturn(mod_subtreeModified);
88 Mockito.when(change_write.getRootPath()).thenReturn(dataTreeIdentifier);
89 Mockito.when(change_write.getRootNode()).thenReturn(mod_write);
90 Mockito.when(mod_del.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
91 Mockito.when(mod_subtreeModified.getModificationType()).
92 thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
93 Mockito.when(mod_write.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
95 Mockito.when(brokerMock.registerDataTreeChangeListener(Mockito.any(DataTreeIdentifier.class),
96 Mockito.any(AuthenticationKeyDataListener.class))).thenReturn(registrationMock);
97 authenticationKeyDataListener = new AuthenticationKeyDataListener(brokerMock, smcMock);
101 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE mod type, binary eid.
104 @SuppressWarnings("unchecked")
105 public void onDataTreeChangedTest_delete_BinaryEid() {
106 Mockito.when(mod_del.getDataBefore()).thenReturn(AUTHENTICATION_KEY_1);
108 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_del));
109 Mockito.verify(smcMock).removeAuthenticationKey(IPV4_BINARY_EID_1);
113 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE mod type, Ipv4Prefix eid.
116 @SuppressWarnings("unchecked")
117 public void onDataTreeChangedTest_delete_Ipv4PrefixEid() {
118 Mockito.when(mod_del.getDataBefore()).thenReturn(AUTHENTICATION_KEY_PREFIX);
120 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_del));
121 Mockito.verify(smcMock).removeAuthenticationKey(IPV4_PREFIX_BINARY_EID);
125 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with SUBTREE_MODIFIED mod type.
128 @SuppressWarnings("unchecked")
129 public void onDataTreeChangedTest_subtreeModified() {
130 Mockito.when(mod_subtreeModified.getDataAfter()).thenReturn(AUTHENTICATION_KEY_2);
132 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_subtreeModified));
133 Mockito.verify(smcMock).addAuthenticationKey(IPV4_BINARY_EID_2, MAPPING_AUTHKEY);
137 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with WRITE mod type.
140 @SuppressWarnings("unchecked")
141 public void onDataTreeChangedTest_write() {
142 Mockito.when(mod_write.getDataAfter()).thenReturn(AUTHENTICATION_KEY_3);
144 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_write));
145 Mockito.verify(smcMock).addAuthenticationKey(IPV4_BINARY_EID_3, MAPPING_AUTHKEY);
149 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with null mod type.
152 @SuppressWarnings("unchecked")
153 public void onDataTreeChangedTest_nullModType() {
154 final DataTreeModification<AuthenticationKey> change_nullModType = Mockito.mock(DataTreeModification.class);
155 final DataObjectModification mod_nullModType = Mockito.mock(DataObjectModification.class);
156 Mockito.when(change_nullModType.getRootNode()).thenReturn(mod_nullModType);
157 Mockito.when(mod_nullModType.getModificationType()).thenReturn(null);
159 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_nullModType));
160 Mockito.verifyZeroInteractions(smcMock);
164 * Tests {@link AuthenticationKeyDataListener#closeDataChangeListener} method.
167 public void closeDataChangeListenerTest() {
168 authenticationKeyDataListener.closeDataChangeListener();
169 Mockito.verify(registrationMock).close();
172 private static AuthenticationKey getAuthenticationKey(Eid eid) {
173 return new AuthenticationKeyBuilder()
175 .setMappingAuthkey(MAPPING_AUTHKEY)