diff options
Diffstat (limited to 'src/test/java/org/onap/music/unittests')
13 files changed, 1890 insertions, 2219 deletions
diff --git a/src/test/java/org/onap/music/unittests/MusicDataStoreTest.java b/src/test/java/org/onap/music/unittests/MusicDataStoreTest.java index ab10335e..6b7dc781 100644 --- a/src/test/java/org/onap/music/unittests/MusicDataStoreTest.java +++ b/src/test/java/org/onap/music/unittests/MusicDataStoreTest.java @@ -34,10 +34,10 @@ import org.junit.runners.MethodSorters; import org.mockito.Mock; import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.main.CachingUtil; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.onap.music.authentication.CachingUtil; import org.onap.music.datastore.MusicDataStore; import org.onap.music.datastore.PreparedQueryObject; @@ -67,7 +67,7 @@ public class MusicDataStoreTest { testObject = new PreparedQueryObject(); testObject.appendQueryString(CassandraCQL.dropKeyspace); dataStore.executePut(testObject, "eventual"); - dataStore.close(); + //dataStore.close(); //CachingUtil.resetStatementBank(); } diff --git a/src/test/java/org/onap/music/unittests/MusicUtilTest.java b/src/test/java/org/onap/music/unittests/MusicUtilTest.java index b117c330..9835034b 100644 --- a/src/test/java/org/onap/music/unittests/MusicUtilTest.java +++ b/src/test/java/org/onap/music/unittests/MusicUtilTest.java @@ -31,6 +31,7 @@ import java.util.Map; import java.util.UUID; import org.apache.cassandra.exceptions.PreparedQueryNotFoundException; import org.junit.Test; +import org.onap.music.authentication.CachingUtil; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.exceptions.MusicServiceException; import org.onap.music.main.MusicUtil; @@ -91,7 +92,7 @@ public class MusicUtilTest { @Test public void testGetPropkeys() { - assertEquals(MusicUtil.getPropkeys()[2],"music.ip"); + assertEquals(MusicUtil.getPropkeys()[2],"debug"); } @Test @@ -203,5 +204,15 @@ public class MusicUtilTest { assertTrue(result.contains("name")); assertTrue(result.contains("value")); } + + @Test + public void test_generateUUID() { + //this function shouldn't be in cachingUtil + System.out.println("Testing getUUID"); + String uuid1 = MusicUtil.generateUUID(); + String uuid2 = MusicUtil.generateUUID(); + assertFalse(uuid1==uuid2); + } + } diff --git a/src/test/java/org/onap/music/unittests/TestMusicCore.java b/src/test/java/org/onap/music/unittests/TestMusicCore.java deleted file mode 100644 index 5f9bf8f6..00000000 --- a/src/test/java/org/onap/music/unittests/TestMusicCore.java +++ /dev/null @@ -1,666 +0,0 @@ -/* - * ============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.service.impl.MusicZKCore.mLockHandle; - -import java.util.HashMap; -import java.util.Map; -import org.apache.zookeeper.KeeperException.NoNodeException; -import org.junit.Before; -import org.junit.Ignore; -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.cassandra.MusicLockState; -import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus; -import org.onap.music.lockingservice.zookeeper.MusicLockingService; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; -import org.onap.music.main.ReturnType; -import org.onap.music.service.impl.MusicZKCore; -import org.onap.music.datastore.Condition; -import org.onap.music.datastore.MusicDataStore; -import org.onap.music.datastore.MusicDataStoreHandle; -import org.onap.music.datastore.PreparedQueryObject; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.eelf.logging.format.AppMessages; -import org.onap.music.eelf.logging.format.ErrorSeverity; -import org.onap.music.eelf.logging.format.ErrorTypes; -import com.att.eelf.exception.EELFException; -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Session; - - -public class TestMusicCore { - - - private Condition condition; - - - private ResultSet rs; - - - private PreparedQueryObject preparedQueryObject; - - - private Session session; - - @Mock - MusicZKCore musicZKCore; - - @Before - public void setUp() { - mLockHandle = Mockito.mock(MusicLockingService.class); - musicZKCore = MusicZKCore.getInstance(); - condition=Mockito.mock(Condition.class); - rs =Mockito.mock(ResultSet.class); - preparedQueryObject =Mockito.mock(PreparedQueryObject.class); - session =Mockito.mock(Session.class); - - } - - @Test - public void testCreateLockReferenceforvalidlock() { - Mockito.when(mLockHandle.createLockId("/" + "test")).thenReturn("lock"); - String lockId = musicZKCore.createLockReference("test"); - assertEquals("lock", lockId); - Mockito.verify(mLockHandle).createLockId("/" + "test"); - } - - - @Test - public void testCreateLockReferencefornullname() { - //Mockito.when(mLockHandle.createLockId("/" + "test")).thenReturn("lock"); - String lockId = musicZKCore.createLockReference("x"); //test"); - //System.out.println("cjc exception lockhandle=" + mLockHandle+"lockid="+lockId ); - assertNotEquals("lock", lockId); - //Mockito.verify(mLockHandle).createLockId("/" + "test"); - } - - @Test - public void testIsTableOrKeySpaceLock() { - Boolean result = musicZKCore.isTableOrKeySpaceLock("ks1.tn1"); - assertTrue(result); - } - - @Test - public void testIsTableOrKeySpaceLockwithPrimarykey() { - Boolean result = musicZKCore.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 = MusicZKCore.getMusicLockState("ks1.tb1.pk1"); - assertEquals(musicLockState, mls); - Mockito.verify(mLockHandle).getLockState("ks1.tb1.pk1"); - } - - @Test - public void testAcquireLockifisMyTurnTrue() throws MusicLockingException { - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - ReturnType lock = musicZKCore.acquireLock("ks1.tn1", "id1"); - assertEquals(lock.getResult(), ResultType.SUCCESS); - Mockito.verify(mLockHandle).isMyTurn("id1"); - } - - @Test - public void testAcquireLockifisMyTurnFalse() throws MusicLockingException { - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); - ReturnType lock = musicZKCore.acquireLock("ks1.ts1", "id1"); - assertEquals(lock.getResult(), ResultType.FAILURE); - Mockito.verify(mLockHandle).isMyTurn("id1"); - } - - @Test - public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockTrue() throws MusicLockingException { - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - ReturnType lock = musicZKCore.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 = musicZKCore.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 = musicZKCore.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 = musicZKCore.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 = musicZKCore.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 = musicZKCore.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 = musicZKCore.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 = musicZKCore.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 = musicZKCore.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); - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - rs = Mockito.mock(ResultSet.class); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executeQuorumConsistencyGet(preparedQueryObject)).thenReturn(rs); - ResultSet rs1 = musicZKCore.quorumGet(preparedQueryObject); - assertNotNull(rs1); - } - - @Test - public void testGetLockNameFromId() { - String lockname = MusicZKCore.getLockNameFromId("lockName$id"); - assertEquals("lockName", lockname); - } - - @Test - public void testDestroyLockRef() throws NoNodeException { - Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1"); - musicZKCore.destroyLockRef("id1"); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1"); - } - - @Test - public void testreleaseLockwithvoluntaryReleaseTrue() throws NoNodeException { - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); - Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1"); - MusicLockState musicLockState1 = musicZKCore.releaseLock("id1", true); - assertEquals(musicLockState.getLockStatus(), musicLockState1.getLockStatus()); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1"); - } - - @Test - public void testreleaseLockwithvoluntaryReleaseFalse() throws NoNodeException { - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); - Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1"); - MusicLockState musicLockState1 = musicZKCore.releaseLock("id1", false); - assertEquals(musicLockState.getLockStatus(), musicLockState1.getLockStatus()); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1"); - } - - @Test - public void testDeleteLock() throws MusicLockingException { - Mockito.doNothing().when(mLockHandle).deleteLock("/" + "id1"); - musicZKCore.deleteLock("id1"); - Mockito.verify(mLockHandle).deleteLock("/" + "id1"); - } - - /* - * @Test public void testNonKeyRelatedPut() throws Exception { MusicDataStoreHandle.mDstoreHandle = - * Mockito.mock(MusicDataStore.class); Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut("qu1", - * "consistency")).thenReturn(true); Boolean result = MusicCore.nonKeyRelatedPut("qu1", - * "consistency"); assertTrue(result); Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut("qu1", - * "consistency"); } - */ - - @Test - public void testEventualPutPreparedQuery() throws MusicServiceException, MusicQueryException { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(true); - ReturnType actualResult = musicZKCore.eventualPut(preparedQueryObject); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "eventual"); - } - - @Test - public void testEventualPutPreparedQuerywithResultFalse() - throws MusicServiceException, MusicQueryException { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(false); - ReturnType actualResult = musicZKCore.eventualPut(preparedQueryObject); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "eventual"); - //Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, MusicUtil.EVENTUAL); - } - - @Test - public void testCriticalPutPreparedQuerywithValidLockId() - throws Exception { - MusicDataStoreHandle.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(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true); - ReturnType returnType = musicZKCore.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(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "critical"); - } - - @Test - public void testCriticalPutPreparedQuerywithInvalidLockId() throws MusicLockingException { - MusicDataStoreHandle.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 = musicZKCore.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 { - MusicDataStoreHandle.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 = musicZKCore.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 { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "consistency")).thenReturn(true); - ResultType result = musicZKCore.nonKeyRelatedPut(preparedQueryObject, "consistency"); - assertEquals(ResultType.SUCCESS, result); - Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "consistency"); - } - - @Test - public void testAtomicPutPreparedQuery() throws Exception { - MusicDataStoreHandle.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(MusicDataStoreHandle.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(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true); - ReturnType returnType = - musicZKCore.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(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "critical"); - } - - @Test - public void testAtomicPutPreparedQuerywithAcquireLockWithLeaseFalse() throws MusicLockingException { - MusicDataStoreHandle.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 = - musicZKCore.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 { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - rs = Mockito.mock(ResultSet.class); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.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(MusicDataStoreHandle.mDstoreHandle.executeQuorumConsistencyGet(preparedQueryObject)).thenReturn(rs); - ResultSet rs1 = musicZKCore.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(MusicDataStoreHandle.mDstoreHandle).executeQuorumConsistencyGet(preparedQueryObject); - } - - @Test - public void testAtomicGetPreparedQuerywithAcquireLockWithLeaseFalse() - throws MusicServiceException, MusicLockingException { - MusicDataStoreHandle.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 = musicZKCore.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 { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - rs = Mockito.mock(ResultSet.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executeOneConsistencyGet(preparedQueryObject)).thenReturn(rs); - ResultSet rs1 = musicZKCore.get(preparedQueryObject); - assertNotNull(rs1); - Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executeOneConsistencyGet(preparedQueryObject); - - } - - @Test - public void testcriticalGetPreparedQuery() throws MusicServiceException, MusicQueryException, MusicLockingException { - MusicDataStoreHandle.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(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executeQuorumConsistencyGet(preparedQueryObject)).thenReturn(rs); - ResultSet rs1 = musicZKCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1"); - assertNotNull(rs1); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()) - .getLockState("ks1" + "." + "tn1" + "." + "pk1"); - Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executeQuorumConsistencyGet(preparedQueryObject); - } - - @Test - public void testcriticalGetPreparedQuerywithInvalidLockId() throws MusicServiceException, MusicLockingException { - MusicDataStoreHandle.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 = musicZKCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1"); - assertNull(rs1); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()) - .getLockState("ks1" + "." + "tn1" + "." + "pk1"); - } - - @Test - public void testAtomicGetPreparedQuerywithDeleteLockWithLeaseFalse() - throws MusicServiceException, MusicLockingException { - MusicDataStoreHandle.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 = musicZKCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject); - assertNull(rs1); - Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); - Mockito.verify(mLockHandle).isMyTurn("id1"); - } - - @Test - public void testCondition() throws Exception { - //Condition conClass = Mockito.mock(Condition.class); - // boolean ret=true; - //Mockito.when(conClass.testCondition().thenReturn(ret); - Map<String, Object> conditionsx=null; - PreparedQueryObject selectQueryForTheRowx=null; - try { - Condition con = new Condition(conditionsx,selectQueryForTheRowx); - assertTrue(con.testCondition()); - } catch (Exception e) { - assertFalse(false); - } - } - //getLockingServiceHandl - - @Ignore - @Test(expected = MusicLockingException.class) //("Failed to aquire Locl store handle " + e)) - public void testgetLockingServiceHandle() throws Exception { - // MusicLockingService mLockHandlex = Mockito.mock(MusicLockingService.class); - //MusicLockingService mLockHandlea = mLockHandle; - //mLockHandle=null; - System.out.println("cjc 0 locking test n"); - // Mockito.when(MusicCore.getLockingServiceHandle()).thenReturn(mLockHandle); - //mLockHandle=null; - //System.out.println("cjc 0-1 locking test n"); - MusicLockingService mLockHandlea = mLockHandle; - mLockHandle=null; - - MusicLockingService mLockHandley=null; //MusicCore.getLockingServiceHandle(); - Mockito.when(MusicZKCore.getLockingServiceHandle()).thenReturn(mLockHandley); - System.out.println("cjc locking test n"); - mLockHandle=mLockHandlea; - assertTrue(true); - - } - //add mocking - @Ignore - @Test - public void testGetDSHandleIp() throws MusicServiceException, MusicQueryException { - // rs = Mockito.mock(ResultSet.class); - // session = Mockito.mock(Session.class); - //Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - //Mockito.when(MusicDataStoreHandle.mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs); - - //MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - //MusicUtil mMusicUtil=Mockito.mock(MusicUtil.class); - System.out.println("cjc 0 getDsHandleIP"); - Mockito.when(MusicDataStoreHandle.getDSHandle("1.127.0.1")).thenReturn(MusicDataStoreHandle.mDstoreHandle); - System.out.println("cjc localhost"); - // Mockito.when(mMusicUtil.getMyCassaHost().equals("localhost")).thenReturn(null); - System.out.println("cjc 1 localhost IP"); - // MusicDataStoreHandle.mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost()); - // } else { - // MusicDataStoreHandle.mDstoreHandle = new MusicDataStore(); - // } - assertTrue(true); - } - - @Ignore - @Test - public void testPureZkCreate() { - try { - MusicZKCore.pureZkCreate("local"); - } catch(NullPointerException e) { - System.out.println("cjc zkcreate null pointwer exception:"+ e); - } - } - - @Ignore - @Test - public void testPureZkRead() { //String nodeName) { - byte[] data = MusicZKCore.pureZkRead("localhost"); - } - - //need fixing - @Ignore - @Test - public void testPureZkWrite() { //String nodeName, byte[] data) { - /* - long start = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Performing zookeeper write to " + nodeName); - try { - getLockingServiceHandle().getzkLockHandle().setNodeData(nodeName, data); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - logger.info(EELFLoggerDelegate.applicationLogger,"Performed zookeeper write to " + nodeName); - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the actual zk put:" + (end - start) + " ms"); - */ - - // MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - // rs = Mockito.mock(ResultSet.class); - // session = Mockito.mock(Session.class); - //Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - - byte[] data= "Testing Zoo Keeper".getBytes(); - MusicZKCore.pureZkWrite("1.127.0.1", data); - // assertNotNull(rs1); - } - - @Test - public void testWhoseTurnIsIt() { //(String lockName) { - - /* - try { - return getLockingServiceHandle().whoseTurnIsIt("/" + lockName) + ""; - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKINGERROR+lockName ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - return null; - */ - - String lockName="xxx"; - if (musicZKCore.whoseTurnIsIt(lockName) != null) assertTrue(true); - - - - } - - @Test - public void testMarshallResults() { - Map<String, HashMap<String, Object>> ret=null; - //ResultSet results =null; - rs = Mockito.mock(ResultSet.class); - try { - ret= MusicDataStoreHandle.marshallResults(rs); - - } catch( Exception e ) { - - } - - if (ret != null) assertTrue(true); - } - -} diff --git a/src/test/java/org/onap/music/unittests/TestMusicCoreIntegration.java b/src/test/java/org/onap/music/unittests/TestMusicCoreIntegration.java deleted file mode 100644 index f7a22d64..00000000 --- a/src/test/java/org/onap/music/unittests/TestMusicCoreIntegration.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * ============LICENSE_START========================================== org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * Modifications Copyright (c) 2019 IBM - * =================================================================== - * 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.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import java.io.File; -import java.util.List; -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.runners.MethodSorters; -import org.onap.music.datastore.MusicDataStoreHandle; -import org.onap.music.datastore.PreparedQueryObject; -import org.onap.music.exceptions.MusicQueryException; -import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.lockingservice.cassandra.MusicLockState; -import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus; -import org.onap.music.lockingservice.zookeeper.MusicLockingService; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; -import org.onap.music.main.ReturnType; -import org.onap.music.service.impl.MusicZKCore; - -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; - -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class TestMusicCoreIntegration { - - static TestingServer zkServer; - static PreparedQueryObject testObject; - static String lockId = null; - static String lockName = "ks1.tb1.pk1"; - static MusicZKCore musicZkCore ; - - @BeforeClass - public static void init() throws Exception { - try { - MusicDataStoreHandle.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra(); - musicZkCore = MusicZKCore.getInstance(); - zkServer = new TestingServer(2181, new File("/tmp/zk")); - MusicZKCore.mLockHandle = new MusicLockingService(); - } catch (Exception e) { - e.printStackTrace(); - } - System.out.println("####Port:" + zkServer.getPort()); - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - System.out.println("After class"); - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.dropKeyspace); - musicZkCore.eventualPut(testObject); - musicZkCore.deleteLock(lockName); - MusicDataStoreHandle.mDstoreHandle.close(); - MusicZKCore.mLockHandle.getzkLockHandle().close(); - MusicZKCore.mLockHandle.close(); - zkServer.stop(); - - } - - @Test - public void Test1_SetUp() throws MusicServiceException, MusicQueryException { - MusicZKCore.mLockHandle = new MusicLockingService(); - ResultType result = ResultType.FAILURE; - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createKeySpace); - musicZkCore.eventualPut(testObject); - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createTableEmployees); - result = musicZkCore.nonKeyRelatedPut(testObject, MusicUtil.EVENTUAL); - assertEquals(ResultType.SUCCESS, result); - } - - @Test - public void Test2_atomicPut() throws Exception { - testObject = new PreparedQueryObject(); - testObject = CassandraCQL.setPreparedInsertQueryObject1(); - ReturnType returnType = musicZkCore.atomicPut("testCassa", "employees", "Mr Test one", - testObject, null); - assertEquals(ResultType.SUCCESS, returnType.getResult()); - } - - @Test - public void Test3_atomicPutWithDeleteLock() throws Exception { - testObject = new PreparedQueryObject(); - testObject = CassandraCQL.setPreparedInsertQueryObject2(); - ReturnType returnType = musicZkCore.atomicPutWithDeleteLock("testCassa", "employees", - "Mr Test two", testObject, null); - assertEquals(ResultType.SUCCESS, returnType.getResult()); - } - - @Test - public void Test4_atomicGetWithDeleteLock() throws Exception { - testObject = new PreparedQueryObject(); - testObject = CassandraCQL.setPreparedGetQuery(); - ResultSet resultSet = musicZkCore.atomicGetWithDeleteLock("testCassa", "employees", - "Mr Test one", testObject); - List<Row> rows = resultSet.all(); - assertEquals(1, rows.size()); - } - - @Test - public void Test5_atomicGet() throws Exception { - testObject = new PreparedQueryObject(); - testObject = CassandraCQL.setPreparedGetQuery(); - ResultSet resultSet = - musicZkCore.atomicGet("testCassa", "employees", "Mr Test two", testObject); - List<Row> rows = resultSet.all(); - assertEquals(1, rows.size()); - } - - @Test - public void Test6_createLockReference() throws Exception { - lockId = musicZkCore.createLockReference(lockName); - assertNotNull(lockId); - } - - @Test - public void Test7_acquireLockwithLease() throws Exception { - ReturnType lockLeaseStatus = musicZkCore.acquireLockWithLease(lockName, lockId, 1000); - assertEquals(ResultType.SUCCESS, lockLeaseStatus.getResult()); - } - - @Test - public void Test8_acquireLock() throws Exception { - ReturnType lockStatus = musicZkCore.acquireLock(lockName, lockId); - assertEquals(ResultType.SUCCESS, lockStatus.getResult()); - } - - @Test - public void Test9_release() throws Exception { - MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); - MusicLockState musicLockState1 = new MusicLockState(LockStatus.UNLOCKED, "id1"); - musicZkCore.whoseTurnIsIt(lockName); - MusicLockState mls = MusicZKCore.getMusicLockState(lockName); - boolean voluntaryRelease = true; - MusicLockState mls1 = musicZkCore.releaseLock(lockId, voluntaryRelease); - assertEquals(musicLockState.getLockStatus(), mls.getLockStatus()); - assertEquals(musicLockState1.getLockStatus(), mls1.getLockStatus()); - } - - @Test - public void Test10_create() { - MusicZKCore.pureZkCreate("/nodeName"); - } - - @Test - public void Test11_write() { - MusicZKCore.pureZkWrite("nodeName", "I'm Test".getBytes()); - } - - @Test - public void Test12_read() { - byte[] data = MusicZKCore.pureZkRead("nodeName"); - String data1 = new String(data); - assertEquals("I'm Test", data1); - } - @Test - public void Test13_ParameterizedConstructorCall() throws MusicServiceException, MusicQueryException { - MusicZKCore.mLockHandle = new MusicLockingService("localhost"); - ResultType result = ResultType.FAILURE; - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createKeySpace); - musicZkCore.eventualPut(testObject); - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createTableEmployees); - result = musicZkCore.nonKeyRelatedPut(testObject, MusicUtil.EVENTUAL); - assertEquals(ResultType.SUCCESS, result); - } - -} diff --git a/src/test/java/org/onap/music/unittests/TestRestMusicQAPI.java b/src/test/java/org/onap/music/unittests/TestRestMusicQAPI.java index f44f7171..4594ba2c 100644 --- a/src/test/java/org/onap/music/unittests/TestRestMusicQAPI.java +++ b/src/test/java/org/onap/music/unittests/TestRestMusicQAPI.java @@ -35,7 +35,6 @@ import javax.servlet.http.HttpServletResponse; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; -import org.apache.curator.test.TestingServer; import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -85,7 +84,6 @@ public class TestRestMusicQAPI { RestMusicLocksAPI lock = new RestMusicLocksAPI(); RestMusicQAPI qData = new RestMusicQAPI(); static PreparedQueryObject testObject; - static TestingServer zkServer; @Mock static HttpServletResponse http; @@ -214,10 +212,8 @@ public class TestRestMusicQAPI { testObject = new PreparedQueryObject(); testObject.appendQueryString("DROP KEYSPACE IF EXISTS admin"); MusicCore.eventualPut(testObject); - if (MusicDataStoreHandle.mDstoreHandle!=null) - MusicDataStoreHandle.mDstoreHandle.close(); - if (zkServer!=null) - zkServer.stop(); + if (MusicDataStoreHandle.mDstoreHandle!=null) {} + //MusicDataStoreHandle.mDstoreHandle.close(); } diff --git a/src/test/java/org/onap/music/unittests/TestAPISuite.java b/src/test/java/org/onap/music/unittests/TestsUsingCassandra.java index 65c8de6a..ef2867d8 100644 --- a/src/test/java/org/onap/music/unittests/TestAPISuite.java +++ b/src/test/java/org/onap/music/unittests/TestsUsingCassandra.java @@ -24,7 +24,6 @@ package org.onap.music.unittests; import java.util.List; import java.util.UUID; - import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.runner.RunWith; @@ -44,8 +43,8 @@ import com.sun.jersey.core.util.Base64; @RunWith(Suite.class) @SuiteClasses({ TstRestMusicDataAPI.class, TstRestMusicLockAPI.class, TstRestMusicAdminAPI.class, - TstRestMusicConditionalAPI.class}) -public class TestAPISuite { + TstRestMusicConditionalAPI.class, TstCachingUtil.class}) +public class TestsUsingCassandra { static String appName = "TestApp"; static String userId = "TestUser"; @@ -60,6 +59,7 @@ public class TestAPISuite { static String tableName = "employees"; static String xLatestVersion = "X-latestVersion"; static String onboardUUID = null; + static String aid = "abc66ccc-d857-4e90-b1e5-df98a3d40ce6"; @BeforeClass public static void beforeClass() throws Exception { @@ -91,7 +91,7 @@ public class TestAPISuite { + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)"); testObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), - MusicUtil.DEFAULTKEYSPACENAME)); + keyspaceName)); testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True")); testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), BCrypt.hashpw(password, BCrypt.gensalt()))); @@ -107,6 +107,7 @@ public class TestAPISuite { List<Row> rows = rs.all(); if (rows.size() > 0) { System.out.println("#######UUID is:" + rows.get(0).getUUID("uuid")); + onboardUUID = rows.get(0).getUUID("uuid").toString(); } } } diff --git a/src/test/java/org/onap/music/unittests/TstCachingUtil.java b/src/test/java/org/onap/music/unittests/TstCachingUtil.java new file mode 100644 index 00000000..ea47b7c1 --- /dev/null +++ b/src/test/java/org/onap/music/unittests/TstCachingUtil.java @@ -0,0 +1,167 @@ +/* + * ============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.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import javax.ws.rs.core.Response; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mindrot.jbcrypt.BCrypt; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.music.authentication.CachingUtil; +import org.onap.music.datastore.MusicDataStoreHandle; +import org.onap.music.datastore.PreparedQueryObject; +import org.onap.music.datastore.jsonobjects.JsonInsert; +import org.onap.music.datastore.jsonobjects.JsonLeasedLock; +import org.onap.music.datastore.jsonobjects.JsonTable; +import org.onap.music.exceptions.MusicServiceException; +import org.onap.music.lockingservice.cassandra.CassaLockStore; +import org.onap.music.main.MusicCore; +import org.onap.music.main.MusicUtil; +import org.onap.music.rest.RestMusicDataAPI; +import org.onap.music.rest.RestMusicLocksAPI; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.sun.jersey.core.util.Base64; + +@RunWith(MockitoJUnitRunner.class) +public class TstCachingUtil { + + RestMusicLocksAPI lock = new RestMusicLocksAPI(); + RestMusicDataAPI data = new RestMusicDataAPI(); + static PreparedQueryObject testObject; + + static String appName = TestsUsingCassandra.appName; + static String userId = TestsUsingCassandra.userId; + static String password = TestsUsingCassandra.password; + static String authData = TestsUsingCassandra.authData; + static String authorization = TestsUsingCassandra.authorization; + static boolean isAAF = TestsUsingCassandra.isAAF; + static UUID uuid = TestsUsingCassandra.uuid; + static String keyspaceName = TestsUsingCassandra.keyspaceName; + static String tableName = TestsUsingCassandra.tableName; + static String onboardUUID = null; + + @BeforeClass + public static void init() throws Exception { + System.out.println("Testing CachingUtil class"); + try { + + } catch (Exception e) { + e.printStackTrace(); + throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage()); + } + } + + @After + public void afterEachTest( ) throws MusicServiceException { + + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + testObject = new PreparedQueryObject(); + testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName); + MusicCore.eventualPut(testObject); + } + + @Test + public void test_verifyOnboard() throws Exception { + System.out.println("Testing onboarding of app"); + CachingUtil cache = new CachingUtil(); + cache.initializeAafCache(); + Map<String, Object> authMap = CachingUtil.verifyOnboarding(appName, userId, password); + assertEquals(0, authMap.size()); + } + + @Test + public void test_authenticateAIDUser() throws Exception { + System.out.println("Testing authentication of AID user"); + createKeyspace(); + CachingUtil cache = new CachingUtil(); + cache.initializeAafCache(); + Map<String, Object> authMap = CachingUtil.authenticateAIDUser(appName, + userId, password, keyspaceName); + System.out.println("authMap is: " + authMap); + assertEquals(0, authMap.size()); + } + + @Test + public void test_getAppName() throws MusicServiceException { + System.out.println("Testing getAppName"); + CachingUtil cache = new CachingUtil(); + cache.initializeAafCache(); + assertEquals(appName, CachingUtil.getAppName(keyspaceName)); + } + + @Test + public void test_getUUIDFromCache() throws MusicServiceException { + System.out.println("Testing getUUID"); + CachingUtil cache = new CachingUtil(); + cache.initializeAafCache(); + assertEquals(uuid.toString(), CachingUtil.getUuidFromMusicCache(keyspaceName)); + } + + @Test + public void test_isAAFApplcation() throws MusicServiceException { + System.out.println("Testing to see if cache gets correct isAAF info"); + CachingUtil cache = new CachingUtil(); + cache.initializeAafCache(); + assertEquals(isAAF, Boolean.valueOf(CachingUtil.isAAFApplication(appName))); + } + + private static void createKeyspace() throws Exception { + //shouldn't really be doing this here, but create keyspace is currently turned off + PreparedQueryObject query = new PreparedQueryObject(); + query.appendQueryString(CassandraCQL.createKeySpace); + MusicCore.eventualPut(query); + + boolean isAAF = false; + String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt()); + query = new PreparedQueryObject(); + query.appendQueryString( + "INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " + + "password, username, is_aaf) values (?,?,?,?,?,?,?)"); + query.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); + query.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName)); + query.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); + query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True")); + query.addValue(MusicUtil.convertToActualDataType(DataType.text(), hashedpwd)); + query.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); + query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF)); + CachingUtil.updateMusicCache(keyspaceName, appName); + CachingUtil.updateMusicValidateCache(appName, userId, hashedpwd); + MusicCore.eventualPut(query); + } + +} diff --git a/src/test/java/org/onap/music/unittests/TstRestMusicAdminAPI.java b/src/test/java/org/onap/music/unittests/TstRestMusicAdminAPI.java index b00c4fcc..02b7f3a4 100644 --- a/src/test/java/org/onap/music/unittests/TstRestMusicAdminAPI.java +++ b/src/test/java/org/onap/music/unittests/TstRestMusicAdminAPI.java @@ -1,16 +1,20 @@ /* - * ============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 + * ============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 + * 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. + * 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============================================= * ==================================================================== @@ -19,66 +23,37 @@ package org.onap.music.unittests; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; - -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.Response; -import javax.ws.rs.core.UriInfo; -import org.apache.curator.test.TestingServer; + +import org.junit.After; import org.junit.AfterClass; +import org.junit.Before; import org.junit.BeforeClass; -import org.junit.FixMethodOrder; import org.junit.Ignore; import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; -import org.mindrot.jbcrypt.BCrypt; -import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.onap.music.authentication.MusicAuthentication; -import org.onap.music.conductor.conditionals.JsonConditional; -import org.onap.music.conductor.conditionals.RestMusicConditionalAPI; -import org.onap.music.datastore.MusicDataStoreHandle; +import org.mockito.internal.util.reflection.FieldSetter; +import org.onap.music.authentication.MusicAAFAuthentication; 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.JsonLeasedLock; import org.onap.music.datastore.jsonobjects.JsonOnboard; -import org.onap.music.datastore.jsonobjects.JsonTable; import org.onap.music.main.MusicCore; -import org.onap.music.main.MusicUtil; import org.onap.music.rest.RestMusicAdminAPI; -import org.onap.music.rest.RestMusicHealthCheckAPI; - -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; import com.sun.jersey.core.util.Base64; public class TstRestMusicAdminAPI { RestMusicAdminAPI admin = new RestMusicAdminAPI(); static PreparedQueryObject testObject; - - @Mock - HttpServletResponse http; - + @Mock - UriInfo info; + MusicAAFAuthentication authMock; static String appName = "TestApp"; static String userId = "TestUser"; @@ -92,7 +67,7 @@ public class TstRestMusicAdminAPI { static String authorization = new String(Base64.encode(authData.getBytes())); static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes())); static String adminAuthorization = new String(Base64.encode(adminAuthData.getBytes())); - static String worngAdminAuthorization = new String(Base64.encode(wrongAdminAuthData.getBytes())); + static String wrongAdminAuthorization = new String(Base64.encode(wrongAdminAuthData.getBytes())); static boolean isAAF = false; static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6"); @@ -100,7 +75,7 @@ public class TstRestMusicAdminAPI { static String tableName = "employees"; static String tableNameConditional = "Conductor"; static String xLatestVersion = "X-latestVersion"; - static String onboardUUID = null; + static String onboardUUID = TestsUsingCassandra.onboardUUID; static String lockId = null; static String lockName = "testCassa.employees.sample3"; @@ -109,256 +84,366 @@ public class TstRestMusicAdminAPI { System.out.println("Testing RestMusicAdmin class"); //PowerMockito.mockStatic(MusicAuthentication.class); try { - MusicDataStoreHandle.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra(); - createAdminTable(); + //MusicDataStoreHandle.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra(); } catch (Exception e) { e.printStackTrace(); throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage()); } } + @Before + public void beforeEach() throws NoSuchFieldException { + authenticateAdminTrue(); + } + + @After + public void afterEach() { + testObject = new PreparedQueryObject(); + testObject.appendQueryString("DELETE * FROM admin.keyspace_master;"); + MusicCore.eventualPut(testObject); + } + @AfterClass public static void tearDownAfterClass() throws Exception { testObject = new PreparedQueryObject(); testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName); MusicCore.eventualPut(testObject); - } - - @Ignore - @Test - public void test6_onboard() throws Exception { - System.out.println("Testing application onboarding"); - - JsonOnboard jsonOnboard = new JsonOnboard(); - jsonOnboard.setAppname("TestApp2"); - jsonOnboard.setIsAAF("false"); jsonOnboard.setUserId("TestUser2"); - jsonOnboard.setPassword("TestPassword2"); - - Response response = admin.onboardAppWithMusic(jsonOnboard,adminAuthorization); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - /*resultMap.containsKey("success"); onboardUUID = - resultMap.get("Generated AID").toString(); - assertEquals("Your application TestApp2 has been onboarded with MUSIC.", - resultMap.get("Success")); */ - assertEquals(200, response.getStatus()); } @Test - public void test6_onboardCantReachAAF() throws Exception { - System.out.println("Testing application onboarding without reaching aaf"); + public void test6_onboard() throws Exception { + System.out.println("Testing application onboarding"); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setAppname("TestApp2"); jsonOnboard.setIsAAF("false"); jsonOnboard.setUserId("TestUser2"); jsonOnboard.setPassword("TestPassword2"); Response response = admin.onboardAppWithMusic(jsonOnboard,adminAuthorization); + + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } + + @Test + public void test6_onboard_wrongCredentials() throws Exception { + System.out.println("Testing application onboarding wrong credentials"); + JsonOnboard jsonOnboard = new JsonOnboard(); + jsonOnboard.setAppname("TestApp2"); + jsonOnboard.setIsAAF("false"); jsonOnboard.setUserId("TestUser2"); + jsonOnboard.setPassword("TestPassword2"); + + Response response = admin.onboardAppWithMusic(jsonOnboard,wrongAdminAuthorization); System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - //TODO FIX when we can authenticate assertEquals(401, response.getStatus()); } - @Ignore @Test public void test6_onboard_duplicate() throws Exception { + System.out.println("Testing a duplicate onboarding call"); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setAppname("TestApp2"); jsonOnboard.setIsAAF("false"); jsonOnboard.setUserId("TestUser2"); jsonOnboard.setPassword("TestPassword2"); Response response = admin.onboardAppWithMusic(jsonOnboard,adminAuthorization); - assertEquals(204, response.getStatus()); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); } // Missing appname - @Ignore @Test - public void test6_onboard1() throws Exception { + public void test6_onboard_noAppName() throws Exception { + System.out.println("Testing onboard missing app name"); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setIsAAF("false"); jsonOnboard.setUserId("TestUser2"); jsonOnboard.setPassword("TestPassword2"); - Map<String, Object> resultMap = (Map<String, Object>) admin.onboardAppWithMusic(jsonOnboard,adminAuthorization).getEntity(); -// assertTrue(resultMap.containsKey("error")); - //System.out.println("--->" + resultMap.toString()); - //assertEquals("Unauthorized: Please check the request parameters. Some of the required values appName(ns), userId, password, isAAF are missing.", resultMap.get("Exception")); + Response response = admin.onboardAppWithMusic(jsonOnboard,adminAuthorization); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); } @Test - public void test7_onboardSearch() throws Exception { - System.out.println("Testing application onboarding search w/o reaching aaf"); + public void test7_onboardSearch_notOnboarded() throws Exception { + System.out.println("Testing application onboarding search for app that isn't onboarded"); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setAppname("TestApp2"); jsonOnboard.setIsAAF("false"); jsonOnboard.setAid(onboardUUID); Response response = admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization); System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - //TODO FIX when we can authenticate - //should be 401 error - assertEquals(204, response.getStatus()); + //Application is not onboarded + assertEquals(400, response.getStatus()); } - - // Missing appname - @Ignore + @Test - public void test7_onboardSearch1() throws Exception { + public void test7_onboardSearch() throws Exception { + System.out.println("Testing application onboarding search no matching app"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); + jsonOnboard.setAppname("TestApp2"); jsonOnboard.setIsAAF("false"); jsonOnboard.setAid(onboardUUID); - Map<String, Object> resultMap = (Map<String, Object>) admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization).getEntity(); - System.out.println("--->" + resultMap.toString()); - resultMap.containsKey("success"); - assertEquals(null, resultMap.get(onboardUUID)); + + Response response = admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); } - @Ignore @Test - public void test7_onboardSearch_empty() throws Exception { + public void test7_onboardSearch_wrongCredentials() throws Exception { + System.out.println("Testing application onboarding search w/ wrong credentials"); + JsonOnboard jsonOnboard = new JsonOnboard(); - Response response = admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization); - // assertEquals(400, response.getStatus()); + jsonOnboard.setAppname("TestApp2"); + jsonOnboard.setIsAAF("false"); + jsonOnboard.setAid(onboardUUID); + Response response = admin.getOnboardedInfoSearch(jsonOnboard,wrongAdminAuthorization); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(401, response.getStatus()); } - @Ignore + // Missing appname @Test - public void test7_onboardSearch_invalidAid() throws Exception { + public void test7_onboardSearch_noAppName() throws Exception { + System.out.println("Testing application onboarding search w/o appname"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); - jsonOnboard.setAppname("TestApp2"); jsonOnboard.setIsAAF("false"); - jsonOnboard.setAid("abc66ccc-d857-4e90-b1e5-df98a3d40ce6"); + jsonOnboard.setAid(onboardUUID); Response response = admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization); - // assertEquals(400, response.getStatus()); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); + } + + @Test + public void test7_onboardSearch_empty() throws Exception { + System.out.println("Testing onboard search no app information"); + + JsonOnboard jsonOnboard = new JsonOnboard(); + Response response = admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); } - @Ignore @Test public void test8_onboardUpdate() throws Exception { + System.out.println("Testing application onboarding update"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setIsAAF("false"); jsonOnboard.setUserId("TestUser3"); jsonOnboard.setPassword("TestPassword3"); jsonOnboard.setAid(onboardUUID); - Map<String, Object> resultMap = (Map<String, Object>) admin.updateOnboardApp(jsonOnboard,adminAuthorization).getEntity(); - System.out.println("--->" + resultMap.toString()); - resultMap.containsKey("success"); - assertNotNull(resultMap); + Response response = admin.updateOnboardApp(jsonOnboard,adminAuthorization); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); } + @Ignore //not working correctly @Test - public void test8_onboardUpdateNoAAF() throws Exception { - System.out.println("Testing update application onboarding search w/o reaching aaf"); + public void test8_onboardUpdate_withAppName() throws Exception { + System.out.println("Testing application onboarding update w appname"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setIsAAF("false"); jsonOnboard.setUserId("TestUser3"); jsonOnboard.setPassword("TestPassword3"); + jsonOnboard.setAppname("TestApp2"); jsonOnboard.setAid(onboardUUID); Response response = admin.updateOnboardApp(jsonOnboard,adminAuthorization); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - //Should be 401 error - assertEquals(204, response.getStatus()); - } - // Aid null - @Ignore + assertEquals(200, response.getStatus()); + } + @Test - public void test8_onboardUpdate1() throws Exception { + public void test8_onboardUpdate_noUUID() throws Exception { + System.out.println("Testing application onboarding update null uuid"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setIsAAF("false"); jsonOnboard.setUserId("TestUser3"); jsonOnboard.setPassword("TestPassword3"); - Map<String, Object> resultMap = (Map<String, Object>) admin.updateOnboardApp(jsonOnboard,adminAuthorization).getEntity(); - System.out.println("--->" + resultMap.toString()); - assertNotNull(resultMap); - } + jsonOnboard.setAid(null); + Response response = admin.updateOnboardApp(jsonOnboard,adminAuthorization); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // Appname not null - @Ignore + assertEquals(400, response.getStatus()); + } + @Test - public void test8_onboardUpdate2() throws Exception { + public void test8_onboardUpdate_wrongCredentialsNoAAF() throws Exception { + System.out.println("Testing update application onboarding search w/ wrong credentials"); JsonOnboard jsonOnboard = new JsonOnboard(); - jsonOnboard.setAppname("TestApp2"); jsonOnboard.setIsAAF("false"); jsonOnboard.setUserId("TestUser3"); jsonOnboard.setPassword("TestPassword3"); jsonOnboard.setAid(onboardUUID); - Map<String, Object> resultMap = (Map<String, Object>) admin.updateOnboardApp(jsonOnboard,adminAuthorization).getEntity(); - assertNotNull(resultMap); + Response response = admin.updateOnboardApp(jsonOnboard,wrongAdminAuthorization); + + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); } // All null - @Ignore @Test - public void test8_onboardUpdate3() throws Exception { + public void test8_onboardUpdate_noAppInfo() throws Exception { + System.out.println("Testing update application onboarding update no app information"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setAid(onboardUUID); - Map<String, Object> resultMap = (Map<String, Object>) admin.updateOnboardApp(jsonOnboard,adminAuthorization).getEntity(); - assertNotNull(resultMap); + Response response = admin.updateOnboardApp(jsonOnboard,adminAuthorization); + + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); } @Test public void test9_onboardDelete() throws Exception { + System.out.println("Testing update application onboarding delete"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setAppname("TestApp2"); jsonOnboard.setAid(onboardUUID); - Map<String, Object> resultMap = (Map<String, Object>) admin.deleteOnboardApp(jsonOnboard,adminAuthorization).getEntity(); - resultMap.containsKey("success"); - assertNotNull(resultMap); + Response response = admin.deleteOnboardApp(jsonOnboard,adminAuthorization); + + //only 1 app matches keyspace + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); } + @Ignore //not working as expected @Test - public void test9_onboardDeleteNoAAF() throws Exception { - System.out.println("Testing onboard delete without aaf"); + public void test9_onboardDelete_noAID() throws Exception { + System.out.println("Testing update application onboarding delete no AID"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setAppname("TestApp2"); - jsonOnboard.setAid(onboardUUID); + jsonOnboard.setAid(null); Response response = admin.deleteOnboardApp(jsonOnboard,adminAuthorization); + //only 1 app matches name System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - //Should be 401 error - assertEquals(204, response.getStatus()); + assertEquals(200, response.getStatus()); } - - @Ignore + + @Test + public void test9_onboardDelete_noAIDManyMatch() throws Exception { + System.out.println("Testing update application onboarding delete no AID many apps in namespace"); + onboardApp(); + onboardApp(); + + JsonOnboard jsonOnboard = new JsonOnboard(); + jsonOnboard.setAppname("TestApp2"); + jsonOnboard.setAid(null); + Response response = admin.deleteOnboardApp(jsonOnboard,adminAuthorization); + + //multiple apps matches name + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + + @Test + public void test9_onboardDelete_noAID_noApp() throws Exception { + System.out.println("Testing update application onboarding delete no AID, app not onboarded"); + + JsonOnboard jsonOnboard = new JsonOnboard(); + jsonOnboard.setAppname("TestApp2"); + jsonOnboard.setAid(null); + Response response = admin.deleteOnboardApp(jsonOnboard,adminAuthorization); + + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + @Test - public void test9_onboardDelete1() throws Exception { + public void test9_onboardDelete_noAppToDelete() throws Exception { + System.out.println("Testing update application onboarding delete no app information"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); - Map<String, Object> resultMap = (Map<String, Object>) admin.deleteOnboardApp(jsonOnboard,adminAuthorization).getEntity(); - assertNotNull(resultMap); + jsonOnboard.setAppname(null); + jsonOnboard.setAid(null); + Response response = admin.deleteOnboardApp(jsonOnboard,adminAuthorization); + + //only 1 app matches keyspace + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); } + @Test + public void test9_onboardDelete_wrongCredentials() throws Exception { + System.out.println("Testing onboard delete with wrong credentials"); + JsonOnboard jsonOnboard = new JsonOnboard(); + jsonOnboard.setAppname("TestApp2"); + jsonOnboard.setAid(onboardUUID); + Response response = admin.deleteOnboardApp(jsonOnboard,wrongAdminAuthorization); + + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } - private static void createAdminTable() throws Exception { - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createAdminKeyspace); - MusicCore.eventualPut(testObject); - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createAdminTable); - 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(), BCrypt.hashpw(password, BCrypt.gensalt()))); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); - 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 test10_delete() throws Exception { + System.out.println("Testing GUI delete call"); + onboardApp(); + + assertTrue(admin.delete(adminAuthorization, onboardUUID)); + } + + @Test + public void test11_getAll() { + System.out.println("Testing GUI getall call"); + + System.out.println("admin.getall(adminAuthorization)"); + } + + /** + * Inject our mocked class and accept admin credentials + * @throws NoSuchFieldException + */ + public void authenticateAdminTrue() throws NoSuchFieldException { + authMock = Mockito.mock(MusicAAFAuthentication.class); + FieldSetter.setField(admin, admin.getClass().getDeclaredField("authenticator"), authMock); + + Mockito.when(authMock.authenticateAdmin(Mockito.matches(adminAuthorization))).thenReturn(true); + } + + /** + * onboard the application and store generate uuid into {@link onboardUUID} + * @param onboard + * @throws Exception + */ + public void onboardApp() throws Exception { + JsonOnboard jsonOnboard = new JsonOnboard(); + jsonOnboard.setAppname("TestApp2"); + jsonOnboard.setIsAAF("false"); + jsonOnboard.setUserId("TestUser2"); + jsonOnboard.setPassword("TestPassword2"); + + Response response = admin.onboardAppWithMusic(jsonOnboard,adminAuthorization); + Map<String, String> respMap = (Map<String, String>) response.getEntity(); + onboardUUID = respMap.get("Generated AID"); + } }
\ No newline at end of file diff --git a/src/test/java/org/onap/music/unittests/TstRestMusicConditionalAPI.java b/src/test/java/org/onap/music/unittests/TstRestMusicConditionalAPI.java index 1f0e24c2..3adab912 100644 --- a/src/test/java/org/onap/music/unittests/TstRestMusicConditionalAPI.java +++ b/src/test/java/org/onap/music/unittests/TstRestMusicConditionalAPI.java @@ -1,16 +1,15 @@ /* * ============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 + * =================================================================== 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. + * 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============================================= * ==================================================================== @@ -40,6 +39,7 @@ import org.mindrot.jbcrypt.BCrypt; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.onap.music.authentication.CachingUtil; import org.onap.music.conductor.conditionals.JsonConditional; import org.onap.music.conductor.conditionals.RestMusicConditionalAPI; import org.onap.music.datastore.MusicDataStoreHandle; @@ -51,7 +51,6 @@ 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.MusicServiceException; -import org.onap.music.main.CachingUtil; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; @@ -66,58 +65,58 @@ import com.sun.jersey.core.util.MultivaluedMapImpl; public class TstRestMusicConditionalAPI { RestMusicDataAPI data = new RestMusicDataAPI(); - RestMusicConditionalAPI cond = new RestMusicConditionalAPI(); - static PreparedQueryObject testObject; + RestMusicConditionalAPI cond = new RestMusicConditionalAPI(); + static PreparedQueryObject testObject; - @Mock - HttpServletResponse http; + @Mock + HttpServletResponse http; - @Mock - UriInfo info; + @Mock + UriInfo info; - static String appName = "TestApp"; - static String userId = "TestUser"; - static String password = "TestPassword"; - static String authData = userId+":"+password; - static String wrongAuthData = userId+":"+"pass"; - static String authorization = new String(Base64.encode(authData.getBytes())); - static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes())); - 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"; - static String onboardUUID = null; + static String appName = "TestApp"; + static String userId = "TestUser"; + static String password = "TestPassword"; + static String authData = userId + ":" + password; + static String wrongAuthData = userId + ":" + "pass"; + static String authorization = new String(Base64.encode(authData.getBytes())); + static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes())); + 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"; + static String onboardUUID = null; - @BeforeClass - public static void init() throws Exception { - System.out.println("Testing RestMusicConditional class"); - try { - createKeyspace(); - } catch (Exception e) { - e.printStackTrace(); - throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage()); - } - } - - @After - public void afterEachTest( ) throws MusicServiceException { - clearAllTablesFromKeyspace(); - } + @BeforeClass + public static void init() throws Exception { + System.out.println("Testing RestMusicConditional class"); + try { + createKeyspace(); + } catch (Exception e) { + e.printStackTrace(); + throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage()); + } + } - @AfterClass - public static void tearDownAfterClass() throws Exception { - testObject = new PreparedQueryObject(); - testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName); - MusicCore.eventualPut(testObject); - } - - @Test - public void test_insertIntoTable() throws Exception { - System.out.println("Testing conditional insert into table"); - createTable(); - - JsonConditional jsonCond = new JsonConditional(); + @After + public void afterEachTest() throws MusicServiceException { + clearAllTablesFromKeyspace(); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + testObject = new PreparedQueryObject(); + testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName); + MusicCore.eventualPut(testObject); + } + + @Test + public void test_insertIntoTable() throws Exception { + System.out.println("Testing conditional insert into table"); + createTable(); + + JsonConditional jsonCond = new JsonConditional(); Map<String, String> consistencyInfo = new HashMap<>(); Map<String, Object> values = new HashMap<>(); values.put("id", "test_id"); @@ -135,7 +134,7 @@ public class TstRestMusicConditionalAPI { nonexists.put("status", "underway"); condition.put("exists", exists); condition.put("nonexists", nonexists); - + jsonCond.setPrimaryKey("id"); jsonCond.setPrimaryKeyValue("testname"); jsonCond.setCasscadeColumnName("plans"); @@ -143,94 +142,62 @@ public class TstRestMusicConditionalAPI { jsonCond.setCasscadeColumnData(cascadeData); jsonCond.setConditions(condition); - Response response = cond.insertConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, keyspaceName, tableName, jsonCond); - - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } -/* - @Test - public void test4_insertIntoTable2() throws Exception { - System.out.println("Testing insert into table #2"); - createTable(); - 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); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } - - // Auth Error - @Test - public void test4_insertIntoTable3() throws Exception { - System.out.println("Testing insert into table with bad credentials"); - createTable(); - 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); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, wrongAuthorization, - jsonInsert, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + Response response = cond.insertConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, keyspaceName, tableName, jsonCond); - assertEquals(401, response.getStatus()); - } + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // Table wrong - @Test - public void test4_insertIntoTable4() throws Exception { - System.out.println("Testing insert into wrong table"); - createTable(); - 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); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, "wrong"); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } + /* + * @Test public void test4_insertIntoTable2() throws Exception { System.out.println("Testing insert into table #2"); + * createTable(); 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); Response response = data.insertIntoTable("1", + * "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, jsonInsert, keyspaceName, tableName); + * System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + * + * assertEquals(200, response.getStatus()); } + * + * // Auth Error + * + * @Test public void test4_insertIntoTable3() throws Exception { + * System.out.println("Testing insert into table with bad credentials"); createTable(); 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); Response response = data.insertIntoTable("1", + * "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, wrongAuthorization, jsonInsert, keyspaceName, + * tableName); System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + * + * assertEquals(401, response.getStatus()); } + * + * // Table wrong + * + * @Test public void test4_insertIntoTable4() throws Exception { + * System.out.println("Testing insert into wrong table"); createTable(); 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); Response response = data.insertIntoTable("1", + * "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, jsonInsert, keyspaceName, "wrong"); + * System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + * + * assertEquals(400, response.getStatus()); } + */ - assertEquals(400, response.getStatus()); - } -*/ + @Test + public void test5_updateTable() throws Exception { + System.out.println("Testing conditional update table"); + createAndInsertIntoTable(); - @Test - public void test5_updateTable() throws Exception { - System.out.println("Testing conditional update table"); - createAndInsertIntoTable(); - Map<String, String> consistencyInfo = new HashMap<>(); consistencyInfo.put("type", "eventual"); - + JsonConditional jsonCond = new JsonConditional(); Map<String, Object> values = new HashMap<>(); values.put("id", "test_id"); @@ -240,154 +207,85 @@ public class TstRestMusicConditionalAPI { cascadeValue.put("updated", "world"); cascadeData.put("key", "p1"); cascadeData.put("value", cascadeValue); - + jsonCond.setPrimaryKey("id"); jsonCond.setPrimaryKeyValue("test_id"); jsonCond.setCasscadeColumnName("plans"); jsonCond.setTableValues(values); jsonCond.setCasscadeColumnData(cascadeData); - - Response response = cond.updateConditional("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - keyspaceName, tableName, jsonCond); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } -/* - // need mock code to create error for MusicCore methods - @Test - public void test5_updateTableAuthE() throws Exception { - System.out.println("Testing update table #2"); - createTable(); - //MockitoAnnotations.initMocks(this); - 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", "testname"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - //add ttl & timestamp - //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - //Map<String, Object> m1= new HashMap<>() ; - //Mockito.when(MusicCore.autheticateUser(appName,userId,password,keyspaceName,"abc66ccc-d857-4e90-b1e5-df98a3d40ce6","updateTable")).thenReturn(m1); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } - - @Ignore - @Test - public void test5_updateTableAuthException1() throws Exception { - System.out.println("Testing update table authentication error"); - createTable(); - 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", "testname"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - - Mockito.when(info.getQueryParameters()).thenReturn(row); - String authDatax = ":"; - String authorizationx = new String(Base64.encode(authDatax.getBytes())); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorizationx, jsonUpdate, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(401, response.getStatus()); - } + Response response = cond.updateConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, keyspaceName, tableName, jsonCond); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - @Ignore - @Test - public void test5_updateTableAuthEmpty() throws Exception { - System.out.println("Testing update table without authentication"); - createTable(); - - 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", "testname"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); + assertEquals(200, response.getStatus()); + } + /* + * // need mock code to create error for MusicCore methods + * + * @Test public void test5_updateTableAuthE() throws Exception { System.out.println("Testing update table #2"); + * createTable(); //MockitoAnnotations.initMocks(this); 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", "testname"); values.put("emp_salary", 2500); + * consistencyInfo.put("type", "atomic"); jsonUpdate.setConsistencyInfo(consistencyInfo); + * jsonUpdate.setKeyspaceName(keyspaceName); jsonUpdate.setTableName(tableName); jsonUpdate.setValues(values); //add + * ttl & timestamp //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + * Mockito.when(info.getQueryParameters()).thenReturn(row); //Map<String, Object> m1= new HashMap<>() ; + * //Mockito.when(MusicCore.autheticateUser(appName,userId,password,keyspaceName, + * "abc66ccc-d857-4e90-b1e5-df98a3d40ce6","updateTable")).thenReturn(m1); Response response = data.updateTable("1", + * "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, jsonUpdate, keyspaceName, tableName, + * info); System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + * + * assertEquals(200, response.getStatus()); } + * + * @Ignore + * + * @Test public void test5_updateTableAuthException1() throws Exception { + * System.out.println("Testing update table authentication error"); createTable(); 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", "testname"); + * values.put("emp_salary", 2500); consistencyInfo.put("type", "atomic"); + * jsonUpdate.setConsistencyInfo(consistencyInfo); jsonUpdate.setKeyspaceName(keyspaceName); + * jsonUpdate.setTableName(tableName); jsonUpdate.setValues(values); + * + * Mockito.when(info.getQueryParameters()).thenReturn(row); String authDatax = ":"; String authorizationx = new + * String(Base64.encode(authDatax.getBytes())); Response response = data.updateTable("1", "1", "1", + * "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorizationx, jsonUpdate, keyspaceName, tableName, info); + * System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + * + * assertEquals(401, response.getStatus()); } + * + * @Ignore + * + * @Test public void test5_updateTableAuthEmpty() throws Exception { + * System.out.println("Testing update table without authentication"); createTable(); + * + * 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", "testname"); values.put("emp_salary", 2500); consistencyInfo.put("type", "atomic"); + * jsonUpdate.setConsistencyInfo(consistencyInfo); jsonUpdate.setKeyspaceName(keyspaceName); + * jsonUpdate.setTableName(tableName); jsonUpdate.setValues(values); + * + * Mockito.when(info.getQueryParameters()).thenReturn(row); String authDatax =":"+password; String authorizationx = + * new String(Base64.encode(authDatax.getBytes())); String appNamex="xx"; Response response = data.updateTable("1", + * "1", "1", "", appNamex, authorizationx, jsonUpdate, keyspaceName, tableName, info); System.out.println("Status: " + * + response.getStatus() + ". Entity " + response.getEntity()); + * + * assertEquals(401, response.getStatus()); } + * + */ - Mockito.when(info.getQueryParameters()).thenReturn(row); - String authDatax =":"+password; - String authorizationx = new String(Base64.encode(authDatax.getBytes())); - String appNamex="xx"; - Response response = data.updateTable("1", "1", "1", "", appNamex, - authorizationx, jsonUpdate, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(401, response.getStatus()); - } - -*/ - - private static void createAdminTable() throws Exception { - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createAdminKeyspace); - MusicCore.eventualPut(testObject); - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createAdminTable); - 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(), BCrypt.hashpw(password, BCrypt.gensalt()))); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); - 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")); - } - } - + private static void createKeyspace() throws Exception { + // shouldn't really be doing this here, but create keyspace is currently turned off + PreparedQueryObject query = new PreparedQueryObject(); + query.appendQueryString(CassandraCQL.createKeySpace); + MusicCore.eventualPut(query); - private static void createKeyspace() throws Exception { - //shouldn't really be doing this here, but create keyspace is currently turned off - PreparedQueryObject query = new PreparedQueryObject(); - query.appendQueryString(CassandraCQL.createKeySpace); - MusicCore.eventualPut(query); - - boolean isAAF = false; + boolean isAAF = false; String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt()); query = new PreparedQueryObject(); - query.appendQueryString( - "INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " - + "password, username, is_aaf) values (?,?,?,?,?,?,?)"); + query.appendQueryString("INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " + + "password, username, is_aaf) values (?,?,?,?,?,?,?)"); query.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); query.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName)); query.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); @@ -398,52 +296,54 @@ public class TstRestMusicConditionalAPI { CachingUtil.updateMusicCache(keyspaceName, appName); CachingUtil.updateMusicValidateCache(appName, userId, hashedpwd); MusicCore.eventualPut(query); - } - - private void clearAllTablesFromKeyspace() throws MusicServiceException { - ArrayList<String> tableNames = new ArrayList<>(); - PreparedQueryObject query = new PreparedQueryObject(); - query.appendQueryString("SELECT table_name FROM system_schema.tables WHERE keyspace_name = '"+keyspaceName+"';"); - ResultSet rs = MusicCore.get(query); - for (Row row: rs) { - tableNames.add(row.getString("table_name")); - } - for (String table: tableNames) { - query = new PreparedQueryObject(); - query.appendQueryString("DROP TABLE " + keyspaceName + "." + table); - MusicCore.eventualPut(query); - } - } - - /** - * Create a table {@link tableName} in {@link keyspaceName} - * @throws Exception - */ - private void createTable() throws Exception { - JsonTable jsonTable = new JsonTable(); - Map<String, String> consistencyInfo = new HashMap<>(); - Map<String, String> fields = new HashMap<>(); - fields.put("id", "text"); - fields.put("plans", "map<text,text>"); - fields.put("PRIMARY KEY", "(id)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("id"); - jsonTable.setTableName(tableName); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",appName, authorization, - jsonTable, keyspaceName, tableName); - } - - /** - * Create table {@link createTable} and insert into said table - * @throws Exception - */ - private void createAndInsertIntoTable() throws Exception { - createTable(); - + } + + private void clearAllTablesFromKeyspace() throws MusicServiceException { + ArrayList<String> tableNames = new ArrayList<>(); + PreparedQueryObject query = new PreparedQueryObject(); + query.appendQueryString( + "SELECT table_name FROM system_schema.tables WHERE keyspace_name = '" + keyspaceName + "';"); + ResultSet rs = MusicCore.get(query); + for (Row row : rs) { + tableNames.add(row.getString("table_name")); + } + for (String table : tableNames) { + query = new PreparedQueryObject(); + query.appendQueryString("DROP TABLE " + keyspaceName + "." + table); + MusicCore.eventualPut(query); + } + } + + /** + * Create a table {@link tableName} in {@link keyspaceName} + * + * @throws Exception + */ + private void createTable() throws Exception { + JsonTable jsonTable = new JsonTable(); + Map<String, String> consistencyInfo = new HashMap<>(); + Map<String, String> fields = new HashMap<>(); + fields.put("id", "text"); + fields.put("plans", "map<text,text>"); + fields.put("PRIMARY KEY", "(id)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("id"); + jsonTable.setTableName(tableName); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableName); + } + + /** + * Create table {@link createTable} and insert into said table + * + * @throws Exception + */ + private void createAndInsertIntoTable() throws Exception { + createTable(); + Map<String, String> consistencyInfo = new HashMap<>(); consistencyInfo.put("type", "eventual"); JsonConditional jsonCond = new JsonConditional(); @@ -462,7 +362,7 @@ public class TstRestMusicConditionalAPI { nonexists.put("status", "underway"); condition.put("exists", exists); condition.put("nonexists", nonexists); - + jsonCond.setPrimaryKey("id"); jsonCond.setPrimaryKeyValue("test_id"); jsonCond.setCasscadeColumnName("plans"); @@ -470,7 +370,7 @@ public class TstRestMusicConditionalAPI { jsonCond.setCasscadeColumnData(cascadeData); jsonCond.setConditions(condition); - Response response = cond.insertConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, keyspaceName, tableName, jsonCond); - } + Response response = cond.insertConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, keyspaceName, tableName, jsonCond); + } } diff --git a/src/test/java/org/onap/music/unittests/TstRestMusicDataAPI.java b/src/test/java/org/onap/music/unittests/TstRestMusicDataAPI.java index 0f0545e8..18692915 100644 --- a/src/test/java/org/onap/music/unittests/TstRestMusicDataAPI.java +++ b/src/test/java/org/onap/music/unittests/TstRestMusicDataAPI.java @@ -1,16 +1,20 @@ /* - * ============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 + * ============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 + * 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. + * 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============================================= * ==================================================================== @@ -40,6 +44,7 @@ import org.mindrot.jbcrypt.BCrypt; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.onap.music.authentication.CachingUtil; import org.onap.music.datastore.MusicDataStoreHandle; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.datastore.jsonobjects.JsonDelete; @@ -49,11 +54,11 @@ 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.MusicServiceException; -import org.onap.music.main.CachingUtil; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.rest.RestMusicDataAPI; +import org.onap.music.rest.RestMusicLocksAPI; import com.datastax.driver.core.DataType; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; @@ -64,6 +69,7 @@ import com.sun.jersey.core.util.MultivaluedMapImpl; public class TstRestMusicDataAPI { RestMusicDataAPI data = new RestMusicDataAPI(); + RestMusicLocksAPI lock = new RestMusicLocksAPI(); static PreparedQueryObject testObject; @Mock @@ -72,88 +78,89 @@ public class TstRestMusicDataAPI { @Mock UriInfo info; - static String appName = "TestApp"; - static String userId = "TestUser"; - static String password = "TestPassword"; - static String authData = userId+":"+password; - static String wrongAuthData = userId+":"+"pass"; - static String authorization = new String(Base64.encode(authData.getBytes())); - static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes())); - 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"; - static String onboardUUID = null; - - @BeforeClass - public static void init() throws Exception { - System.out.println("Testing RestMusicData class"); - try { - createKeyspace(); - } catch (Exception e) { - e.printStackTrace(); - throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage()); - } - } - - @After - public void afterEachTest( ) throws MusicServiceException { - clearAllTablesFromKeyspace(); - } + static String appName = "TestApp"; + static String userId = "TestUser"; + static String password = "TestPassword"; + static String authData = userId + ":" + password; + static String wrongAuthData = userId + ":" + "pass"; + static String authorization = new String(Base64.encode(authData.getBytes())); + static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes())); + 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"; + static String onboardUUID = null; + static String aid = TestsUsingCassandra.aid; + + @BeforeClass + public static void init() throws Exception { + System.out.println("Testing RestMusicData class"); + try { + createKeyspace(); + } catch (Exception e) { + e.printStackTrace(); + throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage()); + } + } - @AfterClass - public static void tearDownAfterClass() throws Exception { - testObject = new PreparedQueryObject(); - testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName); - MusicCore.eventualPut(testObject); - } + @After + public void afterEachTest() throws MusicServiceException { + clearAllTablesFromKeyspace(); + } - @Test - public void test1_createKeyspace() throws Exception { - System.out.println("Testing create keyspace"); - 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()); - Response response = data.createKeySpace("1", "1", "1", null,authorization, appName, jsonKeyspace, keyspaceName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(400,response.getStatus()); - Map<String,String> respMap = (Map<String, String>) response.getEntity(); - assertEquals(ResultType.FAILURE, respMap.get("status")); - } + @AfterClass + public static void tearDownAfterClass() throws Exception { + testObject = new PreparedQueryObject(); + testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName); + MusicCore.eventualPut(testObject); + } - @Test - public void test3_createTable() throws Exception { - System.out.println("Testing create table"); - 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); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",appName, authorization, - jsonTable, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(200, response.getStatus()); - } - - @Test + @Test + public void test1_createKeyspace() throws Exception { + System.out.println("Testing create keyspace"); + 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()); + Response response = + data.createKeySpace("1", "1", "1", null, authorization, appName, jsonKeyspace, keyspaceName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + Map<String, String> respMap = (Map<String, String>) response.getEntity(); + assertEquals(ResultType.FAILURE, respMap.get("status")); + } + + @Test + public void test3_createTable() throws Exception { + System.out.println("Testing create table"); + 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); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } + + @Test public void test3_createTableNoName() throws Exception { System.out.println("Testing create table without name"); JsonTable jsonTable = new JsonTable(); @@ -169,330 +176,347 @@ public class TstRestMusicDataAPI { jsonTable.setPrimaryKey("emp_name"); jsonTable.setTableName(""); jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",appName, authorization, - jsonTable, keyspaceName, ""); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, ""); System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); assertEquals(400, response.getStatus()); } - @Test - public void test3_createTableClusterOrderBad() throws Exception { - System.out.println("Testing create table bad clustering"); - 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,emp_salary)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("emp_name,emp_salary"); - jsonTable.setClusteringOrder("ASC"); - jsonTable.setTableName(tableName); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",appName, authorization, - jsonTable, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(400, response.getStatus()); - } + @Test + public void test3_createTableClusterOrderBad() throws Exception { + System.out.println("Testing create table bad clustering"); + 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,emp_salary)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("emp_name,emp_salary"); + jsonTable.setClusteringOrder("ASC"); + jsonTable.setTableName(tableName); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } - @Test - public void test3_createTable_withPropertiesNotNull() throws Exception { - System.out.println("Testing create table with properties"); - 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"); - Map<String, Object> properties = new HashMap<>(); - properties.put("comment","Testing prperties not null"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("emp_name"); - String tableName_prop=tableName+"_Prop"; - jsonTable.setTableName(tableName_prop); - jsonTable.setFields(fields); - jsonTable.setProperties(properties); - - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",appName, authorization, - jsonTable, keyspaceName, tableName_prop); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(200, response.getStatus()); - } + @Test + public void test3_createTable_withPropertiesNotNull() throws Exception { + System.out.println("Testing create table with properties"); + 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"); + Map<String, Object> properties = new HashMap<>(); + properties.put("comment", "Testing prperties not null"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("emp_name"); + String tableName_prop = tableName + "_Prop"; + jsonTable.setTableName(tableName_prop); + jsonTable.setFields(fields); + jsonTable.setProperties(properties); - @Test - public void test3_createTable_duplicateTable() throws Exception { - System.out.println("Testing creating duplicate tables"); - 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"); - String tableNameDup=tableName+"x"; - jsonTable.setTableName(tableNameDup); - jsonTable.setFields(fields); - //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response1 = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameDup); - - Response response2 = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameDup); - System.out.println("Status: " + response2.getStatus() + ". Entity " + response2.getEntity()); - - assertEquals(400, response2.getStatus()); - Map<String,String> respMap = (Map<String, String>) response2.getEntity(); - assertEquals(ResultType.FAILURE, respMap.get("status")); - assertEquals("Table " + keyspaceName + "." + tableNameDup + " already exists", - respMap.get("error")); - } + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableName_prop); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } - // Improper Auth - @Test - public void test3_createTable1() throws Exception { - System.out.println("Testing create table w/ improper authentication"); - 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); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, wrongAuthorization, - jsonTable, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(401, response.getStatus()); - } + @Test + public void test3_createTable_duplicateTable() throws Exception { + System.out.println("Testing creating duplicate tables"); + 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"); + String tableNameDup = tableName + "x"; + jsonTable.setTableName(tableNameDup); + jsonTable.setFields(fields); + // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Response response1 = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameDup); + + Response response2 = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameDup); + System.out.println("Status: " + response2.getStatus() + ". Entity " + response2.getEntity()); + + assertEquals(400, response2.getStatus()); + Map<String, String> respMap = (Map<String, String>) response2.getEntity(); + assertEquals(ResultType.FAILURE, respMap.get("status")); + assertEquals("Table " + keyspaceName + "." + tableNameDup + " already exists", respMap.get("error")); + } - // Improper keyspace - @Test - public void test3_createTable3() throws Exception { - System.out.println("Testing create table for wrong keyspace"); - 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); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, "wrong", tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(401, response.getStatus()); - } + // Improper Auth + @Test + public void test3_createTable1() throws Exception { + System.out.println("Testing create table w/ improper authentication"); + 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); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + wrongAuthorization, jsonTable, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } - // Improper parenthesis in key field - @Test - public void test3_createTable_badParantesis() throws Exception { - System.out.println("Testing malformed create table request"); - String tableNameC ="testTable0"; - 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),emp_id)"); - fields.put("emp_id", "varint"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("emp_name"); - jsonTable.setTableName(tableNameC); - jsonTable.setClusteringOrder("emp_id Desc"); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + // Improper keyspace + @Test + public void test3_createTable3() throws Exception { + System.out.println("Testing create table for wrong keyspace"); + 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); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, "wrong", tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } - assertEquals(400, response.getStatus()); - } + // Improper parenthesis in key field + @Test + public void test3_createTable_badParantesis() throws Exception { + System.out.println("Testing malformed create table request"); + String tableNameC = "testTable0"; + 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),emp_id)"); + fields.put("emp_id", "varint"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("emp_name"); + jsonTable.setTableName(tableNameC); + jsonTable.setClusteringOrder("emp_id Desc"); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } - // good clustering key - @Test - public void test3_createTable_1_clusterKey_good() throws Exception { - System.out.println("Testing create w/ clusterKey"); - String tableNameC ="testTableC1"; - 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),emp_salary)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - // jsonTable.setPrimaryKey("emp_name"); - jsonTable.setTableName(tableNameC); - jsonTable.setClusteringOrder("emp_salary ASC"); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + // good clustering key + @Test + public void test3_createTable_1_clusterKey_good() throws Exception { + System.out.println("Testing create w/ clusterKey"); - assertEquals(200, response.getStatus()); - } + String tableNameC = "testTableC1"; + 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),emp_salary)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + // jsonTable.setPrimaryKey("emp_name"); + jsonTable.setTableName(tableNameC); + jsonTable.setClusteringOrder("emp_salary ASC"); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // bad partition key=clustering key - @Test - public void test3_createTable_2_clusterKey_bad() throws Exception { - System.out.println("Testing create w/ bad clusterKey"); - String tableNameC ="testTableC2"; - 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),emp_name)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("emp_name"); // "PRIMARY KEY" overrides if primaryKey present - jsonTable.setTableName(tableNameC); - jsonTable.setClusteringOrder("emp_salary ASC"); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } - assertEquals(400, response.getStatus()); - } + // bad partition key=clustering key + @Test + public void test3_createTable_2_clusterKey_bad() throws Exception { + System.out.println("Testing create w/ bad clusterKey"); + String tableNameC = "testTableC2"; + 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),emp_name)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("emp_name"); // "PRIMARY KEY" overrides if primaryKey present + jsonTable.setTableName(tableNameC); + jsonTable.setClusteringOrder("emp_salary ASC"); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // good composite partition key,clustering key - @Test - public void test3_createTable_3_pfartition_clusterKey_good() throws Exception { - System.out.println("Testing create w/ composite partition key, clusterKey"); + assertEquals(400, response.getStatus()); + } - String tableNameC ="testTableC3"; - 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_id", "varint"); - fields.put("emp_salary", "varint"); - fields.put("PRIMARY KEY", "((emp_name,emp_id),emp_salary)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("emp_name"); - jsonTable.setTableName(tableNameC); - jsonTable.setClusteringOrder("emp_salary ASC"); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + // good composite partition key,clustering key + @Test + public void test3_createTable_3_pfartition_clusterKey_good() throws Exception { + System.out.println("Testing create w/ composite partition key, clusterKey"); - assertEquals(200, response.getStatus()); - } + String tableNameC = "testTableC3"; + 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_id", "varint"); + fields.put("emp_salary", "varint"); + fields.put("PRIMARY KEY", "((emp_name,emp_id),emp_salary)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("emp_name"); + jsonTable.setTableName(tableNameC); + jsonTable.setClusteringOrder("emp_salary ASC"); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // bad - wrong cols in order by of composite partition key,clustering key - @Test - public void test3_createTable_5_clusteringOrder_bad() throws Exception { - System.out.println("Testing create table bad request with clustering & composite keys"); - String tableNameC ="testTableC5"; - 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_id", "varint"); - fields.put("emp_salary", "varint"); - fields.put("PRIMARY KEY", "((uuid,emp_name),emp_id,emp_salary)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("emp_name"); - jsonTable.setTableName(tableNameC); - jsonTable.setClusteringOrder("emp_idx desc, emp_salary ASC"); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } - assertEquals(400, response.getStatus()); - } + // bad - wrong cols in order by of composite partition key,clustering key + @Test + public void test3_createTable_5_clusteringOrder_bad() throws Exception { + System.out.println("Testing create table bad request with clustering & composite keys"); + String tableNameC = "testTableC5"; + 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_id", "varint"); + fields.put("emp_salary", "varint"); + fields.put("PRIMARY KEY", "((uuid,emp_name),emp_id,emp_salary)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("emp_name"); + jsonTable.setTableName(tableNameC); + jsonTable.setClusteringOrder("emp_idx desc, emp_salary ASC"); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } - // good clustering key, need to pass queryparameter - @Test - public void test3_createTableIndex_1() throws Exception { - System.out.println("Testing index in create table"); - String tableNameC ="testTableCinx"; - 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),emp_salary)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setTableName(tableNameC); - jsonTable.setClusteringOrder("emp_salary ASC"); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); - // if 200 print to log otherwise fail assertEquals(200, response.getStatus()); - // info.setQueryParameters("index_name=inx_uuid"); - Map<String,String> queryParametersMap =new HashMap<String, String>(); - - queryParametersMap.put("index_name","inxuuid"); - Mockito.when(info.getQueryParameters()).thenReturn(new MultivaluedHashMap<String, String>(queryParametersMap)); - response = data.createIndex("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - keyspaceName, tableNameC,"uuid",info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(200, response.getStatus()); - } - - // create index without table name + // good clustering key, need to pass queryparameter + @Test + public void test3_createTableIndex_1() throws Exception { + System.out.println("Testing index in create table"); + String tableNameC = "testTableCinx"; + 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),emp_salary)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setTableName(tableNameC); + jsonTable.setClusteringOrder("emp_salary ASC"); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); + // if 200 print to log otherwise fail assertEquals(200, response.getStatus()); + // info.setQueryParameters("index_name=inx_uuid"); + Map<String, String> queryParametersMap = new HashMap<String, String>(); + + queryParametersMap.put("index_name", "inxuuid"); + Mockito.when(info.getQueryParameters()).thenReturn(new MultivaluedHashMap<String, String>(queryParametersMap)); + response = data.createIndex("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, + keyspaceName, tableNameC, "uuid", info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); + } + + // good clustering key, need to pass queryparameter + @Test + public void test3_createTableIndex_badAuth() throws Exception { + System.out.println("Testing index in create table w/ wrong authorization"); + String tableNameC = "testTableCinx"; + 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),emp_salary)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setTableName(tableNameC); + jsonTable.setClusteringOrder("emp_salary ASC"); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); + // if 200 print to log otherwise fail assertEquals(200, response.getStatus()); + // info.setQueryParameters("index_name=inx_uuid"); + Map<String, String> queryParametersMap = new HashMap<String, String>(); + + queryParametersMap.put("index_name", "inxuuid"); + response = data.createIndex("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + wrongAuthorization, keyspaceName, tableNameC, "uuid", info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(401, response.getStatus()); + } + + // create index without table name @Test public void test3_createTableIndexNoName() throws Exception { System.out.println("Testing index in create table w/o tablename"); - String tableNameC ="testTableCinx"; + String tableNameC = "testTableCinx"; JsonTable jsonTable = new JsonTable(); Map<String, String> consistencyInfo = new HashMap<>(); Map<String, String> fields = new HashMap<>(); @@ -506,47 +530,45 @@ public class TstRestMusicDataAPI { jsonTable.setTableName(tableNameC); jsonTable.setClusteringOrder("emp_salary ASC"); jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); // if 200 print to log otherwise fail assertEquals(200, response.getStatus()); // info.setQueryParameters("index_name=inx_uuid"); - Map<String,String> queryParametersMap =new HashMap<String, String>(); + Map<String, String> queryParametersMap = new HashMap<String, String>(); - queryParametersMap.put("index_name","inxuuid"); - response = data.createIndex("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - "", "","uuid",info); + queryParametersMap.put("index_name", "inxuuid"); + response = data.createIndex("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, "", + "", "uuid", info); System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); assertEquals(400, response.getStatus()); } - @Test - public void test4_insertIntoTable() throws Exception { - System.out.println("Testing insert into table"); - createTable(); - 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", "testname"); - values.put("emp_salary", 500); - consistencyInfo.put("type", "eventual"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, jsonInsert, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + @Test + public void test4_insertIntoTable() throws Exception { + System.out.println("Testing insert into table"); + createTable(); + 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", "testname"); + values.put("emp_salary", 500); + consistencyInfo.put("type", "eventual"); + jsonInsert.setConsistencyInfo(consistencyInfo); + jsonInsert.setKeyspaceName(keyspaceName); + jsonInsert.setTableName(tableName); + jsonInsert.setValues(values); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonInsert, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(200, response.getStatus()); - } - - @Test + assertEquals(200, response.getStatus()); + } + + @Test public void test4_insertIntoTableCriticalNoLockID() throws Exception { - System.out.println("Testing atomic insert into table without lockid"); + System.out.println("Testing critical insert into table without lockid"); createTable(); JsonInsert jsonInsert = new JsonInsert(); Map<String, String> consistencyInfo = new HashMap<>(); @@ -566,7 +588,29 @@ public class TstRestMusicDataAPI { assertEquals(400, response.getStatus()); } - @Test + @Test + public void test4_insertIntoTableAtomic() throws Exception { + System.out.println("Testing atomic insert into table without lockid"); + createTable(); + 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", "testname"); + values.put("emp_salary", 500); + consistencyInfo.put("type", "atomic"); + jsonInsert.setConsistencyInfo(consistencyInfo); + jsonInsert.setKeyspaceName(keyspaceName); + jsonInsert.setTableName(tableName); + jsonInsert.setValues(values); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, authorization, jsonInsert, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); + } + + @Test public void test4_insertIntoTableNoName() throws Exception { System.out.println("Testing insert into table w/o table name"); createTable(); @@ -588,108 +632,102 @@ public class TstRestMusicDataAPI { assertEquals(400, response.getStatus()); } - @Test - public void test4_insertIntoTable2() throws Exception { - System.out.println("Testing insert into table #2"); - createTable(); - 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); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } + @Test + public void test4_insertIntoTable2() throws Exception { + System.out.println("Testing insert into table #2"); + createTable(); + 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); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonInsert, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // Auth Error - @Test - public void test4_insertIntoTable3() throws Exception { - System.out.println("Testing insert into table with bad credentials"); - createTable(); - 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); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, wrongAuthorization, - jsonInsert, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } - assertEquals(401, response.getStatus()); - } + // Auth Error + @Test + public void test4_insertIntoTable3() throws Exception { + System.out.println("Testing insert into table with bad credentials"); + createTable(); + 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); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + wrongAuthorization, jsonInsert, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // Table wrong - @Test - public void test4_insertIntoTable4() throws Exception { - System.out.println("Testing insert into wrong table"); - createTable(); - 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); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, "wrong"); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } - assertEquals(400, response.getStatus()); - } + // Table wrong + @Test + public void test4_insertIntoTable4() throws Exception { + System.out.println("Testing insert into wrong table"); + createTable(); + 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); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonInsert, keyspaceName, "wrong"); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); + } + @Test + public void test4_insertBlobIntoTable() throws Exception { + System.out.println("Testing insert a blob into table"); + createTable(); + 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", "testname"); + values.put("emp_salary", 500); + values.put("binary", "somestuffhere"); + consistencyInfo.put("type", "eventual"); + jsonInsert.setConsistencyInfo(consistencyInfo); + jsonInsert.setKeyspaceName(keyspaceName); + jsonInsert.setTableName(tableName); + jsonInsert.setValues(values); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonInsert, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - @Test - public void test5_updateTable() throws Exception { - System.out.println("Testing update table"); - createTable(); - - 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", "testname"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } - - @Test - public void test5_updateTableNoName() throws Exception { - System.out.println("Testing update table without tablename"); + assertEquals(200, response.getStatus()); + } + + @Test + public void test5_updateTable() throws Exception { + System.out.println("Testing update table"); createTable(); - + JsonUpdate jsonUpdate = new JsonUpdate(); Map<String, String> consistencyInfo = new HashMap<>(); MultivaluedMap<String, String> row = new MultivaluedMapImpl(); @@ -701,125 +739,169 @@ public class TstRestMusicDataAPI { jsonUpdate.setKeyspaceName(keyspaceName); jsonUpdate.setTableName(tableName); jsonUpdate.setValues(values); + Mockito.when(info.getQueryParameters()).thenReturn(row); + Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonUpdate, keyspaceName, tableName, info); + + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); + } + + @Test + public void test5_updateTable_wrongAuth() throws Exception { + System.out.println("Testing update table w/ wrong credentials"); + createTable(); + + JsonUpdate jsonUpdate = new JsonUpdate(); + Map<String, String> consistencyInfo = new HashMap<>(); + Map<String, Object> values = new HashMap<>(); + values.put("emp_salary", 2500); + consistencyInfo.put("type", "atomic"); + jsonUpdate.setConsistencyInfo(consistencyInfo); + jsonUpdate.setKeyspaceName(keyspaceName); + jsonUpdate.setTableName(tableName); + jsonUpdate.setValues(values); + Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + wrongAuthorization, jsonUpdate, keyspaceName, tableName, info); + + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(401, response.getStatus()); + } + + @Test + public void test5_updateTable_tableDNE() throws Exception { + System.out.println("Testing update table that does not exist"); + createTable(); + + JsonUpdate jsonUpdate = new JsonUpdate(); + Map<String, String> consistencyInfo = new HashMap<>(); + Map<String, Object> values = new HashMap<>(); + values.put("emp_salary", 2500); + consistencyInfo.put("type", "atomic"); + jsonUpdate.setConsistencyInfo(consistencyInfo); + jsonUpdate.setKeyspaceName(keyspaceName); + jsonUpdate.setTableName("wrong_"+tableName); + jsonUpdate.setValues(values); + Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonUpdate, keyspaceName, "wrong_"+ tableName, info); + + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); + } + + @Test + public void test5_updateTableNoName() throws Exception { + System.out.println("Testing update table without tablename"); + createTable(); + + JsonUpdate jsonUpdate = new JsonUpdate(); + Map<String, String> consistencyInfo = new HashMap<>(); + Map<String, Object> values = new HashMap<>(); + values.put("emp_salary", 2500); + consistencyInfo.put("type", "atomic"); + jsonUpdate.setConsistencyInfo(consistencyInfo); + jsonUpdate.setKeyspaceName(keyspaceName); + jsonUpdate.setTableName(tableName); + jsonUpdate.setValues(values); Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, jsonUpdate, "", "", info); - + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - + assertEquals(400, response.getStatus()); } - // need mock code to create error for MusicCore methods - @Test - public void test5_updateTableAuthE() throws Exception { - System.out.println("Testing update table #2"); - createTable(); - //MockitoAnnotations.initMocks(this); - 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", "testname"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - //add ttl & timestamp - //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - //Map<String, Object> m1= new HashMap<>() ; - //Mockito.when(MusicCore.autheticateUser(appName,userId,password,keyspaceName,"abc66ccc-d857-4e90-b1e5-df98a3d40ce6","updateTable")).thenReturn(m1); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + // need mock code to create error for MusicCore methods + @Test + public void test5_updateTableAuthE() throws Exception { + System.out.println("Testing update table #2"); + createTable(); + // MockitoAnnotations.initMocks(this); + 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", "testname"); + values.put("emp_salary", 2500); + consistencyInfo.put("type", "atomic"); + jsonUpdate.setConsistencyInfo(consistencyInfo); + jsonUpdate.setKeyspaceName(keyspaceName); + jsonUpdate.setTableName(tableName); + jsonUpdate.setValues(values); + // add ttl & timestamp + // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Mockito.when(info.getQueryParameters()).thenReturn(row); + // Map<String, Object> m1= new HashMap<>() ; + // Mockito.when(MusicCore.autheticateUser(appName,userId,password,keyspaceName,"abc66ccc-d857-4e90-b1e5-df98a3d40ce6","updateTable")).thenReturn(m1); + Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonUpdate, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(200, response.getStatus()); - } + assertEquals(200, response.getStatus()); + } - @Ignore - @Test - public void test5_updateTableAuthException1() throws Exception { - System.out.println("Testing update table authentication error"); - createTable(); - 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", "testname"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - - Mockito.when(info.getQueryParameters()).thenReturn(row); - String authDatax = ":"; - String authorizationx = new String(Base64.encode(authDatax.getBytes())); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorizationx, jsonUpdate, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + @Ignore + @Test + public void test5_updateTableAuthException1() throws Exception { + System.out.println("Testing update table authentication error"); + createTable(); + 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", "testname"); + values.put("emp_salary", 2500); + consistencyInfo.put("type", "atomic"); + jsonUpdate.setConsistencyInfo(consistencyInfo); + jsonUpdate.setKeyspaceName(keyspaceName); + jsonUpdate.setTableName(tableName); + jsonUpdate.setValues(values); - assertEquals(401, response.getStatus()); - } + Mockito.when(info.getQueryParameters()).thenReturn(row); + String authDatax = ":"; + String authorizationx = new String(Base64.encode(authDatax.getBytes())); + Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorizationx, jsonUpdate, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - @Ignore - @Test - public void test5_updateTableAuthEmpty() throws Exception { - System.out.println("Testing update table without authentication"); - createTable(); - - 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", "testname"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); + assertEquals(401, response.getStatus()); + } - Mockito.when(info.getQueryParameters()).thenReturn(row); - String authDatax =":"+password; - String authorizationx = new String(Base64.encode(authDatax.getBytes())); - String appNamex="xx"; - Response response = data.updateTable("1", "1", "1", "", appNamex, - authorizationx, jsonUpdate, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + @Ignore + @Test + public void test5_updateTableAuthEmpty() throws Exception { + System.out.println("Testing update table without authentication"); + createTable(); - assertEquals(401, response.getStatus()); - } + 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", "testname"); + values.put("emp_salary", 2500); + consistencyInfo.put("type", "atomic"); + jsonUpdate.setConsistencyInfo(consistencyInfo); + jsonUpdate.setKeyspaceName(keyspaceName); + jsonUpdate.setTableName(tableName); + jsonUpdate.setValues(values); + Mockito.when(info.getQueryParameters()).thenReturn(row); + String authDatax = ":" + password; + String authorizationx = new String(Base64.encode(authDatax.getBytes())); + String appNamex = "xx"; + Response response = data.updateTable("1", "1", "1", "", appNamex, authorizationx, jsonUpdate, keyspaceName, + tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - @Test - public void test6_select() throws Exception { - System.out.println("Testing select"); - createAndInsertIntoTable(); - JsonSelect jsonSelect = new JsonSelect(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - row.add("emp_name", "testname"); - consistencyInfo.put("type", "atomic"); - jsonSelect.setConsistencyInfo(consistencyInfo); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.select("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, keyspaceName, tableName, info); - HashMap<String,HashMap<String,Object>> map = (HashMap<String, HashMap<String, Object>>) response.getEntity(); - HashMap<String, Object> result = map.get("result"); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - Map<String, String> row0 = (Map<String, String>) result.get("row 0"); - assertEquals("testname", row0.get("emp_name")); - assertEquals(BigInteger.valueOf(500), row0.get("emp_salary")); - } + assertEquals(401, response.getStatus()); + } @Test - public void test6_selectCritical() throws Exception { - System.out.println("Testing select critical"); + public void test6_critical_selectAtomic() throws Exception { + System.out.println("Testing critical select atomic"); createAndInsertIntoTable(); JsonInsert jsonInsert = new JsonInsert(); Map<String, String> consistencyInfo = new HashMap<>(); @@ -838,149 +920,268 @@ public class TstRestMusicDataAPI { assertEquals("testname", row0.get("emp_name")); assertEquals(BigInteger.valueOf(500), row0.get("emp_salary")); } - + @Test - public void test6_deleteFromTable() throws Exception { - System.out.println("Testing delete from table"); - createAndInsertIntoTable(); - 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.when(info.getQueryParameters()).thenReturn(row); - Response response = data.deleteFromTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonDelete, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + public void test6_critical_selectCritical_nolockid() throws Exception { + System.out.println("Testing critical select critical w/o lockid"); + createAndInsertIntoTable(); + JsonInsert jsonInsert = new JsonInsert(); + Map<String, String> consistencyInfo = new HashMap<>(); + MultivaluedMap<String, String> row = new MultivaluedMapImpl(); + row.add("emp_name", "testname"); + consistencyInfo.put("type", "critical"); + jsonInsert.setConsistencyInfo(consistencyInfo); + Mockito.when(info.getQueryParameters()).thenReturn(row); + Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, authorization, jsonInsert, keyspaceName, tableName,info); + HashMap<String,HashMap<String,Object>> map = (HashMap<String, HashMap<String, Object>>) response.getEntity(); + HashMap<String, Object> result = map.get("result"); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); + } + + @Test + public void test6_critical_select_wrongAuth() throws Exception { + System.out.println("Testing critical select w/ wrong authentication"); + createAndInsertIntoTable(); + JsonInsert jsonInsert = new JsonInsert(); + Map<String, String> consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonInsert.setConsistencyInfo(consistencyInfo); + Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, wrongAuthorization, jsonInsert, keyspaceName, tableName,info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(401, response.getStatus()); + } + + @Test + public void test6_critical_select_nulltable() throws Exception { + System.out.println("Testing critical select w/ null tablename"); + createAndInsertIntoTable(); + JsonInsert jsonInsert = new JsonInsert(); + Map<String, String> consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonInsert.setConsistencyInfo(consistencyInfo); + Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, authorization, jsonInsert, keyspaceName, null,info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); + } - assertEquals(200, response.getStatus()); - } + @Test + public void test6_select() throws Exception { + System.out.println("Testing select"); + createAndInsertIntoTable(); + JsonSelect jsonSelect = new JsonSelect(); + Map<String, String> consistencyInfo = new HashMap<>(); + MultivaluedMap<String, String> row = new MultivaluedMapImpl(); + row.add("emp_name", "testname"); + consistencyInfo.put("type", "atomic"); + jsonSelect.setConsistencyInfo(consistencyInfo); + Mockito.when(info.getQueryParameters()).thenReturn(row); + Response response = data.select("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, + keyspaceName, tableName, info); + HashMap<String, HashMap<String, Object>> map = (HashMap<String, HashMap<String, Object>>) response.getEntity(); + HashMap<String, Object> result = map.get("result"); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // Values - @Ignore - @Test - public void test6_deleteFromTable1() throws Exception { - System.out.println("Testing delete from table missing delete object"); - createAndInsertIntoTable(); - - JsonDelete jsonDelete = new JsonDelete(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - consistencyInfo.put("type", "atomic"); - jsonDelete.setConsistencyInfo(consistencyInfo); - //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.deleteFromTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonDelete, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + Map<String, String> row0 = (Map<String, String>) result.get("row 0"); + assertEquals("testname", row0.get("emp_name")); + assertEquals(BigInteger.valueOf(500), row0.get("emp_salary")); + } + + @Test + public void test6_select_wrongAuth() throws Exception { + System.out.println("Testing select w/ wrong authentication"); + createAndInsertIntoTable(); + JsonSelect jsonSelect = new JsonSelect(); + Map<String, String> consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonSelect.setConsistencyInfo(consistencyInfo); + Response response = data.select("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, wrongAuthorization, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(400, response.getStatus()); - } + assertEquals(401, response.getStatus()); + } + + @Test + public void test6_select_nullTablename() throws Exception { + System.out.println("Testing select w/ null tablename"); + createAndInsertIntoTable(); + JsonSelect jsonSelect = new JsonSelect(); + Map<String, String> consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonSelect.setConsistencyInfo(consistencyInfo); + Response response = data.select("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, wrongAuthorization, keyspaceName, null, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // delObj - @Test - public void test6_deleteFromTable2() throws Exception { - System.out.println("Testing delete from table missing delete object"); - createAndInsertIntoTable(); - JsonDelete jsonDelete = new JsonDelete(); - Map<String, String> consistencyInfo = new HashMap<>(); - consistencyInfo.put("type", "atomic"); - jsonDelete.setConsistencyInfo(consistencyInfo); - Response response = data.deleteFromTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - null, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(400, response.getStatus()); - } + assertEquals(400, response.getStatus()); + } - @Test - public void test7_dropTable() throws Exception { - System.out.println("Testing drop table"); - createTable(); - JsonTable jsonTable = new JsonTable(); - Map<String, String> consistencyInfo = new HashMap<>(); - consistencyInfo.put("type", "atomic"); - jsonTable.setConsistencyInfo(consistencyInfo); - Response response = data.dropTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } + @Test + public void test6_deleteFromTable() throws Exception { + System.out.println("Testing delete from table"); + createAndInsertIntoTable(); + 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.when(info.getQueryParameters()).thenReturn(row); + Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonDelete, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } + + @Test + public void test6_deleteFromTable_wrongAuth() throws Exception { + System.out.println("Testing delete from table"); + createAndInsertIntoTable(); + JsonDelete jsonDelete = new JsonDelete(); + Map<String, String> consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonDelete.setConsistencyInfo(consistencyInfo); + Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + wrongAuthorization, jsonDelete, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } + @Test + public void test6_deleteFromTable_missingTablename() throws Exception { + System.out.println("Testing delete from table w/ null tablename"); + createAndInsertIntoTable(); + JsonDelete jsonDelete = new JsonDelete(); + Map<String, String> consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonDelete.setConsistencyInfo(consistencyInfo); + Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + wrongAuthorization, jsonDelete, keyspaceName, null, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + + // Values + @Ignore + @Test + public void test6_deleteFromTable1() throws Exception { + System.out.println("Testing delete from table missing delete object"); + createAndInsertIntoTable(); - @Test - public void test8_deleteKeyspace() throws Exception { - System.out.println("Testing drop keyspace"); + JsonDelete jsonDelete = new JsonDelete(); + Map<String, String> consistencyInfo = new HashMap<>(); + MultivaluedMap<String, String> row = new MultivaluedMapImpl(); + consistencyInfo.put("type", "atomic"); + jsonDelete.setConsistencyInfo(consistencyInfo); + Mockito.when(info.getQueryParameters()).thenReturn(row); + Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonDelete, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - 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); - Response response = data.dropKeySpace("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", authorization,appName, keyspaceName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } - assertEquals(400, response.getStatus()); - } - - private static void createAdminTable() throws Exception { - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createAdminKeyspace); - MusicCore.eventualPut(testObject); - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createAdminTable); - 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(), BCrypt.hashpw(password, BCrypt.gensalt()))); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); - 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")); - } - } - + // delObj + @Test + public void test6_deleteFromTable2() throws Exception { + System.out.println("Testing delete from table missing delete object"); + createAndInsertIntoTable(); + JsonDelete jsonDelete = new JsonDelete(); + Map<String, String> consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonDelete.setConsistencyInfo(consistencyInfo); + Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, null, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - private static void createKeyspace() throws Exception { - //shouldn't really be doing this here, but create keyspace is currently turned off - PreparedQueryObject query = new PreparedQueryObject(); - query.appendQueryString(CassandraCQL.createKeySpace); - MusicCore.eventualPut(query); - - boolean isAAF = false; + assertEquals(400, response.getStatus()); + } + + @Test + public void test7_dropTable() throws Exception { + System.out.println("Testing drop table"); + createTable(); + JsonTable jsonTable = new JsonTable(); + Map<String, String> consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonTable.setConsistencyInfo(consistencyInfo); + Response response = data.dropTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); + } + + @Test + public void test7_dropTable_wrongAuth() throws Exception { + System.out.println("Testing drop table w/ wrong auth"); + createTable(); + JsonTable jsonTable = new JsonTable(); + Map<String, String> consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonTable.setConsistencyInfo(consistencyInfo); + Response response = data.dropTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + wrongAuthorization, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(401, response.getStatus()); + } + + @Test + public void test7_dropTable_nullTablename() throws Exception { + System.out.println("Testing drop table w/ null tablename"); + createTable(); + JsonTable jsonTable = new JsonTable(); + Map<String, String> consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonTable.setConsistencyInfo(consistencyInfo); + Response response = data.dropTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, keyspaceName, null); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); + } + + + @Test + public void test8_deleteKeyspace() throws Exception { + System.out.println("Testing drop keyspace"); + + 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); + Response response = data.dropKeySpace("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", authorization, + appName, keyspaceName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); + } + + private static void createKeyspace() throws Exception { + // shouldn't really be doing this here, but create keyspace is currently turned off + PreparedQueryObject query = new PreparedQueryObject(); + query.appendQueryString(CassandraCQL.createKeySpace); + MusicCore.eventualPut(query); + + boolean isAAF = false; String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt()); query = new PreparedQueryObject(); - query.appendQueryString( - "INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " - + "password, username, is_aaf) values (?,?,?,?,?,?,?)"); + query.appendQueryString("INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " + + "password, username, is_aaf) values (?,?,?,?,?,?,?)"); query.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); query.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName)); query.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); @@ -991,66 +1192,70 @@ public class TstRestMusicDataAPI { CachingUtil.updateMusicCache(keyspaceName, appName); CachingUtil.updateMusicValidateCache(appName, userId, hashedpwd); MusicCore.eventualPut(query); - } - - private void clearAllTablesFromKeyspace() throws MusicServiceException { - ArrayList<String> tableNames = new ArrayList<>(); - PreparedQueryObject query = new PreparedQueryObject(); - query.appendQueryString("SELECT table_name FROM system_schema.tables WHERE keyspace_name = '"+keyspaceName+"';"); - ResultSet rs = MusicCore.get(query); - for (Row row: rs) { - tableNames.add(row.getString("table_name")); - } - for (String table: tableNames) { - query = new PreparedQueryObject(); - query.appendQueryString("DROP TABLE " + keyspaceName + "." + table); - MusicCore.eventualPut(query); - } - } - - /** - * Create a table {@link tableName} in {@link keyspaceName} - * @throws Exception - */ - private void 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()); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",appName, authorization, - jsonTable, keyspaceName, tableName); - } - - /** - * Create table {@link createTable} and insert into said table - * @throws Exception - */ - private void createAndInsertIntoTable() throws Exception { - createTable(); - - 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", "testname"); - values.put("emp_salary", 500); - consistencyInfo.put("type", "eventual"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, jsonInsert, keyspaceName, tableName); - } + } + + private void clearAllTablesFromKeyspace() throws MusicServiceException { + ArrayList<String> tableNames = new ArrayList<>(); + PreparedQueryObject query = new PreparedQueryObject(); + query.appendQueryString( + "SELECT table_name FROM system_schema.tables WHERE keyspace_name = '" + keyspaceName + "';"); + ResultSet rs = MusicCore.get(query); + for (Row row : rs) { + tableNames.add(row.getString("table_name")); + } + for (String table : tableNames) { + query = new PreparedQueryObject(); + query.appendQueryString("DROP TABLE " + keyspaceName + "." + table); + MusicCore.eventualPut(query); + } + } + + /** + * Create a table {@link tableName} in {@link keyspaceName} + * + * @throws Exception + */ + private void 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("binary", "blob"); + 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()); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableName); + } + + /** + * Create table {@link createTable} and insert into said table + * + * @throws Exception + */ + private void createAndInsertIntoTable() throws Exception { + createTable(); + + 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", "testname"); + values.put("emp_salary", 500); + values.put("binary", "binarydatahere"); + consistencyInfo.put("type", "eventual"); + jsonInsert.setConsistencyInfo(consistencyInfo); + jsonInsert.setKeyspaceName(keyspaceName); + jsonInsert.setTableName(tableName); + jsonInsert.setValues(values); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonInsert, keyspaceName, tableName); + } } diff --git a/src/test/java/org/onap/music/unittests/TstRestMusicLockAPI.java b/src/test/java/org/onap/music/unittests/TstRestMusicLockAPI.java index 7e45e160..92c5d818 100644 --- a/src/test/java/org/onap/music/unittests/TstRestMusicLockAPI.java +++ b/src/test/java/org/onap/music/unittests/TstRestMusicLockAPI.java @@ -1,16 +1,20 @@ /* - * ============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 + * ============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 + * 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. + * 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============================================= * ==================================================================== @@ -36,6 +40,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mindrot.jbcrypt.BCrypt; import org.mockito.junit.MockitoJUnitRunner; +import org.onap.music.authentication.CachingUtil; import org.onap.music.datastore.MusicDataStoreHandle; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.datastore.jsonobjects.JsonInsert; @@ -43,7 +48,6 @@ import org.onap.music.datastore.jsonobjects.JsonLeasedLock; import org.onap.music.datastore.jsonobjects.JsonTable; import org.onap.music.exceptions.MusicServiceException; import org.onap.music.lockingservice.cassandra.CassaLockStore; -import org.onap.music.main.CachingUtil; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.rest.RestMusicDataAPI; @@ -73,6 +77,7 @@ public class TstRestMusicLockAPI { static String tableName = "employees"; static String onboardUUID = null; static String lockName = "testcassa.employees.testname"; + static String malformedLock = "malformedLock"; @BeforeClass public static void init() throws Exception { @@ -111,6 +116,28 @@ public class TstRestMusicLockAPI { assertTrue(respMap.containsKey("lock")); assertTrue(((Map<String,String>) respMap.get("lock")).containsKey("lock")); } + + @Test + public void test_createLockReference_wrongAuth() throws Exception { + System.out.println("Testing create lockref with wrong auth"); + createAndInsertIntoTable(); + Response response =lock.createLockReference(lockName,"1","1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(401, response.getStatus()); + } + + @Test + public void test_createLockReference_malformedLockName() throws Exception { + System.out.println("Testing create lockref w/ malformed lock"); + createAndInsertIntoTable(); + Response response =lock.createLockReference(malformedLock,"1","1",authorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); + } @Test public void test_accquireLock() throws Exception { @@ -125,6 +152,44 @@ public class TstRestMusicLockAPI { } @Test + public void test_accquireLock_wrongAuth() throws Exception { + System.out.println("Testing acquire lock w/ wrong auth"); + createAndInsertIntoTable(); + String lockRef = createLockReference(); + + Response response = lock.accquireLock(lockRef, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } + + @Test + public void test_accquireBadLock() throws Exception { + System.out.println("Testing acquire lock that is not lock-holder"); + createAndInsertIntoTable(); + + String lockRef1 = createLockReference(); + String lockRef2 = createLockReference(); + + + Response response = lock.accquireLock(lockRef2, "1", "1", authorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + + @Test + public void test_accquireLock_malformedLock() throws Exception { + System.out.println("Testing acquire lock w/ malformedLock"); + createAndInsertIntoTable(); + + Response response = lock.accquireLock(malformedLock, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + + @Test public void test_accquireLockWLease() throws Exception { System.out.println("Testing acquire lock with lease"); createAndInsertIntoTable(); @@ -139,18 +204,31 @@ public class TstRestMusicLockAPI { } @Test - public void test_accquireBadLock() throws Exception { - System.out.println("Testing acquire lock that is not lock-holder"); - createAndInsertIntoTable(); - - String lockRef1 = createLockReference(); - String lockRef2 = createLockReference(); + public void test_accquireLockWLease_wrongAuth() throws Exception { + System.out.println("Testing acquire lock with lease w/ wrong Auth"); + createAndInsertIntoTable(); + String lockRef = createLockReference(); + JsonLeasedLock jsonLock = new JsonLeasedLock(); + jsonLock.setLeasePeriod(10000); //10 second lease period? + Response response = lock.accquireLockWithLease(jsonLock, lockRef, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } + + @Test + public void test_accquireLockWLease_malformedLock() throws Exception { + System.out.println("Testing acquire lock with lease w/ malformed lock"); + createAndInsertIntoTable(); + String lockRef = createLockReference(); - Response response = lock.accquireLock(lockRef2, "1", "1", authorization, - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(400, response.getStatus()); + JsonLeasedLock jsonLock = new JsonLeasedLock(); + jsonLock.setLeasePeriod(10000); //10 second lease period? + Response response = lock.accquireLockWithLease(jsonLock, malformedLock, "1", "1", + wrongAuthorization, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); } @Test @@ -169,6 +247,32 @@ public class TstRestMusicLockAPI { } @Test + public void test_currentLockHolder_wrongAuth() throws Exception { + System.out.println("Testing get current lock holder w/ wrong Auth"); + createAndInsertIntoTable(); + + String lockRef = createLockReference(); + + Response response = lock.currentLockHolder(lockName, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } + + @Test + public void test_currentLockHolder_malformedLock() throws Exception { + System.out.println("Testing get current lock holder w/ malformed lock"); + createAndInsertIntoTable(); + + String lockRef = createLockReference(); + + Response response = lock.currentLockHolder(malformedLock, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + + @Test public void test_unLock() throws Exception { System.out.println("Testing unlock"); createAndInsertIntoTable(); @@ -181,6 +285,30 @@ public class TstRestMusicLockAPI { } @Test + public void test_unLock_wrongAuth() throws Exception { + System.out.println("Testing unlock w/ wrong auth"); + createAndInsertIntoTable(); + String lockRef = createLockReference(); + + Response response = lock.unLock(lockRef, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } + + @Test + public void test_unLock_malformedLock() throws Exception { + System.out.println("Testing unlock w/ malformedLock"); + createAndInsertIntoTable(); + String lockRef = createLockReference(); + + Response response = lock.unLock("malformedLock", "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + + @Test public void test_getLockState() throws Exception { System.out.println("Testing get lock state"); createAndInsertIntoTable(); @@ -196,8 +324,34 @@ public class TstRestMusicLockAPI { } @Test + public void test_getLockState_wrongAuth() throws Exception { + System.out.println("Testing get lock state w/ wrong auth"); + createAndInsertIntoTable(); + + String lockRef = createLockReference(); + + Response response = lock.currentLockState(lockName, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } + + @Test + public void test_getLockState_malformedLock() throws Exception { + System.out.println("Testing get lock state w/ malformedLock"); + createAndInsertIntoTable(); + + String lockRef = createLockReference(); + + Response response = lock.currentLockState(malformedLock, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + + @Test public void test_deleteLock() throws Exception { - System.out.println("Testing get lock state"); + System.out.println("Testing delete lock"); createAndInsertIntoTable(); String lockRef = createLockReference(); @@ -207,6 +361,32 @@ public class TstRestMusicLockAPI { System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); assertEquals(200, response.getStatus()); } + + @Test + public void test_deleteLock_wrongAuth() throws Exception { + System.out.println("Testing delete lock w/ wrong auth"); + createAndInsertIntoTable(); + + String lockRef = createLockReference(); + + Response response = lock.deleteLock(lockName, "1", "1", + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", wrongAuthorization, appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } + + @Test + public void test_deleteLock_malformedLock() throws Exception { + System.out.println("Testing delete lock w/ malformed lock"); + createAndInsertIntoTable(); + + String lockRef = createLockReference(); + + Response response = lock.deleteLock(malformedLock, "1", "1", + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", wrongAuthorization, appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } /** * Create table and lock reference diff --git a/src/test/java/org/onap/music/unittests/jsonobjects/JsonTableTest.java b/src/test/java/org/onap/music/unittests/jsonobjects/JsonTableTest.java index 2279cf0b..4e3b4629 100644 --- a/src/test/java/org/onap/music/unittests/jsonobjects/JsonTableTest.java +++ b/src/test/java/org/onap/music/unittests/jsonobjects/JsonTableTest.java @@ -78,13 +78,6 @@ public class JsonTableTest { } @Test - public void testGetSortingKey() { - String sortKey = "sortkey"; - jt.setSortingKey(sortKey); - assertEquals(sortKey,jt.getSortingKey()); - } - - @Test public void testGetClusteringOrder() { String clusteringOrder = "clusteringOrder"; jt.setClusteringOrder(clusteringOrder); diff --git a/src/test/java/org/onap/music/unittests/jsonobjects/MusicHealthCheckTest.java b/src/test/java/org/onap/music/unittests/jsonobjects/MusicHealthCheckTest.java index f48ecd40..ceda3f3a 100644 --- a/src/test/java/org/onap/music/unittests/jsonobjects/MusicHealthCheckTest.java +++ b/src/test/java/org/onap/music/unittests/jsonobjects/MusicHealthCheckTest.java @@ -44,11 +44,5 @@ public class MusicHealthCheckTest { musicHealthCheck.setCassandrHost("9042"); assertEquals("9042", musicHealthCheck.getCassandrHost()); } - - @Test - public void testZookeeperHost() - { - musicHealthCheck.setZookeeperHost("ZookeeperHost"); - assertEquals("ZookeeperHost", musicHealthCheck.getZookeeperHost()); - } + } |