diff options
author | Tschaen, Brendan <ctschaen@att.com> | 2018-10-16 20:22:35 -0400 |
---|---|---|
committer | Tschaen, Brendan <ctschaen@att.com> | 2018-10-16 20:27:37 -0400 |
commit | 46350c084766789ea59e83f1917c57c81d653048 (patch) | |
tree | 3463279fcee9c26d190d854324bbd7f86422fe2e /src/test/java/org | |
parent | 287bdcbb5482f94df091c3b7b766fed0007efa2e (diff) |
Include Cassandra locking
Change-Id: I085acf8336d5f27782ee12768846a5befd3ee60d
Issue-ID: MUSIC-148
Signed-off-by: Tschaen, Brendan <ctschaen@att.com>
Diffstat (limited to 'src/test/java/org')
13 files changed, 477 insertions, 2032 deletions
diff --git a/src/test/java/org/onap/music/unittests/CassandraCQL.java b/src/test/java/org/onap/music/unittests/CassandraCQL.java index a4c250c2..a9cbe109 100644 --- a/src/test/java/org/onap/music/unittests/CassandraCQL.java +++ b/src/test/java/org/onap/music/unittests/CassandraCQL.java @@ -45,7 +45,7 @@ import com.datastax.driver.core.exceptions.NoHostAvailableException; import org.apache.cassandra.exceptions.ConfigurationException; import org.apache.thrift.transport.TTransportException; import org.cassandraunit.utils.EmbeddedCassandraServerHelper; -import org.onap.music.datastore.MusicDataStore; +import org.onap.music.datastore.CassaDataStore; import org.onap.music.datastore.PreparedQueryObject; public class CassandraCQL { @@ -222,7 +222,7 @@ public class CassandraCQL { return allPossibleIps; } - public static MusicDataStore connectToEmbeddedCassandra() { + public static CassaDataStore connectToEmbeddedCassandra() { Iterator<String> it = getAllPossibleLocalIps().iterator(); String address = "localhost"; @@ -249,7 +249,7 @@ public class CassandraCQL { } } - return new MusicDataStore(cluster, session); + return new CassaDataStore(cluster, session); } diff --git a/src/test/java/org/onap/music/unittests/MusicDataStoreTest.java b/src/test/java/org/onap/music/unittests/MusicDataStoreTest.java index 16d2af02..3f7fd3b7 100644 --- a/src/test/java/org/onap/music/unittests/MusicDataStoreTest.java +++ b/src/test/java/org/onap/music/unittests/MusicDataStoreTest.java @@ -33,7 +33,7 @@ import org.mockito.Mock; import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.datastore.MusicDataStore; +import org.onap.music.datastore.CassaDataStore; import org.onap.music.datastore.PreparedQueryObject; import com.datastax.driver.core.DataType; @@ -44,7 +44,7 @@ import com.datastax.driver.core.TableMetadata; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class MusicDataStoreTest { - static MusicDataStore dataStore; + static CassaDataStore dataStore; static PreparedQueryObject testObject; @BeforeClass diff --git a/src/test/java/org/onap/music/unittests/MusicLockStoreTest.java b/src/test/java/org/onap/music/unittests/MusicLockStoreTest.java new file mode 100644 index 00000000..86774538 --- /dev/null +++ b/src/test/java/org/onap/music/unittests/MusicLockStoreTest.java @@ -0,0 +1,170 @@ +/* + * ============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.assertNotNull; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +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.datastore.CassaDataStore; +import org.onap.music.datastore.CassaLockStore; +import org.onap.music.datastore.PreparedQueryObject; + +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.core.TableMetadata; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class MusicLockStoreTest { + + static CassaDataStore dataStore; + static CassaLockStore lockStore; + static PreparedQueryObject testObject; + + @BeforeClass + public static void init() { + dataStore = CassandraCQL.connectToEmbeddedCassandra(); + lockStore = new CassaLockStore(dataStore); + + } + + @AfterClass + public static void close() throws MusicServiceException, MusicQueryException { + testObject = new PreparedQueryObject(); + testObject.appendQueryString(CassandraCQL.dropKeyspace); + dataStore.executePut(testObject, "eventual"); + dataStore.close(); + + } + + @Test + public void Test1_SetUp() throws MusicServiceException, MusicQueryException { + boolean result = false; + testObject = new PreparedQueryObject(); + testObject.appendQueryString(CassandraCQL.createKeySpace); + result = dataStore.executePut(testObject, "eventual");; + testObject = new PreparedQueryObject(); + testObject.appendQueryString(CassandraCQL.createTableEmployees); + result = dataStore.executePut(testObject, "eventual"); + assertEquals(true, result); + + } + + + @Test + public void Test_createLockQueue() throws MusicServiceException, MusicQueryException { + String keyspace = "testCassa"; + String table = "employees"; + /* + * above two hard coded since this depends on the keyspace and table created in the + * CassandraCQL class. Need to change this.. + */ + boolean result = lockStore.createLockQueue(keyspace,table); + assertEquals(true, result); + } + + @Test + public void Test3_ExecutePut_critical_insert() throws MusicServiceException, MusicQueryException { + testObject = CassandraCQL.setPreparedInsertQueryObject2(); + boolean result = dataStore.executePut(testObject, "Critical"); + assertEquals(true, result); + } + + @Test + public void Test4_ExecutePut_eventual_update() throws MusicServiceException, MusicQueryException { + testObject = CassandraCQL.setPreparedUpdateQueryObject(); + boolean result = false; + result = dataStore.executePut(testObject, "eventual"); + assertEquals(true, result); + } + + @Test + public void Test5_ExecuteEventualGet() throws MusicServiceException, MusicQueryException { + testObject = new PreparedQueryObject(); + testObject.appendQueryString(CassandraCQL.selectALL); + boolean result = false; + int count = 0; + ResultSet output = null; + output = dataStore.executeEventualGet(testObject); + System.out.println(output); + ; + for (Row row : output) { + count++; + System.out.println(row.toString()); + } + if (count == 2) { + result = true; + } + assertEquals(true, result); + } + + @Test + public void Test6_ExecuteCriticalGet() throws MusicServiceException, MusicQueryException { + testObject = CassandraCQL.setPreparedGetQuery(); + boolean result = false; + int count = 0; + ResultSet output = null; + output = dataStore.executeCriticalGet(testObject); + System.out.println(output); + ; + for (Row row : output) { + count++; + System.out.println(row.toString()); + } + if (count == 1) { + result = true; + } + assertEquals(true, result); + } + + @Test(expected = NullPointerException.class) + public void Test7_exception() { + PreparedQueryObject queryObject = null; + try { + dataStore.executePut(queryObject, "critical"); + } catch (MusicQueryException | MusicServiceException e) { + System.out.println(e.getMessage()); + } + } + + @Test + public void Test8_columnDataType() { + DataType data = dataStore.returnColumnDataType("testCassa", "employees", "empName"); + String datatype = data.toString(); + assertEquals("text",datatype); + } + + @Test + public void Test8_columnMetdaData() { + TableMetadata data = dataStore.returnColumnMetadata("testCassa", "employees"); + assertNotNull(data); + } +} diff --git a/src/test/java/org/onap/music/unittests/TestCassaLockStore.java b/src/test/java/org/onap/music/unittests/TestCassaLockStore.java new file mode 100644 index 00000000..bf058121 --- /dev/null +++ b/src/test/java/org/onap/music/unittests/TestCassaLockStore.java @@ -0,0 +1,71 @@ +package org.onap.music.unittests; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import org.onap.music.datastore.CassaLockStore; +import org.onap.music.datastore.PreparedQueryObject; +import org.onap.music.exceptions.MusicQueryException; +import org.onap.music.exceptions.MusicServiceException; +import org.onap.music.main.MusicCore; + +public class TestCassaLockStore { + + + public static void main(String[] args) { + + + try { + CassaLockStore lockStore = new CassaLockStore(); + String keyspace = "ks_testLockStore"; + String table = "table_testLockStore"; + + Map<String,Object> replicationInfo = new HashMap<String, Object>(); + replicationInfo.put("'class'", "'SimpleStrategy'"); + replicationInfo.put("'replication_factor'", 1); + + PreparedQueryObject queryObject = new PreparedQueryObject(); + queryObject.appendQueryString("CREATE KEYSPACE " + keyspace + " WITH REPLICATION = " + replicationInfo.toString().replaceAll("=", ":")); + MusicCore.nonKeyRelatedPut(queryObject, "eventual"); + + + queryObject = new PreparedQueryObject(); + queryObject.appendQueryString("CREATE TABLE " + keyspace + "." + table + " (name text PRIMARY KEY, count varint);"); + MusicCore.createTable(keyspace, table, queryObject, "eventual"); + + + lockStore.createLockQueue(keyspace, table); + + String lockRefb1 = lockStore.genLockRefandEnQueue(keyspace, table, "bharath"); + + + + String lockRefc1 = lockStore.genLockRefandEnQueue(keyspace, table, "cat"); + + String lockRefc2 = lockStore.genLockRefandEnQueue(keyspace, table, "cat"); + + + assert(lockStore.peekLockQueue(keyspace, table, "cat").equals(lockRefc1)); + + assert(!lockStore.peekLockQueue(keyspace, table, "cat").equals(lockRefc2)); + + + assert(lockStore.peekLockQueue(keyspace, table, "bharath").equals(lockRefb1)); + + + lockStore.deQueueLockRef(keyspace, table, "cat", lockRefc1); + + assert(lockStore.peekLockQueue(keyspace, table, "cat").equals(lockRefc2)); + + } catch (MusicServiceException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (MusicQueryException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + +} diff --git a/src/test/java/org/onap/music/unittests/TestLockStore.java b/src/test/java/org/onap/music/unittests/TestLockStore.java deleted file mode 100644 index 4dbc7b4f..00000000 --- a/src/test/java/org/onap/music/unittests/TestLockStore.java +++ /dev/null @@ -1,53 +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 org.apache.log4j.Logger; -import org.onap.music.lockingservice.MusicLockingService; - -public class TestLockStore { - final static Logger logger = Logger.getLogger(TestLockStore.class); - - public static void main(String[] args) throws Exception { - String lockName = "/achristmkllas"; - MusicLockingService ml = new MusicLockingService(); - ml.deleteLock(lockName); - - - logger.info("lockname:" + lockName); - - String lockId1 = ml.createLockId(lockName); - logger.info("lockId1 " + lockId1); - logger.info(ml.isMyTurn(lockId1)); - - String lockId2 = ml.createLockId(lockName); - logger.info("lockId2 " + lockId2); - logger.info("check " + ml.isMyTurn("$bank$x-94608776321630264-0000000000")); - logger.info(ml.isMyTurn(lockId2)); - - // zkClient.unlock(lockId1); - // logger.info(ml.lock(lockId2)); - // zkClient.unlock(lockId2); - } - - -} diff --git a/src/test/java/org/onap/music/unittests/TestMusicCore.java b/src/test/java/org/onap/music/unittests/TestMusicCore.java index ed9c2f68..3a28f963 100644 --- a/src/test/java/org/onap/music/unittests/TestMusicCore.java +++ b/src/test/java/org/onap/music/unittests/TestMusicCore.java @@ -1,678 +1,81 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ package org.onap.music.unittests; -import static org.junit.Assert.*; -import static org.onap.music.main.MusicCore.mDstoreHandle; -import static org.onap.music.main.MusicCore.mLockHandle; +import static org.junit.Assert.assertEquals; + import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Map; -import org.apache.zookeeper.KeeperException.NoNodeException; -import org.junit.Before; -import org.junit.Ignore; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; 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.junit.runners.MethodSorters; +import org.onap.music.datastore.CassaDataStore; +import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.lockingservice.MusicLockState; -import org.onap.music.lockingservice.MusicLockingService; -import org.onap.music.lockingservice.MusicLockState.LockStatus; import org.onap.music.main.MusicCore; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; -import org.onap.music.main.ReturnType; -import org.onap.music.main.MusicCore.Condition; -import org.onap.music.datastore.MusicDataStore; -import org.onap.music.datastore.PreparedQueryObject; -import 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; -@RunWith(MockitoJUnitRunner.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class TestMusicCore { + + static CassaDataStore dataStore; + String keyspace = "MusicCoreUnitTestKp"; + String table = "SampleTable"; - @Mock - private Condition condition; - - @Mock - private ResultSet rs; - - @Mock - private PreparedQueryObject preparedQueryObject; - - @Mock - private Session session; - - @Before - public void setUp() { - mLockHandle = Mockito.mock(MusicLockingService.class); - - } - - @Test - public void testCreateLockReferenceforvalidlock() { - Mockito.when(mLockHandle.createLockId("/" + "test")).thenReturn("lock"); - String lockId = MusicCore.createLockReference("test"); - assertEquals("lock", lockId); - Mockito.verify(mLockHandle).createLockId("/" + "test"); - } - - - @Test - public void testCreateLockReferencefornullname() { - Mockito.when(mLockHandle.createLockId("/" + "test")).thenReturn("lock"); - String lockId = MusicCore.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 = MusicCore.isTableOrKeySpaceLock("ks1.tn1"); - assertTrue(result); - } - - @Test - public void testIsTableOrKeySpaceLockwithPrimarykey() { - Boolean result = MusicCore.isTableOrKeySpaceLock("ks1.tn1.pk1"); - assertFalse(result); - } - - @Test - public void testGetMusicLockState() throws MusicLockingException { - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); - Mockito.when(mLockHandle.getLockState("ks1.tb1.pk1")).thenReturn(musicLockState); - MusicLockState mls = MusicCore.getMusicLockState("ks1.tb1.pk1"); - assertEquals(musicLockState, mls); - Mockito.verify(mLockHandle).getLockState("ks1.tb1.pk1"); - } - - @Test - public void testAcquireLockifisMyTurnTrue() throws MusicLockingException { - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - ReturnType lock = MusicCore.acquireLock("ks1.tn1", "id1"); - assertEquals(lock.getResult(), ResultType.SUCCESS); - Mockito.verify(mLockHandle).isMyTurn("id1"); - } - - @Test - public void testAcquireLockifisMyTurnFalse() throws MusicLockingException { - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); - ReturnType lock = MusicCore.acquireLock("ks1.ts1", "id1"); - assertEquals(lock.getResult(), ResultType.FAILURE); - Mockito.verify(mLockHandle).isMyTurn("id1"); - } - - @Test - public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockTrue() throws MusicLockingException { - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - ReturnType lock = MusicCore.acquireLock("ks1.tn1", "id1"); - assertEquals(lock.getResult(), ResultType.SUCCESS); - Mockito.verify(mLockHandle).isMyTurn("id1"); - } + @BeforeClass + public static void init() { + dataStore = CassandraCQL.connectToEmbeddedCassandra(); + MusicCore.mDstoreHandle = dataStore; - @Test - public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandHaveLock() throws MusicLockingException { - MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - ReturnType lock = MusicCore.acquireLock("ks1.tn1.pk1", "id1"); - assertEquals(lock.getResult(), ResultType.SUCCESS); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1"); - } - @Test - public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandDontHaveLock() throws MusicLockingException { - MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id2"); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - ReturnType lock = MusicCore.acquireLock("ks1.tn1.pk1", "id1"); - assertEquals(lock.getResult(), ResultType.SUCCESS); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1"); } - @Test - public void testAcquireLockifLockRefDoesntExist() throws MusicLockingException { - Mockito.when(mLockHandle.lockIdExists("bs1")).thenReturn(false); - ReturnType lock = MusicCore.acquireLock("ks1.ts1", "bs1"); - assertEquals(lock.getResult(), ResultType.FAILURE); - assertEquals(lock.getMessage(), "Lockid doesn't exist"); - Mockito.verify(mLockHandle).lockIdExists("bs1"); - } - - @Test - public void testAcquireLockWithLeasewithLease() throws MusicLockingException { - MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); - musicLockState.setLeasePeriod(0); - ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); + @AfterClass + public static void close() throws MusicServiceException, MusicQueryException { + dataStore.close(); + } + + @Test + public void Test1_createKeyspace() throws MusicServiceException, MusicQueryException { + + Map<String,Object> replicationInfo = new HashMap<String, Object>(); + replicationInfo.put("'class'", "'SimpleStrategy'"); + replicationInfo.put("'replication_factor'", 1); + + PreparedQueryObject queryObject = new PreparedQueryObject(); + queryObject.appendQueryString( + "CREATE KEYSPACE " + keyspace + " WITH REPLICATION = " + replicationInfo.toString().replaceAll("=", ":")); + MusicCore.nonKeyRelatedPut(queryObject, "eventual"); + + + //check with the system table in cassandra + queryObject = new PreparedQueryObject(); + String systemQuery = "SELECT keyspace_name FROM system_schema.keyspaces where keyspace_name='"+keyspace.toLowerCase()+"';"; + queryObject.appendQueryString(systemQuery); + ResultSet rs = dataStore.executeEventualGet(queryObject); + assert rs.all().size()> 0; } @Test - public void testAcquireLockWithLeasewithException() throws MusicLockingException { - ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "failure"); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenThrow(new MusicLockingException()); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); - } - - @Test - public void testAcquireLockWithLeasewithLockStatusLOCKED() throws MusicLockingException { - MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); - ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); - } - - @Test - public void testAcquireLockWithLeasewithLockStatusUNLOCKED() throws MusicLockingException { - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); - ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); - - } - - @Test - public void testAcquireLockWithLeaseIfNotMyTurn() throws MusicLockingException { - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); - ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); - ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1"); - } - - @Test - public void testQuorumGet() throws MusicServiceException, MusicQueryException { - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - mDstoreHandle = Mockito.mock(MusicDataStore.class); - rs = Mockito.mock(ResultSet.class); - session = Mockito.mock(Session.class); - Mockito.when(mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs); - ResultSet rs1 = MusicCore.quorumGet(preparedQueryObject); - assertNotNull(rs1); - } - - @Test - public void testGetLockNameFromId() { - String lockname = MusicCore.getLockNameFromId("lockName$id"); - assertEquals("lockName", lockname); - } - - @Test - public void testDestroyLockRef() throws NoNodeException { - Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1"); - MusicCore.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 = MusicCore.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 = MusicCore.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"); - MusicCore.deleteLock("id1"); - Mockito.verify(mLockHandle).deleteLock("/" + "id1"); - } - - /* - * @Test public void testNonKeyRelatedPut() throws Exception { mDstoreHandle = - * Mockito.mock(MusicDataStore.class); Mockito.when(mDstoreHandle.executePut("qu1", - * "consistency")).thenReturn(true); Boolean result = MusicCore.nonKeyRelatedPut("qu1", - * "consistency"); assertTrue(result); Mockito.verify(mDstoreHandle).executePut("qu1", - * "consistency"); } - */ - - @Test - public void testEventualPutPreparedQuery() throws MusicServiceException, MusicQueryException { - mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); - session = Mockito.mock(Session.class); - Mockito.when(mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(true); - ReturnType actualResult = MusicCore.eventualPut(preparedQueryObject); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "eventual"); - } - - @Test - public void testEventualPutPreparedQuerywithResultFalse() - throws MusicServiceException, MusicQueryException { - mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); - session = Mockito.mock(Session.class); - Mockito.when(mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(false); - ReturnType actualResult = MusicCore.eventualPut(preparedQueryObject); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "eventual"); - //Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, MusicUtil.EVENTUAL); - } - - @Test - public void testCriticalPutPreparedQuerywithValidLockId() - throws Exception { - mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); - Mockito.when(condition.testCondition()).thenReturn(true); - session = Mockito.mock(Session.class); - Mockito.when(mDstoreHandle.getSession()).thenReturn(session); - ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true); - ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject, - "id1", condition); - assertEquals(expectedResult.getResult(), returnType.getResult()); - Mockito.verify(condition).testCondition(); - Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1"); - Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "critical"); - } - - @Test - public void testCriticalPutPreparedQuerywithInvalidLockId() throws MusicLockingException { - mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); - ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject, - "id1", condition); - assertEquals(expectedResult.getResult(), returnType.getResult()); - Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1"); - } - - @Test - public void testCriticalPutPreparedQuerywithvalidLockIdandTestConditionFalse() throws Exception { - mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); - Mockito.when(condition.testCondition()).thenReturn(false); - ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject, - "id1", condition); - assertEquals(expectedResult.getResult(), returnType.getResult()); - Mockito.verify(condition).testCondition(); - Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1"); - } - - @Test - public void testNonKeyRelatedPutPreparedQuery() throws Exception { - mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - session = Mockito.mock(Session.class); - Mockito.when(mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "consistency")).thenReturn(true); - ResultType result = MusicCore.nonKeyRelatedPut(preparedQueryObject, "consistency"); - assertEquals(ResultType.SUCCESS, result); - Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "consistency"); - } - - @Test - public void testAtomicPutPreparedQuery() throws Exception { - mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); - MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); - ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); - session = Mockito.mock(Session.class); - Mockito.when(mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - Mockito.when(condition.testCondition()).thenReturn(true); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true); - ReturnType returnType = - MusicCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition); - assertEquals(expectedResult.getResult(), returnType.getResult()); - Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(condition).testCondition(); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()) - .getLockState("ks1" + "." + "tn1" + "." + "pk1"); - Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "critical"); - } - - @Test - public void testAtomicPutPreparedQuerywithAcquireLockWithLeaseFalse() throws MusicLockingException { - mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); - ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); - ReturnType returnType = - MusicCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition); - assertEquals(expectedResult.getResult(), returnType.getResult()); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); - } - - @Test - public void testAtomicGetPreparedQuery() throws MusicServiceException, MusicQueryException, MusicLockingException { - mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - rs = Mockito.mock(ResultSet.class); - session = Mockito.mock(Session.class); - Mockito.when(mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); - MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs); - ResultSet rs1 = MusicCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject); - assertNotNull(rs1); - Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()) - .getLockState("ks1" + "." + "tn1" + "." + "pk1"); - Mockito.verify(mDstoreHandle).executeCriticalGet(preparedQueryObject); - } - - @Test - public void testAtomicGetPreparedQuerywithAcquireLockWithLeaseFalse() - throws MusicServiceException, MusicLockingException { - mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - rs = Mockito.mock(ResultSet.class); - Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); - ResultSet rs1 = MusicCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject); - assertNull(rs1); - Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); - Mockito.verify(mLockHandle).isMyTurn("id1"); - } - - @Test - public void testGetPreparedQuery() throws MusicServiceException, MusicQueryException { - mDstoreHandle = Mockito.mock(MusicDataStore.class); - rs = Mockito.mock(ResultSet.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - session = Mockito.mock(Session.class); - Mockito.when(mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(mDstoreHandle.executeEventualGet(preparedQueryObject)).thenReturn(rs); - ResultSet rs1 = MusicCore.get(preparedQueryObject); - assertNotNull(rs1); - Mockito.verify(mDstoreHandle).executeEventualGet(preparedQueryObject); + public void Test1_createTable() throws MusicServiceException, MusicQueryException { + PreparedQueryObject queryObject = new PreparedQueryObject(); + queryObject.appendQueryString( + "CREATE TABLE " + keyspace + "." + table + " (name text PRIMARY KEY, count varint);"); + MusicCore.createTable(keyspace, table, queryObject, "eventual"); + + //check with the system table in cassandra + queryObject = new PreparedQueryObject(); + String systemQuery = "SELECT table_name FROM system_schema.tables where keyspace_name='"+keyspace.toLowerCase()+"' and table_name='"+table.toLowerCase()+"';"; + queryObject.appendQueryString(systemQuery); + ResultSet rs = dataStore.executeEventualGet(queryObject); + assert rs.all().size()> 0; } - @Test - public void testcriticalGetPreparedQuery() throws MusicServiceException, MusicQueryException, MusicLockingException { - mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); - rs = Mockito.mock(ResultSet.class); - session = Mockito.mock(Session.class); - Mockito.when(mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs); - ResultSet rs1 = MusicCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1"); - assertNotNull(rs1); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()) - .getLockState("ks1" + "." + "tn1" + "." + "pk1"); - Mockito.verify(mDstoreHandle).executeCriticalGet(preparedQueryObject); - } - - @Test - public void testcriticalGetPreparedQuerywithInvalidLockId() throws MusicServiceException, MusicLockingException { - mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - ResultSet rs1 = MusicCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1"); - assertNull(rs1); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()) - .getLockState("ks1" + "." + "tn1" + "." + "pk1"); - } - - @Test - public void testAtomicGetPreparedQuerywithDeleteLockWithLeaseFalse() - throws MusicServiceException, MusicLockingException { - mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - rs = Mockito.mock(ResultSet.class); - Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); - ResultSet rs1 = MusicCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject); - assertNull(rs1); - Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); - Mockito.verify(mLockHandle).isMyTurn("id1"); - } - - @Test - public void 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(MusicCore.getLockingServiceHandle()).thenReturn(mLockHandley); - System.out.println("cjc locking test n"); - mLockHandle=mLockHandlea; - assertTrue(true); - - } - - @Test(expected = MusicServiceException.class) - public void testGetDSHandle() throws MusicServiceException, MusicQueryException { - // rs = Mockito.mock(ResultSet.class); - // session = Mockito.mock(Session.class); - //Mockito.when(mDstoreHandle.getSession()).thenReturn(session); - //Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs); - - MusicDataStore mDstoreHandlea = Mockito.mock(MusicDataStore.class); - //MusicUtil mMusicUtil=Mockito.mock(MusicUtil.class); - //System.out.println("cjc 0 getDsHandle"); - Mockito.when(MusicCore.getDSHandle()).thenReturn(mDstoreHandlea); - // System.out.println("cjc localhost"); - // Mockito.when(mMusicUtil.getMyCassaHost().equals("localhost")).thenReturn(null); - //System.out.println("cjc 1 localhost"); - // mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost()); - // } else { - // mDstoreHandle = new MusicDataStore(); - // } - assertTrue(true); - } - - //add mocking - @Ignore - @Test - public void testGetDSHandleIp() throws MusicServiceException, MusicQueryException { - // rs = Mockito.mock(ResultSet.class); - // session = Mockito.mock(Session.class); - //Mockito.when(mDstoreHandle.getSession()).thenReturn(session); - //Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs); - - //mDstoreHandle = Mockito.mock(MusicDataStore.class); - //MusicUtil mMusicUtil=Mockito.mock(MusicUtil.class); - System.out.println("cjc 0 getDsHandleIP"); - Mockito.when(MusicCore.getDSHandle("1.127.0.1")).thenReturn(mDstoreHandle); - System.out.println("cjc localhost"); - // Mockito.when(mMusicUtil.getMyCassaHost().equals("localhost")).thenReturn(null); - System.out.println("cjc 1 localhost IP"); - // mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost()); - // } else { - // mDstoreHandle = new MusicDataStore(); - // } - assertTrue(true); - } - - @Ignore - @Test - public void testPureZkCreate() { - try { - MusicCore.pureZkCreate("local"); - } catch(NullPointerException e) { - System.out.println("cjc zkcreate null pointwer exception:"+ e); - } - } - - @Ignore - @Test - public void testPureZkRead() { //String nodeName) { - byte[] data = MusicCore.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"); - */ - - // mDstoreHandle = Mockito.mock(MusicDataStore.class); - // rs = Mockito.mock(ResultSet.class); - // session = Mockito.mock(Session.class); - //Mockito.when(mDstoreHandle.getSession()).thenReturn(session); - - byte[] data= "Testing Zoo Keeper".getBytes(); - MusicCore.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 (MusicCore.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= MusicCore.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 index d327d0f0..b47c812a 100644 --- a/src/test/java/org/onap/music/unittests/TestMusicCoreIntegration.java +++ b/src/test/java/org/onap/music/unittests/TestMusicCoreIntegration.java @@ -31,9 +31,9 @@ import org.junit.runners.MethodSorters; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.lockingservice.MusicLockState; -import org.onap.music.lockingservice.MusicLockingService; -import org.onap.music.lockingservice.MusicLockState.LockStatus; +import org.onap.music.datastore.CassaLockStore; +import org.onap.music.datastore.MusicLockState; +import org.onap.music.datastore.MusicLockState.LockStatus; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; @@ -53,8 +53,7 @@ public class TestMusicCoreIntegration { public static void init() throws Exception { try { MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra(); - zkServer = new TestingServer(2181, new File("/tmp/zk")); - MusicCore.mLockHandle = new MusicLockingService(); + MusicCore.mLockHandle = new CassaLockStore(MusicCore.mDstoreHandle); } catch (Exception e) { e.printStackTrace(); } @@ -69,15 +68,14 @@ public class TestMusicCoreIntegration { MusicCore.eventualPut(testObject); MusicCore.deleteLock(lockName); MusicCore.mDstoreHandle.close(); - MusicCore.mLockHandle.getzkLockHandle().close(); - MusicCore.mLockHandle.close(); zkServer.stop(); } @Test public void Test1_SetUp() throws MusicServiceException, MusicQueryException { - MusicCore.mLockHandle = new MusicLockingService(); + MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra(); + MusicCore.mLockHandle = new CassaLockStore(MusicCore.mDstoreHandle); ResultType result = ResultType.FAILURE; testObject = new PreparedQueryObject(); testObject.appendQueryString(CassandraCQL.createKeySpace); @@ -97,24 +95,6 @@ public class TestMusicCoreIntegration { assertEquals(ResultType.SUCCESS, returnType.getResult()); } - @Test - public void Test3_atomicPutWithDeleteLock() throws Exception { - testObject = new PreparedQueryObject(); - testObject = CassandraCQL.setPreparedInsertQueryObject2(); - ReturnType returnType = MusicCore.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 = MusicCore.atomicGetWithDeleteLock("testCassa", "employees", - "Mr Test one", testObject); - List<Row> rows = resultSet.all(); - assertEquals(1, rows.size()); - } @Test public void Test5_atomicGet() throws Exception { @@ -150,27 +130,8 @@ public class TestMusicCoreIntegration { MusicLockState musicLockState1 = new MusicLockState(LockStatus.UNLOCKED, "id1"); MusicCore.whoseTurnIsIt(lockName); MusicLockState mls = MusicCore.getMusicLockState(lockName); - boolean voluntaryRelease = true; - MusicLockState mls1 = MusicCore.releaseLock(lockId, voluntaryRelease); + MusicLockState mls1 = MusicCore.voluntaryReleaseLock(lockName,lockId); assertEquals(musicLockState.getLockStatus(), mls.getLockStatus()); assertEquals(musicLockState1.getLockStatus(), mls1.getLockStatus()); } - - @Test - public void Test10_create() { - MusicCore.pureZkCreate("/nodeName"); - } - - @Test - public void Test11_write() { - MusicCore.pureZkWrite("nodeName", "I'm Test".getBytes()); - } - - @Test - public void Test12_read() { - byte[] data = MusicCore.pureZkRead("nodeName"); - String data1 = new String(data); - assertEquals("I'm Test", data1); - } - } diff --git a/src/test/java/org/onap/music/unittests/TestRestAdminData.java b/src/test/java/org/onap/music/unittests/TestRestMusicData.java index 892a20f6..e9435ee1 100644 --- a/src/test/java/org/onap/music/unittests/TestRestAdminData.java +++ b/src/test/java/org/onap/music/unittests/TestRestMusicData.java @@ -20,12 +20,8 @@ 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 static org.onap.music.main.MusicCore.mLockHandle; - import java.io.File; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -35,7 +31,6 @@ 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.apache.http.impl.client.AutoRetryHttpClient; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.FixMethodOrder; @@ -48,9 +43,7 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.mockito.runners.MockitoJUnitRunner; -import org.onap.music.conductor.conditionals.JsonConditional; -import org.onap.music.conductor.conditionals.MusicConditional; -import org.onap.music.conductor.conditionals.RestMusicConditionalAPI; +import org.onap.music.datastore.CassaLockStore; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.datastore.jsonobjects.JsonDelete; import org.onap.music.datastore.jsonobjects.JsonInsert; @@ -59,24 +52,13 @@ import org.onap.music.datastore.jsonobjects.JsonOnboard; 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.lockingservice.MusicLockState; -import org.onap.music.lockingservice.MusicLockingService; -import org.onap.music.lockingservice.MusicLockState.LockStatus; 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.main.ReturnType; import org.onap.music.rest.RestMusicAdminAPI; -import org.onap.music.rest.RestMusicBmAPI; import org.onap.music.rest.RestMusicDataAPI; -import org.onap.music.rest.RestMusicHealthCheckAPI; import org.onap.music.rest.RestMusicLocksAPI; -import org.onap.music.rest.RestMusicTestAPI; -import org.onap.music.rest.RestMusicVersionAPI; - -import com.datastax.driver.core.ConsistencyLevel; import com.datastax.driver.core.DataType; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; @@ -85,7 +67,7 @@ import com.sun.jersey.core.util.MultivaluedMapImpl; @FixMethodOrder(MethodSorters.NAME_ASCENDING) @RunWith(MockitoJUnitRunner.class) -public class TestRestAdminData { +public class TestRestMusicData { RestMusicDataAPI data = new RestMusicDataAPI(); RestMusicAdminAPI admin = new RestMusicAdminAPI(); @@ -118,7 +100,6 @@ public class TestRestAdminData { static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6"); static String keyspaceName = "testCassa"; static String tableName = "employees"; - static String tableNameConditional = "Conductor"; static String xLatestVersion = "X-latestVersion"; static String onboardUUID = null; static String lockId = null; @@ -128,8 +109,7 @@ public class TestRestAdminData { public static void init() throws Exception { try { MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra(); - zkServer = new TestingServer(2181, new File("/tmp/zk")); - MusicCore.mLockHandle = new MusicLockingService(); + MusicCore.mLockHandle = new CassaLockStore(MusicCore.mDstoreHandle); } catch (Exception e) { e.printStackTrace(); } @@ -144,8 +124,6 @@ public class TestRestAdminData { testObject.appendQueryString("DROP KEYSPACE IF EXISTS admin"); MusicCore.eventualPut(testObject); MusicCore.mDstoreHandle.close(); - MusicCore.mLockHandle.getzkLockHandle().close(); - MusicCore.mLockHandle.close(); zkServer.stop(); } @@ -224,25 +202,6 @@ public class TestRestAdminData { } @Test - public void Test2_createKeyspace1() throws Exception { - JsonKeySpace jsonKeyspace = new JsonKeySpace(); - Map<String, String> consistencyInfo = new HashMap<>(); - Map<String, Object> replicationInfo = new HashMap<>(); - consistencyInfo.put("type", "eventual"); - replicationInfo.put("class", "SimpleStrategy"); - replicationInfo.put("replication_factor", 1); - jsonKeyspace.setConsistencyInfo(consistencyInfo); - jsonKeyspace.setDurabilityOfWrites("true"); - jsonKeyspace.setKeyspaceName(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 is " + response.getStatus()); - System.out.println("Entity" + response.getEntity()); - assertEquals(200,response.getStatus()); - } - - @Test public void Test2_createKeyspace0() throws Exception { JsonKeySpace jsonKeyspace = new JsonKeySpace(); Map<String, String> consistencyInfo = new HashMap<>(); @@ -309,9 +268,9 @@ public class TestRestAdminData { String authorizationx = new String(Base64.encode(authDatax.getBytes())); try { Response response = data.createKeySpace("1", "1", "1", null,authorizationx, appName, jsonKeyspace, keyspaceName); - //System.out.println("#######status is " + response.getStatus()); - //System.out.println("Entity" + response.getEntity()); - //assertNotEquals(200,response.getStatus()); + System.out.println("#######status is " + response.getStatus()); + System.out.println("Entity" + response.getEntity()); + assertNotEquals(200,response.getStatus()); } catch (RuntimeException e ) { System.out.println("#######status is runtime exception= " + e); } @@ -482,55 +441,6 @@ public class TestRestAdminData { assertEquals(401, response.getStatus()); } - @Test - public void Test3_createTable3_with_samePartition_clusteringKeys() 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, emp_name)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPartitionKey("emp_name"); - jsonTable.setClusteringKey("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); - System.out.println("#######status is " + response.getStatus()); - System.out.println("Entity" + response.getEntity()); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test3_createTable3_with_Partition_clusteringKeys() 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"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPartitionKey("emp_name"); - jsonTable.setClusteringKey("uuid"); - 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, "tableName1"); - System.out.println("#######status is " + response.getStatus()); - System.out.println("Entity" + response.getEntity()); - assertEquals(200, response.getStatus()); - } - // Improper parenthesis in key field @Test public void Test3_createTable_badParantesis() throws Exception { @@ -728,6 +638,7 @@ public class TestRestAdminData { } + // good clustering key, need to pass queryparameter @Test public void Test3_createTableIndex_1() throws Exception { String tableNameC ="testTableCinx"; @@ -741,6 +652,7 @@ public class TestRestAdminData { 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); @@ -748,71 +660,22 @@ public class TestRestAdminData { Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, jsonTable, keyspaceName, tableNameC); - MultivaluedMap<String, String> rowParams = Mockito.mock(MultivaluedMap.class); - Mockito.when(info.getQueryParameters()).thenReturn(rowParams); - Mockito.when(rowParams.getFirst("index_name")).thenReturn("my_index"); - response = data.createIndex("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - keyspaceName, tableNameC,"uuid",info); - assertEquals(200, response.getStatus()); - } + System.out.println("#######status is " + response.getStatus()); + System.out.println("Entity" + response.getEntity()); + // 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>(); - @Test - public void Test3_createTableIndex_badindexname() throws Exception { - 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); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); - MultivaluedMap<String, String> rowParams = Mockito.mock(MultivaluedMap.class); - Mockito.when(info.getQueryParameters()).thenReturn(rowParams); - Mockito.when(rowParams.getFirst("index_name")).thenReturn("my index"); + queryParametersMap.put("index_name","inxuuid"); + // Mockito.when(info.getQueryParameters()).thenReturn(new MultivaluedMap<String, String>(queryParametersMap)); + try { response = data.createIndex("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, keyspaceName, tableNameC,"uuid",info); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test3_createTableIndex_wrongindex() throws Exception { - 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); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); - MultivaluedMap<String, String> rowParams = Mockito.mock(MultivaluedMap.class); - Mockito.when(info.getQueryParameters()).thenReturn(rowParams); - Mockito.when(rowParams.getFirst("index_name")).thenReturn("my_index"); - response = data.createIndex("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - keyspaceName, tableNameC,"id",info); - assertEquals(400, response.getStatus()); + assertEquals(200, response.getStatus()); + } catch (NullPointerException e) { + + } } @Test @@ -896,176 +759,8 @@ public class TestRestAdminData { assertEquals(400, response.getStatus()); } - @Test - public void Test4_insertIntoTable5() throws Exception { - JsonInsert jsonInsert = new JsonInsert(); - Map<String, String> consistencyInfo = new HashMap<>(); - Map<String, Object> values = new HashMap<>(); - values.put("id", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); - values.put("emp_name", "test1"); - values.put("emp_salary", 1500); - consistencyInfo.put("type", "eventual"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, tableName); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test4_insertIntoTable6() throws Exception { - JsonInsert jsonInsert = new JsonInsert(); - Map<String, String> consistencyInfo = new HashMap<>(); - Map<String, Object> values = new HashMap<>(); - values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); - values.put("emp_salary", 1500); - consistencyInfo.put("type", "eventual"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, tableName); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test4_insertIntoTable7() throws Exception { - JsonInsert jsonInsert = new JsonInsert(); - Map<String, String> consistencyInfo = new HashMap<>(); - Map<String, Object> values = new HashMap<>(); - values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); - values.put("emp_name", "test2"); - values.put("emp_salary", 1500); - consistencyInfo.put("type", "eventual"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - jsonInsert.setTtl("1000"); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, tableName); - assertEquals(200, response.getStatus()); - } - - @Test - public void Test4_insertIntoTable8() throws Exception { - JsonInsert jsonInsert = new JsonInsert(); - Map<String, String> consistencyInfo = new HashMap<>(); - Map<String, Object> values = new HashMap<>(); - values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); - values.put("emp_name", "test3"); - values.put("emp_salary", 1500); - consistencyInfo.put("type", "eventual"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - jsonInsert.setTimestamp("15000"); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, tableName); - assertEquals(200, response.getStatus()); - } @Test - public void Test4_insertIntoTable9() throws Exception { - JsonInsert jsonInsert = new JsonInsert(); - Map<String, String> consistencyInfo = new HashMap<>(); - Map<String, Object> values = new HashMap<>(); - values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); - values.put("emp_name", "test4"); - values.put("emp_salary", 1500); - consistencyInfo.put("type", "eventual"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - jsonInsert.setTtl("1000"); - jsonInsert.setTimestamp("15000"); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, tableName); - assertEquals(200, response.getStatus()); - } - - @Test - public void Test4_insertIntoTable10() throws Exception { - JsonInsert jsonInsert = new JsonInsert(); - Map<String, String> consistencyInfo = new HashMap<>(); - Map<String, Object> values = new HashMap<>(); - values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); - values.put("emp_name", "test5"); - values.put("emp_salary", 1500); - consistencyInfo.put("type", "critical"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - jsonInsert.setTtl("1000"); - jsonInsert.setTimestamp("15000"); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, tableName); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test4_insertIntoTable11() throws Exception { - JsonInsert jsonInsert = new JsonInsert(); - Map<String, String> consistencyInfo = new HashMap<>(); - Map<String, Object> values = new HashMap<>(); - values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); - values.put("emp_name", "test6"); - values.put("emp_salary", 1500); - consistencyInfo.put("type", "atomic_delete_lock"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - jsonInsert.setTtl("1000"); - jsonInsert.setTimestamp("15000"); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, tableName); - assertEquals(200, response.getStatus()); - } - - @Test - public void Test4_insertIntoTable12() throws Exception { - JsonInsert jsonInsert = new JsonInsert(); - Map<String, String> consistencyInfo = new HashMap<>(); - Map<String, Object> values = new HashMap<>(); - values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); - values.put("emp_name", "test7"); - values.put("emp_salary", 1500); - consistencyInfo.put("type", "atomic"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - jsonInsert.setTtl("1000"); - jsonInsert.setTimestamp("15000"); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, tableName); - assertEquals(200, response.getStatus()); - } - - @Test public void Test5_updateTable() throws Exception { JsonUpdate jsonUpdate = new JsonUpdate(); Map<String, String> consistencyInfo = new HashMap<>(); @@ -1083,8 +778,8 @@ public class TestRestAdminData { Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, jsonUpdate, keyspaceName, tableName, info); assertEquals(200, response.getStatus()); - } - + } + // need mock code to create error for MusicCore methods @Test public void Test5_updateTableAuthE() throws Exception { @@ -1100,13 +795,20 @@ public class TestRestAdminData { 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); + try { Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, jsonUpdate, keyspaceName, tableName, info); assertEquals(200, response.getStatus()); - } - + } catch(NullPointerException e) { + + } + } + @Test public void Test5_updateTableAuthException1() throws Exception { JsonUpdate jsonUpdate = new JsonUpdate(); @@ -1120,16 +822,18 @@ public class TestRestAdminData { jsonUpdate.setKeyspaceName(keyspaceName); jsonUpdate.setTableName(tableName); jsonUpdate.setValues(values); + Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); Mockito.when(info.getQueryParameters()).thenReturn(row); String authDatax = ":";//+password; String authorizationx = new String(Base64.encode(authDatax.getBytes())); try { Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorizationx, jsonUpdate, keyspaceName, tableName, info); + authorizationx, jsonUpdate, keyspaceName, tableName, info); assertEquals(200, response.getStatus()); } catch(RuntimeException e) { System.out.println("Update table Runtime exception="+e); + } } @@ -1146,6 +850,7 @@ public class TestRestAdminData { jsonUpdate.setKeyspaceName(keyspaceName); jsonUpdate.setTableName(tableName); jsonUpdate.setValues(values); + Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); Mockito.when(info.getQueryParameters()).thenReturn(row); String authDatax =":"+password; @@ -1158,218 +863,11 @@ public class TestRestAdminData { assertEquals(200, response.getStatus()); } catch(RuntimeException e) { System.out.println("Update table Runtime exception="+e); + } } - @Test - public void Test5_updateTable_wrongauth() throws Exception { - JsonUpdate jsonUpdate = new JsonUpdate(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - Map<String, Object> values = new HashMap<>(); - row.add("emp_name", "testName"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - wrongAuthorization, jsonUpdate, keyspaceName, tableName, info); - assertEquals(401, response.getStatus()); - } - - @Test - public void Test5_updateTable_invalidColumn() throws Exception { - JsonUpdate jsonUpdate = new JsonUpdate(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - Map<String, Object> values = new HashMap<>(); - row.add("id", "testName"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test5_updateTable_ttl() throws Exception { - JsonUpdate jsonUpdate = new JsonUpdate(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - Map<String, Object> values = new HashMap<>(); - row.add("emp_name", "testName8"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - jsonUpdate.setTtl("1000"); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test5_updateTable_timsetamp() throws Exception { - JsonUpdate jsonUpdate = new JsonUpdate(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - Map<String, Object> values = new HashMap<>(); - row.add("emp_name", "testName9"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - jsonUpdate.setTimestamp("15000"); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test5_updateTable_ttl_timestamp() throws Exception { - JsonUpdate jsonUpdate = new JsonUpdate(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - Map<String, Object> values = new HashMap<>(); - row.add("emp_name", "testName10"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - jsonUpdate.setTtl("1000"); - jsonUpdate.setTimestamp("15000"); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test5_updateTable_rowIdEmpty() throws Exception { - JsonUpdate jsonUpdate = new JsonUpdate(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - Map<String, Object> values = new HashMap<>(); - //row.add("emp_name", "testName3"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - jsonUpdate.setTtl("1000"); - jsonUpdate.setTimestamp("15000"); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test5_updateTable_conditions() throws Exception { - JsonUpdate jsonUpdate = new JsonUpdate(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - Map<String, Object> values = new HashMap<>(); - Map<String, Object> conditions = new HashMap<>(); - conditions.put("emp_name","testName3"); - row.add("emp_name", "testName3"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - jsonUpdate.setConditions(conditions); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test5_updateTable_eventual() throws Exception { - JsonUpdate jsonUpdate = new JsonUpdate(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - Map<String, Object> values = new HashMap<>(); - row.add("emp_name", "testName"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "eventual"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - assertEquals(200, response.getStatus()); - } - - @Test - public void Test5_updateTable_critical() throws Exception { - JsonUpdate jsonUpdate = new JsonUpdate(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - Map<String, Object> values = new HashMap<>(); - row.add("emp_name", "testName"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "critical"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test5_updateTable_atomic_delete_lock() throws Exception { - JsonUpdate jsonUpdate = new JsonUpdate(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - Map<String, Object> values = new HashMap<>(); - row.add("emp_name", "testName"); - values.put("emp_salary", 2500); - consistencyInfo.put("type", "atomic_delete_lock"); - jsonUpdate.setConsistencyInfo(consistencyInfo); - jsonUpdate.setKeyspaceName(keyspaceName); - jsonUpdate.setTableName(tableName); - jsonUpdate.setValues(values); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - assertEquals(200, response.getStatus()); - } - + @Test public void Test6_select() throws Exception { JsonSelect jsonSelect = new JsonSelect(); @@ -1380,7 +878,7 @@ public class TestRestAdminData { jsonSelect.setConsistencyInfo(consistencyInfo); Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.select("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + 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"); @@ -1388,38 +886,6 @@ public class TestRestAdminData { } @Test - public void Test6_select_withException() throws Exception { - 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.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - RestMusicDataAPI spyData = Mockito.spy(RestMusicDataAPI.class); - Mockito.doThrow(MusicServiceException.class).when(spyData).selectSpecificQuery("v2", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password, keyspaceName, tableName, info, -1); - Response response = spyData.select("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, keyspaceName, tableName, info); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test6_select_nodata() throws Exception { - JsonSelect jsonSelect = new JsonSelect(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - row.add("emp_name", "testName12"); - consistencyInfo.put("type", "atomic"); - jsonSelect.setConsistencyInfo(consistencyInfo); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.select("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, keyspaceName, tableName, info); - assertEquals(200, response.getStatus()); - } - - @Test public void Test6_selectCritical() throws Exception { JsonInsert jsonInsert = new JsonInsert(); Map<String, String> consistencyInfo = new HashMap<>(); @@ -1429,7 +895,7 @@ public class TestRestAdminData { jsonInsert.setConsistencyInfo(consistencyInfo); Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + 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"); @@ -1437,65 +903,6 @@ public class TestRestAdminData { } @Test - public void Test6_selectCritical_without_lockID() throws Exception { - 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.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, jsonInsert, keyspaceName, tableName,info); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test6_selectCritical_with_atomic_delete_lock() throws Exception { - JsonInsert jsonInsert = new JsonInsert(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - row.add("emp_name", "testName"); - consistencyInfo.put("type", "atomic_delete_lock"); - jsonInsert.setConsistencyInfo(consistencyInfo); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, jsonInsert, keyspaceName, tableName,info); - assertEquals(200, response.getStatus()); - } - - @Test - public void Test6_selectCritical_with_nodata() throws Exception { - JsonInsert jsonInsert = new JsonInsert(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - row.add("emp_name", "testName12"); - consistencyInfo.put("type", "atomic_delete_lock"); - jsonInsert.setConsistencyInfo(consistencyInfo); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, jsonInsert, keyspaceName, tableName,info); - assertEquals(200, response.getStatus()); - } - - @Test - public void Test6_select_all() throws Exception { - JsonSelect jsonSelect = new JsonSelect(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl();; - consistencyInfo.put("type", "atomic"); - jsonSelect.setConsistencyInfo(consistencyInfo); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.select("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, keyspaceName, tableName, info); - assertEquals(200, response.getStatus()); - } - - @Test public void Test6_deleteFromTable() throws Exception { JsonDelete jsonDelete = new JsonDelete(); Map<String, String> consistencyInfo = new HashMap<>(); @@ -1545,93 +952,6 @@ public class TestRestAdminData { } @Test - public void Test6_deleteFromTable_columns() throws Exception { - JsonDelete jsonDelete = new JsonDelete(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - row.add("emp_name", "test1"); - consistencyInfo.put("type", "atomic"); - jsonDelete.setConsistencyInfo(consistencyInfo); - ArrayList<String> columns = new ArrayList<>(); - columns.add("uuid"); - jsonDelete.setColumns(columns); - 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); - assertEquals(200, response.getStatus()); - } - - @Test - public void Test6_deleteFromTable_conditions() throws Exception { - JsonDelete jsonDelete = new JsonDelete(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - Map<String, Object> conditions = new HashMap<>(); - conditions.put("emp_name","testName3"); - row.add("emp_name", "test1"); - consistencyInfo.put("type", "atomic"); - jsonDelete.setConsistencyInfo(consistencyInfo); - ArrayList<String> columns = new ArrayList<>(); - jsonDelete.setConditions(conditions); - 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); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test6_deleteFromTable_eventual() throws Exception { - JsonDelete jsonDelete = new JsonDelete(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - row.add("emp_name", "test2"); - consistencyInfo.put("type", "eventual"); - 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); - assertEquals(200, response.getStatus()); - } - - @Test - public void Test6_deleteFromTable_critical() throws Exception { - JsonDelete jsonDelete = new JsonDelete(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - row.add("emp_name", "test2"); - consistencyInfo.put("type", "critical"); - 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); - assertEquals(400, response.getStatus()); - } - - @Test - public void Test6_deleteFromTable_atomic_delete_lock() throws Exception { - JsonDelete jsonDelete = new JsonDelete(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - row.add("emp_name", "test3"); - consistencyInfo.put("type", "atomic_delete_lock"); - 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); - assertEquals(200, response.getStatus()); - } - - @Test public void Test7_dropTable() throws Exception { JsonTable jsonTable = new JsonTable(); Map<String, String> consistencyInfo = new HashMap<>(); @@ -1664,7 +984,7 @@ public class TestRestAdminData { } @Test - public void Test8_deleteKeyspace1() throws Exception { + public void Test8_deleteKeyspace2() throws Exception { JsonKeySpace jsonKeyspace = new JsonKeySpace(); Map<String, String> consistencyInfo = new HashMap<>(); Map<String, Object> replicationInfo = new HashMap<>(); @@ -1677,28 +997,23 @@ public class TestRestAdminData { jsonKeyspace.setReplicationInfo(replicationInfo); Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); Response response = data.dropKeySpace("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - authorization,appName, "keyspaceName"); - assertEquals(200, response.getStatus()); + wrongAuthorization, appName, keyspaceName); + assertEquals(401, response.getStatus()); } @Test - public void Test8_deleteKeyspace2() throws Exception { + public void Test8_deleteKeyspace3() throws Exception { JsonKeySpace jsonKeyspace = new JsonKeySpace(); Map<String, String> consistencyInfo = new HashMap<>(); Map<String, Object> replicationInfo = new HashMap<>(); - consistencyInfo.put("type", "eventual"); - replicationInfo.put("class", "SimpleStrategy"); - replicationInfo.put("replication_factor", 1); - jsonKeyspace.setConsistencyInfo(consistencyInfo); - jsonKeyspace.setDurabilityOfWrites("true"); - jsonKeyspace.setKeyspaceName("TestApp1"); - jsonKeyspace.setReplicationInfo(replicationInfo); Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); Response response = data.dropKeySpace("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - wrongAuthorization, appName, keyspaceName); - assertEquals(401, response.getStatus()); + authorization, appName, keyspaceName); + assertEquals(400, response.getStatus()); } + + @Test public void Test6_onboard() throws Exception { JsonOnboard jsonOnboard = new JsonOnboard(); @@ -1711,18 +1026,6 @@ public class TestRestAdminData { onboardUUID = resultMap.get("Generated AID").toString(); assertEquals("Your application TestApp2 has been onboarded with MUSIC.", resultMap.get("Success")); } - - @Test - public void Test6_onboard_duplicate() throws Exception { - JsonOnboard jsonOnboard = new JsonOnboard(); - jsonOnboard.setAppname("TestApp2"); - jsonOnboard.setIsAAF("false"); - jsonOnboard.setUserId("TestUser2"); - jsonOnboard.setPassword("TestPassword2"); - Response response = admin.onboardAppWithMusic(jsonOnboard); - assertEquals(400, response.getStatus()); - } - // Missing appname @Test public void Test6_onboard1() throws Exception { @@ -1736,7 +1039,7 @@ public class TestRestAdminData { assertEquals("Unauthorized: Please check the request parameters. Some of the required values appName(ns), userId, password, isAAF are missing.", resultMap.get("Exception")); } - + @Test public void Test7_onboardSearch() throws Exception { JsonOnboard jsonOnboard = new JsonOnboard(); @@ -1746,6 +1049,7 @@ public class TestRestAdminData { Map<String, Object> resultMap = (Map<String, Object>) admin.getOnboardedInfoSearch(jsonOnboard).getEntity(); resultMap.containsKey("success"); assertEquals(MusicUtil.DEFAULTKEYSPACENAME, resultMap.get(onboardUUID)); + } // Missing appname @@ -1758,23 +1062,7 @@ public class TestRestAdminData { System.out.println("--->" + resultMap.toString()); resultMap.containsKey("success"); assertEquals(MusicUtil.DEFAULTKEYSPACENAME, resultMap.get(onboardUUID)); - } - - @Test - public void Test7_onboardSearch_empty() throws Exception { - JsonOnboard jsonOnboard = new JsonOnboard(); - Response response = admin.getOnboardedInfoSearch(jsonOnboard); - assertEquals(400, response.getStatus()); - } - @Test - public void Test7_onboardSearch_invalidAid() throws Exception { - JsonOnboard jsonOnboard = new JsonOnboard(); - jsonOnboard.setAppname("TestApp2"); - jsonOnboard.setIsAAF("false"); - jsonOnboard.setAid("abc66ccc-d857-4e90-b1e5-df98a3d40ce6"); - Response response = admin.getOnboardedInfoSearch(jsonOnboard); - assertEquals(400, response.getStatus()); } @Test @@ -1881,364 +1169,4 @@ public class TestRestAdminData { Map<String, Object> resultMap = (Map<String, Object>) lock.deleteLock(lockName,"1","1", null,authorization, appName).getEntity(); assertEquals(ResultType.SUCCESS, resultMap.get("status")); } - - // Version api - @Test - public void Test1_version( ) { - RestMusicVersionAPI versionapi = new RestMusicVersionAPI(); - HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class); - Map<String, Object> resultMap = versionapi.version(servletResponse); - assertEquals(ResultType.SUCCESS, resultMap.get("status")); - } - - //Music Test Api - @Test - public void Test2_testAPI() { - RestMusicTestAPI musicTest = new RestMusicTestAPI(); - HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class); - Map<String, HashMap<String, String>> resultMap = musicTest.simpleTests(servletResponse); - assertNotNull(resultMap); - } - - //Music Health Check - @Test - public void Test3_HealthCheck_cassandra() { - String consistency = "ONE"; - RestMusicHealthCheckAPI healthCheck = new RestMusicHealthCheckAPI(); - HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class); - Response response = healthCheck.cassandraStatus(servletResponse, consistency); - assertEquals(200, response.getStatus()); - } - - @Test - public void Test3_HealthCheck_cassandra_cosistencyQuorum() { - String consistency = "QUORUM"; - RestMusicHealthCheckAPI healthCheck = new RestMusicHealthCheckAPI(); - HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class); - Response response = healthCheck.cassandraStatus(servletResponse, consistency); - assertEquals(200, response.getStatus()); - } - - @Test - public void Test3_HealthCheck_zookeeper() { - RestMusicHealthCheckAPI healthCheck = new RestMusicHealthCheckAPI(); - HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class); - Response response = healthCheck.ZKStatus(servletResponse); - assertEquals(200, response.getStatus()); - } - - @Test - public void Test4_pureZKcreate() throws Exception { - RestMusicBmAPI bmApi = new RestMusicBmAPI(); - bmApi.pureZkCreate("sample"); - } - - @Test - public void Test4_pureZKUpdate() throws Exception { - RestMusicBmAPI bmApi = new RestMusicBmAPI(); - bmApi.pureZkCreate("sample1"); - 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_create"); - values.put("emp_salary", 500); - consistencyInfo.put("type", "eventual"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - bmApi.pureZkUpdate(jsonInsert, "sampleNode1"); - } - - @Test - public void Test4_pureZKGet() throws Exception { - RestMusicBmAPI bmApi = new RestMusicBmAPI(); - bmApi.pureZkGet("sample"); - } - - @Test - public void Test5_ZKAtomicPut_atomic() throws Exception { - RestMusicBmAPI bmApi = new RestMusicBmAPI(); - 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_create"); - values.put("emp_salary", 1500); - consistencyInfo.put("type", "atomic"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - bmApi.pureZkAtomicPut(jsonInsert, "lockname", "sampleNode1"); - } - - @Test - public void Test5_ZKAtomicPut_atomic_with_delete() throws Exception { - RestMusicBmAPI bmApi = new RestMusicBmAPI(); - 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_create"); - values.put("emp_salary", 1500); - consistencyInfo.put("type", "atomic_delete_lock"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - bmApi.pureZkAtomicPut(jsonInsert, "lockname", "sampleNode1"); - } - - @Test - public void Test5_ZKAtomicGet_atomic() throws Exception { - RestMusicBmAPI bmApi = new RestMusicBmAPI(); - 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_create"); - values.put("emp_salary", 1500); - consistencyInfo.put("type", "atomic_delete_lock"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - bmApi.pureZkAtomicGet(jsonInsert, "lockname", "sampleNode1"); - } - - @Test - public void Test5_ZKAtomicGet_atomic_with_delete() throws Exception { - RestMusicBmAPI bmApi = new RestMusicBmAPI(); - 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_create"); - values.put("emp_salary", 1500); - consistencyInfo.put("type", "atomic_delete_lock"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - bmApi.pureZkAtomicGet(jsonInsert, "lockname", "sampleNode1"); - } - - @Test - public void Test5_updateCassa() throws Exception { - RestMusicBmAPI bmApi = new RestMusicBmAPI(); - JsonInsert jsonInsert = new JsonInsert(); - Map<String, String> consistencyInfo = new HashMap<>(); - Map<String, Object> values = new HashMap<>(); - values.put("emp_salary", 1500); - consistencyInfo.put("type", "eventual"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - row.add("emp_name", "testName_create"); - Mockito.when(info.getQueryParameters()).thenReturn(row); - bmApi.updateTableCassa(jsonInsert, keyspaceName, tableName, info); - } - - // RestMusicConditional - @Test - public void Test5_createTable_conditional() 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(tableNameConditional); - 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, tableNameConditional); - System.out.println("#######status is " + response.getStatus()); - System.out.println("Entity" + response.getEntity()); - assertEquals(200, response.getStatus()); - } - - @Test - public void Test6_insertConditional() throws Exception { - RestMusicConditionalAPI conditionalApi = new RestMusicConditionalAPI(); - JsonConditional json = new JsonConditional(); - json.setPrimaryKey("id"); - json.setPrimaryKeyValue("123|abc|port"); - json.setCasscadeColumnName("plans"); - Map<String, Object> tableValues = new HashMap<>(); - tableValues.put("id", "123|abc|port"); - json.setTableValues(tableValues); - Map<String, Object> columnData = new HashMap<>(); - Map<String, String> column = new HashMap<>(); - column.put("created", "time"); - columnData.put("key", "P2"); - columnData.put("value", column); - json.setCasscadeColumnData(columnData); - Map<String, String> cond = new HashMap<>(); - Map<String, String> cond1 = new HashMap<>(); - Map<String, Map<String, String>> conditions = new HashMap<String, Map<String, String>>(); - cond.put("status", "under-spin-up"); - cond1.put("status", "parked"); - conditions.put("exists", cond); - conditions.put("nonexists", cond1); - json.setConditions(conditions); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = conditionalApi.insertConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, keyspaceName, tableNameConditional, json); - assertEquals(200, response.getStatus()); - } - - @Test - public void Test6_insertConditional_primaryKey_null() throws Exception { - RestMusicConditionalAPI conditionalApi = new RestMusicConditionalAPI(); - JsonConditional json = new JsonConditional(); - json.setPrimaryKeyValue("123|abc|port"); - json.setCasscadeColumnName("plans"); - Map<String, Object> tableValues = new HashMap<>(); - tableValues.put("id", "123|abc|port"); - json.setTableValues(tableValues); - Map<String, Object> columnData = new HashMap<>(); - Map<String, String> column = new HashMap<>(); - column.put("created", "time"); - columnData.put("key", "P2"); - columnData.put("value", column); - json.setCasscadeColumnData(columnData); - Map<String, String> cond = new HashMap<>(); - Map<String, String> cond1 = new HashMap<>(); - Map<String, Map<String, String>> conditions = new HashMap<String, Map<String, String>>(); - cond.put("status", "under-spin-up"); - cond1.put("status", "parked"); - conditions.put("exists", cond); - conditions.put("nonexists", cond1); - json.setConditions(conditions); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = conditionalApi.insertConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, keyspaceName, tableNameConditional, json); - assertEquals(401, response.getStatus()); - } - - @Test - public void Test6_insertConditional_wrongAuth() throws Exception { - RestMusicConditionalAPI conditionalApi = new RestMusicConditionalAPI(); - JsonConditional json = new JsonConditional(); - json.setPrimaryKey("id"); - json.setPrimaryKeyValue("123|abc|port"); - json.setCasscadeColumnName("plans"); - Map<String, Object> tableValues = new HashMap<>(); - tableValues.put("id", "123|abc|port"); - json.setTableValues(tableValues); - Map<String, Object> columnData = new HashMap<>(); - Map<String, String> column = new HashMap<>(); - column.put("created", "time"); - columnData.put("key", "P2"); - columnData.put("value", column); - json.setCasscadeColumnData(columnData); - Map<String, String> cond = new HashMap<>(); - Map<String, String> cond1 = new HashMap<>(); - Map<String, Map<String, String>> conditions = new HashMap<String, Map<String, String>>(); - cond.put("status", "under-spin-up"); - cond1.put("status", "parked"); - conditions.put("exists", cond); - conditions.put("nonexists", cond1); - json.setConditions(conditions); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = conditionalApi.insertConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, wrongAuthorization, keyspaceName, tableNameConditional, json); - assertEquals(401, response.getStatus()); - } - - @Test - public void Test7_updateConditional() throws Exception { - RestMusicConditionalAPI conditionalApi = new RestMusicConditionalAPI(); - JsonConditional json = new JsonConditional(); - json.setPrimaryKey("id"); - json.setPrimaryKeyValue("123|abc|port"); - json.setCasscadeColumnName("plans"); - Map<String, Object> tableValues = new HashMap<>(); - tableValues.put("id", "123|abc|port"); - json.setTableValues(tableValues); - Map<String, Object> columnData = new HashMap<>(); - Map<String, String> column = new HashMap<>(); - column.put("created", "time"); - columnData.put("key", "P2"); - columnData.put("value", column); - json.setCasscadeColumnData(columnData); - Map<String, String> cond = new HashMap<>(); - Map<String, String> cond1 = new HashMap<>(); - Map<String, Map<String, String>> conditions = new HashMap<String, Map<String, String>>(); - cond.put("updated", "new time"); - conditions.put("exists", cond); - conditions.put("nonexists", cond1); - json.setConditions(conditions); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = conditionalApi.updateConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, keyspaceName, tableNameConditional, json); - assertEquals(200, response.getStatus()); - } - - @Test - public void Test7_updateConditional_wrongAuth() throws Exception { - RestMusicConditionalAPI conditionalApi = new RestMusicConditionalAPI(); - JsonConditional json = new JsonConditional(); - json.setPrimaryKey("id"); - json.setPrimaryKeyValue("123|abc|port"); - json.setCasscadeColumnName("plans"); - Map<String, Object> tableValues = new HashMap<>(); - tableValues.put("id", "123|abc|port"); - json.setTableValues(tableValues); - Map<String, Object> columnData = new HashMap<>(); - Map<String, String> column = new HashMap<>(); - column.put("created", "time"); - columnData.put("key", "P2"); - columnData.put("value", column); - json.setCasscadeColumnData(columnData); - Map<String, String> cond = new HashMap<>(); - Map<String, String> cond1 = new HashMap<>(); - Map<String, Map<String, String>> conditions = new HashMap<String, Map<String, String>>(); - cond.put("updated", "new time"); - conditions.put("exists", cond); - conditions.put("nonexists", cond1); - json.setConditions(conditions); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = conditionalApi.updateConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, wrongAuthorization, keyspaceName, tableNameConditional, json); - assertEquals(401, response.getStatus()); - } - - @Test - public void Test7_updateConditional_primarykey_null() throws Exception { - RestMusicConditionalAPI conditionalApi = new RestMusicConditionalAPI(); - JsonConditional json = new JsonConditional(); - json.setPrimaryKeyValue("123|abc|port"); - json.setCasscadeColumnName("plans"); - Map<String, Object> tableValues = new HashMap<>(); - tableValues.put("id", "123|abc|port"); - json.setTableValues(tableValues); - Map<String, Object> columnData = new HashMap<>(); - Map<String, String> column = new HashMap<>(); - column.put("created", "time"); - columnData.put("key", "P2"); - columnData.put("value", column); - json.setCasscadeColumnData(columnData); - Map<String, String> cond = new HashMap<>(); - Map<String, String> cond1 = new HashMap<>(); - Map<String, Map<String, String>> conditions = new HashMap<String, Map<String, String>>(); - cond.put("updated", "new time"); - conditions.put("exists", cond); - conditions.put("nonexists", cond1); - json.setConditions(conditions); - Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = conditionalApi.updateConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, keyspaceName, tableNameConditional, json); - assertEquals(401, response.getStatus()); - } }
\ No newline at end of file diff --git a/src/test/java/org/onap/music/unittests/TestRestMusicQAPI.java b/src/test/java/org/onap/music/unittests/TestRestMusicQAPI.java index e75835d1..bdc6558a 100644 --- a/src/test/java/org/onap/music/unittests/TestRestMusicQAPI.java +++ b/src/test/java/org/onap/music/unittests/TestRestMusicQAPI.java @@ -46,6 +46,7 @@ import org.mindrot.jbcrypt.BCrypt; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; +import org.onap.music.datastore.CassaLockStore; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.datastore.jsonobjects.JsonDelete; import org.onap.music.datastore.jsonobjects.JsonInsert; @@ -55,7 +56,6 @@ import org.onap.music.datastore.jsonobjects.JsonKeySpace; import org.onap.music.datastore.jsonobjects.JsonSelect; import org.onap.music.datastore.jsonobjects.JsonTable; import org.onap.music.datastore.jsonobjects.JsonUpdate; -import org.onap.music.lockingservice.MusicLockingService; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; //import org.onap.music.main.ResultType; @@ -122,11 +122,9 @@ public class TestRestMusicQAPI { @BeforeClass public static void init() throws Exception { try { - System.out.println("before class cassandra"); MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra(); - System.out.println("before class zoo"); - zkServer = new TestingServer(2181, new File("/tmp/zk")); - MusicCore.mLockHandle = new MusicLockingService(); + MusicCore.mLockHandle = new CassaLockStore(MusicCore.mDstoreHandle); + // System.out.println("before class keysp"); //resp=data.createKeySpace(majorV,minorV,patchV,aid,appName,userId,password,kspObject,keyspaceName); //System.out.println("after keyspace="+keyspaceName); @@ -213,8 +211,6 @@ public class TestRestMusicQAPI { testObject.appendQueryString("DROP KEYSPACE IF EXISTS admin"); MusicCore.eventualPut(testObject); MusicCore.mDstoreHandle.close(); - MusicCore.mLockHandle.getzkLockHandle().close(); - MusicCore.mLockHandle.close(); zkServer.stop(); } diff --git a/src/test/java/org/onap/music/unittests/TestVotingApp.java b/src/test/java/org/onap/music/unittests/TestVotingApp.java new file mode 100644 index 00000000..19b10fe8 --- /dev/null +++ b/src/test/java/org/onap/music/unittests/TestVotingApp.java @@ -0,0 +1,115 @@ +package org.onap.music.unittests; + +import java.util.HashMap; +import java.util.Map; + +import org.onap.music.datastore.PreparedQueryObject; +import org.onap.music.exceptions.MusicLockingException; +import org.onap.music.exceptions.MusicQueryException; +import org.onap.music.exceptions.MusicServiceException; +import org.onap.music.main.MusicCore; + +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; + +/** + * + */ +public class TestVotingApp +{ + String keyspaceName; + String tableName; + + public TestVotingApp() throws MusicServiceException { + keyspaceName = "VotingAppForMusic"+System.currentTimeMillis(); + tableName = "votecount"; + } + + private void initialize() throws MusicServiceException { + createVotingKeyspace(); + System.out.println("Created keyspaces"); + createVotingTable(); + System.out.println("Created tables"); + + createEntryForCandidate("Popeye"); + createEntryForCandidate("Judy"); + createEntryForCandidate("Flash"); + createEntryForCandidate("Mickey"); + System.out.println("Created candidates"); + } + + private void createVotingKeyspace() throws MusicServiceException { + Map<String,Object> replicationInfo = new HashMap<String, Object>(); + replicationInfo.put("'class'", "'SimpleStrategy'"); + replicationInfo.put("'replication_factor'", 1); + + PreparedQueryObject queryObject = new PreparedQueryObject(); + queryObject.appendQueryString( + "CREATE KEYSPACE " + keyspaceName + " WITH REPLICATION = " + replicationInfo.toString().replaceAll("=", ":")); + + try { + MusicCore.nonKeyRelatedPut(queryObject, "eventual"); + } catch (MusicServiceException e) { + throw(e); + } + } + + private void createVotingTable() throws MusicServiceException { + PreparedQueryObject queryObject = new PreparedQueryObject(); + queryObject.appendQueryString( + "CREATE TABLE " + keyspaceName + "." + tableName + " (name text PRIMARY KEY, count int);"); + + try { + MusicCore.createTable(keyspaceName, tableName, queryObject, "eventual"); + } catch (MusicServiceException e) { + throw (e); + } + } + + private void createEntryForCandidate(String candidateName) throws MusicServiceException { + PreparedQueryObject queryObject = new PreparedQueryObject(); + queryObject.appendQueryString( + "INSERT INTO " + keyspaceName + "." + tableName + " (name, count) " + + "VALUES ('"+candidateName+"', 0);"); + + MusicCore.nonKeyRelatedPut(queryObject, "eventual"); + } + + + private void updateVoteCount(String candidateName, int numVotes) throws MusicLockingException, MusicQueryException, MusicServiceException { + PreparedQueryObject queryObject = new PreparedQueryObject(); + queryObject.appendQueryString( + "UPDATE " + keyspaceName + "." + tableName + " SET count="+numVotes + " where name='"+candidateName+"';"); + MusicCore.atomicPut(keyspaceName, tableName, candidateName, queryObject, null); + } + + private HashMap<String, Integer> readAllVotes() throws MusicServiceException { + PreparedQueryObject queryObject = new PreparedQueryObject(); + queryObject.appendQueryString("SELECT * FROM " + keyspaceName + "." + tableName); + ResultSet rs = MusicCore.get(queryObject); + HashMap<String, Integer> voteCount = new HashMap<String, Integer>(); + for(Row candidate : rs.all()) { + voteCount.put(candidate.getString("name"), candidate.getInt("count")); + } + return voteCount; + } + + public static void main( String[] args ) throws Exception { + TestVotingApp tva = new TestVotingApp(); + tva.initialize(); + + tva.updateVoteCount("Popeye",5); + tva.updateVoteCount("Judy",9); + tva.updateVoteCount("Mickey",8); + tva.updateVoteCount("Flash",1); + tva.updateVoteCount("Flash",2); + + HashMap<String, Integer> voteCount = tva.readAllVotes(); + System.out.println(voteCount); + assert(voteCount.get("Popeye") == 5); + assert(voteCount.get("Judy") == 9); + assert(voteCount.get("Mickey") == 8); + assert(voteCount.get("Flash") == 2); + } + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/music/unittests/jsonobjects/JSONObjectTest.java b/src/test/java/org/onap/music/unittests/jsonobjects/JSONObjectTest.java deleted file mode 100644 index 7f6af4c5..00000000 --- a/src/test/java/org/onap/music/unittests/jsonobjects/JSONObjectTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 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.jsonobjects; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import org.onap.music.datastore.jsonobjects.JSONObject; - -public class JSONObjectTest { - private JSONObject jsonObject; - - @Before - public void setUp() { - jsonObject = new JSONObject(); - } - - @Test - public void testGetSetData() { - jsonObject.setData("data"); - Assert.assertEquals("data", jsonObject.getData()); - } -} diff --git a/src/test/java/org/onap/music/unittests/jsonobjects/JsonNotifyClientResponseTest.java b/src/test/java/org/onap/music/unittests/jsonobjects/JsonNotifyClientResponseTest.java deleted file mode 100644 index 07f4dbb4..00000000 --- a/src/test/java/org/onap/music/unittests/jsonobjects/JsonNotifyClientResponseTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2018 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.jsonobjects; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import org.onap.music.datastore.jsonobjects.JsonNotifyClientResponse; - -public class JsonNotifyClientResponseTest { - private JsonNotifyClientResponse response; - - @Before - public void setUp() { - response = new JsonNotifyClientResponse(); - } - - @Test - public void testGetSetMethods() { - response.setMessage("message"); - response.setStatus("success"); - Assert.assertEquals("message", response.getMessage()); - Assert.assertEquals("success", response.getStatus()); - } -} diff --git a/src/test/java/org/onap/music/unittests/jsonobjects/NameSpaceTest.java b/src/test/java/org/onap/music/unittests/jsonobjects/NameSpaceTest.java deleted file mode 100644 index ee4d3933..00000000 --- a/src/test/java/org/onap/music/unittests/jsonobjects/NameSpaceTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2018 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.jsonobjects; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import org.onap.music.datastore.jsonobjects.NameSpace; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class NameSpaceTest { - private NameSpace nameSpace; - - @Before - public void setUp() { - nameSpace = new NameSpace(); - } - - @Test - public void testGetSetAdmin() { - List<String> list = new ArrayList<String>(); - list.add("admin"); - nameSpace.setAdmin(list); - Assert.assertEquals(list, nameSpace.getAdmin()); - } - - @Test - public void testGetSetName() { - nameSpace.setName("name"); - Assert.assertEquals("name", nameSpace.getName()); - } -} |