diff options
Diffstat (limited to 'music-core/src/test/java')
18 files changed, 1644 insertions, 282 deletions
diff --git a/music-core/src/test/java/org/onap/music/unittests/MusicUtilTest.java b/music-core/src/test/java/org/onap/music/cassandra/MusicUtilTest.java index 39432d07..b7245d78 100644 --- a/music-core/src/test/java/org/onap/music/unittests/MusicUtilTest.java +++ b/music-core/src/test/java/org/onap/music/cassandra/MusicUtilTest.java @@ -21,7 +21,7 @@ * ==================================================================== */ -package org.onap.music.unittests; +package org.onap.music.cassandra; import static org.junit.Assert.*; import java.math.BigInteger; @@ -75,7 +75,7 @@ public class MusicUtilTest { mc = null; mc = MusicUtil.getMusicCoreService(); assertTrue(mc != null); - + } @Test @@ -123,7 +123,7 @@ public class MusicUtilTest { assertEquals(MusicUtil.buildVersion("1","2",null),"1.2"); } -/* + /* @Test public void testBuileVersionResponse() { assertTrue(MusicUtil.buildVersionResponse("1","2","3").getClass().getSimpleName().equals("Builder")); @@ -131,7 +131,7 @@ public class MusicUtilTest { assertTrue(MusicUtil.buildVersionResponse("1","2",null).getClass().getSimpleName().equals("Builder")); assertTrue(MusicUtil.buildVersionResponse(null,null,null).getClass().getSimpleName().equals("Builder")); } -*/ + */ @Test public void testGetConsistency() { assertTrue(ConsistencyLevel.ONE.equals(MusicUtil.getConsistencyLevel("one"))); @@ -174,7 +174,7 @@ public class MusicUtilTest { System.out.println("#######" + myQueryObject.getQuery().isEmpty()); assertFalse(MusicUtil.isValidQueryObject(false,myQueryObject)); - + } @@ -233,7 +233,7 @@ public class MusicUtilTest { assertTrue(result.contains("name")); assertTrue(result.contains("value")); } - + @Test public void test_generateUUID() { //this function shouldn't be in cachingUtil @@ -255,62 +255,62 @@ public class MusicUtilTest { MusicUtil.setLockUsing("testlock"); assertEquals("testlock", MusicUtil.getLockUsing()); } - + @Test public void testCassaPort() { MusicUtil.setCassandraPort(1234); assertEquals(1234, MusicUtil.getCassandraPort()); } - + @Test public void testBuild() { MusicUtil.setBuild("testbuild"); assertEquals("testbuild", MusicUtil.getBuild()); } - + @Test public void testTransId() { MusicUtil.setTransIdPrefix("prefix"); assertEquals("prefix-", MusicUtil.getTransIdPrefix()); } - - + + @Test public void testConversationIdPrefix() { MusicUtil.setConversationIdPrefix("prefix-"); assertEquals("prefix-", MusicUtil.getConversationIdPrefix()); } - + @Test public void testClientIdPrefix() { MusicUtil.setClientIdPrefix("clientIdPrefix"); assertEquals("clientIdPrefix-", MusicUtil.getClientIdPrefix()); } - + @Test public void testMessageIdPrefix() { MusicUtil.setMessageIdPrefix("clientIdPrefix"); assertEquals("clientIdPrefix-", MusicUtil.getMessageIdPrefix()); } - + @Test public void testTransIdPrefix() { MusicUtil.setTransIdPrefix("transIdPrefix"); assertEquals("transIdPrefix-", MusicUtil.getTransIdPrefix()); } - + @Test public void testConvIdReq() { MusicUtil.setConversationIdRequired(true); assertEquals(true, MusicUtil.getConversationIdRequired()); } - + @Test public void testClientIdRequired() { MusicUtil.setClientIdRequired(true); assertEquals(true, MusicUtil.getClientIdRequired()); } - + @Test public void testMessageIdRequired() { MusicUtil.setMessageIdRequired(true); @@ -322,11 +322,24 @@ public class MusicUtilTest { MusicUtil.setTransIdRequired(true); assertEquals(true,MusicUtil.getTransIdRequired()); } -/* + + @Test + public void testGetCassandraConnectTimeOutMS() { + MusicUtil.setCassandraConnectTimeOutMS(2000); + assertEquals(2000,MusicUtil.getCassandraConnectTimeOutMS()); + } + + @Test + public void testGetCassandraReadTimeOutMS() { + MusicUtil.setCassandraReadTimeOutMS(2000); + assertEquals(2000,MusicUtil.getCassandraReadTimeOutMS()); + } + + /* @Test public void testLoadProperties() { PropertiesLoader pl = new PropertiesLoader(); pl.loadProperties(); } -*/ + */ } diff --git a/music-core/src/test/java/org/onap/music/datastore/MusicDataStoreTest.java b/music-core/src/test/java/org/onap/music/datastore/MusicDataStoreTest.java new file mode 100644 index 00000000..4de995f7 --- /dev/null +++ b/music-core/src/test/java/org/onap/music/datastore/MusicDataStoreTest.java @@ -0,0 +1,440 @@ +/******************************************************************************* + * ============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.datastore; + +import static org.junit.Assert.*; +import java.math.BigInteger; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.function.Consumer; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.music.exceptions.MusicQueryException; +import org.onap.music.exceptions.MusicServiceException; +import org.onap.music.main.MusicUtil; +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.CodecRegistry; +import com.datastax.driver.core.ColumnDefinitions; +import com.datastax.driver.core.ColumnDefinitions.Definition; +import com.datastax.driver.core.exceptions.WriteTimeoutException; +import com.datastax.driver.core.ColumnMetadata; +import com.datastax.driver.core.Configuration; +import com.datastax.driver.core.ConsistencyLevel; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.KeyspaceMetadata; +import com.datastax.driver.core.Metadata; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.SimpleStatement; +import com.datastax.driver.core.Statement; +import com.datastax.driver.core.TableMetadata; +import com.datastax.driver.core.WriteType; + +@RunWith(MockitoJUnitRunner.class) +public class MusicDataStoreTest { + + MusicDataStore dataStore; + + @Mock + Session session; + + @Mock + Cluster cluster; + + @Before + public void before() { + CodecRegistry cr = Mockito.mock(CodecRegistry.class); + Configuration config = Mockito.mock(Configuration.class); + Mockito.when(cluster.getConfiguration()).thenReturn(config); + Mockito.when(config.getCodecRegistry()).thenReturn(cr); + dataStore = new MusicDataStore(cluster, session); + } + + @Test + public void testMusicDataStore() { + //MusicDataStore mds = new MusicDataStore(); + } + + @Test + public void testMusicDataStoreClusterSession() { + Session session = Mockito.mock(Session.class); + Cluster cluster = Mockito.mock(Cluster.class); + + CodecRegistry cr = Mockito.mock(CodecRegistry.class); + Configuration config = Mockito.mock(Configuration.class); + Mockito.when(cluster.getConfiguration()).thenReturn(config); + Mockito.when(config.getCodecRegistry()).thenReturn(cr); + + + MusicDataStore mds = new MusicDataStore(cluster, session); + assertEquals(session, mds.getSession()); + assertEquals(cluster, mds.getCluster()); + } + + @Test + public void testSession() { + Session session = Mockito.mock(Session.class); + dataStore.setSession(session); + assertEquals(session, dataStore.getSession()); + } + + @Test + public void testCluster() { + Cluster cluster = Mockito.mock(Cluster.class); + CodecRegistry cr = Mockito.mock(CodecRegistry.class); + Configuration config = Mockito.mock(Configuration.class); + Mockito.when(cluster.getConfiguration()).thenReturn(config); + Mockito.when(config.getCodecRegistry()).thenReturn(cr); + + dataStore.setCluster(cluster); + assertEquals(cluster, dataStore.getCluster()); + } + + @Test + public void testClose() { + dataStore.close(); + Mockito.verify(session).close(); + } + + @Test + public void testReturnColumnDataType() { + Metadata meta = Mockito.mock(Metadata.class); + Mockito.when(cluster.getMetadata()).thenReturn(meta); + KeyspaceMetadata ksmd = Mockito.mock(KeyspaceMetadata.class); + Mockito.when(meta.getKeyspace("keyspace")).thenReturn(ksmd); + TableMetadata tmd = Mockito.mock(TableMetadata.class); + Mockito.when(ksmd.getTable("table")).thenReturn(tmd); + ColumnMetadata cmd = Mockito.mock(ColumnMetadata.class); + Mockito.when(tmd.getColumn("columnName")).thenReturn(cmd); + Mockito.when(cmd.getType()).thenReturn(com.datastax.driver.core.DataType.text()); + + com.datastax.driver.core.DataType dt = dataStore.returnColumnDataType("keyspace", "table", "columnName"); + assertEquals(com.datastax.driver.core.DataType.text(), dt); + } + + @Test + public void testReturnColumnMetadata() { + Metadata meta = Mockito.mock(Metadata.class); + Mockito.when(cluster.getMetadata()).thenReturn(meta); + KeyspaceMetadata ksmd = Mockito.mock(KeyspaceMetadata.class); + Mockito.when(meta.getKeyspace("keyspace")).thenReturn(ksmd); + TableMetadata tmd = Mockito.mock(TableMetadata.class); + Mockito.when(ksmd.getTable("tableName")).thenReturn(tmd); + + dataStore.returnColumnMetadata("keyspace", "tableName"); + assertEquals(tmd, dataStore.returnColumnMetadata("keyspace", "tableName")); + } + + @Test + public void testReturnKeyspaceMetadata() { + Metadata meta = Mockito.mock(Metadata.class); + Mockito.when(cluster.getMetadata()).thenReturn(meta); + KeyspaceMetadata ksmd = Mockito.mock(KeyspaceMetadata.class); + Mockito.when(meta.getKeyspace("keyspace")).thenReturn(ksmd); + + assertEquals(ksmd, dataStore.returnKeyspaceMetadata("keyspace")); + } + + @Test + public void testGetColValue() { + Row row = Mockito.mock(Row.class); + Mockito.when(row.getString("columnName")).thenReturn("value"); + UUID uuid = UUID.randomUUID(); + Mockito.when(row.getUUID("columnName")).thenReturn(uuid); + Mockito.when(row.getVarint("columnName")).thenReturn(BigInteger.ONE); + Mockito.when(row.getLong("columnName")).thenReturn((long) 117); + Mockito.when(row.getInt("columnName")).thenReturn(5); + Mockito.when(row.getFloat("columnName")).thenReturn(Float.MAX_VALUE); + Mockito.when(row.getDouble("columnName")).thenReturn(Double.valueOf("2.5")); + Mockito.when(row.getBool("columnName")).thenReturn(true); + Mockito.when(row.getMap("columnName", String.class, String.class)).thenReturn(new HashMap<String, String>()); + Mockito.when(row.getList("columnName", String.class)).thenReturn(new ArrayList<String>()); + + + assertEquals("value", dataStore.getColValue(row, "columnName", DataType.varchar())); + assertEquals(uuid, dataStore.getColValue(row, "columnName", DataType.uuid())); + assertEquals(BigInteger.ONE, dataStore.getColValue(row, "columnName", DataType.varint())); + assertEquals((long) 117, dataStore.getColValue(row, "columnName", DataType.bigint())); + assertEquals(5, dataStore.getColValue(row, "columnName", DataType.cint())); + assertEquals(Float.MAX_VALUE, dataStore.getColValue(row, "columnName", DataType.cfloat())); + assertEquals(2.5, dataStore.getColValue(row, "columnName", DataType.cdouble())); + assertEquals(true, dataStore.getColValue(row, "columnName", DataType.cboolean())); + assertEquals(0, ((Map<String, String>) dataStore.getColValue(row, "columnName", + DataType.map(DataType.varchar(), DataType.varchar()))).size()); + assertEquals(0, + ((List<String>) dataStore.getColValue(row, "columnName", DataType.list(DataType.varchar()))).size()); + } + + @Test + public void testGetBlobValue() { + Row row = Mockito.mock(Row.class); + Mockito.when(row.getBytes("col")).thenReturn(ByteBuffer.allocate(16)); + + byte[] byteArray = dataStore.getBlobValue(row, "col", DataType.blob()); + assertEquals(16, byteArray.length); + } + + @Test + public void testDoesRowSatisfyCondition() throws Exception { + Row row = Mockito.mock(Row.class); + ColumnDefinitions cd = Mockito.mock(ColumnDefinitions.class); + Mockito.when(row.getColumnDefinitions()).thenReturn(cd); + Mockito.when(cd.getType("col1")).thenReturn(DataType.varchar()); + + Map<String, Object> condition = new HashMap<>(); + condition.put("col1", "val1"); + + Mockito.when(row.getString("col1")).thenReturn("val1"); + + assertTrue(dataStore.doesRowSatisfyCondition(row, condition)); + + condition.put("col1", "val2"); + assertFalse(dataStore.doesRowSatisfyCondition(row, condition)); + } + + @Test + public void testMarshalData() { + ResultSet results = Mockito.mock(ResultSet.class); + Row row = Mockito.mock(Row.class); + Mockito.when(row.getString("colName")).thenReturn("rowValue"); + //mock for (Row row: results) + Iterator mockIterator = Mockito.mock(Iterator.class); + //Mockito.doCallRealMethod().when(results).forEach(Mockito.any(Consumer.class)); + Mockito.when(results.iterator()).thenReturn(mockIterator); + Mockito.when(mockIterator.hasNext()).thenReturn(true, false); + Mockito.when(mockIterator.next()).thenReturn(row); + + ColumnDefinitions cd = Mockito.mock(ColumnDefinitions.class); + Mockito.when(row.getColumnDefinitions()).thenReturn(cd); + //for (Definition: colDefinitions) + Iterator mockIterator2 = Mockito.mock(Iterator.class); + //Mockito.doCallRealMethod().when(cd).forEach(Mockito.any(Consumer.class)); + Mockito.when(cd.iterator()).thenReturn(mockIterator2); + Mockito.when(mockIterator2.hasNext()).thenReturn(true, false); + Definition def = Mockito.mock(Definition.class); + Mockito.when(mockIterator2.next()).thenReturn(def); + Mockito.when(def.getType()).thenReturn(DataType.varchar()); + Mockito.when(def.getName()).thenReturn("colName"); + + Map<String, HashMap<String, Object>> data = dataStore.marshalData(results); + System.out.println("Marshalled data: " + data); + + assertTrue(data.containsKey("row 0")); + assertEquals("rowValue", data.get("row 0").get("colName")); + } + + private ArgumentCaptor<SimpleStatement> sessionExecuteResponse() { + ResultSet rs = Mockito.mock(ResultSet.class); + Mockito.when(session.execute(Mockito.any(Statement.class))).thenReturn(rs); + + ArgumentCaptor<SimpleStatement> argument = ArgumentCaptor.forClass(SimpleStatement.class); + return argument; + } + + @Test + public void testExecutePutPreparedQueryObjectString() throws Exception { + ArgumentCaptor<SimpleStatement> argument = sessionExecuteResponse(); + String queryString = "INSERT INTO cycling.cyclist_name (lastname, firstname) VALUES (?,?);"; + String lastName = "KRUIKSWIJK"; + String firstName = "Steven"; + + PreparedQueryObject query = new PreparedQueryObject(queryString, lastName, firstName); + dataStore.executePut(query, MusicUtil.CRITICAL); + + Mockito.verify(session).execute(argument.capture()); + assertEquals(ConsistencyLevel.QUORUM, argument.getValue().getConsistencyLevel()); + assertEquals(queryString, argument.getValue().getQueryString()); + assertEquals(2, argument.getValue().valuesCount()); + } + + @Test + public void testExecutePut_ONE() throws Exception { + ArgumentCaptor<SimpleStatement> argument = sessionExecuteResponse(); + String queryString = "INSERT INTO cycling.cyclist_name (lastname, firstname) VALUES (?,?);"; + String lastName = "KRUIKSWIJK"; + String firstName = "Steven"; + + PreparedQueryObject query = new PreparedQueryObject(queryString, lastName, firstName); + dataStore.executePut(query, MusicUtil.ONE); + + Mockito.verify(session).execute(argument.capture()); + assertEquals(ConsistencyLevel.ONE, argument.getValue().getConsistencyLevel()); + assertEquals(queryString, argument.getValue().getQueryString()); + assertEquals(2, argument.getValue().valuesCount()); + } + + @Test + public void testExecutePut_quorum() throws Exception { + ArgumentCaptor<SimpleStatement> argument = sessionExecuteResponse(); + String queryString = "INSERT INTO cycling.cyclist_name (lastname, firstname) VALUES (?,?);"; + String lastName = "KRUIKSWIJK"; + String firstName = "Steven"; + + PreparedQueryObject query = new PreparedQueryObject(queryString, lastName, firstName); + dataStore.executePut(query, MusicUtil.QUORUM); + + Mockito.verify(session).execute(argument.capture()); + //should be quorum! + assertEquals(ConsistencyLevel.LOCAL_QUORUM, argument.getValue().getConsistencyLevel()); + assertEquals(queryString, argument.getValue().getQueryString()); + assertEquals(2, argument.getValue().valuesCount()); + } + + @Test + public void testExecutePut_ALL() throws Exception { + ArgumentCaptor<SimpleStatement> argument = sessionExecuteResponse(); + String queryString = "INSERT INTO cycling.cyclist_name (lastname, firstname) VALUES (?,?);"; + String lastName = "KRUIKSWIJK"; + String firstName = "Steven"; + + PreparedQueryObject query = new PreparedQueryObject(queryString, lastName, firstName); + dataStore.executePut(query, MusicUtil.ALL); + + Mockito.verify(session).execute(argument.capture()); + assertEquals(ConsistencyLevel.ALL, argument.getValue().getConsistencyLevel()); + assertEquals(queryString, argument.getValue().getQueryString()); + assertEquals(2, argument.getValue().valuesCount()); + } + + @Test(expected = MusicQueryException.class) + public void testExecutePut_BadQueryObj() throws Exception { + String queryString = "INSERT INTO cycling.cyclist_name (lastname, firstname) VALUES (?,?);"; + String lastName = "KRUIKSWIJK"; + String firstName = "Steven"; + + //Provide extra value here, middle initial + PreparedQueryObject query = new PreparedQueryObject(queryString, lastName, firstName, "P"); + try { + dataStore.executePut(query, MusicUtil.CRITICAL); + } catch (Exception e) { + System.out.println(e.getMessage()); + throw e; + } + + fail("Should have throw error"); + } + + @Test + public void testExecutePutPreparedQueryObjectStringLong() throws Exception { + ArgumentCaptor<SimpleStatement> argument = sessionExecuteResponse(); + String queryString = "INSERT INTO cycling.cyclist_name (lastname, firstname) VALUES ('KRUIKSWIJK','Steven');"; + + + PreparedQueryObject query = new PreparedQueryObject(queryString); + dataStore.executePut(query, MusicUtil.EVENTUAL, 10); + + Mockito.verify(session).execute(argument.capture()); + assertEquals(ConsistencyLevel.ONE, argument.getValue().getConsistencyLevel()); + assertEquals(queryString, argument.getValue().getQueryString()); + } + + @Test + public void testExecuteGet() throws Exception { + ArgumentCaptor<SimpleStatement> argument = sessionExecuteResponse(); + + PreparedQueryObject query = new PreparedQueryObject("SELECT * FROM KEYSPACE.TABLE"); + + dataStore.executeGet(query, MusicUtil.ONE); + + Mockito.verify(session).execute(argument.capture()); + assertEquals(ConsistencyLevel.ONE, argument.getValue().getConsistencyLevel()); + assertEquals("SELECT * FROM KEYSPACE.TABLE", argument.getValue().getQueryString()); + } + + @Test (expected = MusicQueryException.class) + public void testExecuteGet_badQuery() throws Exception { + ArgumentCaptor<SimpleStatement> argument = sessionExecuteResponse(); + + PreparedQueryObject query = new PreparedQueryObject("SELECT * FROM KEYSPACE.TABLE", "broken"); + + dataStore.executeGet(query, MusicUtil.ONE); + + Mockito.verify(session).execute(argument.capture()); + assertEquals(ConsistencyLevel.ONE, argument.getValue().getConsistencyLevel()); + assertEquals("SELECT * FROM KEYSPACE.TABLE", argument.getValue().getQueryString()); + } + + @Test + public void testExecuteOneConsistencyGet() throws Exception { + ArgumentCaptor<SimpleStatement> argument = sessionExecuteResponse(); + PreparedQueryObject query = new PreparedQueryObject("SELECT * FROM KEYSPACE.TABLE"); + + dataStore.executeOneConsistencyGet(query); + + + Mockito.verify(session).execute(argument.capture()); + assertEquals(ConsistencyLevel.ONE, argument.getValue().getConsistencyLevel()); + assertEquals("SELECT * FROM KEYSPACE.TABLE", argument.getValue().getQueryString()); + } + + @Test + public void testExecuteLocalQuorumConsistencyGet() throws Exception { + ArgumentCaptor<SimpleStatement> argument = sessionExecuteResponse(); + PreparedQueryObject query = new PreparedQueryObject("SELECT * FROM KEYSPACE.TABLE"); + + dataStore.executeLocalQuorumConsistencyGet(query); + + Mockito.verify(session).execute(argument.capture()); + assertEquals(ConsistencyLevel.LOCAL_QUORUM, argument.getValue().getConsistencyLevel()); + assertEquals("SELECT * FROM KEYSPACE.TABLE", argument.getValue().getQueryString()); + } + + @Test + public void testExecuteQuorumConsistencyGet() throws Exception { + ArgumentCaptor<SimpleStatement> argument = sessionExecuteResponse(); + PreparedQueryObject query = new PreparedQueryObject("SELECT * FROM KEYSPACE.TABLE"); + + dataStore.executeQuorumConsistencyGet(query); + + Mockito.verify(session).execute(argument.capture()); + assertEquals(ConsistencyLevel.QUORUM, argument.getValue().getConsistencyLevel()); + assertEquals("SELECT * FROM KEYSPACE.TABLE", argument.getValue().getQueryString()); + } + + + @Test + public void testExecutePut() { + Mockito.when(session.execute(Mockito.any(SimpleStatement.class))) + .thenThrow(new WriteTimeoutException(ConsistencyLevel.QUORUM, WriteType.CAS, 1, 3)); + + try { + dataStore.executePut(new PreparedQueryObject("Test query"), "critical"); + } catch (MusicServiceException e) { + return; + } catch (MusicQueryException e) { + // should never reach here + fail(); + } + fail(); + } +} diff --git a/music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonDeleteTest.java b/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonDeleteTest.java index a069b81d..0014f823 100644 --- a/music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonDeleteTest.java +++ b/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonDeleteTest.java @@ -22,7 +22,7 @@ * ==================================================================== */ -package org.onap.music.unittests.jsonobjects; +package org.onap.music.datastore.jsonobjects; import static org.junit.Assert.*; import java.util.ArrayList; @@ -31,7 +31,6 @@ import java.util.List; import java.util.Map; import org.junit.Before; import org.junit.Test; -import org.onap.music.datastore.jsonobjects.JsonDelete; public class JsonDeleteTest { diff --git a/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonIndexTest.java b/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonIndexTest.java new file mode 100644 index 00000000..0d89a339 --- /dev/null +++ b/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonIndexTest.java @@ -0,0 +1,74 @@ +/* + * ============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.datastore.jsonobjects; + +import static org.junit.Assert.*; +import java.util.HashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.onap.music.datastore.PreparedQueryObject; + +public class JsonIndexTest { + + JsonIndex ji = null; + + + @Before + public void init() { + ji = new JsonIndex(null, null, null, null); + } + + + + @Test + public void testKeyspace() { + ji.setKeyspaceName("keyspaceName"); + assertEquals("keyspaceName", ji.getKeyspaceName()); + } + + @Test + public void testIndexName() { + ji.setIndexName("indexName"); + assertEquals("indexName", ji.getIndexName()); + } + + @Test + public void testFieldName() { + ji.setFieldName("field"); + assertEquals("field", ji.getFieldName()); + } + + @Test + public void testTableName() { + ji.setTableName("table"); + assertEquals("table", ji.getTableName()); + } + + @Test + public void testCreateIndexQuery() { + JsonIndex ji2 = new JsonIndex("index", "keyspace", "table", "field"); + PreparedQueryObject query = ji2.genCreateIndexQuery(); + assertEquals("Create index if not exists index on keyspace.table (field);", query.getQuery()); + } +} diff --git a/music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonInsertTest.java b/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonInsertTest.java index 4992af7b..ad71c9ea 100644 --- a/music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonInsertTest.java +++ b/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonInsertTest.java @@ -22,17 +22,31 @@ * ==================================================================== */ -package org.onap.music.unittests.jsonobjects; +package org.onap.music.datastore.jsonobjects; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; - +import static org.junit.Assert.assertTrue; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.commons.lang3.SerializationUtils; import org.junit.Test; -import org.onap.music.datastore.jsonobjects.JsonInsert; +import org.mockito.Mockito; +import org.mockito.internal.util.reflection.FieldSetter; +import org.onap.music.datastore.MusicDataStore; +import org.onap.music.datastore.MusicDataStoreHandle; +import org.onap.music.datastore.PreparedQueryObject; +import org.onap.music.exceptions.MusicQueryException; +import org.onap.music.exceptions.MusicServiceException; +import com.datastax.driver.core.ColumnMetadata; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.Session; +//import org.mockito.internal.util.reflection.Whitebox; +import com.datastax.driver.core.TableMetadata; + public class JsonInsertTest { @@ -107,5 +121,56 @@ public class JsonInsertTest { ji.setObjectMap(map); assertEquals(map, ji.getObjectMap()); } + + @Test + public void testPrimaryKey() { + ji.setPrimaryKeyVal("primKey"); + assertEquals("primKey", ji.getPrimaryKeyVal()); + } + + @Test + public void testGenInsertPreparedQueryObj() throws Exception { + ji.setKeyspaceName("keyspace"); + ji.setTableName("table"); + ji.setPrimaryKeyVal("value"); + Map<String,Object> rowSpec = new HashMap<>(); + rowSpec.put("val1","one"); + rowSpec.put("val2","two"); + ji.setRowSpecification(rowSpec); + Map<String,Object> vals = new HashMap<>(); + vals.put("val1","one"); + vals.put("val2","two"); + ji.setValues(vals); + + Map<String,String> cons = new HashMap<>(); + cons.put("type","quorum"); + ji.setConsistencyInfo(cons); + + MusicDataStore mds = Mockito.mock(MusicDataStore.class); + Session session = Mockito.mock(Session.class); + Mockito.when(mds.getSession()).thenReturn(session); + MusicDataStoreHandle mdsh = Mockito.mock(MusicDataStoreHandle.class); + FieldSetter.setField(mdsh, mdsh.getClass().getDeclaredField("mDstoreHandle"), mds); + TableMetadata tableMeta = Mockito.mock(TableMetadata.class); + Mockito.when(mds.returnColumnMetadata(Mockito.anyString(), Mockito.anyString())) + .thenReturn(tableMeta); + + ColumnMetadata cmd = Mockito.mock(ColumnMetadata.class); + List<ColumnMetadata> listcmd = new ArrayList<>(); + listcmd.add(cmd); + Mockito.when(tableMeta.getPrimaryKey()).thenReturn(listcmd); + Mockito.when(cmd.getName()).thenReturn("val1"); + Mockito.when(tableMeta.getColumn("val1")).thenReturn(cmd); + Mockito.when(tableMeta.getColumn("val2")).thenReturn(cmd); + Mockito.when(cmd.getType()).thenReturn(DataType.text()); + + PreparedQueryObject query = ji.genInsertPreparedQueryObj(); + System.out.println(query.getQuery()); + System.out.println(query.getValues()); + + + assertEquals("INSERT INTO keyspace.table (vector_ts,val2,val1) VALUES (?,?,?);", query.getQuery()); + assertTrue(query.getValues().containsAll(vals.values())); + } } diff --git a/music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonKeySpaceTest.java b/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonKeySpaceTest.java index 0f4abd7c..a3fa58e4 100644 --- a/music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonKeySpaceTest.java +++ b/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonKeySpaceTest.java @@ -20,14 +20,13 @@ * ==================================================================== */ -package org.onap.music.unittests.jsonobjects; +package org.onap.music.datastore.jsonobjects; import static org.junit.Assert.*; import java.util.HashMap; import java.util.Map; import org.junit.Before; import org.junit.Test; -import org.onap.music.datastore.jsonobjects.JsonKeySpace; public class JsonKeySpaceTest { diff --git a/music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonSelectTest.java b/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonSelectTest.java index 37d1787a..21c022ab 100644 --- a/music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonSelectTest.java +++ b/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonSelectTest.java @@ -22,7 +22,7 @@ * ==================================================================== */ -package org.onap.music.unittests.jsonobjects; +package org.onap.music.datastore.jsonobjects; import static org.junit.Assert.assertEquals; @@ -31,7 +31,6 @@ import java.util.HashMap; import java.util.Map; import org.junit.Test; -import org.onap.music.datastore.jsonobjects.JsonSelect; public class JsonSelectTest { diff --git a/music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonTableTest.java b/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonTableTest.java index 4e3b4629..3ab32d40 100644 --- a/music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonTableTest.java +++ b/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonTableTest.java @@ -22,14 +22,14 @@ * ==================================================================== */ -package org.onap.music.unittests.jsonobjects; +package org.onap.music.datastore.jsonobjects; import static org.junit.Assert.*; import java.util.HashMap; import java.util.Map; import org.junit.Before; import org.junit.Test; -import org.onap.music.datastore.jsonobjects.JsonTable; +import org.onap.music.exceptions.MusicQueryException; public class JsonTableTest { @@ -53,6 +53,7 @@ public class JsonTableTest { Map<String, Object> properties = new HashMap<>(); properties.put("k1", "one"); jt.setProperties(properties); + assertEquals(properties.size(), jt.getProperties().size()); } @Test @@ -83,6 +84,13 @@ public class JsonTableTest { jt.setClusteringOrder(clusteringOrder); assertEquals(clusteringOrder,jt.getClusteringOrder()); } + + @Test + public void testGetClusterKey() { + String clusterKey = "clusterKey"; + jt.setClusteringKey(clusterKey); + assertEquals(clusterKey, jt.getClusteringKey()); + } @Test public void testGetPrimaryKey() { @@ -96,5 +104,50 @@ public class JsonTableTest { jt.setFilteringKey("FilteringKey"); assertEquals("FilteringKey",jt.getFilteringKey()); } + + @Test + public void testPartitionKey() { + jt.setPartitionKey("ParitionKey"); + assertEquals("ParitionKey",jt.getPartitionKey()); + } + @Test + public void genCreateTableQuery() throws MusicQueryException { + JsonTable jt2 = new JsonTable(); + jt2.setKeyspaceName("keyspace"); + jt2.setTableName("table"); + Map<String, String> fields = new HashMap<>(); + fields.put("k1", "one"); + jt2.setFields(fields); + jt2.setPrimaryKey("k1"); + Map<String, String> mapSs = new HashMap<>(); + mapSs.put("k1", "one"); + jt2.setConsistencyInfo(mapSs); + String clusteringOrder = "clusteringOrder"; + jt.setClusteringOrder(clusteringOrder); + String clusterKey = "clusterKey"; + jt.setClusteringKey(clusterKey); + + System.out.println(jt2.genCreateTableQuery().getQuery()); + assertEquals("CREATE TABLE keyspace.table (vector_ts text,k1 one, PRIMARY KEY ( (k1) ));", + jt2.genCreateTableQuery().getQuery()); + } + + @Test + public void genDropTableQuery() throws MusicQueryException { + JsonTable jt2 = new JsonTable(); + jt2.setKeyspaceName("keyspace"); + jt2.setTableName("table"); + Map<String, String> fields = new HashMap<>(); + fields.put("k1", "one"); + jt2.setFields(fields); + jt2.setPrimaryKey("k1"); + Map<String, String> mapSs = new HashMap<>(); + mapSs.put("k1", "one"); + jt.setConsistencyInfo(mapSs); + + System.out.println(jt2.genDropTableQuery().getQuery()); + assertEquals("DROP TABLE keyspace.table;", + jt2.genDropTableQuery().getQuery()); + } } diff --git a/music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonUpdateTest.java b/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonUpdateTest.java index e00cb463..37c729f4 100644 --- a/music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonUpdateTest.java +++ b/music-core/src/test/java/org/onap/music/datastore/jsonobjects/JsonUpdateTest.java @@ -22,14 +22,22 @@ * ==================================================================== *******************************************************************************/ -package org.onap.music.unittests.jsonobjects; +package org.onap.music.datastore.jsonobjects; import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; +import javax.ws.rs.core.MultivaluedMap; import org.junit.Before; import org.junit.Test; -import org.onap.music.datastore.jsonobjects.JsonUpdate; +import org.mockito.Mockito; +import org.onap.music.datastore.jsonobjects.JsonUpdate.RowIdentifier; +import com.datastax.driver.core.ColumnMetadata; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.TableMetadata; public class JsonUpdateTest { @@ -108,4 +116,51 @@ public class JsonUpdateTest { assertTrue(ju.serialize() instanceof byte[]); } + @Test + public void testRowIdString() { + ju.setRowIdString("testing"); + assertEquals("testing", ju.getRowIdString()); + } + + @Test + public void testPrimaryKeyValue() { + ju.setPrimarKeyValue("primeKey"); + assertEquals("primeKey", ju.getPrimarKeyValue()); + } + + @Test + public void testGenUpdatePreparedQueryObj() throws Exception { + JsonUpdate ju = Mockito.spy(JsonUpdate.class); + MultivaluedMap<String, String> rowParams = Mockito.mock(MultivaluedMap.class); + + ju.setKeyspaceName("keyspace"); + ju.setTableName("table"); + ju.setPrimarKeyValue("primaryKeyValue"); + Map<String, String> consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "critical"); + ju.setConsistencyInfo(consistencyInfo); + Map<String, Object> values = new HashMap<>(); + values.put("col1", "val1"); + ju.setValues(values); + + TableMetadata tmd = Mockito.mock(TableMetadata.class); + Mockito.doReturn(tmd).when(ju).returnColumnMetadata(Mockito.anyString(), Mockito.anyString()); + ColumnMetadata cmd = Mockito.mock(ColumnMetadata.class); + Mockito.when(tmd.getColumn("col1")).thenReturn(cmd); + List<ColumnMetadata> colList = new ArrayList<>(); + colList.add(cmd); + Mockito.when(tmd.getPrimaryKey()).thenReturn(colList); + Mockito.when(cmd.getType()).thenReturn(DataType.varchar()); + + RowIdentifier rowId = Mockito.mock(RowIdentifier.class); + Mockito.doReturn(rowId).when(ju).getRowIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.any(), + Mockito.any()); + + Mockito.when(rowId.getRowIdString()).thenReturn("col1"); + Mockito.when(rowId.getPrimaryKeyValue()).thenReturn("val1"); + + + assertEquals("UPDATE keyspace.table SET vector_ts=?,col1= ? WHERE col1;", + ju.genUpdatePreparedQueryObj(rowParams).getQuery()); + } } diff --git a/music-core/src/test/java/org/onap/music/eelf/logging/format/ErrorSeverityTest.java b/music-core/src/test/java/org/onap/music/eelf/logging/format/ErrorSeverityTest.java new file mode 100644 index 00000000..b2b0fafa --- /dev/null +++ b/music-core/src/test/java/org/onap/music/eelf/logging/format/ErrorSeverityTest.java @@ -0,0 +1,41 @@ +/* + * ============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.eelf.logging.format; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ErrorSeverityTest { + + @Test + public void TestErrorServerity() + { + assertEquals("INFO",ErrorSeverity.INFO.name()); + assertEquals("WARN",ErrorSeverity.WARN.name()); + assertEquals("ERROR",ErrorSeverity.ERROR.name()); + assertEquals("FATAL",ErrorSeverity.FATAL.name()); + assertEquals("CRITICAL",ErrorSeverity.CRITICAL.name()); + assertEquals("MAJOR",ErrorSeverity.MAJOR.name()); + assertEquals("MINOR",ErrorSeverity.MINOR.name()); + assertEquals("NONE",ErrorSeverity.NONE.name()); + } +} diff --git a/music-core/src/test/java/org/onap/music/exceptions/MusicDeadlockExceptionTest.java b/music-core/src/test/java/org/onap/music/exceptions/MusicDeadlockExceptionTest.java new file mode 100644 index 00000000..db0e10ea --- /dev/null +++ b/music-core/src/test/java/org/onap/music/exceptions/MusicDeadlockExceptionTest.java @@ -0,0 +1,135 @@ +/* + * ============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.exceptions; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class MusicDeadlockExceptionTest { + + private String owner = "tester"; + private String keyspace = "testing"; + private String table = "lockq"; + private String key = "test"; + + @Test + public void TestException1() { + String s1 = "Value1"; + String s2 = "value2"; + try { + if (!s1.equalsIgnoreCase(s2)) { + throw new MusicDeadlockException(); + } + } catch (MusicDeadlockException mde) { + assertEquals("org.onap.music.exceptions.MusicDeadlockException", mde.getClass().getName()); + } + + } + + @Test + public void TestException6() { + String s1 = "Value1"; + String s2 = "value2"; + try { + if (!s1.equalsIgnoreCase(s2)) { + throw new MusicDeadlockException("org.onap.music.exceptions.MusicDeadlockException"); + } + } catch (MusicDeadlockException mde) { + assertEquals(mde.getMessage(),"org.onap.music.exceptions.MusicDeadlockException"); + } + + } + + @Test + public void TestException2() { + String s1 = "Value1"; + String s2 = "value2"; + try { + if (!s1.equalsIgnoreCase(s2)) { + throw new MusicDeadlockException("MusicDeadlockException Exception occured.."); + } + } catch (MusicDeadlockException mde) { + assertEquals(mde.getMessage(), "MusicDeadlockException Exception occured.."); + } + + } + + @Test + public void TestException3() { + String s1 = "Value1"; + String s2 = "value2"; + try { + if (!s1.equalsIgnoreCase(s2)) { + throw new MusicDeadlockException(new Throwable()); + } + } catch (MusicDeadlockException mve) { + assertEquals("org.onap.music.exceptions.MusicDeadlockException", mve.getClass().getName()); + } + + } + + @Test + public void TestException4() { + String message = "Exception occured"; + String s1 = "Value1"; + String s2 = "value2"; + try { + if (!s1.equalsIgnoreCase(s2)) { + throw new MusicDeadlockException(message, new Throwable()); + } + } catch (MusicDeadlockException mde) { + assertEquals("org.onap.music.exceptions.MusicDeadlockException", mde.getClass().getName()); + } + + } + + @Test + public void TestException5() { + String message = "Exception occured"; + boolean enableSuppression = true; + boolean writableStackTrace = false; + String s1 = "Value1"; + String s2 = "value2"; + try { + if (!s1.equalsIgnoreCase(s2)) { + throw new MusicDeadlockException(message, new Throwable(), enableSuppression, + writableStackTrace); + } + } catch (MusicDeadlockException mde) { + assertEquals("org.onap.music.exceptions.MusicDeadlockException", mde.getClass().getName()); + } + + } + + @Test + public void TestSetValues() + { + MusicDeadlockException mde=new MusicDeadlockException(); + mde.setValues(owner,keyspace,table,key); + assertEquals("tester",mde.getOwner()); + assertEquals("testing",mde.getKeyspace()); + assertEquals("lockq",mde.getTable()); + assertEquals("test",mde.getKey()); + } +} 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..a608a970 --- /dev/null +++ b/music-core/src/test/java/org/onap/music/lockingservice/cassandra/CassaLockStoreTest.java @@ -0,0 +1,311 @@ +/* + * ============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 static org.junit.Assert.fail; +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.ConsistencyLevel; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.core.WriteType; +import com.datastax.driver.core.exceptions.WriteTimeoutException; + +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(); + } + } + + @Test + public void testDequeueLockRef() throws Exception { + cassaLockStore.deQueueLockRef("keyspace1", "table1", "key6", "6", 2); + + // note only expecting 1 call to this instance, expecting it to succeed + Mockito.verify(dsHandle, Mockito.times(1)).executePut(Mockito.any(), Mockito.anyString()); + } + + @Test + public void testDequeueLockRefWriteTimeout() throws Exception { + int retryCount = 22; + try { + Mockito.when(dsHandle.executePut(Mockito.any(), Mockito.anyString())) + .thenThrow(new MusicServiceException("Cassandra timeout during...")); + cassaLockStore.deQueueLockRef("keyspace1", "table1", "key6", "6", retryCount); + + // Should never reach here + fail(); + } catch (MusicServiceException | MusicQueryException | MusicLockingException e) { + // should throw an error + } + + Mockito.verify(dsHandle, Mockito.times(retryCount)).executePut(Mockito.any(), Mockito.anyString()); + } +} 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)); + } +} diff --git a/music-core/src/test/java/org/onap/music/unittests/ResultTypeTest.java b/music-core/src/test/java/org/onap/music/main/ResultTypeTest.java index 012629e0..d6ccc1f1 100644 --- a/music-core/src/test/java/org/onap/music/unittests/ResultTypeTest.java +++ b/music-core/src/test/java/org/onap/music/main/ResultTypeTest.java @@ -20,11 +20,10 @@ * ==================================================================== */ -package org.onap.music.unittests; +package org.onap.music.main; import static org.junit.Assert.*; import org.junit.Test; -import org.onap.music.main.ResultType; public class ResultTypeTest { diff --git a/music-core/src/test/java/org/onap/music/unittests/ReturnTypeTest.java b/music-core/src/test/java/org/onap/music/main/ReturnTypeTest.java index 490020ac..fbb5f84d 100644 --- a/music-core/src/test/java/org/onap/music/unittests/ReturnTypeTest.java +++ b/music-core/src/test/java/org/onap/music/main/ReturnTypeTest.java @@ -20,7 +20,7 @@ * ==================================================================== */ -package org.onap.music.unittests; +package org.onap.music.main; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -28,8 +28,6 @@ import static org.junit.Assert.assertTrue; import java.util.Map; import org.junit.Test; -import org.onap.music.main.ResultType; -import org.onap.music.main.ReturnType; public class ReturnTypeTest { diff --git a/music-core/src/test/java/org/onap/music/service/impl/MusicCassaCoreTest.java b/music-core/src/test/java/org/onap/music/service/impl/MusicCassaCoreTest.java new file mode 100644 index 00000000..280ba207 --- /dev/null +++ b/music-core/src/test/java/org/onap/music/service/impl/MusicCassaCoreTest.java @@ -0,0 +1,284 @@ +/******************************************************************************* + * ============LICENSE_START========================================== + * org.onap.music + * =================================================================== + * Copyright (c) 2018 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.service.impl; + +import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +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.lockingservice.cassandra.CassaLockStore; +import org.onap.music.lockingservice.cassandra.CassaLockStore.LockObject; +import org.onap.music.lockingservice.cassandra.LockType; +import org.onap.music.main.MusicUtil; +import org.onap.music.main.ResultType; +import org.onap.music.main.ReturnType; + +@RunWith(MockitoJUnitRunner.class) +public class MusicCassaCoreTest { + + @Mock + private CassaLockStore mLockHandle; + + MusicCassaCore core; + + @Before + public void before() { + core = MusicCassaCore.getInstance(); + MusicCassaCore.setmLockHandle(mLockHandle); + } + + @Test + public void testGetmLockHandle() { + assertEquals(mLockHandle, MusicCassaCore.getmLockHandle()); + } + + @Test + public void testSetmLockHandle() { + CassaLockStore m2 = Mockito.mock(CassaLockStore.class); + MusicCassaCore.setmLockHandle(m2); + assertEquals(m2, MusicCassaCore.getmLockHandle()); + //revert back to original handle + MusicCassaCore.setmLockHandle(mLockHandle); + } + + @Test + public void testGetInstance() { + assertEquals(core, MusicCassaCore.getInstance()); + } + + @Test + public void testGetLockingServiceHandle() { + assertEquals(mLockHandle, MusicCassaCore.getmLockHandle()); + } + + @Test + public void testCreateLockReferenceAtomicString() throws Exception { + String fullyQualifiedKey = "keyspace.table.lockName"; + Mockito.when(mLockHandle.genLockRefandEnQueue("keyspace", "table", "lockName", LockType.WRITE, null)) + .thenReturn("lockReturned"); + + String lockRef = core.createLockReferenceAtomic(fullyQualifiedKey); + + Mockito.verify(mLockHandle).genLockRefandEnQueue("keyspace", "table", "lockName", LockType.WRITE, null); + assertEquals("lockReturned", lockRef); + } + + @Test + public void testCreateLockReferenceStringString() throws Exception { + String fullyQualifiedKey = "keyspace.table.lockName"; + String owner = "owner1"; + Mockito.when(mLockHandle.genLockRefandEnQueue("keyspace", "table", "lockName", LockType.WRITE, owner)) + .thenReturn("lockReturned"); + + String lockRef = core.createLockReference(fullyQualifiedKey, owner); + + Mockito.verify(mLockHandle).genLockRefandEnQueue("keyspace", "table", "lockName", LockType.WRITE, owner); + assertEquals("lockReturned", lockRef); + } + + @Test + public void testCreateLockReferenceAtomicStringLockType() throws Exception { + String fullyQualifiedKey = "keyspace.table.lockName"; + Mockito.when(mLockHandle.genLockRefandEnQueue("keyspace", "table", "lockName", LockType.READ, null)) + .thenReturn("lockReturned"); + + String lockRef = core.createLockReferenceAtomic(fullyQualifiedKey, LockType.READ); + + Mockito.verify(mLockHandle).genLockRefandEnQueue("keyspace", "table", "lockName", LockType.READ, null); + assertEquals("lockReturned", lockRef); + } + + @Test + public void testCreateLockReferenceStringLockTypeString() throws Exception { + String fullyQualifiedKey = "keyspace.table.lockName"; + String owner = "owner1"; + Mockito.when(mLockHandle.genLockRefandEnQueue("keyspace", "table", "lockName", LockType.READ, owner)) + .thenReturn("lockReturned"); + + String lockRef = core.createLockReference(fullyQualifiedKey, LockType.READ, owner); + + Mockito.verify(mLockHandle).genLockRefandEnQueue("keyspace", "table", "lockName", LockType.READ, owner); + assertEquals("lockReturned", lockRef); + } + + @Test + public void testPromoteLock() throws Exception { + String lockId = "$keyspace.table.lockName$1"; + Mockito.when(mLockHandle.promoteLock("keyspace", "table", "lockName", "1")) + .thenReturn(new ReturnType(ResultType.SUCCESS, "Lock Promoted")); + + ReturnType rt = core.promoteLock(lockId); + assertEquals(ResultType.SUCCESS, rt.getResult()); + assertEquals("Lock Promoted", rt.getMessage()); + } + + @Test + public void testAcquireLockWithLease() throws Exception { + String fullyQualifiedKey = "keyspace.table.lockName"; + String lockId = "$keyspace.table.lockName$1"; + long leasePeriod = 1000; + String currTime = String.valueOf(System.currentTimeMillis()); + Mockito.when(mLockHandle.peekLockQueue("keyspace", "table", "lockName")) + .thenReturn(mLockHandle.new LockObject(true, lockId, currTime, currTime, LockType.WRITE, null)); + Mockito.when(mLockHandle.getLockInfo("keyspace", "table", "lockName", "1")) + .thenReturn(mLockHandle.new LockObject(false, lockId, null, null, LockType.WRITE, null)); + + ReturnType rt = core.acquireLockWithLease(fullyQualifiedKey, lockId, leasePeriod); + assertEquals(ResultType.FAILURE, rt.getResult()); + } + + @Test + public void testAcquireLock() throws Exception { + String fullyQualifiedKey = "keyspace.table.lockName"; + String lockId = "$keyspace.table.lockName$1"; + Mockito.when(mLockHandle.getLockInfo("keyspace", "table", "lockName", "1")) + .thenReturn(mLockHandle.new LockObject(false, lockId, null, null, LockType.WRITE, null)); + + ReturnType rt = core.acquireLock(fullyQualifiedKey, lockId); + + assertEquals(ResultType.FAILURE, rt.getResult()); + Mockito.verify(mLockHandle).getLockInfo("keyspace", "table", "lockName", "1"); + /*TODO: if we successfully acquire the lock we hit an error by trying to read MusicDatastoreHandle */ + } + + @Test + public void testWhoseTurnIsIt() throws Exception { + String fullyQualifiedKey = "keyspace.table.lockName"; + Mockito.when(mLockHandle.peekLockQueue("keyspace", "table", "lockName")) + .thenReturn(mLockHandle.new LockObject(true, "1", "", "", LockType.WRITE, null)); + + String topOfQ = core.whoseTurnIsIt(fullyQualifiedKey); + System.out.println(topOfQ); + + assertEquals("$"+fullyQualifiedKey+"$1", topOfQ); + } + + @Test + public void testGetCurrentLockHolders() throws Exception { + String fullyQualifiedKey = "keyspace.table.lockName"; + List<String> currentHolders = new ArrayList<>(); + currentHolders.add("$"+fullyQualifiedKey+"$1"); + currentHolders.add("$"+fullyQualifiedKey+"$2"); + Mockito.when(mLockHandle.getCurrentLockHolders("keyspace", "table", "lockName")) + .thenReturn(currentHolders); + + List<String> holders = core.getCurrentLockHolders(fullyQualifiedKey); + + assertTrue(currentHolders.containsAll(holders) && holders.containsAll(currentHolders)); + } + + @Test + public void testGetLockNameFromId() { + String lockId = "$keyspace.table.lockName$1"; + assertEquals("keyspace.table.lockName", core.getLockNameFromId(lockId)); + } + + @Test + public void testDestroyLockRefString() throws Exception { + String lockId = "$keyspace.table.lockName$1"; + + core.destroyLockRef(lockId); + Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount()); + } + + @Test + public void testDestroyLockRefStringString() throws Exception { + String fullyQualifiedKey = "keyspace.table.lockName"; + String lockReference = "1"; + + core.destroyLockRef(fullyQualifiedKey, lockReference); + Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount()); + } + + @Test + public void testReleaseLock() throws Exception { + String lockId = "$keyspace.table.lockName$1"; + + core.releaseLock(lockId, true); + + Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount()); + } + + @Test + public void testVoluntaryReleaseLock() throws Exception { + String fullyQualifiedKey = "keyspace.table.lockName"; + String lockReference = "1"; + + core.voluntaryReleaseLock(fullyQualifiedKey, lockReference); + + Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount()); + } + + @Test + public void testReleaseAllLocksForOwner() throws Exception { + List<String> ownersLocks = new ArrayList<>(); + ownersLocks.add("lockName$1"); + ownersLocks.add("lockName$2"); + Mockito.when(mLockHandle.getAllLocksForOwner("ownerId", "keyspace", "table")) + .thenReturn(ownersLocks); + + List<String> locksReleased = core.releaseAllLocksForOwner("ownerId", "keyspace", "table"); + + Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount()); + Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "2", MusicUtil.getRetryCount()); + assertTrue(ownersLocks.containsAll(locksReleased) && locksReleased.containsAll(ownersLocks)); + } + + + @Test + public void testValidateLock() { + String lockId = "$keyspace.table.lockName$1"; + + assertFalse(core.validateLock(lockId).containsKey("Error")); + } + + @Test + public void testGetLockQueue() throws Exception { + String fullyQualifiedKey = "keyspace.table.lockName"; + List<String> myList = new ArrayList<>(); + Mockito.when(mLockHandle.getLockQueue("keyspace", "table", "lockName")) + .thenReturn(myList); + List<String> theirList = core.getLockQueue(fullyQualifiedKey); + + assertEquals(myList, theirList); + } + + @Test + public void testGetLockQueueSize() throws Exception { + String fullyQualifiedKey = "keyspace.table.lockName"; + Mockito.when(mLockHandle.getLockQueueSize("keyspace", "table", "lockName")) + .thenReturn((long) 23); + long theirSize = core.getLockQueueSize(fullyQualifiedKey); + + assertEquals(23, theirSize); + } + +} diff --git a/music-core/src/test/java/org/onap/music/unittests/CassandraCQL.java b/music-core/src/test/java/org/onap/music/unittests/CassandraCQL.java deleted file mode 100644 index 582744fb..00000000 --- a/music-core/src/test/java/org/onap/music/unittests/CassandraCQL.java +++ /dev/null @@ -1,245 +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; - -/** - * @author srupane - * - */ - -import java.math.BigInteger; -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.net.SocketException; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; - -//import org.apache.thrift.transport.TTransportException; -import org.cassandraunit.utils.EmbeddedCassandraServerHelper; -import org.onap.music.datastore.MusicDataStore; -import org.onap.music.datastore.PreparedQueryObject; -import org.onap.music.lockingservice.cassandra.LockType; -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.Session; -import com.datastax.driver.extras.codecs.enums.EnumNameCodec; - -public class CassandraCQL { - public static final String createAdminKeyspace = "CREATE KEYSPACE admin WITH REPLICATION = " - + "{'class' : 'SimpleStrategy' , 'replication_factor': 1} AND DURABLE_WRITES = true"; - - public static final String createAdminTable = "CREATE TABLE admin.keyspace_master (" + " uuid uuid, keyspace_name text," - + " application_name text, is_api boolean," - + " password text, username text," - + " is_aaf boolean, PRIMARY KEY (uuid)\n" + ");"; - - public static final String createKeySpace = - "CREATE KEYSPACE IF NOT EXISTS testcassa WITH replication = " - +"{'class':'SimpleStrategy','replication_factor':1} AND durable_writes = true;"; - - public static final String dropKeyspace = "DROP KEYSPACE IF EXISTS testcassa"; - - public static final String createTableEmployees = - "CREATE TABLE IF NOT EXISTS testcassa.employees " - + "(vector_ts text,empid uuid,empname text,empsalary varint,address Map<text,text>,PRIMARY KEY (empname)) " - + "WITH comment='Financial Info of employees' " - + "AND compression={'sstable_compression':'DeflateCompressor','chunk_length_kb':64} " - + "AND compaction={'class':'SizeTieredCompactionStrategy','min_threshold':6};"; - - public static final String insertIntoTablePrepared1 = - "INSERT INTO testcassa.employees (vector_ts,empid,empname,empsalary) VALUES (?,?,?,?); "; - - public static final String insertIntoTablePrepared2 = - "INSERT INTO testcassa.employees (vector_ts,empid,empname,empsalary,address) VALUES (?,?,?,?,?);"; - - public static final String selectALL = "SELECT * FROM testcassa.employees;"; - - public static final String selectSpecific = - "SELECT * FROM testcassa.employees WHERE empname= ?;"; - - public static final String updatePreparedQuery = - "UPDATE testcassa.employees SET vector_ts=?,address= ? WHERE empname= ?;"; - - public static final String deleteFromTable = " "; - - public static final String deleteFromTablePrepared = " "; - - // Set Values for Prepared Query - - public static List<Object> setPreparedInsertValues1() { - - List<Object> preppreparedInsertValues1 = new ArrayList<>(); - String vectorTs = - String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); - UUID empId = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40cd6"); - BigInteger empSalary = BigInteger.valueOf(23443); - String empName = "Mr Test one"; - preppreparedInsertValues1.add(vectorTs); - preppreparedInsertValues1.add(empId); - preppreparedInsertValues1.add(empName); - preppreparedInsertValues1.add(empSalary); - return preppreparedInsertValues1; - } - - public static List<Object> setPreparedInsertValues2() { - - List<Object> preparedInsertValues2 = new ArrayList<>(); - String vectorTs = - String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); - UUID empId = UUID.fromString("abc434cc-d657-4e90-b4e5-df4223d40cd6"); - BigInteger empSalary = BigInteger.valueOf(45655); - String empName = "Mr Test two"; - Map<String, String> address = new HashMap<>(); - preparedInsertValues2.add(vectorTs); - preparedInsertValues2.add(empId); - preparedInsertValues2.add(empName); - preparedInsertValues2.add(empSalary); - address.put("Street", "1 some way"); - address.put("City", "Some town"); - preparedInsertValues2.add(address); - return preparedInsertValues2; - } - - public static List<Object> setPreparedUpdateValues() { - - List<Object> preparedUpdateValues = new ArrayList<>(); - String vectorTs = - String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); - Map<String, String> address = new HashMap<>(); - preparedUpdateValues.add(vectorTs); - String empName = "Mr Test one"; - address.put("Street", "101 Some Way"); - address.put("City", "New York"); - preparedUpdateValues.add(address); - preparedUpdateValues.add(empName); - return preparedUpdateValues; - } - - // Generate Different Prepared Query Objects - /** - * Query Object for Get. - * - * @return - */ - public static PreparedQueryObject setPreparedGetQuery() { - - PreparedQueryObject queryObject = new PreparedQueryObject(); - String empName1 = "Mr Test one"; - queryObject.appendQueryString(selectSpecific); - queryObject.addValue(empName1); - return queryObject; - } - - /** - * Query Object 1 for Insert. - * - * @return {@link PreparedQueryObject} - */ - public static PreparedQueryObject setPreparedInsertQueryObject1() { - - PreparedQueryObject queryobject = new PreparedQueryObject(); - queryobject.appendQueryString(insertIntoTablePrepared1); - List<Object> values = setPreparedInsertValues1(); - if (!values.isEmpty() || values != null) { - for (Object o : values) { - queryobject.addValue(o); - } - } - return queryobject; - - } - - /** - * Query Object 2 for Insert. - * - * @return {@link PreparedQueryObject} - */ - public static PreparedQueryObject setPreparedInsertQueryObject2() { - - PreparedQueryObject queryobject = new PreparedQueryObject(); - queryobject.appendQueryString(insertIntoTablePrepared2); - List<Object> values = setPreparedInsertValues2(); - if (!values.isEmpty() || values != null) { - for (Object o : values) { - queryobject.addValue(o); - } - } - return queryobject; - - } - - /** - * Query Object for Update. - * - * @return {@link PreparedQueryObject} - */ - public static PreparedQueryObject setPreparedUpdateQueryObject() { - - PreparedQueryObject queryobject = new PreparedQueryObject(); - queryobject.appendQueryString(updatePreparedQuery); - List<Object> values = setPreparedUpdateValues(); - if (!values.isEmpty() || values != null) { - for (Object o : values) { - queryobject.addValue(o); - } - } - return queryobject; - - } - - private static ArrayList<String> getAllPossibleLocalIps() { - ArrayList<String> allPossibleIps = new ArrayList<String>(); - try { - Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); - while (en.hasMoreElements()) { - NetworkInterface ni = (NetworkInterface) en.nextElement(); - Enumeration<InetAddress> ee = ni.getInetAddresses(); - while (ee.hasMoreElements()) { - InetAddress ia = (InetAddress) ee.nextElement(); - allPossibleIps.add(ia.getHostAddress()); - } - } - } catch (SocketException e) { - System.out.println(e.getMessage()); - } - return allPossibleIps; - } - - public static MusicDataStore connectToEmbeddedCassandra() throws Exception { - System.setProperty("log4j.configuration", "log4j.properties"); - - String address = "localhost"; - - EmbeddedCassandraServerHelper.startEmbeddedCassandra(); - Cluster cluster = new Cluster.Builder().withoutJMXReporting().withoutMetrics().addContactPoint(address).withPort(9142).build(); - cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(5000); - - Session session = cluster.connect(); - - return new MusicDataStore(cluster, session); - } - -} |