aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGadicherla, Shiva <sg592n@att.com>2019-12-04 21:52:39 +0000
committerGadicherla, Shiva <sg592n@att.com>2019-12-04 21:57:12 +0000
commit9277729f6cd6fdc0313b98d303fddff2ca8a63af (patch)
treec98e5cb715d20f2be8a1f0f18626c78bdc484cbc
parentf85d0e6b4c429f66896d3dbf06b01653bc2d410b (diff)
Unit test cases for DeadLockDetectionUtil, CipherUtil and CassaLockStore
Issue-ID: MUSIC-521 Signed-off-by: Gadicherla, Shiva <sg592n@att.com> Change-Id: I8544a2003548eff9d26c48f65e5109154924bfe9
-rw-r--r--music-core/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java10
-rw-r--r--music-core/src/test/java/org/onap/music/lockingservice/cassandra/CassaLockStoreTest.java285
-rw-r--r--music-core/src/test/java/org/onap/music/main/CipherUtilTest.java55
-rw-r--r--music-core/src/test/java/org/onap/music/main/DeadlockDetectionUtilTest.java87
4 files changed, 436 insertions, 1 deletions
diff --git a/music-core/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java b/music-core/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java
index edce3fff..6a68b81c 100644
--- a/music-core/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java
+++ b/music-core/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java
@@ -478,7 +478,7 @@ public class CassaLockStore {
queryObject.appendQueryString(" WHERE lockType = ? ALLOW FILTERING");
queryObject.addValue(LockType.WRITE);
- DeadlockDetectionUtil ddu = new DeadlockDetectionUtil();
+ DeadlockDetectionUtil ddu = getDeadlockDetectionUtil();
ResultSet rs = dsHandle.executeLocalQuorumConsistencyGet(queryObject);
logger.debug("rs has " + rs.getAvailableWithoutFetching() + (rs.isFullyFetched()?"":" (or more!)") );
@@ -493,6 +493,14 @@ public class CassaLockStore {
return deadlock;
}
+ /**
+ * This is used for testing purpose
+ * @return new DeadlockDetectionUtil object
+ */
+ DeadlockDetectionUtil getDeadlockDetectionUtil() {
+ return new DeadlockDetectionUtil();
+ }
+
public List<String> getAllLocksForOwner(String ownerId, String keyspace, String table) throws MusicServiceException, MusicQueryException {
List<String> toRet = new ArrayList<String>();
String lockTable = table_prepend_name + table;
diff --git a/music-core/src/test/java/org/onap/music/lockingservice/cassandra/CassaLockStoreTest.java b/music-core/src/test/java/org/onap/music/lockingservice/cassandra/CassaLockStoreTest.java
new file mode 100644
index 00000000..eccdeb5b
--- /dev/null
+++ b/music-core/src/test/java/org/onap/music/lockingservice/cassandra/CassaLockStoreTest.java
@@ -0,0 +1,285 @@
+/*
+ * ============LICENSE_START==========================================
+ * org.onap.music
+ * ===================================================================
+ * Copyright (c) 2019 AT&T Intellectual Property
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ============LICENSE_END=============================================
+ * ====================================================================
+ */
+
+package org.onap.music.lockingservice.cassandra;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.onap.music.datastore.MusicDataStore;
+import org.onap.music.exceptions.MusicLockingException;
+import org.onap.music.exceptions.MusicQueryException;
+import org.onap.music.exceptions.MusicServiceException;
+import org.onap.music.main.DeadlockDetectionUtil;
+
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Row;
+
+public class CassaLockStoreTest {
+
+ private CassaLockStore cassaLockStore;
+ private CassaLockStore.LockObject lockObject;
+ private MusicDataStore dsHandle;
+
+ @Before
+ public void setUp() {
+ dsHandle = Mockito.mock(MusicDataStore.class);
+ cassaLockStore = new CassaLockStore(dsHandle);
+ lockObject = cassaLockStore.new LockObject(false, null, null, null, null, null);
+ }
+
+ @Test
+ public void testLockOwner() {
+ lockObject.setIsLockOwner(true);
+ assertEquals(true, lockObject.getIsLockOwner());
+
+ lockObject.setIsLockOwner(false);
+ assertEquals(false, lockObject.getIsLockOwner());
+ }
+
+ @Test
+ public void testAcquireTime() {
+ lockObject.setAcquireTime("2019-11-11T15:42:12+00:00");
+ assertEquals("2019-11-11T15:42:12+00:00", lockObject.getAcquireTime());
+ }
+
+ @Test
+ public void testCreateTime() {
+ lockObject.setCreateTime("2019-11-11T15:43:44+00:00");
+ assertEquals("2019-11-11T15:43:44+00:00", lockObject.getCreateTime());
+ }
+
+ @Test
+ public void testLockRef() {
+ lockObject.setLockRef("LockReference");
+ assertEquals("LockReference", lockObject.getLockRef());
+ }
+
+ @Test
+ public void testLockType() {
+ lockObject.setLocktype(LockType.READ);
+ assertEquals(LockType.READ, lockObject.getLocktype());
+ }
+
+ @Test
+ public void testOwner() {
+ lockObject.setOwner("Owner");
+ assertEquals("Owner", lockObject.getOwner());
+ }
+
+ @Test
+ public void testCreateLockQueue() {
+ try {
+ Mockito.when(dsHandle.executePut(Mockito.any(), Mockito.any())).thenReturn(true);
+ assertEquals(true, cassaLockStore.createLockQueue("keyspace1", "table1"));
+ } catch (MusicServiceException | MusicQueryException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testGenLockRefandEnQueue() {
+ ResultSet resultSetMock = Mockito.mock(ResultSet.class);
+ List<Row> latestGuardRow = Mockito.mock(List.class);
+ Mockito.when(latestGuardRow.isEmpty()).thenReturn(false);
+ Row row = Mockito.mock(Row.class);
+ Mockito.when(latestGuardRow.get(0)).thenReturn(row);
+ Mockito.when(row.getLong(0)).thenReturn((long) 4);
+ Mockito.when(resultSetMock.all()).thenReturn(latestGuardRow);
+ try {
+ Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
+ Mockito.when(dsHandle.executePut(Mockito.any(), Mockito.any())).thenReturn(true);
+ assertEquals("$keyspace2.table2.lockName2$5",
+ cassaLockStore.genLockRefandEnQueue("keyspace2", "table2", "lockName2", LockType.READ, "owner2"));
+ } catch (MusicServiceException | MusicQueryException | MusicLockingException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testGetLockQueue() {
+ ResultSet resultSetMock = Mockito.mock(ResultSet.class);
+ Iterator<Row> iterator = Mockito.mock(Iterator.class);
+ Mockito.when(iterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
+ Row row = Mockito.mock(Row.class);
+ Mockito.when(row.getLong("lockReference")).thenReturn((long)1).thenReturn((long)2).thenReturn((long)3);
+ Mockito.when(row.get("lockType", LockType.class)).thenReturn(LockType.WRITE).thenReturn(LockType.WRITE).thenReturn(LockType.WRITE);
+ Mockito.when(iterator.next()).thenReturn(row).thenReturn(row).thenReturn(row);
+ Mockito.when(resultSetMock.iterator()).thenReturn(iterator);
+
+ try {
+ Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
+ assertEquals("2", cassaLockStore.getLockQueue("keyspace2", "table2", "key2").get(1));
+ } catch (MusicServiceException | MusicQueryException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testGetLockQueueSize() {
+ ResultSet resultSetMock = Mockito.mock(ResultSet.class);
+ Row row = Mockito.mock(Row.class);
+ Mockito.when(resultSetMock.one()).thenReturn(row);
+ Mockito.when(row.getLong("count")).thenReturn((long) 6);
+ try {
+ Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
+ assertEquals(6, cassaLockStore.getLockQueueSize("keyspace3", "table3", "key3"));
+ } catch (MusicServiceException | MusicQueryException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testPeekLockQueue() {
+ ResultSet resultSetMock = Mockito.mock(ResultSet.class);
+ Row row = Mockito.mock(Row.class);
+ Mockito.when(row.isNull("lockReference")).thenReturn(false);
+ Mockito.when(row.getLong("lockReference")).thenReturn((long) 6);
+ Mockito.when(row.getString("createTime")).thenReturn("2019-11-13T15:05:45+00:00");
+ Mockito.when(row.getString("acquireTime")).thenReturn("2019-11-13T15:05:45+00:00");
+ Mockito.when(row.isNull("lockReference")).thenReturn(false);
+ Mockito.when(resultSetMock.one()).thenReturn(row);
+ try {
+ Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
+ assertEquals("6", cassaLockStore.peekLockQueue("keyspace4", "table4", "key4").getLockRef());
+ } catch (MusicServiceException | MusicQueryException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testGetCurrentLockHolders() {
+ ResultSet resultSetMock = Mockito.mock(ResultSet.class);
+ Iterator<Row> iterator = Mockito.mock(Iterator.class);
+ Mockito.when(iterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
+ Row row = Mockito.mock(Row.class);
+ Mockito.when(row.getLong("lockReference")).thenReturn((long) 5).thenReturn((long) 5);
+ Mockito.when(row.get("lockType", LockType.class)).thenReturn(LockType.WRITE);
+ Mockito.when(iterator.next()).thenReturn(row).thenReturn(row);
+ Mockito.when(resultSetMock.iterator()).thenReturn(iterator);
+ try {
+ Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
+ assertEquals("$keyspace5.table5.key5$5", cassaLockStore.getCurrentLockHolders("keyspace5", "table5", "key5").get(1));
+ } catch (MusicServiceException | MusicQueryException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testIsLockOwner() {
+ ResultSet resultSetMock = Mockito.mock(ResultSet.class);
+ Iterator<Row> iterator = Mockito.mock(Iterator.class);
+ Mockito.when(iterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
+ Row row = Mockito.mock(Row.class);
+ Mockito.when(row.getLong("lockReference")).thenReturn((long) 5);
+ Mockito.when(row.get("lockType", LockType.class)).thenReturn(LockType.WRITE);
+ Mockito.when(iterator.next()).thenReturn(row).thenReturn(row);
+ Mockito.when(resultSetMock.iterator()).thenReturn(iterator);
+ try {
+ Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
+ assertEquals(true, cassaLockStore.isLockOwner("keyspace5", "table5", "key5", "5"));
+ } catch (MusicServiceException | MusicQueryException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testGetLockInfo() {
+ ResultSet resultSetMock = Mockito.mock(ResultSet.class);
+ Row row = Mockito.mock(Row.class);
+ Mockito.when(row.isNull("lockReference")).thenReturn(false);
+ Mockito.when(row.getLong("lockReference")).thenReturn((long) 6);
+ Mockito.when(row.getString("createTime")).thenReturn("2019-11-13T15:05:45+00:00");
+ Mockito.when(row.getString("acquireTime")).thenReturn("2019-11-13T15:05:45+00:00");
+ LockType locktype = Mockito.mock(LockType.class);
+ Mockito.when(row.get("lockType", LockType.class)).thenReturn(locktype);
+ Mockito.when(row.getString("owner")).thenReturn("owner6");
+ Mockito.when(resultSetMock.one()).thenReturn(row);
+
+ try {
+ Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
+ CassaLockStore csLockStore = Mockito.spy(cassaLockStore);
+ Mockito.doReturn(true).when(csLockStore).isLockOwner(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
+ assertEquals("6", csLockStore.getLockInfo("keyspace6", "table6", "key6", "6").getLockRef());
+ } catch (MusicServiceException | MusicQueryException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testCheckForDeadlock() {
+ DeadlockDetectionUtil ddu = Mockito.mock(DeadlockDetectionUtil.class);
+ ResultSet resultSetMock = Mockito.mock(ResultSet.class);
+ Iterator<Row> it = Mockito.mock(Iterator.class);
+ Row row = Mockito.mock(Row.class);
+ Mockito.when(it.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
+ Mockito.when(row.getString("key")).thenReturn("key8");
+ Mockito.when(row.getString("owner")).thenReturn("owner8");
+ Mockito.when(row.getString("acquiretime")).thenReturn("1");
+ Mockito.when(it.next()).thenReturn(row).thenReturn(row).thenReturn(row);
+ Mockito.when(resultSetMock.iterator()).thenReturn(it);
+ CassaLockStore csLockStore = Mockito.spy(cassaLockStore);
+ Mockito.doReturn(ddu).when(csLockStore).getDeadlockDetectionUtil();
+ Mockito.when(ddu.checkForDeadlock(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(true);
+ try {
+ Mockito.when(dsHandle.executeLocalQuorumConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
+ assertEquals(false,
+ cassaLockStore.checkForDeadlock("keyspace8", "table8", "lockName8", LockType.WRITE, "owner8", true));
+ } catch (MusicServiceException | MusicQueryException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testGetAllLocksForOwner() {
+ ResultSet resultSetMock = Mockito.mock(ResultSet.class);
+ Iterator<Row> it = Mockito.mock(Iterator.class);
+ Mockito.when(it.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
+ Row row = Mockito.mock(Row.class);
+ Mockito.when(row.getString("key")).thenReturn("key10");
+ Mockito.when(row.getLong("lockreference")).thenReturn((long) 10);
+ Mockito.when(it.next()).thenReturn(row);
+ Mockito.when(resultSetMock.iterator()).thenReturn(it);
+ try {
+ Mockito.when(dsHandle.executeQuorumConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
+ assertEquals("key10$10", cassaLockStore.getAllLocksForOwner("owneer10", "keyspace10", "table10").get(1));
+ } catch (MusicServiceException | MusicQueryException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+}
diff --git a/music-core/src/test/java/org/onap/music/main/CipherUtilTest.java b/music-core/src/test/java/org/onap/music/main/CipherUtilTest.java
new file mode 100644
index 00000000..ff187ffd
--- /dev/null
+++ b/music-core/src/test/java/org/onap/music/main/CipherUtilTest.java
@@ -0,0 +1,55 @@
+/*
+ * ============LICENSE_START==========================================
+ * org.onap.music
+ * ===================================================================
+ * Copyright (c) 2019 IBM Intellectual Property
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ============LICENSE_END=============================================
+ * ====================================================================
+ */
+
+package org.onap.music.main;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class CipherUtilTest {
+
+ private CipherUtil cipherUtil;
+
+ @Before
+ public void setup() {
+ cipherUtil = new CipherUtil();
+ }
+
+ @Test
+ public void testEncryptPKC() {
+ String encryptedText = CipherUtil.encryptPKC("This is another string to be encrypted",
+ "4BFF9DCCD774F3650E20C4D3F69F8C99");
+ System.out.println("*************************" + encryptedText);
+ assertEquals(88, encryptedText.length());
+ }
+
+ @Test
+ public void testDecryptPKC() {
+ String encryptedText = CipherUtil.encryptPKC("This is another string to be encrypted",
+ "4BFF9DCCD774F3650E20C4D3F69F8C99");
+ assertEquals("This is another string to be encrypted",
+ CipherUtil.decryptPKC(encryptedText, "4BFF9DCCD774F3650E20C4D3F69F8C99"));
+ }
+
+}
diff --git a/music-core/src/test/java/org/onap/music/main/DeadlockDetectionUtilTest.java b/music-core/src/test/java/org/onap/music/main/DeadlockDetectionUtilTest.java
new file mode 100644
index 00000000..ab767e17
--- /dev/null
+++ b/music-core/src/test/java/org/onap/music/main/DeadlockDetectionUtilTest.java
@@ -0,0 +1,87 @@
+/*
+ * ============LICENSE_START==========================================
+ * org.onap.music
+ * ===================================================================
+ * Copyright (c) 2019 IBM Intellectual Property
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ============LICENSE_END=============================================
+ * ====================================================================
+ */
+
+package org.onap.music.main;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import org.junit.Before;
+import org.junit.Test;
+//import org.junit.experimental.runners.Enclosed;
+//import org.junit.runner.RunWith;
+import org.onap.music.main.DeadlockDetectionUtil.OwnershipType;
+
+//@RunWith(Enclosed.class)
+public class DeadlockDetectionUtilTest {
+ private DeadlockDetectionUtil ddu;
+
+ @Before
+ public void setup() {
+ ddu = new DeadlockDetectionUtil();
+ }
+
+ @Test
+ public void testListAllNodes() {
+ ddu = new DeadlockDetectionUtil();
+ ddu.setExisting("r1", "o2", OwnershipType.ACQUIRED);
+ ddu.setExisting("r3", "o2", OwnershipType.ACQUIRED);
+
+ ByteArrayOutputStream outContent = new ByteArrayOutputStream();
+ System.setOut(new PrintStream(outContent));
+ ddu.listAllNodes();
+
+ /*
+ * String expectedOutput = "In DeadlockDetectionUtil: \n" +
+ * " o2 : Node [id=o2, links=r3, visited=false, onStack=false]\n" +
+ * " r3 : Node [id=r3, links=, visited=false, onStack=false]\n" +
+ * " r1 : Node [id=r1, links=o2, visited=false, onStack=false]\n";
+ * assertEquals(expectedOutput, outContent.toString());
+ *
+ * ddu = new DeadlockDetectionUtil(); ddu.setExisting("111", "222",
+ * OwnershipType.CREATED); ddu.setExisting("333", "222", OwnershipType.CREATED);
+ * outContent = new ByteArrayOutputStream(); System.setOut(new
+ * PrintStream(outContent)); ddu.listAllNodes(); expectedOutput =
+ * "In DeadlockDetectionUtil: \n" +
+ * " o222 : Node [id=o222, links=r111r333, visited=false, onStack=false]\n" +
+ * " r333 : Node [id=r333, links=, visited=false, onStack=false]\n" +
+ * " r111 : Node [id=r111, links=, visited=false, onStack=false]";
+ * assertEquals(expectedOutput, outContent.toString());
+ */
+ }
+
+ @Test
+ public void testcheckForDeadlock() {
+ ddu = new DeadlockDetectionUtil();
+ ddu.setExisting("111", "222", DeadlockDetectionUtil.OwnershipType.ACQUIRED);
+ ddu.setExisting("333", "444", DeadlockDetectionUtil.OwnershipType.ACQUIRED);
+ assertEquals(false, ddu.checkForDeadlock("111", "444", DeadlockDetectionUtil.OwnershipType.CREATED));
+
+ ddu = new DeadlockDetectionUtil();
+ ddu.setExisting("111", "222", DeadlockDetectionUtil.OwnershipType.ACQUIRED);
+ ddu.setExisting("333", "444", DeadlockDetectionUtil.OwnershipType.ACQUIRED);
+ ddu.setExisting("333", "222", DeadlockDetectionUtil.OwnershipType.CREATED);
+ assertEquals(true, ddu.checkForDeadlock("111", "444", DeadlockDetectionUtil.OwnershipType.CREATED));
+ }
+}