import static org.junit.Assert.*;
import static org.onap.music.main.MusicCore.mDstoreHandle;
import static org.onap.music.main.MusicCore.mLockHandle;
+
+import org.apache.zookeeper.KeeperException.NoNodeException;
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 PreparedQueryObject preparedQueryObject;
+
+ @Mock
+ private Session session;
@Before
public void setUp() {
}
@Test
- public void testGetMusicLockState() {
+ 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");
}
@Test
- public void testAcquireLockifisMyTurnTrue() {
+ public void testAcquireLockifisMyTurnTrue() throws MusicLockingException {
Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
- Boolean lock = MusicCore.acquireLock("ks1.tn1", "id1");
- assertTrue(lock);
+ ReturnType lock = MusicCore.acquireLock("ks1.tn1", "id1");
+ assertEquals(lock.getResult(), ResultType.SUCCESS);
Mockito.verify(mLockHandle).isMyTurn("id1");
}
@Test
- public void testAcquireLockifisMyTurnFalse() {
+ public void testAcquireLockifisMyTurnFalse() throws MusicLockingException {
Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
- Boolean lock = MusicCore.acquireLock("ks1.ts1", "id1");
- assertFalse(lock);
+ ReturnType lock = MusicCore.acquireLock("ks1.ts1", "id1");
+ assertEquals(lock.getResult(), ResultType.FAILURE);
Mockito.verify(mLockHandle).isMyTurn("id1");
}
@Test
- public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockTrue() {
+ public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockTrue() throws MusicLockingException {
Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
- Boolean lock = MusicCore.acquireLock("ks1.tn1", "id1");
- assertTrue(lock);
+ ReturnType lock = MusicCore.acquireLock("ks1.tn1", "id1");
+ assertEquals(lock.getResult(), ResultType.SUCCESS);
Mockito.verify(mLockHandle).isMyTurn("id1");
}
@Test
- public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandHaveLock() {
+ 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);
- Boolean lock = MusicCore.acquireLock("ks1.tn1.pk1", "id1");
- assertTrue(lock);
+ 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() {
+ 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);
- Boolean lock = MusicCore.acquireLock("ks1.tn1.pk1", "id1");
- assertTrue(lock);
+ 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() throws MusicLockingException {
+ 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() {
+ 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);
}
@Test
- public void testAcquireLockWithLeasewithLockStatusUNLOCKED() {
+ 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);
}
@Test
- public void testAcquireLockWithLeaseIfNotMyTurn() {
+ 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.verify(mLockHandle).getLockState("ks1.tn1.pk1");
}
- /*
- * @Test public void testQuorumGet() { mDstoreHandle = Mockito.mock(MusicDataStore.class); rs =
- * Mockito.mock(ResultSet.class);
- * Mockito.when(mDstoreHandle.executeCriticalGet("qu1")).thenReturn(rs); ResultSet rs1 =
- * MusicCore.quorumGet("qu1"); assertNotNull(rs1);
- * Mockito.verify(mDstoreHandle).executeCriticalGet("qu1");
- *
- * }
- */
+ @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() {
}
@Test
- public void testDestroyLockRef() {
+ public void testDestroyLockRef() throws NoNodeException {
Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1");
MusicCore.destroyLockRef("id1");
Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1");
}
@Test
- public void testreleaseLockwithvoluntaryReleaseTrue() {
+ public void testreleaseLockwithvoluntaryReleaseTrue() throws NoNodeException {
MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1");
MusicLockState musicLockState1 = MusicCore.releaseLock("id1", true);
}
@Test
- public void testreleaseLockwithvoluntaryReleaseFalse() {
+ public void testreleaseLockwithvoluntaryReleaseFalse() throws NoNodeException {
MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1");
MusicLockState musicLockState1 = MusicCore.releaseLock("id1", false);
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());
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 MusicServiceException, MusicQueryException {
+ 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);
}
@Test
- public void testCriticalPutPreparedQuerywithInvalidLockId() {
+ public void testCriticalPutPreparedQuerywithInvalidLockId() throws MusicLockingException {
mDstoreHandle = Mockito.mock(MusicDataStore.class);
preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
}
@Test
- public void testCriticalPutPreparedQuerywithvalidLockIdandTestConditionFalse() {
+ public void testCriticalPutPreparedQuerywithvalidLockIdandTestConditionFalse() throws Exception {
mDstoreHandle = Mockito.mock(MusicDataStore.class);
preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
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);
+ ResultType result = MusicCore.nonKeyRelatedPut(preparedQueryObject, "consistency");
+ assertEquals(ResultType.SUCCESS, result);
Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "consistency");
}
@Test
- public void testAtomicPutPreparedQuery() throws MusicServiceException, MusicQueryException {
+ 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);
}
@Test
- public void testAtomicPutPreparedQuerywithAcquireLockWithLeaseFalse() {
+ public void testAtomicPutPreparedQuerywithAcquireLockWithLeaseFalse() throws MusicLockingException {
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.FAILURE, "Failure");
- Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
- Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
- .thenReturn(musicLockState);
ReturnType returnType =
MusicCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition);
assertEquals(expectedResult.getResult(), returnType.getResult());
- 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(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
}
@Test
- public void testAtomicGetPreparedQuery() throws MusicServiceException, MusicQueryException {
+ 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);
@Test
public void testAtomicGetPreparedQuerywithAcquireLockWithLeaseFalse()
- throws MusicServiceException {
+ 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");
- MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
- Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
- Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
- .thenReturn(musicLockState);
ResultSet rs1 = MusicCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject);
assertNull(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");
-
}
@Test
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);
}
@Test
- public void testcriticalGetPreparedQuery() throws MusicServiceException, MusicQueryException {
+ 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);
}
@Test
- public void testcriticalGetPreparedQuerywithInvalidLockId() throws MusicServiceException {
+ public void testcriticalGetPreparedQuerywithInvalidLockId() throws MusicServiceException, MusicLockingException {
mDstoreHandle = Mockito.mock(MusicDataStore.class);
preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");