aboutsummaryrefslogtreecommitdiffstats
path: root/music-core/src/test/java/org/onap/music/main/MusicCoreTest.java
blob: 4714778b9834428c151f9df5dd52d25811513f5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*******************************************************************************
 * ============LICENSE_START========================================== org.onap.music
 * =================================================================== Copyright (c) 2019 AT&T
 * Intellectual Property ===================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 * 
 * ============LICENSE_END=============================================
 * ====================================================================
 *******************************************************************************/

package org.onap.music.main;

import static org.junit.Assert.assertEquals;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.MultivaluedMap;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.internal.util.reflection.FieldSetter;
import org.onap.music.datastore.Condition;
import org.onap.music.datastore.PreparedQueryObject;
import org.onap.music.datastore.jsonobjects.JsonDelete;
import org.onap.music.datastore.jsonobjects.JsonIndex;
import org.onap.music.datastore.jsonobjects.JsonInsert;
import org.onap.music.datastore.jsonobjects.JsonKeySpace;
import org.onap.music.datastore.jsonobjects.JsonSelect;
import org.onap.music.datastore.jsonobjects.JsonTable;
import org.onap.music.datastore.jsonobjects.JsonUpdate;
import org.onap.music.exceptions.MusicLockingException;
import org.onap.music.exceptions.MusicQueryException;
import org.onap.music.exceptions.MusicServiceException;
import org.onap.music.lockingservice.cassandra.CassaLockStore;
import org.onap.music.lockingservice.cassandra.LockType;
import org.onap.music.lockingservice.cassandra.MusicLockState;
import org.onap.music.service.MusicCoreService;
import com.datastax.driver.core.ResultSet;

public class MusicCoreTest {

    MusicCore mCore;
    MusicCoreService musicCore;
    CassaLockStore mLockHandle;

    @Before
    public void setup() {
        mCore = new MusicCore();
        musicCore = Mockito.mock(MusicCoreService.class);
        mLockHandle = Mockito.mock(CassaLockStore.class);
        try {
            FieldSetter.setField(mCore, mCore.getClass().getDeclaredField("musicCore"), musicCore);
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void testAcquireLock() throws MusicLockingException, MusicQueryException, MusicServiceException {
        ReturnType returnType = Mockito.mock(ReturnType.class);
        ReturnType result = null;
        Mockito.when(musicCore.acquireLock(Mockito.any(), Mockito.any())).thenReturn(returnType);
        result = MusicCore.acquireLock("key1", "lockid1");
        assertEquals(returnType, result);
    }

    @Test
    public void testacquireLockWithLease() throws MusicLockingException, MusicQueryException, MusicServiceException {
        ReturnType returnType = Mockito.mock(ReturnType.class);
        ReturnType result = null;
        Mockito.when(musicCore.acquireLockWithLease(Mockito.anyString(), Mockito.anyString(), Mockito.anyLong()))
                .thenReturn(returnType);
        result = MusicCore.acquireLockWithLease("key1", "lockid1", 100L);
        assertEquals(returnType, result);
    }

    @Test
    public void testCreateLockReferenceAtomic() throws MusicLockingException {
        String result = null;
        Mockito.when(musicCore.createLockReferenceAtomic(Mockito.any())).thenReturn("lockreference1");
        result = MusicCore.createLockReferenceAtomic("key2");
        assertEquals("lockreference1", result);
    }

    @Test
    public void testCreateLockReference() throws MusicLockingException {
        String result = null;
        Mockito.when(musicCore.createLockReference(Mockito.any(), Mockito.any())).thenReturn("lockreference2");
        result = MusicCore.createLockReference("key3", "owner3");
        assertEquals("lockreference2", result);
    }

    @Test
    public void testCreateLockReferenceAtomic2() throws MusicLockingException {
        String result = null;
        Mockito.when(musicCore.createLockReferenceAtomic(Mockito.any(), Mockito.any())).thenReturn("lockreference3");
        result = MusicCore.createLockReferenceAtomic("key4", LockType.READ);
        assertEquals("lockreference3", result);
    }

    @Test
    public void testCreateLockReference2() throws MusicLockingException {
        String result = null;
        Mockito.when(musicCore.createLockReference(Mockito.any(), Mockito.any(), Mockito.any()))
                .thenReturn("lockreference4");
        result = MusicCore.createLockReference("key4", LockType.READ, "owner4");
        assertEquals("lockreference4", result);
    }

    @Test
    public void testCreateTable() throws MusicServiceException {
        ResultType resultType = Mockito.mock(ResultType.class);
        ResultType result = null;
        Mockito.when(musicCore.createTable(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
                .thenReturn(resultType);
        result = MusicCore.createTable("keyspace1", "table1", new PreparedQueryObject(), "consistency");
        assertEquals(resultType, result);
    }

    @Test
    public void testQuorumGet() {
        ResultSet rs = Mockito.mock(ResultSet.class);
        Mockito.when(musicCore.quorumGet(Mockito.any())).thenReturn(rs);
        assertEquals(rs, MusicCore.quorumGet(new PreparedQueryObject()));
    }

    @Test
    public void testWhoseTurnIsIt() {
        Mockito.when(musicCore.whoseTurnIsIt(Mockito.any())).thenReturn("turn");
        assertEquals("turn", MusicCore.whoseTurnIsIt("key5"));
    }

    @Test
    public void testGetCurrentLockHolders() {
        List<String> result = Mockito.mock(List.class);
        Mockito.when(musicCore.getCurrentLockHolders(Mockito.any())).thenReturn(result);
        assertEquals(result, MusicCore.getCurrentLockHolders("key6"));
    }

    @Test
    public void testPromoteLock() throws MusicLockingException {
        ReturnType returnType = Mockito.mock(ReturnType.class);
        ReturnType result = null;
        Mockito.when(musicCore.promoteLock(Mockito.any())).thenReturn(returnType);
        result = MusicCore.promoteLock("lockid2");
        assertEquals(returnType, result);
    }

    @Test
    public void testEventualPut() {
        ReturnType returnType = Mockito.mock(ReturnType.class);
        Mockito.when(musicCore.eventualPut(Mockito.any())).thenReturn(returnType);
        assertEquals(returnType, MusicCore.eventualPut(new PreparedQueryObject()));
    }

    @Test
    public void testEventualPut_nb() {
        ReturnType returnType = Mockito.mock(ReturnType.class);
        Mockito.when(musicCore.eventualPut_nb(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
                .thenReturn(returnType);
        assertEquals(returnType,
                MusicCore.eventualPut_nb(new PreparedQueryObject(), "keyspace2", "table2", "primarykey1"));
    }

    @Test
    public void testCriticalPut() {
        ReturnType returnType = Mockito.mock(ReturnType.class);
        Mockito.when(musicCore.criticalPut(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(),
                Mockito.any())).thenReturn(returnType);
        assertEquals(returnType, MusicCore.criticalPut("keyspace3", "table3", "primarykey2", new PreparedQueryObject(),
                "lockreference2", new Condition(new HashMap(), new PreparedQueryObject())));
    }

    @Test
    public void testNonKeyRelatedPut() throws MusicServiceException, MusicQueryException {
        ResultType resultType = Mockito.mock(ResultType.class);
        ResultType result = null;
        Mockito.when(musicCore.nonKeyRelatedPut(Mockito.any(), Mockito.any())).thenReturn(resultType);
        result = MusicCore.nonKeyRelatedPut(new PreparedQueryObject(), "consistency2");
        assertEquals(resultType, result);
    }

    @Test
    public void testGet() throws MusicServiceException {
        ResultSet rs = Mockito.mock(ResultSet.class);
        ResultSet result = null;
        Mockito.when(musicCore.get(Mockito.any())).thenReturn(rs);
        result = MusicCore.get(new PreparedQueryObject());
        assertEquals(rs, result);
    }

    @Test
    public void testCriticalGet() throws MusicServiceException {
        ResultSet rs = Mockito.mock(ResultSet.class);
        ResultSet result = null;
        Mockito.when(musicCore.criticalGet(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
                .thenReturn(rs);
        result = MusicCore.criticalGet("keyspace4", "table4", "primarykey3", new PreparedQueryObject(),
                "lockreference3");
        assertEquals(rs, result);
    }

    @Test
    public void testAtomicPut() throws MusicLockingException, MusicQueryException, MusicServiceException {
        ReturnType returnType = Mockito.mock(ReturnType.class);
        ReturnType result = null;
        Mockito.when(musicCore.atomicPut(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
                .thenReturn(returnType);
        result = MusicCore.atomicPut("keyspace5", "table5", "primarykey4", new PreparedQueryObject(),
                new Condition(new HashMap(), new PreparedQueryObject()));
        assertEquals(returnType, result);
    }

    @Test
    public void testAtomicGet() throws MusicServiceException, MusicLockingException, MusicQueryException {
        ResultSet rs = Mockito.mock(ResultSet.class);
        ResultSet result = null;
        Mockito.when(musicCore.atomicGet(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(rs);
        result = MusicCore.atomicGet("keyspace5", "table5", "primarykey4", new PreparedQueryObject());
        assertEquals(rs, result);
    }

    @Test
    public void testGetLockQueue() throws MusicServiceException, MusicQueryException, MusicLockingException {
        List<String> result = Mockito.mock(List.class);
        List<String> rst = null;
        Mockito.when(musicCore.getLockQueue(Mockito.any())).thenReturn(result);
        rst = MusicCore.getLockQueue("key5");
        assertEquals(result, rst);
    }

    @Test
    public void testGetLockQueueSize() throws MusicServiceException, MusicQueryException, MusicLockingException {
        long result = 0L;
        Mockito.when(musicCore.getLockQueueSize(Mockito.any())).thenReturn(100L);
        result = MusicCore.getLockQueueSize("key6");
        assertEquals(100L, result);
    }

    @Test
    public void testatomicPutWithDeleteLock() throws MusicLockingException {
        ReturnType returnType = Mockito.mock(ReturnType.class);
        ReturnType result = null;
        Mockito.when(musicCore.atomicPutWithDeleteLock(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(),
                Mockito.any())).thenReturn(returnType);
        result = MusicCore.atomicPutWithDeleteLock("keyspace5", "table5", "primarykey4", new PreparedQueryObject(),
                new Condition(new HashMap(), new PreparedQueryObject()));
        assertEquals(returnType, result);
    }

    @Test
    public void testAtomicGetWithDeleteLock() throws MusicServiceException, MusicLockingException {
        ResultSet rs = Mockito.mock(ResultSet.class);
        ResultSet result = null;
        Mockito.when(musicCore.atomicGetWithDeleteLock(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
                .thenReturn(rs);
        result = MusicCore.atomicGetWithDeleteLock("keyspace5", "table5", "primarykey4", new PreparedQueryObject());
        assertEquals(rs, result);
    }

    @Test
    public void testValidateLock() {
        Map<String, Object> map = Mockito.mock(Map.class);
        Mockito.when(musicCore.validateLock(Mockito.any())).thenReturn(map);
        assertEquals(map, MusicCore.validateLock("lockname"));
    }

    @Test
    public void testReleaseLock() throws MusicLockingException {
        MusicLockState musicLockState = Mockito.mock(MusicLockState.class);
        MusicLockState result = null;
        Mockito.when(musicCore.releaseLock(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(musicLockState);
        result = MusicCore.releaseLock("lockid", true);
        assertEquals(musicLockState, result);
    }

    @Test
    public void testReleaseAllLocksForOwner() throws MusicLockingException, MusicServiceException, MusicQueryException {
        List<String> result = Mockito.mock(List.class);
        List<String> rst = null;
        Mockito.when(musicCore.releaseAllLocksForOwner(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(result);
        rst = MusicCore.releaseAllLocksForOwner("owner2", "keyspace6", "table6");
        assertEquals(result, rst);
    }

    @Test
    public void testCreateKeyspace() throws MusicServiceException, MusicQueryException {
        ResultType resultType = Mockito.mock(ResultType.class);
        ResultType result = null;
        Mockito.when(musicCore.createKeyspace(Mockito.any(), Mockito.any())).thenReturn(resultType);
        result = MusicCore.createKeyspace(new JsonKeySpace(), "consistency3");
        assertEquals(resultType, result);
    }

    @Test
    public void testDropKeyspace() throws MusicServiceException, MusicQueryException {
        ResultType resultType = Mockito.mock(ResultType.class);
        ResultType result = null;
        Mockito.when(musicCore.dropKeyspace(Mockito.any(), Mockito.any())).thenReturn(resultType);
        result = MusicCore.dropKeyspace(new JsonKeySpace(), "consistency4");
        assertEquals(resultType, result);
    }

    @Test
    public void testCreateTable2() throws MusicServiceException, MusicQueryException {
        ResultType resultType = Mockito.mock(ResultType.class);
        ResultType result = null;
        Mockito.when(musicCore.createTable(Mockito.any(), Mockito.any())).thenReturn(resultType);
        result = MusicCore.createTable(new JsonTable(), "consistency5");
        assertEquals(resultType, result);
    }

    @Test
    public void testDropTable() throws MusicServiceException, MusicQueryException {
        ResultType resultType = Mockito.mock(ResultType.class);
        ResultType result = null;
        Mockito.when(musicCore.dropTable(Mockito.any(), Mockito.any())).thenReturn(resultType);
        result = MusicCore.dropTable(new JsonTable(), "consistency5");
        assertEquals(resultType, result);
    }

    @Test
    public void testCreateIndex() throws MusicServiceException, MusicQueryException {
        ResultType resultType = Mockito.mock(ResultType.class);
        ResultType result = null;
        Mockito.when(musicCore.createIndex(Mockito.any(), Mockito.any())).thenReturn(resultType);
        result = MusicCore.createIndex(new JsonIndex("indexName", "keyspace7", "table7", "field"), "consistency6");
        assertEquals(resultType, result);
    }

    @Test
    public void testSelect() throws MusicServiceException, MusicQueryException {
        ResultSet rs = Mockito.mock(ResultSet.class);
        ResultSet result = null;
        Mockito.when(musicCore.select(Mockito.any(), Mockito.any())).thenReturn(rs);
        MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
        result = MusicCore.select(new JsonSelect(), map);
        assertEquals(rs, result);
    }

    @Test
    public void testSelectCritical() throws MusicLockingException, MusicQueryException, MusicServiceException {
        ResultSet rs = Mockito.mock(ResultSet.class);
        ResultSet result = null;
        Mockito.when(musicCore.selectCritical(Mockito.any(), Mockito.any())).thenReturn(rs);
        MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
        result = MusicCore.selectCritical(new JsonInsert(), map);
        assertEquals(rs, result);
    }

    @Test
    public void testInsertIntoTable() throws MusicLockingException, MusicQueryException, MusicServiceException {
        ReturnType returnType = Mockito.mock(ReturnType.class);
        ReturnType result = null;
        Mockito.when(musicCore.insertIntoTable(Mockito.any())).thenReturn(returnType);
        result = MusicCore.insertIntoTable(new JsonInsert());
        assertEquals(returnType, result);
    }

    @Test
    public void testUpdateTable() throws MusicLockingException, MusicQueryException, MusicServiceException {
        ReturnType returnType = Mockito.mock(ReturnType.class);
        ReturnType result = null;
        Mockito.when(musicCore.updateTable(Mockito.any(), Mockito.any())).thenReturn(returnType);
        MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
        result = MusicCore.updateTable(new JsonUpdate(), map);
        assertEquals(returnType, result);
    }

    @Test
    public void testDeleteFromTable() throws MusicLockingException, MusicQueryException, MusicServiceException {
        ReturnType returnType = Mockito.mock(ReturnType.class);
        ReturnType result = null;
        MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
        Mockito.when(musicCore.deleteFromTable(Mockito.any(), Mockito.any())).thenReturn(returnType);
        result = MusicCore.deleteFromTable(new JsonDelete(), map);
        assertEquals(returnType, result);
    }
}