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 static org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
12 import com.google.common.collect.Lists;
13 import java.util.List;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.mockito.Mockito;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
23 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container
28 .MappingAuthkeyBuilder;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 public class AuthenticationKeyDataListenerTest {
33 private static IMappingSystem iMappingSystemMock;
34 private static AuthenticationKeyDataListener authenticationKeyDataListener;
36 private static DataTreeModification<AuthenticationKey> change_del;
37 private static DataTreeModification<AuthenticationKey> change_subtreeModified;
38 private static DataTreeModification<AuthenticationKey> change_write;
39 private static DataObjectModification<AuthenticationKey> mod_del;
40 private static DataObjectModification<AuthenticationKey> mod_subtreeModified;
41 private static DataObjectModification<AuthenticationKey> mod_write;
43 private static final String IPV4_STRING_1 = "192.168.0.1";
44 private static final String IPV4_STRING_2 = "192.168.0.2";
45 private static final String IPV4_STRING_3 = "192.168.0.3";
46 private static final Eid IPV4_EID_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
47 private static final Eid IPV4_EID_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
48 private static final Eid IPV4_EID_3 = LispAddressUtil.asIpv4Eid(IPV4_STRING_3);
49 private static final AuthenticationKey AUTHENTICATION_KEY_1 = getAuthenticationKey(IPV4_EID_1, "pass1");
50 private static final AuthenticationKey AUTHENTICATION_KEY_2 = getAuthenticationKey(IPV4_EID_2, "pass2");
51 private static final AuthenticationKey AUTHENTICATION_KEY_3 = getAuthenticationKey(IPV4_EID_3, "pass3");
54 @SuppressWarnings("unchecked")
56 DataBroker dataBrokerMock = Mockito.mock(DataBroker.class);
57 iMappingSystemMock = Mockito.mock(IMappingSystem.class);
58 authenticationKeyDataListener = new AuthenticationKeyDataListener(dataBrokerMock, iMappingSystemMock);
60 final InstanceIdentifier<AuthenticationKey> instanceIdentifierMock = Mockito.mock(InstanceIdentifier.class);
61 final DataTreeIdentifier<AuthenticationKey> dataTreeIdentifier =
62 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceIdentifierMock);
64 change_del = Mockito.mock(DataTreeModification.class);
65 change_subtreeModified = Mockito.mock(DataTreeModification.class);
66 change_write = Mockito.mock(DataTreeModification.class);
67 mod_del = Mockito.mock(DataObjectModification.class);
68 mod_subtreeModified = Mockito.mock(DataObjectModification.class);
69 mod_write = Mockito.mock(DataObjectModification.class);
71 Mockito.when(change_del.getRootPath()).thenReturn(dataTreeIdentifier);
72 Mockito.when(change_del.getRootNode()).thenReturn(mod_del);
73 Mockito.when(change_subtreeModified.getRootPath()).thenReturn(dataTreeIdentifier);
74 Mockito.when(change_subtreeModified.getRootNode()).thenReturn(mod_subtreeModified);
75 Mockito.when(change_write.getRootPath()).thenReturn(dataTreeIdentifier);
76 Mockito.when(change_write.getRootNode()).thenReturn(mod_write);
77 Mockito.when(mod_del.getModificationType()).thenReturn(ModificationType.DELETE);
78 Mockito.when(mod_subtreeModified.getModificationType()).thenReturn(ModificationType.SUBTREE_MODIFIED);
79 Mockito.when(mod_write.getModificationType()).thenReturn(ModificationType.WRITE);
83 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE modification type.
86 @SuppressWarnings("unchecked")
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 @SuppressWarnings("unchecked")
100 public void onDataTreeChangedTest_write() {
101 final List<DataTreeModification<AuthenticationKey>> changes = Lists.newArrayList(change_write);
102 Mockito.when(mod_write.getDataAfter()).thenReturn(AUTHENTICATION_KEY_2);
104 authenticationKeyDataListener.onDataTreeChanged(changes);
105 Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_2, AUTHENTICATION_KEY_2.getMappingAuthkey());
109 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with SUBTREE_MODIFIED modification type.
112 @SuppressWarnings("unchecked")
113 public void onDataTreeChangedTest_subtreeModified() {
114 final List<DataTreeModification<AuthenticationKey>> changes = Lists.newArrayList(change_subtreeModified);
115 Mockito.when(mod_subtreeModified.getDataAfter()).thenReturn(AUTHENTICATION_KEY_3);
117 authenticationKeyDataListener.onDataTreeChanged(changes);
118 Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_3, AUTHENTICATION_KEY_3.getMappingAuthkey());
122 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with multiple modification types.
125 @SuppressWarnings("unchecked")
126 public void onDataTreeChangedTest_multipleModTypes() {
127 final List<DataTreeModification<AuthenticationKey>> changes =
128 Lists.newArrayList(change_del, change_write, change_subtreeModified);
130 Mockito.when(mod_del.getDataBefore()).thenReturn(AUTHENTICATION_KEY_1);
131 Mockito.when(mod_write.getDataAfter()).thenReturn(AUTHENTICATION_KEY_2);
132 Mockito.when(mod_subtreeModified.getDataAfter()).thenReturn(AUTHENTICATION_KEY_3);
134 authenticationKeyDataListener.onDataTreeChanged(changes);
135 Mockito.verify(iMappingSystemMock).removeAuthenticationKey(IPV4_EID_1);
136 Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_2, AUTHENTICATION_KEY_2.getMappingAuthkey());
137 Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_3, AUTHENTICATION_KEY_3.getMappingAuthkey());
141 * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with no modification type.
144 @SuppressWarnings("unchecked")
145 public void onDataTreeChangedTest_noModType() {
146 final DataTreeModification<AuthenticationKey> changeNoModType = Mockito.mock(DataTreeModification.class);
147 final DataObjectModification<AuthenticationKey> modNoType = Mockito.mock(DataObjectModification.class);
148 final List<DataTreeModification<AuthenticationKey>> changes = Lists.newArrayList(changeNoModType);
150 Mockito.when(changeNoModType.getRootNode()).thenReturn(modNoType);
151 Mockito.when(modNoType.getModificationType()).thenReturn(null);
153 authenticationKeyDataListener.onDataTreeChanged(changes);
154 Mockito.verifyZeroInteractions(iMappingSystemMock);
157 private static AuthenticationKey getAuthenticationKey(Eid eid, String password) {
158 return new AuthenticationKeyBuilder()
160 .setMappingAuthkey(new MappingAuthkeyBuilder()
161 .setKeyString(password)
162 .setKeyType(1).build())