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.ListenerRegistration;
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 ListenerRegistration<AuthenticationKeyDataListener>
43 @InjectMocks private static AuthenticationKeyDataListener authenticationKeyDataListener;
45 private static DataTreeModification<AuthenticationKey> change_del;
46 private static DataTreeModification<AuthenticationKey> change_subtreeModified;
47 private static DataTreeModification<AuthenticationKey> change_write;
48 private static DataObjectModification<AuthenticationKey> mod_del;
49 private static DataObjectModification<AuthenticationKey> mod_subtreeModified;
50 private static DataObjectModification<AuthenticationKey> mod_write;
52 private static final String IPV4_STRING_1 = "192.168.0.1";
53 private static final String IPV4_STRING_2 = "192.168.0.2";
54 private static final String IPV4_STRING_3 = "192.168.0.3";
55 private static final String MASK = "/24";
57 private static final Eid IPV4_BINARY_EID_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
58 private static final Eid IPV4_BINARY_EID_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
59 private static final Eid IPV4_BINARY_EID_3 = LispAddressUtil.asIpv4Eid(IPV4_STRING_3);
60 private static final Eid IPV4_PREFIX_EID = LispAddressUtil.asIpv4PrefixEid(IPV4_STRING_1 + MASK);
61 private static final Eid IPV4_PREFIX_BINARY_EID = LispAddressUtil.asIpv4PrefixBinaryEid(IPV4_STRING_1 + MASK);
63 private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder().setKeyType(Uint16.ONE)
64 .setKeyString("key").build();
66 private static final AuthenticationKey AUTHENTICATION_KEY_1 = getAuthenticationKey(IPV4_BINARY_EID_1);
67 private static final AuthenticationKey AUTHENTICATION_KEY_2 = getAuthenticationKey(IPV4_BINARY_EID_2);
68 private static final AuthenticationKey AUTHENTICATION_KEY_3 = getAuthenticationKey(IPV4_BINARY_EID_3);
69 private static final AuthenticationKey AUTHENTICATION_KEY_PREFIX = getAuthenticationKey(IPV4_PREFIX_EID);
72 @SuppressWarnings("unchecked")
74 final InstanceIdentifier<AuthenticationKey> instanceIdentifierMock = Mockito.mock(InstanceIdentifier.class);
75 final DataTreeIdentifier<AuthenticationKey> dataTreeIdentifier =
76 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, instanceIdentifierMock);
78 change_del = Mockito.mock(DataTreeModification.class);
79 change_subtreeModified = Mockito.mock(DataTreeModification.class);
80 change_write = Mockito.mock(DataTreeModification.class);
81 mod_del = Mockito.mock(DataObjectModification.class);
82 mod_subtreeModified = Mockito.mock(DataObjectModification.class);
83 mod_write = Mockito.mock(DataObjectModification.class);
85 Mockito.when(change_del.getRootPath()).thenReturn(dataTreeIdentifier);
86 Mockito.when(change_del.getRootNode()).thenReturn(mod_del);
87 Mockito.when(change_subtreeModified.getRootPath()).thenReturn(dataTreeIdentifier);
88 Mockito.when(change_subtreeModified.getRootNode()).thenReturn(mod_subtreeModified);
89 Mockito.when(change_write.getRootPath()).thenReturn(dataTreeIdentifier);
90 Mockito.when(change_write.getRootNode()).thenReturn(mod_write);
91 Mockito.when(mod_del.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
92 Mockito.when(mod_subtreeModified.getModificationType())
93 .thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
94 Mockito.when(mod_write.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
96 Mockito.when(brokerMock.registerDataTreeChangeListener(Mockito.any(DataTreeIdentifier.class),
97 Mockito.any(AuthenticationKeyDataListener.class))).thenReturn(registrationMock);
98 authenticationKeyDataListener = new AuthenticationKeyDataListener(brokerMock, akdbMock);
102 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE mod type, binary eid.
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(akdbMock).removeAuthenticationKey(IPV4_BINARY_EID_1);
113 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE mod type, Ipv4Prefix eid.
116 public void onDataTreeChangedTest_delete_Ipv4PrefixEid() {
117 Mockito.when(mod_del.getDataBefore()).thenReturn(AUTHENTICATION_KEY_PREFIX);
119 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_del));
120 Mockito.verify(akdbMock).removeAuthenticationKey(IPV4_PREFIX_BINARY_EID);
124 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with SUBTREE_MODIFIED mod type.
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(akdbMock).addAuthenticationKey(IPV4_BINARY_EID_2, MAPPING_AUTHKEY);
135 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with WRITE mod type.
138 public void onDataTreeChangedTest_write() {
139 Mockito.when(mod_write.getDataAfter()).thenReturn(AUTHENTICATION_KEY_3);
141 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_write));
142 Mockito.verify(akdbMock).addAuthenticationKey(IPV4_BINARY_EID_3, MAPPING_AUTHKEY);
146 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with null mod type.
149 @SuppressWarnings("unchecked")
150 public void onDataTreeChangedTest_nullModType() {
151 final DataTreeModification<AuthenticationKey> change_nullModType = Mockito.mock(DataTreeModification.class);
152 final DataObjectModification mod_nullModType = Mockito.mock(DataObjectModification.class);
153 Mockito.when(change_nullModType.getRootNode()).thenReturn(mod_nullModType);
154 Mockito.when(mod_nullModType.getModificationType()).thenReturn(null);
156 authenticationKeyDataListener.onDataTreeChanged(Lists.newArrayList(change_nullModType));
157 Mockito.verifyNoInteractions(akdbMock);
161 * Tests {@link AuthenticationKeyDataListener#closeDataChangeListener} method.
164 public void closeDataChangeListenerTest() {
165 authenticationKeyDataListener.closeDataChangeListener();
166 Mockito.verify(registrationMock).close();
169 private static AuthenticationKey getAuthenticationKey(Eid eid) {
170 return new AuthenticationKeyBuilder()
171 .withKey(new AuthenticationKeyKey(new EidUri("uri-1")))
173 .setMappingAuthkey(MAPPING_AUTHKEY)