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 com.google.common.collect.Lists;
11 import java.util.List;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.mockito.Mockito;
15 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
16 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.DataObjectModification;
19 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
20 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
21 import org.opendaylight.mdsal.binding.api.DataTreeModification;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.EidUri;
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.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.Uint16;
32 public class AuthenticationKeyDataListenerTest {
34 private static IMappingSystem iMappingSystemMock;
35 private static AuthenticationKeyDataListener authenticationKeyDataListener;
37 private static DataTreeModification<AuthenticationKey> change_del;
38 private static DataTreeModification<AuthenticationKey> change_subtreeModified;
39 private static DataTreeModification<AuthenticationKey> change_write;
40 private static DataObjectModification<AuthenticationKey> mod_del;
41 private static DataObjectModification<AuthenticationKey> mod_subtreeModified;
42 private static DataObjectModification<AuthenticationKey> mod_write;
44 private static final String IPV4_STRING_1 = "192.168.0.1";
45 private static final String IPV4_STRING_2 = "192.168.0.2";
46 private static final String IPV4_STRING_3 = "192.168.0.3";
47 private static final Eid IPV4_EID_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
48 private static final Eid IPV4_EID_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
49 private static final Eid IPV4_EID_3 = LispAddressUtil.asIpv4Eid(IPV4_STRING_3);
50 private static final AuthenticationKey AUTHENTICATION_KEY_1 = getAuthenticationKey(IPV4_EID_1, "pass1");
51 private static final AuthenticationKey AUTHENTICATION_KEY_2 = getAuthenticationKey(IPV4_EID_2, "pass2");
52 private static final AuthenticationKey AUTHENTICATION_KEY_3 = getAuthenticationKey(IPV4_EID_3, "pass3");
55 @SuppressWarnings("unchecked")
57 DataBroker dataBrokerMock = Mockito.mock(DataBroker.class);
58 iMappingSystemMock = Mockito.mock(IMappingSystem.class);
59 authenticationKeyDataListener = new AuthenticationKeyDataListener(dataBrokerMock, iMappingSystemMock);
61 final InstanceIdentifier<AuthenticationKey> instanceIdentifierMock = Mockito.mock(InstanceIdentifier.class);
62 final DataTreeIdentifier<AuthenticationKey> dataTreeIdentifier =
63 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, instanceIdentifierMock);
65 change_del = Mockito.mock(DataTreeModification.class);
66 change_subtreeModified = Mockito.mock(DataTreeModification.class);
67 change_write = Mockito.mock(DataTreeModification.class);
68 mod_del = Mockito.mock(DataObjectModification.class);
69 mod_subtreeModified = Mockito.mock(DataObjectModification.class);
70 mod_write = Mockito.mock(DataObjectModification.class);
72 Mockito.when(change_del.getRootPath()).thenReturn(dataTreeIdentifier);
73 Mockito.when(change_del.getRootNode()).thenReturn(mod_del);
74 Mockito.when(change_subtreeModified.getRootPath()).thenReturn(dataTreeIdentifier);
75 Mockito.when(change_subtreeModified.getRootNode()).thenReturn(mod_subtreeModified);
76 Mockito.when(change_write.getRootPath()).thenReturn(dataTreeIdentifier);
77 Mockito.when(change_write.getRootNode()).thenReturn(mod_write);
78 Mockito.when(mod_del.getModificationType()).thenReturn(ModificationType.DELETE);
79 Mockito.when(mod_subtreeModified.getModificationType()).thenReturn(ModificationType.SUBTREE_MODIFIED);
80 Mockito.when(mod_write.getModificationType()).thenReturn(ModificationType.WRITE);
84 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE modification type.
87 public void onDataTreeChangedTest_delete() {
88 final List<DataTreeModification<AuthenticationKey>> changes = Lists.newArrayList(change_del);
89 Mockito.when(mod_del.getDataBefore()).thenReturn(AUTHENTICATION_KEY_1);
91 authenticationKeyDataListener.onDataTreeChanged(changes);
92 Mockito.verify(iMappingSystemMock).removeAuthenticationKey(IPV4_EID_1);
96 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with WRITE modification type.
99 public void onDataTreeChangedTest_write() {
100 final List<DataTreeModification<AuthenticationKey>> changes = Lists.newArrayList(change_write);
101 Mockito.when(mod_write.getDataAfter()).thenReturn(AUTHENTICATION_KEY_2);
103 authenticationKeyDataListener.onDataTreeChanged(changes);
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 final List<DataTreeModification<AuthenticationKey>> changes = Lists.newArrayList(change_subtreeModified);
113 Mockito.when(mod_subtreeModified.getDataAfter()).thenReturn(AUTHENTICATION_KEY_3);
115 authenticationKeyDataListener.onDataTreeChanged(changes);
116 Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_3, AUTHENTICATION_KEY_3.getMappingAuthkey());
120 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with multiple modification types.
123 public void onDataTreeChangedTest_multipleModTypes() {
124 final List<DataTreeModification<AuthenticationKey>> changes =
125 Lists.newArrayList(change_del, change_write, change_subtreeModified);
127 Mockito.when(mod_del.getDataBefore()).thenReturn(AUTHENTICATION_KEY_1);
128 Mockito.when(mod_write.getDataAfter()).thenReturn(AUTHENTICATION_KEY_2);
129 Mockito.when(mod_subtreeModified.getDataAfter()).thenReturn(AUTHENTICATION_KEY_3);
131 authenticationKeyDataListener.onDataTreeChanged(changes);
132 Mockito.verify(iMappingSystemMock).removeAuthenticationKey(IPV4_EID_1);
133 Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_2, AUTHENTICATION_KEY_2.getMappingAuthkey());
134 Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_3, AUTHENTICATION_KEY_3.getMappingAuthkey());
138 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with no modification type.
141 @SuppressWarnings("unchecked")
142 public void onDataTreeChangedTest_noModType() {
143 final DataTreeModification<AuthenticationKey> changeNoModType = Mockito.mock(DataTreeModification.class);
144 final DataObjectModification<AuthenticationKey> modNoType = Mockito.mock(DataObjectModification.class);
145 final List<DataTreeModification<AuthenticationKey>> changes = Lists.newArrayList(changeNoModType);
147 Mockito.when(changeNoModType.getRootNode()).thenReturn(modNoType);
148 Mockito.when(modNoType.getModificationType()).thenReturn(null);
150 authenticationKeyDataListener.onDataTreeChanged(changes);
151 Mockito.verifyZeroInteractions(iMappingSystemMock);
154 private static AuthenticationKey getAuthenticationKey(Eid eid, String password) {
155 return new AuthenticationKeyBuilder()
156 .withKey(new AuthenticationKeyKey(new EidUri("uri-1")))
158 .setMappingAuthkey(new MappingAuthkeyBuilder()
159 .setKeyString(password)
160 .setKeyType(Uint16.valueOf(1)).build())