Unit test cases for DeadLockDetectionUtil, CipherUtil and CassaLockStore
[music.git] / music-core / src / test / java / org / onap / music / lockingservice / cassandra / CassaLockStoreTest.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2019 AT&T Intellectual Property
6  * ===================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.lockingservice.cassandra;
24
25 import static org.junit.Assert.assertEquals;
26
27 import java.util.Iterator;
28 import java.util.List;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 import org.onap.music.datastore.MusicDataStore;
34 import org.onap.music.exceptions.MusicLockingException;
35 import org.onap.music.exceptions.MusicQueryException;
36 import org.onap.music.exceptions.MusicServiceException;
37 import org.onap.music.main.DeadlockDetectionUtil;
38
39 import com.datastax.driver.core.ResultSet;
40 import com.datastax.driver.core.Row;
41
42 public class CassaLockStoreTest {
43
44         private CassaLockStore cassaLockStore;
45         private CassaLockStore.LockObject lockObject;
46         private MusicDataStore dsHandle;
47
48         @Before
49         public void setUp() {
50                 dsHandle = Mockito.mock(MusicDataStore.class);
51                 cassaLockStore = new CassaLockStore(dsHandle);
52                 lockObject = cassaLockStore.new LockObject(false, null, null, null, null, null);
53         }
54
55         @Test
56         public void testLockOwner() {
57                 lockObject.setIsLockOwner(true);
58                 assertEquals(true, lockObject.getIsLockOwner());
59
60                 lockObject.setIsLockOwner(false);
61                 assertEquals(false, lockObject.getIsLockOwner());
62         }
63
64         @Test
65         public void testAcquireTime() {
66                 lockObject.setAcquireTime("2019-11-11T15:42:12+00:00");
67                 assertEquals("2019-11-11T15:42:12+00:00", lockObject.getAcquireTime());
68         }
69
70         @Test
71         public void testCreateTime() {
72                 lockObject.setCreateTime("2019-11-11T15:43:44+00:00");
73                 assertEquals("2019-11-11T15:43:44+00:00", lockObject.getCreateTime());
74         }
75
76         @Test
77         public void testLockRef() {
78                 lockObject.setLockRef("LockReference");
79                 assertEquals("LockReference", lockObject.getLockRef());
80         }
81
82         @Test
83         public void testLockType() {
84                 lockObject.setLocktype(LockType.READ);
85                 assertEquals(LockType.READ, lockObject.getLocktype());
86         }
87
88         @Test
89         public void testOwner() {
90                 lockObject.setOwner("Owner");
91                 assertEquals("Owner", lockObject.getOwner());
92         }
93
94         @Test
95         public void testCreateLockQueue() {
96                 try {
97                         Mockito.when(dsHandle.executePut(Mockito.any(), Mockito.any())).thenReturn(true);
98                         assertEquals(true, cassaLockStore.createLockQueue("keyspace1", "table1"));
99                 } catch (MusicServiceException | MusicQueryException e) {
100                         // TODO Auto-generated catch block
101                         e.printStackTrace();
102                 }
103         }
104
105         @Test
106         public void testGenLockRefandEnQueue() {
107                 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
108                 List<Row> latestGuardRow = Mockito.mock(List.class);
109                 Mockito.when(latestGuardRow.isEmpty()).thenReturn(false);
110                 Row row = Mockito.mock(Row.class);
111                 Mockito.when(latestGuardRow.get(0)).thenReturn(row);
112                 Mockito.when(row.getLong(0)).thenReturn((long) 4);
113                 Mockito.when(resultSetMock.all()).thenReturn(latestGuardRow);
114                 try {
115                         Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
116                         Mockito.when(dsHandle.executePut(Mockito.any(), Mockito.any())).thenReturn(true);
117                         assertEquals("$keyspace2.table2.lockName2$5",
118                                         cassaLockStore.genLockRefandEnQueue("keyspace2", "table2", "lockName2", LockType.READ, "owner2"));
119                 } catch (MusicServiceException | MusicQueryException | MusicLockingException e) {
120                         // TODO Auto-generated catch block
121                         e.printStackTrace();
122                 }
123         }
124
125         @Test
126         public void testGetLockQueue() {
127                 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
128                 Iterator<Row> iterator = Mockito.mock(Iterator.class);
129                 Mockito.when(iterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
130                 Row row = Mockito.mock(Row.class);
131                 Mockito.when(row.getLong("lockReference")).thenReturn((long)1).thenReturn((long)2).thenReturn((long)3);
132                 Mockito.when(row.get("lockType", LockType.class)).thenReturn(LockType.WRITE).thenReturn(LockType.WRITE).thenReturn(LockType.WRITE);
133                 Mockito.when(iterator.next()).thenReturn(row).thenReturn(row).thenReturn(row);
134                 Mockito.when(resultSetMock.iterator()).thenReturn(iterator);
135                 
136                 try {
137                         Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
138                         assertEquals("2", cassaLockStore.getLockQueue("keyspace2", "table2", "key2").get(1));
139                 } catch (MusicServiceException | MusicQueryException e) {
140                         // TODO Auto-generated catch block
141                         e.printStackTrace();
142                 }
143         }
144
145         @Test
146         public void testGetLockQueueSize() {
147                 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
148                 Row row = Mockito.mock(Row.class);
149                 Mockito.when(resultSetMock.one()).thenReturn(row);
150                 Mockito.when(row.getLong("count")).thenReturn((long) 6);
151                 try {
152                         Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
153                         assertEquals(6, cassaLockStore.getLockQueueSize("keyspace3", "table3", "key3"));
154                 } catch (MusicServiceException | MusicQueryException e) {
155                         // TODO Auto-generated catch block
156                         e.printStackTrace();
157                 }
158         }
159
160         @Test
161         public void testPeekLockQueue() {
162                 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
163                 Row row = Mockito.mock(Row.class);
164                 Mockito.when(row.isNull("lockReference")).thenReturn(false);
165                 Mockito.when(row.getLong("lockReference")).thenReturn((long) 6);
166                 Mockito.when(row.getString("createTime")).thenReturn("2019-11-13T15:05:45+00:00");
167                 Mockito.when(row.getString("acquireTime")).thenReturn("2019-11-13T15:05:45+00:00");
168                 Mockito.when(row.isNull("lockReference")).thenReturn(false);
169                 Mockito.when(resultSetMock.one()).thenReturn(row);
170                 try {
171                         Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
172                         assertEquals("6", cassaLockStore.peekLockQueue("keyspace4", "table4", "key4").getLockRef());
173                 } catch (MusicServiceException | MusicQueryException e) {
174                         // TODO Auto-generated catch block
175                         e.printStackTrace();
176                 }
177         }
178
179         @Test
180         public void testGetCurrentLockHolders() {
181                 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
182                 Iterator<Row> iterator = Mockito.mock(Iterator.class);
183                 Mockito.when(iterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
184                 Row row = Mockito.mock(Row.class);
185                 Mockito.when(row.getLong("lockReference")).thenReturn((long) 5).thenReturn((long) 5);
186                 Mockito.when(row.get("lockType", LockType.class)).thenReturn(LockType.WRITE);
187                 Mockito.when(iterator.next()).thenReturn(row).thenReturn(row);
188                 Mockito.when(resultSetMock.iterator()).thenReturn(iterator);
189                 try {
190                         Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
191                         assertEquals("$keyspace5.table5.key5$5", cassaLockStore.getCurrentLockHolders("keyspace5", "table5", "key5").get(1));
192                 } catch (MusicServiceException | MusicQueryException e) {
193                         // TODO Auto-generated catch block
194                         e.printStackTrace();
195                 }
196         }
197
198         @Test
199         public void testIsLockOwner() {
200                 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
201                 Iterator<Row> iterator = Mockito.mock(Iterator.class);
202                 Mockito.when(iterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
203                 Row row = Mockito.mock(Row.class);
204                 Mockito.when(row.getLong("lockReference")).thenReturn((long) 5);
205                 Mockito.when(row.get("lockType", LockType.class)).thenReturn(LockType.WRITE);
206                 Mockito.when(iterator.next()).thenReturn(row).thenReturn(row);
207                 Mockito.when(resultSetMock.iterator()).thenReturn(iterator);
208                 try {
209                         Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
210                         assertEquals(true, cassaLockStore.isLockOwner("keyspace5", "table5", "key5", "5"));
211                 } catch (MusicServiceException | MusicQueryException e) {
212                         // TODO Auto-generated catch block
213                         e.printStackTrace();
214                 }
215         }
216
217         @Test
218         public void testGetLockInfo() {
219                 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
220                 Row row = Mockito.mock(Row.class);
221                 Mockito.when(row.isNull("lockReference")).thenReturn(false);
222                 Mockito.when(row.getLong("lockReference")).thenReturn((long) 6);
223                 Mockito.when(row.getString("createTime")).thenReturn("2019-11-13T15:05:45+00:00");
224                 Mockito.when(row.getString("acquireTime")).thenReturn("2019-11-13T15:05:45+00:00");
225                 LockType locktype = Mockito.mock(LockType.class);
226                 Mockito.when(row.get("lockType", LockType.class)).thenReturn(locktype);
227                 Mockito.when(row.getString("owner")).thenReturn("owner6");
228                 Mockito.when(resultSetMock.one()).thenReturn(row);
229
230                 try {
231                         Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
232                         CassaLockStore csLockStore = Mockito.spy(cassaLockStore);
233                         Mockito.doReturn(true).when(csLockStore).isLockOwner(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
234                         assertEquals("6", csLockStore.getLockInfo("keyspace6", "table6", "key6", "6").getLockRef());
235                 } catch (MusicServiceException | MusicQueryException e) {
236                         // TODO Auto-generated catch block
237                         e.printStackTrace();
238                 }
239         }
240
241         @Test
242         public void testCheckForDeadlock() {
243                 DeadlockDetectionUtil ddu = Mockito.mock(DeadlockDetectionUtil.class);
244                 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
245                 Iterator<Row> it = Mockito.mock(Iterator.class);
246                 Row row = Mockito.mock(Row.class);
247                 Mockito.when(it.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
248                 Mockito.when(row.getString("key")).thenReturn("key8");
249                 Mockito.when(row.getString("owner")).thenReturn("owner8");
250                 Mockito.when(row.getString("acquiretime")).thenReturn("1");
251                 Mockito.when(it.next()).thenReturn(row).thenReturn(row).thenReturn(row);
252                 Mockito.when(resultSetMock.iterator()).thenReturn(it);
253                 CassaLockStore csLockStore = Mockito.spy(cassaLockStore);
254                 Mockito.doReturn(ddu).when(csLockStore).getDeadlockDetectionUtil();
255                 Mockito.when(ddu.checkForDeadlock(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(true);
256                 try {
257                         Mockito.when(dsHandle.executeLocalQuorumConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
258                         assertEquals(false,
259                                         cassaLockStore.checkForDeadlock("keyspace8", "table8", "lockName8", LockType.WRITE, "owner8", true));
260                 } catch (MusicServiceException | MusicQueryException e) {
261                         // TODO Auto-generated catch block
262                         e.printStackTrace();
263                 }
264         }
265
266         @Test
267         public void testGetAllLocksForOwner() {
268                 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
269                 Iterator<Row> it = Mockito.mock(Iterator.class);
270                 Mockito.when(it.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
271                 Row row = Mockito.mock(Row.class);
272                 Mockito.when(row.getString("key")).thenReturn("key10");
273                 Mockito.when(row.getLong("lockreference")).thenReturn((long) 10);
274                 Mockito.when(it.next()).thenReturn(row);
275                 Mockito.when(resultSetMock.iterator()).thenReturn(it);
276                 try {
277                         Mockito.when(dsHandle.executeQuorumConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
278                         assertEquals("key10$10", cassaLockStore.getAllLocksForOwner("owneer10", "keyspace10", "table10").get(1));
279                 } catch (MusicServiceException | MusicQueryException e) {
280                         // TODO Auto-generated catch block
281                         e.printStackTrace();
282                 }
283         }
284
285 }