Merge "Cleanup: remove unused and commented out methods"
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / mdsal / AuthenticationKeyDataListenerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.lispflowmapping.implementation.mdsal;
9
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.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
17 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
22 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
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.db.instance.AuthenticationKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyBuilder;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28
29 public class AuthenticationKeyDataListenerTest {
30
31     private static IMappingSystem iMappingSystemMock;
32     private static AuthenticationKeyDataListener authenticationKeyDataListener;
33
34     private static DataTreeModification<AuthenticationKey> change_del;
35     private static DataTreeModification<AuthenticationKey> change_subtreeModified;
36     private static DataTreeModification<AuthenticationKey> change_write;
37     private static DataObjectModification<AuthenticationKey> mod_del;
38     private static DataObjectModification<AuthenticationKey> mod_subtreeModified;
39     private static DataObjectModification<AuthenticationKey> mod_write;
40
41     private static final String IPV4_STRING_1 = "192.168.0.1";
42     private static final String IPV4_STRING_2 = "192.168.0.2";
43     private static final String IPV4_STRING_3 = "192.168.0.3";
44     private static final Eid IPV4_EID_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
45     private static final Eid IPV4_EID_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
46     private static final Eid IPV4_EID_3 = LispAddressUtil.asIpv4Eid(IPV4_STRING_3);
47     private static final AuthenticationKey AUTHENTICATION_KEY_1 = getAuthenticationKey(IPV4_EID_1, "pass1");
48     private static final AuthenticationKey AUTHENTICATION_KEY_2 = getAuthenticationKey(IPV4_EID_2, "pass2");
49     private static final AuthenticationKey AUTHENTICATION_KEY_3 = getAuthenticationKey(IPV4_EID_3, "pass3");
50
51     @Before
52     @SuppressWarnings("unchecked")
53     public void init() {
54         DataBroker dataBrokerMock = Mockito.mock(DataBroker.class);
55         iMappingSystemMock = Mockito.mock(IMappingSystem.class);
56         authenticationKeyDataListener = new AuthenticationKeyDataListener(dataBrokerMock, iMappingSystemMock);
57
58         final InstanceIdentifier<AuthenticationKey> instanceIdentifierMock = Mockito.mock(InstanceIdentifier.class);
59         final DataTreeIdentifier<AuthenticationKey> dataTreeIdentifier =
60                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceIdentifierMock);
61
62         change_del = Mockito.mock(DataTreeModification.class);
63         change_subtreeModified = Mockito.mock(DataTreeModification.class);
64         change_write = Mockito.mock(DataTreeModification.class);
65         mod_del = Mockito.mock(DataObjectModification.class);
66         mod_subtreeModified = Mockito.mock(DataObjectModification.class);
67         mod_write = Mockito.mock(DataObjectModification.class);
68
69         Mockito.when(change_del.getRootPath()).thenReturn(dataTreeIdentifier);
70         Mockito.when(change_del.getRootNode()).thenReturn(mod_del);
71         Mockito.when(change_subtreeModified.getRootPath()).thenReturn(dataTreeIdentifier);
72         Mockito.when(change_subtreeModified.getRootNode()).thenReturn(mod_subtreeModified);
73         Mockito.when(change_write.getRootPath()).thenReturn(dataTreeIdentifier);
74         Mockito.when(change_write.getRootNode()).thenReturn(mod_write);
75         Mockito.when(mod_del.getModificationType()).thenReturn(ModificationType.DELETE);
76         Mockito.when(mod_subtreeModified.getModificationType()).thenReturn(ModificationType.SUBTREE_MODIFIED);
77         Mockito.when(mod_write.getModificationType()).thenReturn(ModificationType.WRITE);
78     }
79
80     /**
81      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE modification type.
82      */
83     @Test
84     @SuppressWarnings("unchecked")
85     public void onDataTreeChangedTest_delete() {
86         final List<DataTreeModification<AuthenticationKey>> changes = Lists.newArrayList(change_del);
87         Mockito.when(mod_del.getDataBefore()).thenReturn(AUTHENTICATION_KEY_1);
88
89         authenticationKeyDataListener.onDataTreeChanged(changes);
90         Mockito.verify(iMappingSystemMock).removeAuthenticationKey(IPV4_EID_1);
91     }
92
93     /**
94      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with WRITE modification type.
95      */
96     @Test
97     @SuppressWarnings("unchecked")
98     public void onDataTreeChangedTest_write() {
99         final List<DataTreeModification<AuthenticationKey>> changes = Lists.newArrayList(change_write);
100         Mockito.when(mod_write.getDataAfter()).thenReturn(AUTHENTICATION_KEY_2);
101
102         authenticationKeyDataListener.onDataTreeChanged(changes);
103         Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_2, AUTHENTICATION_KEY_2.getMappingAuthkey());
104     }
105
106     /**
107      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with SUBTREE_MODIFIED modification type.
108      */
109     @Test
110     @SuppressWarnings("unchecked")
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);
114
115         authenticationKeyDataListener.onDataTreeChanged(changes);
116         Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_3, AUTHENTICATION_KEY_3.getMappingAuthkey());
117     }
118
119     /**
120      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with multiple modification types.
121      */
122     @Test
123     @SuppressWarnings("unchecked")
124     public void onDataTreeChangedTest_multipleModTypes() {
125         final List<DataTreeModification<AuthenticationKey>> changes =
126                 Lists.newArrayList(change_del, change_write, change_subtreeModified);
127
128         Mockito.when(mod_del.getDataBefore()).thenReturn(AUTHENTICATION_KEY_1);
129         Mockito.when(mod_write.getDataAfter()).thenReturn(AUTHENTICATION_KEY_2);
130         Mockito.when(mod_subtreeModified.getDataAfter()).thenReturn(AUTHENTICATION_KEY_3);
131
132         authenticationKeyDataListener.onDataTreeChanged(changes);
133         Mockito.verify(iMappingSystemMock).removeAuthenticationKey(IPV4_EID_1);
134         Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_2, AUTHENTICATION_KEY_2.getMappingAuthkey());
135         Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_3, AUTHENTICATION_KEY_3.getMappingAuthkey());
136     }
137
138     /**
139      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with no modification type.
140      */
141     @Test
142     @SuppressWarnings("unchecked")
143     public void onDataTreeChangedTest_noModType() {
144         final DataTreeModification<AuthenticationKey> changeNoModType = Mockito.mock(DataTreeModification.class);
145         final DataObjectModification<AuthenticationKey> modNoType = Mockito.mock(DataObjectModification.class);
146         final List<DataTreeModification<AuthenticationKey>> changes = Lists.newArrayList(changeNoModType);
147
148         Mockito.when(changeNoModType.getRootNode()).thenReturn(modNoType);
149         Mockito.when(modNoType.getModificationType()).thenReturn(null);
150
151         authenticationKeyDataListener.onDataTreeChanged(changes);
152         Mockito.verifyZeroInteractions(iMappingSystemMock);
153     }
154
155     private static AuthenticationKey getAuthenticationKey(Eid eid, String password) {
156         return new AuthenticationKeyBuilder()
157                 .setEid(eid)
158                 .setMappingAuthkey(new MappingAuthkeyBuilder()
159                         .setKeyString(password)
160                         .setKeyType(1).build())
161                 .build();
162     }
163 }