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.implementation.mdsal;
10 import java.util.List;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.mockito.Mockito;
14 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
15 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.DataObjectModification;
18 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
19 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
20 import org.opendaylight.mdsal.binding.api.DataTreeModification;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.EidUri;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingDatabase;
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.db.instance.AuthenticationKeyKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.database.VirtualNetworkIdentifier;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.common.Uint16;
33 public class AuthenticationKeyDataListenerTest {
35 private static IMappingSystem iMappingSystemMock;
36 private static AuthenticationKeyDataListener authenticationKeyDataListener;
38 private static DataTreeModification<AuthenticationKey> change_del;
39 private static DataTreeModification<AuthenticationKey> change_subtreeModified;
40 private static DataTreeModification<AuthenticationKey> change_write;
41 private static DataObjectModification<AuthenticationKey> mod_del;
42 private static DataObjectModification<AuthenticationKey> mod_subtreeModified;
43 private static DataObjectModification<AuthenticationKey> mod_write;
45 private static final String IPV4_STRING_1 = "192.168.0.1";
46 private static final String IPV4_STRING_2 = "192.168.0.2";
47 private static final String IPV4_STRING_3 = "192.168.0.3";
48 private static final Eid IPV4_EID_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
49 private static final Eid IPV4_EID_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
50 private static final Eid IPV4_EID_3 = LispAddressUtil.asIpv4Eid(IPV4_STRING_3);
51 private static final AuthenticationKey AUTHENTICATION_KEY_1 = getAuthenticationKey(IPV4_EID_1, "pass1");
52 private static final AuthenticationKey AUTHENTICATION_KEY_2 = getAuthenticationKey(IPV4_EID_2, "pass2");
53 private static final AuthenticationKey AUTHENTICATION_KEY_3 = getAuthenticationKey(IPV4_EID_3, "pass3");
56 @SuppressWarnings("unchecked")
58 DataBroker dataBrokerMock = Mockito.mock(DataBroker.class);
59 iMappingSystemMock = Mockito.mock(IMappingSystem.class);
60 authenticationKeyDataListener = new AuthenticationKeyDataListener(dataBrokerMock, iMappingSystemMock);
62 final DataTreeIdentifier<AuthenticationKey> dataTreeIdentifier = DataTreeIdentifier.of(
63 LogicalDatastoreType.CONFIGURATION,
64 InstanceIdentifier.create(MappingDatabase.class).child(VirtualNetworkIdentifier.class)
65 .child(AuthenticationKey.class));
67 change_del = Mockito.mock(DataTreeModification.class);
68 change_subtreeModified = Mockito.mock(DataTreeModification.class);
69 change_write = Mockito.mock(DataTreeModification.class);
70 mod_del = Mockito.mock(DataObjectModification.class);
71 mod_subtreeModified = Mockito.mock(DataObjectModification.class);
72 mod_write = Mockito.mock(DataObjectModification.class);
74 Mockito.when(change_del.getRootPath()).thenReturn(dataTreeIdentifier);
75 Mockito.when(change_del.getRootNode()).thenReturn(mod_del);
76 Mockito.when(change_subtreeModified.getRootPath()).thenReturn(dataTreeIdentifier);
77 Mockito.when(change_subtreeModified.getRootNode()).thenReturn(mod_subtreeModified);
78 Mockito.when(change_write.getRootPath()).thenReturn(dataTreeIdentifier);
79 Mockito.when(change_write.getRootNode()).thenReturn(mod_write);
80 Mockito.when(mod_del.modificationType()).thenReturn(ModificationType.DELETE);
81 Mockito.when(mod_subtreeModified.modificationType()).thenReturn(ModificationType.SUBTREE_MODIFIED);
82 Mockito.when(mod_write.modificationType()).thenReturn(ModificationType.WRITE);
86 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE modification type.
89 public void onDataTreeChangedTest_delete() {
90 Mockito.when(mod_del.dataBefore()).thenReturn(AUTHENTICATION_KEY_1);
92 authenticationKeyDataListener.onDataTreeChanged(List.of(change_del));
93 Mockito.verify(iMappingSystemMock).removeAuthenticationKey(IPV4_EID_1);
97 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with WRITE modification type.
100 public void onDataTreeChangedTest_write() {
101 Mockito.when(mod_write.dataAfter()).thenReturn(AUTHENTICATION_KEY_2);
103 authenticationKeyDataListener.onDataTreeChanged(List.of(change_write));
104 Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_2, AUTHENTICATION_KEY_2.getMappingAuthkey());
108 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with SUBTREE_MODIFIED modification type.
111 public void onDataTreeChangedTest_subtreeModified() {
112 Mockito.when(mod_subtreeModified.dataAfter()).thenReturn(AUTHENTICATION_KEY_3);
114 authenticationKeyDataListener.onDataTreeChanged(List.of(change_subtreeModified));
115 Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_3, AUTHENTICATION_KEY_3.getMappingAuthkey());
119 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with multiple modification types.
122 public void onDataTreeChangedTest_multipleModTypes() {
123 Mockito.when(mod_del.dataBefore()).thenReturn(AUTHENTICATION_KEY_1);
124 Mockito.when(mod_write.dataAfter()).thenReturn(AUTHENTICATION_KEY_2);
125 Mockito.when(mod_subtreeModified.dataAfter()).thenReturn(AUTHENTICATION_KEY_3);
127 authenticationKeyDataListener.onDataTreeChanged(List.of(change_del, change_write, change_subtreeModified));
128 Mockito.verify(iMappingSystemMock).removeAuthenticationKey(IPV4_EID_1);
129 Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_2, AUTHENTICATION_KEY_2.getMappingAuthkey());
130 Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_3, AUTHENTICATION_KEY_3.getMappingAuthkey());
134 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with no modification type.
137 @SuppressWarnings("unchecked")
138 public void onDataTreeChangedTest_noModType() {
139 final DataTreeModification<AuthenticationKey> changeNoModType = Mockito.mock(DataTreeModification.class);
140 final DataObjectModification<AuthenticationKey> modNoType = Mockito.mock(DataObjectModification.class);
142 Mockito.when(changeNoModType.getRootNode()).thenReturn(modNoType);
143 Mockito.when(modNoType.modificationType()).thenReturn(null);
145 authenticationKeyDataListener.onDataTreeChanged(List.of(changeNoModType));
146 Mockito.verifyZeroInteractions(iMappingSystemMock);
149 private static AuthenticationKey getAuthenticationKey(Eid eid, String password) {
150 return new AuthenticationKeyBuilder()
151 .withKey(new AuthenticationKeyKey(new EidUri("uri-1")))
153 .setMappingAuthkey(new MappingAuthkeyBuilder()
154 .setKeyString(password)
155 .setKeyType(Uint16.valueOf(1)).build())