diff options
author | Thomas Nelson Jr (arthurdent3) tn1381@att.com <tn1381@att.com> | 2018-03-06 12:13:17 -0500 |
---|---|---|
committer | Thomas Nelson Jr (arthurdent3) tn1381@att.com <tn1381@att.com> | 2018-03-06 12:13:17 -0500 |
commit | 0fec5f6d51014d07449c3b9a3c62d77f6c5d0a57 (patch) | |
tree | ca5a75313c1cabc256b7000c7b8aee065ca48296 /src/test/java/org | |
parent | a85726c8f611175a21fb55c139f8430ea82fc39d (diff) |
More Unit tests for Sonar coverage
Change-Id: I5cc3d490bdc70dfa0ed69e1ee6d954ef2846f8e5
Issue-ID: MUSIC-40
Signed-off-by: Thomas Nelson Jr (arthurdent3) tn1381@att.com <tn1381@att.com>
Diffstat (limited to 'src/test/java/org')
-rw-r--r-- | src/test/java/org/onap/music/unittests/TestMusicCore.java | 950 | ||||
-rw-r--r-- | src/test/java/org/onap/music/unittests/TestRestMusicData.java | 357 |
2 files changed, 844 insertions, 463 deletions
diff --git a/src/test/java/org/onap/music/unittests/TestMusicCore.java b/src/test/java/org/onap/music/unittests/TestMusicCore.java index 3355e4cd..5b5665a8 100644 --- a/src/test/java/org/onap/music/unittests/TestMusicCore.java +++ b/src/test/java/org/onap/music/unittests/TestMusicCore.java @@ -1,463 +1,487 @@ -///* -// * ============LICENSE_START========================================== -// * org.onap.music -// * =================================================================== -// * Copyright (c) 2017 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.unittests; -// -//import static org.junit.Assert.*; -//import static org.onap.music.main.MusicCore.mDstoreHandle; -//import static org.onap.music.main.MusicCore.mLockHandle; -//import org.junit.Before; -//import org.junit.Test; -//import org.junit.runner.RunWith; -//import org.mockito.Mock; -//import org.mockito.Mockito; -//import org.mockito.runners.MockitoJUnitRunner; -//import org.onap.music.exceptions.MusicLockingException; -//import org.onap.music.exceptions.MusicQueryException; -//import org.onap.music.exceptions.MusicServiceException; -//import org.onap.music.lockingservice.MusicLockState; -//import org.onap.music.lockingservice.MusicLockingService; -//import org.onap.music.lockingservice.MusicLockState.LockStatus; -//import org.onap.music.main.MusicCore; -//import org.onap.music.main.ResultType; -//import org.onap.music.main.ReturnType; -//import org.onap.music.main.MusicCore.Condition; -//import org.onap.music.datastore.MusicDataStore; -//import org.onap.music.datastore.PreparedQueryObject; -//import com.datastax.driver.core.ResultSet; -// -//@RunWith(MockitoJUnitRunner.class) -//public class TestMusicCore { -// -// @Mock -// private Condition condition; -// -// @Mock -// private ResultSet rs; -// -// @Mock -// private PreparedQueryObject preparedQueryObject; -// -// @Before -// public void setUp() { -// mLockHandle = Mockito.mock(MusicLockingService.class); -// -// } -// -// @Test -// public void testCreateLockReferenceforvalidlock() { -// Mockito.when(mLockHandle.createLockId("/" + "test")).thenReturn("lock"); -// String lockId = MusicCore.createLockReference("test"); -// assertEquals("lock", lockId); -// Mockito.verify(mLockHandle).createLockId("/" + "test"); -// } -// -// @Test -// public void testIsTableOrKeySpaceLock() { -// Boolean result = MusicCore.isTableOrKeySpaceLock("ks1.tn1"); -// assertTrue(result); -// } -// -// @Test -// public void testIsTableOrKeySpaceLockwithPrimarykey() { -// Boolean result = MusicCore.isTableOrKeySpaceLock("ks1.tn1.pk1"); -// assertFalse(result); -// } -// -// @Test -// public void testGetMusicLockState() throws MusicLockingException { -// MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); -// Mockito.when(mLockHandle.getLockState("ks1.tb1.pk1")).thenReturn(musicLockState); -// MusicLockState mls = MusicCore.getMusicLockState("ks1.tb1.pk1"); -// assertEquals(musicLockState, mls); -// Mockito.verify(mLockHandle).getLockState("ks1.tb1.pk1"); -// } -// -// @Test -// public void testAcquireLockifisMyTurnTrue() { -// Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); -// ReturnType lock = MusicCore.acquireLock("ks1.tn1", "id1"); -// assertEquals(lock.getResult(), ResultType.SUCCESS); -// Mockito.verify(mLockHandle).isMyTurn("id1"); -// } -// -// @Test -// public void testAcquireLockifisMyTurnFalse() { -// Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); -// ReturnType lock = MusicCore.acquireLock("ks1.ts1", "id1"); -// assertEquals(lock.getResult(), ResultType.FAILURE); -// Mockito.verify(mLockHandle).isMyTurn("id1"); -// } -// -// @Test -// public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockTrue() { -// Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); -// ReturnType lock = MusicCore.acquireLock("ks1.tn1", "id1"); -// assertEquals(lock.getResult(), ResultType.SUCCESS); -// Mockito.verify(mLockHandle).isMyTurn("id1"); -// } -// -// @Test -// public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandHaveLock() throws MusicLockingException { -// MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); -// Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); -// Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); -// ReturnType lock = MusicCore.acquireLock("ks1.tn1.pk1", "id1"); -// assertEquals(lock.getResult(), ResultType.SUCCESS); -// Mockito.verify(mLockHandle).isMyTurn("id1"); -// Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1"); -// } -// -// @Test -// public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandDontHaveLock() throws MusicLockingException { -// MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id2"); -// Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); -// Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); -// ReturnType lock = MusicCore.acquireLock("ks1.tn1.pk1", "id1"); -// assertEquals(lock.getResult(), ResultType.SUCCESS); -// Mockito.verify(mLockHandle).isMyTurn("id1"); -// Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1"); -// } -// -// @Test -// public void testAcquireLockifLockRefDoesntExist() { -// Mockito.when(mLockHandle.lockIdExists("bs1")).thenReturn(false); -// ReturnType lock = MusicCore.acquireLock("ks1.ts1", "bs1"); -// assertEquals(lock.getResult(), ResultType.FAILURE); -// assertEquals(lock.getMessage(), "Lockid doesn't exist"); -// Mockito.verify(mLockHandle).lockIdExists("bs1"); -// } -// -// @Test -// public void testAcquireLockWithLeasewithLease() throws MusicLockingException { -// MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); -// musicLockState.setLeasePeriod(0); -// ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); -// Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); -// Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); -// ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); -// assertEquals(expectedResult.getResult(), actualResult.getResult()); -// Mockito.verify(mLockHandle).isMyTurn("id1"); -// Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); -// } -// -// @Test -// public void testAcquireLockWithLeasewithException() throws MusicLockingException { -// ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "failure"); -// Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenThrow(new MusicLockingException()); -// Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); -// ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); -// assertEquals(expectedResult.getResult(), actualResult.getResult()); -// Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); -// } -// -// @Test -// public void testAcquireLockWithLeasewithLockStatusLOCKED() throws MusicLockingException { -// MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); -// ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); -// Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); -// Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); -// ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); -// assertEquals(expectedResult.getResult(), actualResult.getResult()); -// Mockito.verify(mLockHandle).isMyTurn("id1"); -// Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); -// } -// -// @Test -// public void testAcquireLockWithLeasewithLockStatusUNLOCKED() throws MusicLockingException { -// MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); -// ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); -// Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); -// Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); -// ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); -// assertEquals(expectedResult.getResult(), actualResult.getResult()); -// Mockito.verify(mLockHandle).isMyTurn("id1"); -// Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); -// -// } -// -// @Test -// public void testAcquireLockWithLeaseIfNotMyTurn() throws MusicLockingException { -// MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); -// ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); -// Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); -// Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); -// ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); -// assertEquals(expectedResult.getResult(), actualResult.getResult()); -// Mockito.verify(mLockHandle).isMyTurn("id1"); -// Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1"); -// } -// -// @Test -// public void testQuorumGet() throws MusicServiceException, MusicQueryException { -// preparedQueryObject = Mockito.mock(PreparedQueryObject.class); -// mDstoreHandle = Mockito.mock(MusicDataStore.class); -// rs = Mockito.mock(ResultSet.class); -// Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs); -// ResultSet rs1 = MusicCore.quorumGet(preparedQueryObject); -// assertNotNull(rs1); -// } -// -// @Test -// public void testGetLockNameFromId() { -// String lockname = MusicCore.getLockNameFromId("lockName$id"); -// assertEquals("lockName", lockname); -// } -// -// @Test -// public void testDestroyLockRef() { -// Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1"); -// MusicCore.destroyLockRef("id1"); -// Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1"); -// } -// -// @Test -// public void testreleaseLockwithvoluntaryReleaseTrue() { -// MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); -// Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1"); -// MusicLockState musicLockState1 = MusicCore.releaseLock("id1", true); -// assertEquals(musicLockState.getLockStatus(), musicLockState1.getLockStatus()); -// Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1"); -// } -// -// @Test -// public void testreleaseLockwithvoluntaryReleaseFalse() { -// MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); -// Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1"); -// MusicLockState musicLockState1 = MusicCore.releaseLock("id1", false); -// assertEquals(musicLockState.getLockStatus(), musicLockState1.getLockStatus()); -// Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1"); -// } -// -// @Test -// public void testDeleteLock() { -// Mockito.doNothing().when(mLockHandle).deleteLock("/" + "id1"); -// MusicCore.deleteLock("id1"); -// Mockito.verify(mLockHandle).deleteLock("/" + "id1"); -// } -// -// /* -// * @Test public void testNonKeyRelatedPut() throws Exception { mDstoreHandle = -// * Mockito.mock(MusicDataStore.class); Mockito.when(mDstoreHandle.executePut("qu1", -// * "consistency")).thenReturn(true); Boolean result = MusicCore.nonKeyRelatedPut("qu1", -// * "consistency"); assertTrue(result); Mockito.verify(mDstoreHandle).executePut("qu1", -// * "consistency"); } -// */ -// -// @Test -// public void testEventualPutPreparedQuery() throws MusicServiceException, MusicQueryException { -// mDstoreHandle = Mockito.mock(MusicDataStore.class); -// preparedQueryObject = Mockito.mock(PreparedQueryObject.class); -// ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); -// Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(true); -// ReturnType actualResult = MusicCore.eventualPut(preparedQueryObject); -// assertEquals(expectedResult.getResult(), actualResult.getResult()); -// Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "eventual"); -// } -// -// @Test -// public void testEventualPutPreparedQuerywithResultFalse() -// throws MusicServiceException, MusicQueryException { -// mDstoreHandle = Mockito.mock(MusicDataStore.class); -// preparedQueryObject = Mockito.mock(PreparedQueryObject.class); -// ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); -// Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(false); -// ReturnType actualResult = MusicCore.eventualPut(preparedQueryObject); -// assertEquals(expectedResult.getResult(), actualResult.getResult()); -// Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "eventual"); -// } -// -// @Test -// public void testCriticalPutPreparedQuerywithValidLockId() -// throws Exception { -// mDstoreHandle = Mockito.mock(MusicDataStore.class); -// preparedQueryObject = Mockito.mock(PreparedQueryObject.class); -// MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); -// Mockito.when(condition.testCondition()).thenReturn(true); -// ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); -// Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) -// .thenReturn(musicLockState); -// Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true); -// ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject, -// "id1", condition); -// assertEquals(expectedResult.getResult(), returnType.getResult()); -// Mockito.verify(condition).testCondition(); -// Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1"); -// Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "critical"); -// } -// -// @Test -// public void testCriticalPutPreparedQuerywithInvalidLockId() throws MusicLockingException { -// mDstoreHandle = Mockito.mock(MusicDataStore.class); -// preparedQueryObject = Mockito.mock(PreparedQueryObject.class); -// MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); -// ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); -// Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) -// .thenReturn(musicLockState); -// ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject, -// "id1", condition); -// assertEquals(expectedResult.getResult(), returnType.getResult()); -// Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1"); -// } -// -// @Test -// public void testCriticalPutPreparedQuerywithvalidLockIdandTestConditionFalse() throws Exception { -// mDstoreHandle = Mockito.mock(MusicDataStore.class); -// preparedQueryObject = Mockito.mock(PreparedQueryObject.class); -// MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); -// Mockito.when(condition.testCondition()).thenReturn(false); -// ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); -// Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) -// .thenReturn(musicLockState); -// ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject, -// "id1", condition); -// assertEquals(expectedResult.getResult(), returnType.getResult()); -// Mockito.verify(condition).testCondition(); -// Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1"); -// } -// -// @Test -// public void testNonKeyRelatedPutPreparedQuery() throws Exception { -// mDstoreHandle = Mockito.mock(MusicDataStore.class); -// preparedQueryObject = Mockito.mock(PreparedQueryObject.class); -// Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "consistency")).thenReturn(true); -// Boolean result = MusicCore.nonKeyRelatedPut(preparedQueryObject, "consistency"); -// assertTrue(result); -// Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "consistency"); -// } -// -// @Test -// public void testAtomicPutPreparedQuery() throws Exception { -// mDstoreHandle = Mockito.mock(MusicDataStore.class); -// preparedQueryObject = Mockito.mock(PreparedQueryObject.class); -// Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); -// MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); -// ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); -// Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); -// Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); -// Mockito.when(condition.testCondition()).thenReturn(true); -// Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) -// .thenReturn(musicLockState); -// Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true); -// ReturnType returnType = -// MusicCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition); -// assertEquals(expectedResult.getResult(), returnType.getResult()); -// Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); -// Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); -// Mockito.verify(mLockHandle).isMyTurn("id1"); -// Mockito.verify(condition).testCondition(); -// Mockito.verify(mLockHandle, Mockito.atLeastOnce()) -// .getLockState("ks1" + "." + "tn1" + "." + "pk1"); -// Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "critical"); -// } -// -// @Test -// public void testAtomicPutPreparedQuerywithAcquireLockWithLeaseFalse() throws MusicLockingException { -// mDstoreHandle = Mockito.mock(MusicDataStore.class); -// preparedQueryObject = Mockito.mock(PreparedQueryObject.class); -// Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); -// ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); -// Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); -// ReturnType returnType = -// MusicCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition); -// assertEquals(expectedResult.getResult(), returnType.getResult()); -// Mockito.verify(mLockHandle).isMyTurn("id1"); -// Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); -// } -// -// @Test -// public void testAtomicGetPreparedQuery() throws MusicServiceException, MusicQueryException, MusicLockingException { -// mDstoreHandle = Mockito.mock(MusicDataStore.class); -// preparedQueryObject = Mockito.mock(PreparedQueryObject.class); -// rs = Mockito.mock(ResultSet.class); -// Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); -// MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); -// Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); -// Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); -// Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) -// .thenReturn(musicLockState); -// Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs); -// ResultSet rs1 = MusicCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject); -// assertNotNull(rs1); -// Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); -// Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); -// Mockito.verify(mLockHandle).isMyTurn("id1"); -// Mockito.verify(mLockHandle, Mockito.atLeastOnce()) -// .getLockState("ks1" + "." + "tn1" + "." + "pk1"); -// Mockito.verify(mDstoreHandle).executeCriticalGet(preparedQueryObject); -// } -// -// @Test -// public void testAtomicGetPreparedQuerywithAcquireLockWithLeaseFalse() -// throws MusicServiceException, MusicLockingException { -// mDstoreHandle = Mockito.mock(MusicDataStore.class); -// preparedQueryObject = Mockito.mock(PreparedQueryObject.class); -// rs = Mockito.mock(ResultSet.class); -// Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); -// Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); -// ResultSet rs1 = MusicCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject); -// assertNull(rs1); -// Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); -// Mockito.verify(mLockHandle).isMyTurn("id1"); -// } -// -// @Test -// public void testGetPreparedQuery() throws MusicServiceException, MusicQueryException { -// mDstoreHandle = Mockito.mock(MusicDataStore.class); -// rs = Mockito.mock(ResultSet.class); -// preparedQueryObject = Mockito.mock(PreparedQueryObject.class); -// Mockito.when(mDstoreHandle.executeEventualGet(preparedQueryObject)).thenReturn(rs); -// ResultSet rs1 = MusicCore.get(preparedQueryObject); -// assertNotNull(rs1); -// Mockito.verify(mDstoreHandle).executeEventualGet(preparedQueryObject); -// -// } -// -// @Test -// public void testcriticalGetPreparedQuery() throws MusicServiceException, MusicQueryException, MusicLockingException { -// mDstoreHandle = Mockito.mock(MusicDataStore.class); -// preparedQueryObject = Mockito.mock(PreparedQueryObject.class); -// MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); -// rs = Mockito.mock(ResultSet.class); -// Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) -// .thenReturn(musicLockState); -// Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs); -// ResultSet rs1 = MusicCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1"); -// assertNotNull(rs1); -// Mockito.verify(mLockHandle, Mockito.atLeastOnce()) -// .getLockState("ks1" + "." + "tn1" + "." + "pk1"); -// Mockito.verify(mDstoreHandle).executeCriticalGet(preparedQueryObject); -// } -// -// @Test -// public void testcriticalGetPreparedQuerywithInvalidLockId() throws MusicServiceException, MusicLockingException { -// mDstoreHandle = Mockito.mock(MusicDataStore.class); -// preparedQueryObject = Mockito.mock(PreparedQueryObject.class); -// MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); -// Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) -// .thenReturn(musicLockState); -// ResultSet rs1 = MusicCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1"); -// assertNull(rs1); -// Mockito.verify(mLockHandle, Mockito.atLeastOnce()) -// .getLockState("ks1" + "." + "tn1" + "." + "pk1"); -// } -// -//} +/* + * ============LICENSE_START========================================== + * org.onap.music + * =================================================================== + * Copyright (c) 2017 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.unittests; + +import static org.junit.Assert.*; +import static org.onap.music.main.MusicCore.mDstoreHandle; +import static org.onap.music.main.MusicCore.mLockHandle; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.music.exceptions.MusicLockingException; +import org.onap.music.exceptions.MusicQueryException; +import org.onap.music.exceptions.MusicServiceException; +import org.onap.music.lockingservice.MusicLockState; +import org.onap.music.lockingservice.MusicLockingService; +import org.onap.music.lockingservice.MusicLockState.LockStatus; +import org.onap.music.main.MusicCore; +import org.onap.music.main.MusicUtil; +import org.onap.music.main.ResultType; +import org.onap.music.main.ReturnType; +import org.onap.music.main.MusicCore.Condition; +import org.onap.music.datastore.MusicDataStore; +import org.onap.music.datastore.PreparedQueryObject; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Session; + +@RunWith(MockitoJUnitRunner.class) +public class TestMusicCore { + + @Mock + private Condition condition; + + @Mock + private ResultSet rs; + + @Mock + private PreparedQueryObject preparedQueryObject; + + @Mock + private Session session; + + @Before + public void setUp() { + mLockHandle = Mockito.mock(MusicLockingService.class); + + } + + @Test + public void testCreateLockReferenceforvalidlock() { + Mockito.when(mLockHandle.createLockId("/" + "test")).thenReturn("lock"); + String lockId = MusicCore.createLockReference("test"); + assertEquals("lock", lockId); + Mockito.verify(mLockHandle).createLockId("/" + "test"); + } + + @Test + public void testIsTableOrKeySpaceLock() { + Boolean result = MusicCore.isTableOrKeySpaceLock("ks1.tn1"); + assertTrue(result); + } + + @Test + public void testIsTableOrKeySpaceLockwithPrimarykey() { + Boolean result = MusicCore.isTableOrKeySpaceLock("ks1.tn1.pk1"); + assertFalse(result); + } + + @Test + public void testGetMusicLockState() throws MusicLockingException { + MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); + Mockito.when(mLockHandle.getLockState("ks1.tb1.pk1")).thenReturn(musicLockState); + MusicLockState mls = MusicCore.getMusicLockState("ks1.tb1.pk1"); + assertEquals(musicLockState, mls); + Mockito.verify(mLockHandle).getLockState("ks1.tb1.pk1"); + } + + @Test + public void testAcquireLockifisMyTurnTrue() { + Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); + ReturnType lock = MusicCore.acquireLock("ks1.tn1", "id1"); + assertEquals(lock.getResult(), ResultType.SUCCESS); + Mockito.verify(mLockHandle).isMyTurn("id1"); + } + + @Test + public void testAcquireLockifisMyTurnFalse() { + Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); + ReturnType lock = MusicCore.acquireLock("ks1.ts1", "id1"); + assertEquals(lock.getResult(), ResultType.FAILURE); + Mockito.verify(mLockHandle).isMyTurn("id1"); + } + + @Test + public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockTrue() { + Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); + ReturnType lock = MusicCore.acquireLock("ks1.tn1", "id1"); + assertEquals(lock.getResult(), ResultType.SUCCESS); + Mockito.verify(mLockHandle).isMyTurn("id1"); + } + + @Test + public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandHaveLock() throws MusicLockingException { + MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); + Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); + Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); + ReturnType lock = MusicCore.acquireLock("ks1.tn1.pk1", "id1"); + assertEquals(lock.getResult(), ResultType.SUCCESS); + Mockito.verify(mLockHandle).isMyTurn("id1"); + Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1"); + } + + @Test + public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandDontHaveLock() throws MusicLockingException { + MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id2"); + Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); + Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); + ReturnType lock = MusicCore.acquireLock("ks1.tn1.pk1", "id1"); + assertEquals(lock.getResult(), ResultType.SUCCESS); + Mockito.verify(mLockHandle).isMyTurn("id1"); + Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1"); + } + + @Test + public void testAcquireLockifLockRefDoesntExist() { + Mockito.when(mLockHandle.lockIdExists("bs1")).thenReturn(false); + ReturnType lock = MusicCore.acquireLock("ks1.ts1", "bs1"); + assertEquals(lock.getResult(), ResultType.FAILURE); + assertEquals(lock.getMessage(), "Lockid doesn't exist"); + Mockito.verify(mLockHandle).lockIdExists("bs1"); + } + + @Test + public void testAcquireLockWithLeasewithLease() throws MusicLockingException { + MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); + musicLockState.setLeasePeriod(0); + ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); + Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); + Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); + ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); + assertEquals(expectedResult.getResult(), actualResult.getResult()); + Mockito.verify(mLockHandle).isMyTurn("id1"); + Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); + } + + @Test + public void testAcquireLockWithLeasewithException() throws MusicLockingException { + ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "failure"); + Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenThrow(new MusicLockingException()); + Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); + ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); + assertEquals(expectedResult.getResult(), actualResult.getResult()); + Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); + } + + @Test + public void testAcquireLockWithLeasewithLockStatusLOCKED() throws MusicLockingException { + MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); + ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); + Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); + Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); + ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); + assertEquals(expectedResult.getResult(), actualResult.getResult()); + Mockito.verify(mLockHandle).isMyTurn("id1"); + Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); + } + + @Test + public void testAcquireLockWithLeasewithLockStatusUNLOCKED() throws MusicLockingException { + MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); + ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); + Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); + Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); + ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); + assertEquals(expectedResult.getResult(), actualResult.getResult()); + Mockito.verify(mLockHandle).isMyTurn("id1"); + Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); + + } + + @Test + public void testAcquireLockWithLeaseIfNotMyTurn() throws MusicLockingException { + MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); + ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); + Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); + Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); + ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); + assertEquals(expectedResult.getResult(), actualResult.getResult()); + Mockito.verify(mLockHandle).isMyTurn("id1"); + Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1"); + } + + @Test + public void testQuorumGet() throws MusicServiceException, MusicQueryException { + preparedQueryObject = Mockito.mock(PreparedQueryObject.class); + mDstoreHandle = Mockito.mock(MusicDataStore.class); + rs = Mockito.mock(ResultSet.class); + session = Mockito.mock(Session.class); + Mockito.when(mDstoreHandle.getSession()).thenReturn(session); + Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs); + ResultSet rs1 = MusicCore.quorumGet(preparedQueryObject); + assertNotNull(rs1); + } + + @Test + public void testGetLockNameFromId() { + String lockname = MusicCore.getLockNameFromId("lockName$id"); + assertEquals("lockName", lockname); + } + + @Test + public void testDestroyLockRef() { + Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1"); + MusicCore.destroyLockRef("id1"); + Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1"); + } + + @Test + public void testreleaseLockwithvoluntaryReleaseTrue() { + MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); + Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1"); + MusicLockState musicLockState1 = MusicCore.releaseLock("id1", true); + assertEquals(musicLockState.getLockStatus(), musicLockState1.getLockStatus()); + Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1"); + } + + @Test + public void testreleaseLockwithvoluntaryReleaseFalse() { + MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); + Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1"); + MusicLockState musicLockState1 = MusicCore.releaseLock("id1", false); + assertEquals(musicLockState.getLockStatus(), musicLockState1.getLockStatus()); + Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1"); + } + + @Test + public void testDeleteLock() { + Mockito.doNothing().when(mLockHandle).deleteLock("/" + "id1"); + MusicCore.deleteLock("id1"); + Mockito.verify(mLockHandle).deleteLock("/" + "id1"); + } + + /* + * @Test public void testNonKeyRelatedPut() throws Exception { mDstoreHandle = + * Mockito.mock(MusicDataStore.class); Mockito.when(mDstoreHandle.executePut("qu1", + * "consistency")).thenReturn(true); Boolean result = MusicCore.nonKeyRelatedPut("qu1", + * "consistency"); assertTrue(result); Mockito.verify(mDstoreHandle).executePut("qu1", + * "consistency"); } + */ + + @Test + public void testEventualPutPreparedQuery() throws MusicServiceException, MusicQueryException { + mDstoreHandle = Mockito.mock(MusicDataStore.class); + preparedQueryObject = Mockito.mock(PreparedQueryObject.class); + ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); + session = Mockito.mock(Session.class); + Mockito.when(mDstoreHandle.getSession()).thenReturn(session); + Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(true); + ReturnType actualResult = MusicCore.eventualPut(preparedQueryObject); + assertEquals(expectedResult.getResult(), actualResult.getResult()); + Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "eventual"); + } + + @Test + public void testEventualPutPreparedQuerywithResultFalse() + throws MusicServiceException, MusicQueryException { + mDstoreHandle = Mockito.mock(MusicDataStore.class); + preparedQueryObject = Mockito.mock(PreparedQueryObject.class); + ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); + session = Mockito.mock(Session.class); + Mockito.when(mDstoreHandle.getSession()).thenReturn(session); + Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(false); + ReturnType actualResult = MusicCore.eventualPut(preparedQueryObject); + assertEquals(expectedResult.getResult(), actualResult.getResult()); + Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "eventual"); + //Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, MusicUtil.EVENTUAL); + } + + @Test + public void testCriticalPutPreparedQuerywithValidLockId() + throws Exception { + mDstoreHandle = Mockito.mock(MusicDataStore.class); + preparedQueryObject = Mockito.mock(PreparedQueryObject.class); + MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); + Mockito.when(condition.testCondition()).thenReturn(true); + session = Mockito.mock(Session.class); + Mockito.when(mDstoreHandle.getSession()).thenReturn(session); + ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); + Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) + .thenReturn(musicLockState); + Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true); + ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject, + "id1", condition); + assertEquals(expectedResult.getResult(), returnType.getResult()); + Mockito.verify(condition).testCondition(); + Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1"); + Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "critical"); + } + + @Test + public void testCriticalPutPreparedQuerywithInvalidLockId() throws MusicLockingException { + mDstoreHandle = Mockito.mock(MusicDataStore.class); + preparedQueryObject = Mockito.mock(PreparedQueryObject.class); + MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); + ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); + Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) + .thenReturn(musicLockState); + ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject, + "id1", condition); + assertEquals(expectedResult.getResult(), returnType.getResult()); + Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1"); + } + + @Test + public void testCriticalPutPreparedQuerywithvalidLockIdandTestConditionFalse() throws Exception { + mDstoreHandle = Mockito.mock(MusicDataStore.class); + preparedQueryObject = Mockito.mock(PreparedQueryObject.class); + MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); + Mockito.when(condition.testCondition()).thenReturn(false); + ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); + Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) + .thenReturn(musicLockState); + ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject, + "id1", condition); + assertEquals(expectedResult.getResult(), returnType.getResult()); + Mockito.verify(condition).testCondition(); + Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1"); + } + + @Test + public void testNonKeyRelatedPutPreparedQuery() throws Exception { + mDstoreHandle = Mockito.mock(MusicDataStore.class); + preparedQueryObject = Mockito.mock(PreparedQueryObject.class); + session = Mockito.mock(Session.class); + Mockito.when(mDstoreHandle.getSession()).thenReturn(session); + Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "consistency")).thenReturn(true); + Boolean result = MusicCore.nonKeyRelatedPut(preparedQueryObject, "consistency"); + assertTrue(result); + Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "consistency"); + } + + @Test + public void testAtomicPutPreparedQuery() throws Exception { + mDstoreHandle = Mockito.mock(MusicDataStore.class); + preparedQueryObject = Mockito.mock(PreparedQueryObject.class); + Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); + MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); + ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); + session = Mockito.mock(Session.class); + Mockito.when(mDstoreHandle.getSession()).thenReturn(session); + Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); + Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); + Mockito.when(condition.testCondition()).thenReturn(true); + Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) + .thenReturn(musicLockState); + Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true); + ReturnType returnType = + MusicCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition); + assertEquals(expectedResult.getResult(), returnType.getResult()); + Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); + Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); + Mockito.verify(mLockHandle).isMyTurn("id1"); + Mockito.verify(condition).testCondition(); + Mockito.verify(mLockHandle, Mockito.atLeastOnce()) + .getLockState("ks1" + "." + "tn1" + "." + "pk1"); + Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "critical"); + } + + @Test + public void testAtomicPutPreparedQuerywithAcquireLockWithLeaseFalse() throws MusicLockingException { + mDstoreHandle = Mockito.mock(MusicDataStore.class); + preparedQueryObject = Mockito.mock(PreparedQueryObject.class); + Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); + ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); + Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); + ReturnType returnType = + MusicCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition); + assertEquals(expectedResult.getResult(), returnType.getResult()); + Mockito.verify(mLockHandle).isMyTurn("id1"); + Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); + } + + @Test + public void testAtomicGetPreparedQuery() throws MusicServiceException, MusicQueryException, MusicLockingException { + mDstoreHandle = Mockito.mock(MusicDataStore.class); + preparedQueryObject = Mockito.mock(PreparedQueryObject.class); + rs = Mockito.mock(ResultSet.class); + session = Mockito.mock(Session.class); + Mockito.when(mDstoreHandle.getSession()).thenReturn(session); + Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); + MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); + Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); + Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); + Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) + .thenReturn(musicLockState); + Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs); + ResultSet rs1 = MusicCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject); + assertNotNull(rs1); + Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); + Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); + Mockito.verify(mLockHandle).isMyTurn("id1"); + Mockito.verify(mLockHandle, Mockito.atLeastOnce()) + .getLockState("ks1" + "." + "tn1" + "." + "pk1"); + Mockito.verify(mDstoreHandle).executeCriticalGet(preparedQueryObject); + } + + @Test + public void testAtomicGetPreparedQuerywithAcquireLockWithLeaseFalse() + throws MusicServiceException, MusicLockingException { + mDstoreHandle = Mockito.mock(MusicDataStore.class); + preparedQueryObject = Mockito.mock(PreparedQueryObject.class); + rs = Mockito.mock(ResultSet.class); + Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); + Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); + ResultSet rs1 = MusicCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject); + assertNull(rs1); + Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); + Mockito.verify(mLockHandle).isMyTurn("id1"); + } + + @Test + public void testGetPreparedQuery() throws MusicServiceException, MusicQueryException { + mDstoreHandle = Mockito.mock(MusicDataStore.class); + rs = Mockito.mock(ResultSet.class); + preparedQueryObject = Mockito.mock(PreparedQueryObject.class); + session = Mockito.mock(Session.class); + Mockito.when(mDstoreHandle.getSession()).thenReturn(session); + Mockito.when(mDstoreHandle.executeEventualGet(preparedQueryObject)).thenReturn(rs); + ResultSet rs1 = MusicCore.get(preparedQueryObject); + assertNotNull(rs1); + Mockito.verify(mDstoreHandle).executeEventualGet(preparedQueryObject); + + } + + @Test + public void testcriticalGetPreparedQuery() throws MusicServiceException, MusicQueryException, MusicLockingException { + mDstoreHandle = Mockito.mock(MusicDataStore.class); + preparedQueryObject = Mockito.mock(PreparedQueryObject.class); + MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); + rs = Mockito.mock(ResultSet.class); + session = Mockito.mock(Session.class); + Mockito.when(mDstoreHandle.getSession()).thenReturn(session); + Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) + .thenReturn(musicLockState); + Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs); + ResultSet rs1 = MusicCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1"); + assertNotNull(rs1); + Mockito.verify(mLockHandle, Mockito.atLeastOnce()) + .getLockState("ks1" + "." + "tn1" + "." + "pk1"); + Mockito.verify(mDstoreHandle).executeCriticalGet(preparedQueryObject); + } + + @Test + public void testcriticalGetPreparedQuerywithInvalidLockId() throws MusicServiceException, MusicLockingException { + mDstoreHandle = Mockito.mock(MusicDataStore.class); + preparedQueryObject = Mockito.mock(PreparedQueryObject.class); + MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); + Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) + .thenReturn(musicLockState); + ResultSet rs1 = MusicCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1"); + assertNull(rs1); + Mockito.verify(mLockHandle, Mockito.atLeastOnce()) + .getLockState("ks1" + "." + "tn1" + "." + "pk1"); + } + +} diff --git a/src/test/java/org/onap/music/unittests/TestRestMusicData.java b/src/test/java/org/onap/music/unittests/TestRestMusicData.java new file mode 100644 index 00000000..bc5e9fb9 --- /dev/null +++ b/src/test/java/org/onap/music/unittests/TestRestMusicData.java @@ -0,0 +1,357 @@ +/* + * ============LICENSE_START========================================== + * org.onap.music + * =================================================================== + * Copyright (c) 2017 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.unittests; + +import static org.junit.Assert.assertEquals; +import static org.onap.music.main.MusicCore.mLockHandle; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.UriInfo; + +import org.apache.curator.test.TestingServer; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.music.datastore.PreparedQueryObject; +import org.onap.music.datastore.jsonobjects.JsonDelete; +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.lockingservice.MusicLockingService; +import org.onap.music.main.CachingUtil; +import org.onap.music.main.MusicCore; +import org.onap.music.main.MusicUtil; +import org.onap.music.rest.RestMusicDataAPI; + +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.sun.jersey.core.util.MultivaluedMapImpl; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@RunWith(MockitoJUnitRunner.class) +public class TestRestMusicData { + + RestMusicDataAPI data = new RestMusicDataAPI(); + static PreparedQueryObject testObject; + static TestingServer zkServer; + + @Mock + HttpServletResponse http; + + @Mock + UriInfo info; + + static String appName = "TestApp"; + static String userId = "TestUser"; + static String password = "TestPassword"; + static boolean isAAF = false; + static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6"); + static String keyspaceName = "testCassa"; + static String tableName = "employees"; + static String xLatestVersion = "X-latestVersion"; + + @BeforeClass + public static void init() throws Exception { + try { + MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra(); + zkServer = new TestingServer(2181, new File("/tmp/zk")); + MusicCore.mLockHandle = new MusicLockingService(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("After class"); + testObject = new PreparedQueryObject(); + testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName); + MusicCore.eventualPut(testObject); + testObject = new PreparedQueryObject(); + testObject.appendQueryString("DROP KEYSPACE IF EXISTS admin"); + MusicCore.eventualPut(testObject); + MusicCore.mDstoreHandle.close(); + MusicCore.mLockHandle.getzkLockHandle().close(); + MusicCore.mLockHandle.close(); + zkServer.stop(); + } + + @Test + public void Test1_createKeyspace() throws Exception { + testObject = new PreparedQueryObject(); + testObject.appendQueryString("CREATE KEYSPACE admin WITH REPLICATION = " + "{'class' : 'SimpleStrategy' , " + + "'replication_factor': 1} AND DURABLE_WRITES = true"); + MusicCore.eventualPut(testObject); + testObject = new PreparedQueryObject(); + testObject.appendQueryString("CREATE TABLE admin.keyspace_master (" + " uuid uuid, keyspace_name text," + + " application_name text, is_api boolean," + " password text, username text," + + " is_aaf boolean, PRIMARY KEY (uuid)\n" + ");"); + MusicCore.eventualPut(testObject); + + testObject = new PreparedQueryObject(); + testObject + .appendQueryString("INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " + + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)"); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), MusicUtil.DEFAULTKEYSPACENAME)); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True")); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), password)); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF)); + MusicCore.eventualPut(testObject); + + testObject = new PreparedQueryObject(); + testObject + .appendQueryString("INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " + + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)"); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), + UUID.fromString("bbc66ccc-d857-4e90-b1e5-df98a3d40de6"))); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), MusicUtil.DEFAULTKEYSPACENAME)); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), "TestApp1")); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True")); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), password)); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), "TestUser1")); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF)); + MusicCore.eventualPut(testObject); + + testObject = new PreparedQueryObject(); + testObject + .appendQueryString("select uuid from admin.keyspace_master where application_name = ? allow filtering"); + testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); + ResultSet rs = MusicCore.get(testObject); + List<Row> rows = rs.all(); + if (rows.size() > 0) { + System.out.println("#######UUID is:" + rows.get(0).getUUID("uuid")); + } + } + + @Test + public void Test2_createKeyspace() throws Exception { + JsonKeySpace jsonKeyspace = new JsonKeySpace(); + Map<String, String> consistencyInfo = new HashMap<>(); + Map<String, Object> replicationInfo = new HashMap<>(); + consistencyInfo.put("type", "eventual"); + replicationInfo.put("class", "SimpleStrategy"); + replicationInfo.put("replication_factor", 1); + jsonKeyspace.setConsistencyInfo(consistencyInfo); + jsonKeyspace.setDurabilityOfWrites("true"); + jsonKeyspace.setKeyspaceName(keyspaceName); + jsonKeyspace.setReplicationInfo(replicationInfo); + Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Map<String, Object> resultMap = data.createKeySpace("1", "1", "1", null, appName, userId, password, + jsonKeyspace, keyspaceName, http); + assertEquals(uuid.toString(), resultMap.get("aid")); + } + + @Test + public void Test3_createKeyspace1() throws Exception { + JsonKeySpace jsonKeyspace = new JsonKeySpace(); + Map<String, String> consistencyInfo = new HashMap<>(); + Map<String, Object> replicationInfo = new HashMap<>(); + consistencyInfo.put("type", "eventual"); + replicationInfo.put("class", "SimpleStrategy"); + replicationInfo.put("replication_factor", 1); + jsonKeyspace.setConsistencyInfo(consistencyInfo); + jsonKeyspace.setDurabilityOfWrites("true"); + jsonKeyspace.setKeyspaceName("TestApp1"); + jsonKeyspace.setReplicationInfo(replicationInfo); + Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Map<String, Object> resultMap = data.createKeySpace("1", "1", "1", null, "TestApp1", "TestUser1", password, + jsonKeyspace, keyspaceName, http); + System.out.println("#######status is " + resultMap.get("Exception")); + assertEquals("Couldn't create keyspace. Please make sure all the information is correct.", + resultMap.get("Exception")); + } + + @Test + public void Test3_createTable() throws Exception { + JsonTable jsonTable = new JsonTable(); + Map<String, String> consistencyInfo = new HashMap<>(); + Map<String, String> fields = new HashMap<>(); + fields.put("uuid", "text"); + fields.put("emp_name", "text"); + fields.put("emp_salary", "varint"); + fields.put("PRIMARY KEY", "(emp_name)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("emp_name"); + jsonTable.setTableName(tableName); + jsonTable.setFields(fields); + Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Map<String, Object> resultMap = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + userId, password, jsonTable, keyspaceName, tableName, http); + assertEquals("SUCCESS", resultMap.get("status")); + } + + @Test + public void Test4_insertIntoTable() throws Exception { + JsonInsert jsonInsert = new JsonInsert(); + Map<String, String> consistencyInfo = new HashMap<>(); + Map<String, Object> values = new HashMap<>(); + values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); + values.put("emp_name", "test"); + values.put("emp_salary", 500); + consistencyInfo.put("type", "eventual"); + jsonInsert.setConsistencyInfo(consistencyInfo); + jsonInsert.setKeyspaceName(keyspaceName); + jsonInsert.setTableName(tableName); + jsonInsert.setValues(values); + Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Map<String, Object> resultMap = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, userId, password, jsonInsert, keyspaceName, tableName, http); + assertEquals("Success", resultMap.get("result")); + } + + @Test + public void Test4_insertIntoTable2() throws Exception { + JsonInsert jsonInsert = new JsonInsert(); + Map<String, String> consistencyInfo = new HashMap<>(); + Map<String, Object> values = new HashMap<>(); + values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); + values.put("emp_name", "test1"); + values.put("emp_salary", 1500); + consistencyInfo.put("type", "eventual"); + jsonInsert.setConsistencyInfo(consistencyInfo); + jsonInsert.setKeyspaceName(keyspaceName); + jsonInsert.setTableName(tableName); + jsonInsert.setValues(values); + Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Map<String, Object> resultMap = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, userId, password, jsonInsert, keyspaceName, tableName, http); + assertEquals("Success", resultMap.get("result")); + } + + @Test + public void Test5_updateTable() throws Exception { + JsonUpdate jsonUpdate = new JsonUpdate(); + Map<String, String> consistencyInfo = new HashMap<>(); + MultivaluedMap<String, String> row = new MultivaluedMapImpl(); + Map<String, Object> values = new HashMap<>(); + row.add("emp_name", "test"); + values.put("emp_salary", 2500); + consistencyInfo.put("type", "atomic"); + jsonUpdate.setConsistencyInfo(consistencyInfo); + jsonUpdate.setKeyspaceName(keyspaceName); + jsonUpdate.setTableName(tableName); + jsonUpdate.setValues(values); + Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Mockito.when(info.getQueryParameters()).thenReturn(row); + Map<String, Object> resultMap = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + userId, password, jsonUpdate, keyspaceName, tableName, info, http); + assertEquals("Success", resultMap.get("result")); + } + + @Test + public void Test6_select() throws Exception { + JsonSelect jsonSelect = new JsonSelect(); + Map<String, String> consistencyInfo = new HashMap<>(); + MultivaluedMap<String, String> row = new MultivaluedMapImpl(); + row.add("emp_name", "test"); + consistencyInfo.put("type", "atomic"); + jsonSelect.setConsistencyInfo(consistencyInfo); + Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Mockito.when(info.getQueryParameters()).thenReturn(row); + Map<String, HashMap<String, Object>> resultMap = data.select("1", "1", "1", + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password, keyspaceName, tableName, info, http); + assertEquals("2500", resultMap.get("row 0").get("emp_salary").toString()); + } + + @Test + public void Test6_selectCritical() throws Exception { + JsonInsert jsonInsert = new JsonInsert(); + Map<String, String> consistencyInfo = new HashMap<>(); + MultivaluedMap<String, String> row = new MultivaluedMapImpl(); + row.add("emp_name", "test"); + consistencyInfo.put("type", "atomic"); + jsonInsert.setConsistencyInfo(consistencyInfo); + Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Mockito.when(info.getQueryParameters()).thenReturn(row); + Map<String, HashMap<String, Object>> resultMap = data.selectCritical("1", "1", "1", + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password, jsonInsert, keyspaceName, tableName, + info, http); + assertEquals("2500", resultMap.get("row 0").get("emp_salary").toString()); + } + + @Test + public void Test6_deleteFromTable() throws Exception { + JsonDelete jsonDelete = new JsonDelete(); + Map<String, String> consistencyInfo = new HashMap<>(); + MultivaluedMap<String, String> row = new MultivaluedMapImpl(); + row.add("emp_name", "test1"); + consistencyInfo.put("type", "atomic"); + jsonDelete.setConsistencyInfo(consistencyInfo); + Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Mockito.when(info.getQueryParameters()).thenReturn(row); + Map<String, Object> resultMap = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, userId, password, jsonDelete, keyspaceName, tableName, info, http); + assertEquals("Success", resultMap.get("result")); + } + + @Test + public void Test7_dropTable() throws Exception { + JsonTable jsonTable = new JsonTable(); + Map<String, String> consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonTable.setConsistencyInfo(consistencyInfo); + Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Map<String, Object> resultMap = data.dropTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + userId, password, jsonTable, keyspaceName, tableName, http); + assertEquals("SUCCESS", resultMap.get("status")); + } + + @Test + public void Test8_deleteKeyspace() throws Exception { + JsonKeySpace jsonKeyspace = new JsonKeySpace(); + Map<String, String> consistencyInfo = new HashMap<>(); + Map<String, Object> replicationInfo = new HashMap<>(); + consistencyInfo.put("type", "eventual"); + replicationInfo.put("class", "SimpleStrategy"); + replicationInfo.put("replication_factor", 1); + jsonKeyspace.setConsistencyInfo(consistencyInfo); + jsonKeyspace.setDurabilityOfWrites("true"); + jsonKeyspace.setKeyspaceName("TestApp1"); + jsonKeyspace.setReplicationInfo(replicationInfo); + Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Map<String, Object> resultMap = data.dropKeySpace("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, userId, password, jsonKeyspace, keyspaceName, http); + assertEquals("SUCCESS", resultMap.get("status")); + } + +} |