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.junit.MockitoJUnitRunner;
18 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
19 import org.opendaylight.lispflowmapping.mapcache.AuthKeyDb;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.binding.api.DataObjectModification;
22 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
23 import org.opendaylight.mdsal.binding.api.DataTreeModification;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
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.EidUri;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyKey;
32 import org.opendaylight.yangtools.concepts.Registration;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.Uint16;
36 @RunWith(MockitoJUnitRunner.class)
37 public class AuthenticationKeyDataListenerTest {
39 @Mock(name = "akdb") private static AuthKeyDb akdbMock;
40 @Mock(name = "broker") private static DataBroker brokerMock;
41 @Mock(name = "registration") private static Registration registrationMock;
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(Uint16.ONE)
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 DataTreeIdentifier.of(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.modificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
91 Mockito.when(mod_subtreeModified.modificationType())
92 .thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
93 Mockito.when(mod_write.modificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
95 Mockito.when(brokerMock.registerTreeChangeListener(Mockito.any(DataTreeIdentifier.class),
96 Mockito.any(AuthenticationKeyDataListener.class))).thenReturn(registrationMock);
97 authenticationKeyDataListener = new AuthenticationKeyDataListener(brokerMock, akdbMock);
101 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE mod type, binary eid.
104 public void onDataTreeChangedTest_delete_BinaryEid() {
105 Mockito.when(mod_del.dataBefore()).thenReturn(AUTHENTICATION_KEY_1);
107 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_del));
108 Mockito.verify(akdbMock).removeAuthenticationKey(IPV4_BINARY_EID_1);
112 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE mod type, Ipv4Prefix eid.
115 public void onDataTreeChangedTest_delete_Ipv4PrefixEid() {
116 Mockito.when(mod_del.dataBefore()).thenReturn(AUTHENTICATION_KEY_PREFIX);
118 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_del));
119 Mockito.verify(akdbMock).removeAuthenticationKey(IPV4_PREFIX_BINARY_EID);
123 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with SUBTREE_MODIFIED mod type.
126 public void onDataTreeChangedTest_subtreeModified() {
127 Mockito.when(mod_subtreeModified.dataAfter()).thenReturn(AUTHENTICATION_KEY_2);
129 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_subtreeModified));
130 Mockito.verify(akdbMock).addAuthenticationKey(IPV4_BINARY_EID_2, MAPPING_AUTHKEY);
134 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with WRITE mod type.
137 public void onDataTreeChangedTest_write() {
138 Mockito.when(mod_write.dataAfter()).thenReturn(AUTHENTICATION_KEY_3);
140 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_write));
141 Mockito.verify(akdbMock).addAuthenticationKey(IPV4_BINARY_EID_3, MAPPING_AUTHKEY);
145 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with null mod type.
148 @SuppressWarnings("unchecked")
149 public void onDataTreeChangedTest_nullModType() {
150 final DataTreeModification<AuthenticationKey> change_nullModType = Mockito.mock(DataTreeModification.class);
151 final DataObjectModification mod_nullModType = Mockito.mock(DataObjectModification.class);
152 Mockito.when(change_nullModType.getRootNode()).thenReturn(mod_nullModType);
153 Mockito.when(mod_nullModType.modificationType()).thenReturn(null);
155 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_nullModType));
156 Mockito.verifyNoInteractions(akdbMock);
160 * Tests {@link AuthenticationKeyDataListener#closeDataChangeListener} method.
163 public void closeDataChangeListenerTest() {
164 authenticationKeyDataListener.closeDataChangeListener();
165 Mockito.verify(registrationMock).close();
168 private static AuthenticationKey getAuthenticationKey(final Eid eid) {
169 return new AuthenticationKeyBuilder()
170 .withKey(new AuthenticationKeyKey(new EidUri("uri-1")))
172 .setMappingAuthkey(MAPPING_AUTHKEY)