de55207a06812e8bd6946595c88a404abedaa153
[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.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;
31
32 public class AuthenticationKeyDataListenerTest {
33
34     private static IMappingSystem iMappingSystemMock;
35     private static AuthenticationKeyDataListener authenticationKeyDataListener;
36
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;
43
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");
53
54     @Before
55     @SuppressWarnings("unchecked")
56     public void init() {
57         DataBroker dataBrokerMock = Mockito.mock(DataBroker.class);
58         iMappingSystemMock = Mockito.mock(IMappingSystem.class);
59         authenticationKeyDataListener = new AuthenticationKeyDataListener(dataBrokerMock, iMappingSystemMock);
60
61         final InstanceIdentifier<AuthenticationKey> instanceIdentifierMock = Mockito.mock(InstanceIdentifier.class);
62         final DataTreeIdentifier<AuthenticationKey> dataTreeIdentifier =
63                 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, instanceIdentifierMock);
64
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);
71
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);
81     }
82
83     /**
84      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with DELETE modification type.
85      */
86     @Test
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);
90
91         authenticationKeyDataListener.onDataTreeChanged(changes);
92         Mockito.verify(iMappingSystemMock).removeAuthenticationKey(IPV4_EID_1);
93     }
94
95     /**
96      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with WRITE modification type.
97      */
98     @Test
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);
102
103         authenticationKeyDataListener.onDataTreeChanged(changes);
104         Mockito.verify(iMappingSystemMock).addAuthenticationKey(IPV4_EID_2, AUTHENTICATION_KEY_2.getMappingAuthkey());
105     }
106
107     /**
108      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with SUBTREE_MODIFIED modification type.
109      */
110     @Test
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     public void onDataTreeChangedTest_multipleModTypes() {
124         final List<DataTreeModification<AuthenticationKey>> changes =
125                 Lists.newArrayList(change_del, change_write, change_subtreeModified);
126
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);
130
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());
135     }
136
137     /**
138      * Tests {@link AuthenticationKeyDataListener#onDataTreeChanged} method with no modification type.
139      */
140     @Test
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);
146
147         Mockito.when(changeNoModType.getRootNode()).thenReturn(modNoType);
148         Mockito.when(modNoType.getModificationType()).thenReturn(null);
149
150         authenticationKeyDataListener.onDataTreeChanged(changes);
151         Mockito.verifyZeroInteractions(iMappingSystemMock);
152     }
153
154     private static AuthenticationKey getAuthenticationKey(Eid eid, String password) {
155         return new AuthenticationKeyBuilder()
156                 .withKey(new AuthenticationKeyKey(new EidUri("uri-1")))
157                 .setEid(eid)
158                 .setMappingAuthkey(new MappingAuthkeyBuilder()
159                         .setKeyString(password)
160                         .setKeyType(Uint16.valueOf(1)).build())
161                 .build();
162     }
163 }