Merge "CadiAuthFilter-junits"
[music.git] / music-core / src / test / java / org / onap / music / service / impl / MusicCassaCoreTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2018 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 package org.onap.music.service.impl;
23
24 import static org.junit.Assert.*;
25 import java.util.ArrayList;
26 import java.util.List;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.onap.music.datastore.PreparedQueryObject;
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.lockingservice.cassandra.CassaLockStore;
38 import org.onap.music.lockingservice.cassandra.CassaLockStore.LockObject;
39 import org.onap.music.lockingservice.cassandra.LockType;
40 import org.onap.music.main.MusicUtil;
41 import org.onap.music.main.ResultType;
42 import org.onap.music.main.ReturnType;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class MusicCassaCoreTest {
46     
47     @Mock
48     private CassaLockStore mLockHandle;
49     
50     MusicCassaCore core;
51     
52     @Before
53     public void before() {
54         core = MusicCassaCore.getInstance();
55         MusicCassaCore.setmLockHandle(mLockHandle);
56     }
57     
58     @Test
59     public void testGetmLockHandle() {
60         assertEquals(mLockHandle, MusicCassaCore.getmLockHandle());
61     }
62
63     @Test
64     public void testSetmLockHandle() {
65         CassaLockStore m2 = Mockito.mock(CassaLockStore.class);
66         MusicCassaCore.setmLockHandle(m2);
67         assertEquals(m2, MusicCassaCore.getmLockHandle());
68         //revert back to original handle
69         MusicCassaCore.setmLockHandle(mLockHandle);
70     }
71
72     @Test
73     public void testGetInstance() {
74         assertEquals(core, MusicCassaCore.getInstance());
75     }
76
77     @Test
78     public void testGetLockingServiceHandle() {
79         assertEquals(mLockHandle, MusicCassaCore.getmLockHandle());
80     }
81
82     @Test
83     public void testCreateLockReferenceAtomicString() throws Exception {
84         String fullyQualifiedKey = "keyspace.table.lockName";
85         Mockito.when(mLockHandle.genLockRefandEnQueue("keyspace", "table", "lockName", LockType.WRITE, null))
86                 .thenReturn("lockReturned");
87
88         String lockRef = core.createLockReferenceAtomic(fullyQualifiedKey);
89
90         Mockito.verify(mLockHandle).genLockRefandEnQueue("keyspace", "table", "lockName", LockType.WRITE, null);
91         assertEquals("lockReturned", lockRef);
92     }
93
94     @Test
95     public void testCreateLockReferenceStringString() throws Exception {
96         String fullyQualifiedKey = "keyspace.table.lockName";
97         String owner = "owner1";
98         Mockito.when(mLockHandle.genLockRefandEnQueue("keyspace", "table", "lockName", LockType.WRITE, owner))
99             .thenReturn("lockReturned");
100         
101         String lockRef = core.createLockReference(fullyQualifiedKey, owner);
102
103         Mockito.verify(mLockHandle).genLockRefandEnQueue("keyspace", "table", "lockName", LockType.WRITE, owner);
104         assertEquals("lockReturned", lockRef);
105     }
106
107     @Test
108     public void testCreateLockReferenceAtomicStringLockType() throws Exception {
109         String fullyQualifiedKey = "keyspace.table.lockName";
110         Mockito.when(mLockHandle.genLockRefandEnQueue("keyspace", "table", "lockName", LockType.READ, null))
111                 .thenReturn("lockReturned");
112
113         String lockRef = core.createLockReferenceAtomic(fullyQualifiedKey, LockType.READ);
114
115         Mockito.verify(mLockHandle).genLockRefandEnQueue("keyspace", "table", "lockName", LockType.READ, null);
116         assertEquals("lockReturned", lockRef);
117     }
118
119     @Test
120     public void testCreateLockReferenceStringLockTypeString() throws Exception {
121         String fullyQualifiedKey = "keyspace.table.lockName";
122         String owner = "owner1";
123         Mockito.when(mLockHandle.genLockRefandEnQueue("keyspace", "table", "lockName", LockType.READ, owner))
124             .thenReturn("lockReturned");
125         
126         String lockRef = core.createLockReference(fullyQualifiedKey, LockType.READ, owner);
127
128         Mockito.verify(mLockHandle).genLockRefandEnQueue("keyspace", "table", "lockName", LockType.READ, owner);
129         assertEquals("lockReturned", lockRef);
130     }
131
132     @Test
133     public void testPromoteLock() throws Exception {
134         String lockId = "$keyspace.table.lockName$1";
135         Mockito.when(mLockHandle.promoteLock("keyspace", "table", "lockName", "1"))
136             .thenReturn(new ReturnType(ResultType.SUCCESS, "Lock Promoted"));
137         
138         ReturnType rt = core.promoteLock(lockId);
139         assertEquals(ResultType.SUCCESS, rt.getResult());
140         assertEquals("Lock Promoted", rt.getMessage());
141     }
142
143     @Test
144     public void testAcquireLockWithLease() throws Exception {
145         String fullyQualifiedKey = "keyspace.table.lockName";
146         String lockId = "$keyspace.table.lockName$1";
147         long leasePeriod = 1000;
148         String currTime = String.valueOf(System.currentTimeMillis());
149         Mockito.when(mLockHandle.peekLockQueue("keyspace", "table", "lockName"))
150             .thenReturn(mLockHandle.new LockObject(true, lockId, currTime, currTime, LockType.WRITE, null));
151         Mockito.when(mLockHandle.getLockInfo("keyspace", "table", "lockName", "1"))
152             .thenReturn(mLockHandle.new LockObject(false, lockId, null, null, LockType.WRITE, null));
153         
154         ReturnType rt = core.acquireLockWithLease(fullyQualifiedKey, lockId, leasePeriod);
155         assertEquals(ResultType.FAILURE, rt.getResult());
156     }
157
158     @Test
159     public void testAcquireLock() throws Exception {
160         String fullyQualifiedKey = "keyspace.table.lockName";
161         String lockId = "$keyspace.table.lockName$1";
162         Mockito.when(mLockHandle.getLockInfo("keyspace", "table", "lockName", "1"))
163             .thenReturn(mLockHandle.new LockObject(false, lockId, null, null, LockType.WRITE, null));
164         
165         ReturnType rt = core.acquireLock(fullyQualifiedKey, lockId);
166         
167         assertEquals(ResultType.FAILURE, rt.getResult());
168         Mockito.verify(mLockHandle).getLockInfo("keyspace", "table", "lockName", "1");
169         /*TODO: if we successfully acquire the lock we hit an error by trying to read MusicDatastoreHandle */
170     }
171
172     @Test
173     public void testWhoseTurnIsIt() throws Exception {
174         String fullyQualifiedKey = "keyspace.table.lockName";
175         Mockito.when(mLockHandle.peekLockQueue("keyspace", "table", "lockName"))
176             .thenReturn(mLockHandle.new LockObject(true, "1", "", "", LockType.WRITE, null));
177         
178         String topOfQ = core.whoseTurnIsIt(fullyQualifiedKey);
179         System.out.println(topOfQ);
180         
181         assertEquals("$"+fullyQualifiedKey+"$1", topOfQ);
182     }
183
184     @Test
185     public void testGetCurrentLockHolders() throws Exception {
186         String fullyQualifiedKey = "keyspace.table.lockName";
187         List<String> currentHolders = new ArrayList<>();
188         currentHolders.add("$"+fullyQualifiedKey+"$1");
189         currentHolders.add("$"+fullyQualifiedKey+"$2");
190         Mockito.when(mLockHandle.getCurrentLockHolders("keyspace", "table", "lockName"))
191             .thenReturn(currentHolders);
192
193         List<String> holders = core.getCurrentLockHolders(fullyQualifiedKey);
194         
195         assertTrue(currentHolders.containsAll(holders) && holders.containsAll(currentHolders));
196     }
197
198     @Test
199     public void testGetLockNameFromId() {
200         String lockId = "$keyspace.table.lockName$1";
201         assertEquals("keyspace.table.lockName", core.getLockNameFromId(lockId));
202     }
203
204     @Test
205     public void testDestroyLockRefString() throws Exception {
206         String lockId = "$keyspace.table.lockName$1";
207
208         core.destroyLockRef(lockId);
209         Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount());
210     }
211
212     @Test
213     public void testDestroyLockRefStringString() throws Exception {
214         String fullyQualifiedKey = "keyspace.table.lockName";
215         String lockReference = "1";
216         
217         core.destroyLockRef(fullyQualifiedKey, lockReference);
218         Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount());
219     }
220
221     @Test
222     public void testReleaseLock() throws Exception {
223         String lockId = "$keyspace.table.lockName$1";
224         
225         core.releaseLock(lockId, true);
226         
227         Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount());
228     }
229
230     @Test
231     public void testVoluntaryReleaseLock() throws Exception {
232         String fullyQualifiedKey = "keyspace.table.lockName";
233         String lockReference = "1";
234         
235         core.voluntaryReleaseLock(fullyQualifiedKey, lockReference);
236         
237         Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount());
238     }
239
240     @Test
241     public void testReleaseAllLocksForOwner() throws Exception {
242         List<String> ownersLocks = new ArrayList<>();
243         ownersLocks.add("lockName$1");
244         ownersLocks.add("lockName$2");
245         Mockito.when(mLockHandle.getAllLocksForOwner("ownerId", "keyspace", "table"))
246             .thenReturn(ownersLocks);
247         
248         List<String> locksReleased = core.releaseAllLocksForOwner("ownerId", "keyspace", "table");
249         
250         Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount());
251         Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "2", MusicUtil.getRetryCount());
252         assertTrue(ownersLocks.containsAll(locksReleased) && locksReleased.containsAll(ownersLocks));
253     }
254
255
256     @Test
257     public void testValidateLock() {
258         String lockId = "$keyspace.table.lockName$1";
259         
260         assertFalse(core.validateLock(lockId).containsKey("Error"));
261     }
262
263     @Test
264     public void testGetLockQueue() throws Exception {
265         String fullyQualifiedKey = "keyspace.table.lockName";
266         List<String> myList = new ArrayList<>();
267         Mockito.when(mLockHandle.getLockQueue("keyspace", "table", "lockName"))
268             .thenReturn(myList);
269         List<String> theirList = core.getLockQueue(fullyQualifiedKey);
270         
271         assertEquals(myList, theirList);
272     }
273
274     @Test
275     public void testGetLockQueueSize() throws Exception {
276         String fullyQualifiedKey = "keyspace.table.lockName";
277         Mockito.when(mLockHandle.getLockQueueSize("keyspace", "table", "lockName"))
278             .thenReturn((long) 23);
279         long theirSize = core.getLockQueueSize(fullyQualifiedKey);
280         
281         assertEquals(23, theirSize);
282     }
283
284 }