Migrate to MD-SAL APIs
[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.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.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                 DataTreeIdentifier.create(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     public void onDataTreeChangedTest_delete() {
85         final List<DataTreeModification<AuthenticationKey>> changes = Lists.newArrayList(change_del);
86         Mockito.when(mod_del.getDataBefore()).thenReturn(AUTHENTICATION_KEY_1);
87
88         authenticationKeyDataListener.onDataTreeChanged(changes);
89         Mockito.verify(iMappingSystemMock).removeAuthenticationKey(IPV4_EID_1);
90     }
91
92     /**
93      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with WRITE modification type.
94      */
95     @Test
96     public void onDataTreeChangedTest_write() {
97         final List<DataTreeModification<AuthenticationKey>> changes = Lists.newArrayList(change_write);
98         Mockito.when(mod_write.getDataAfter()).thenReturn(AUTHENTICATION_KEY_2);
99
100         authenticationKeyDataListener.onDataTreeChanged(changes);
101         Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_2, AUTHENTICATION_KEY_2.getMappingAuthkey());
102     }
103
104     /**
105      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with SUBTREE_MODIFIED modification type.
106      */
107     @Test
108     public void onDataTreeChangedTest_subtreeModified() {
109         final List<DataTreeModification<AuthenticationKey>> changes = Lists.newArrayList(change_subtreeModified);
110         Mockito.when(mod_subtreeModified.getDataAfter()).thenReturn(AUTHENTICATION_KEY_3);
111
112         authenticationKeyDataListener.onDataTreeChanged(changes);
113         Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_3, AUTHENTICATION_KEY_3.getMappingAuthkey());
114     }
115
116     /**
117      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with multiple modification types.
118      */
119     @Test
120     public void onDataTreeChangedTest_multipleModTypes() {
121         final List<DataTreeModification<AuthenticationKey>> changes =
122                 Lists.newArrayList(change_del, change_write, change_subtreeModified);
123
124         Mockito.when(mod_del.getDataBefore()).thenReturn(AUTHENTICATION_KEY_1);
125         Mockito.when(mod_write.getDataAfter()).thenReturn(AUTHENTICATION_KEY_2);
126         Mockito.when(mod_subtreeModified.getDataAfter()).thenReturn(AUTHENTICATION_KEY_3);
127
128         authenticationKeyDataListener.onDataTreeChanged(changes);
129         Mockito.verify(iMappingSystemMock).removeAuthenticationKey(IPV4_EID_1);
130         Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_2, AUTHENTICATION_KEY_2.getMappingAuthkey());
131         Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_3, AUTHENTICATION_KEY_3.getMappingAuthkey());
132     }
133
134     /**
135      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with no modification type.
136      */
137     @Test
138     @SuppressWarnings("unchecked")
139     public void onDataTreeChangedTest_noModType() {
140         final DataTreeModification<AuthenticationKey> changeNoModType = Mockito.mock(DataTreeModification.class);
141         final DataObjectModification<AuthenticationKey> modNoType = Mockito.mock(DataObjectModification.class);
142         final List<DataTreeModification<AuthenticationKey>> changes = Lists.newArrayList(changeNoModType);
143
144         Mockito.when(changeNoModType.getRootNode()).thenReturn(modNoType);
145         Mockito.when(modNoType.getModificationType()).thenReturn(null);
146
147         authenticationKeyDataListener.onDataTreeChanged(changes);
148         Mockito.verifyZeroInteractions(iMappingSystemMock);
149     }
150
151     private static AuthenticationKey getAuthenticationKey(Eid eid, String password) {
152         return new AuthenticationKeyBuilder()
153                 .setEid(eid)
154                 .setMappingAuthkey(new MappingAuthkeyBuilder()
155                         .setKeyString(password)
156                         .setKeyType(1).build())
157                 .build();
158     }
159 }