From: Thomas Nelson Date: Fri, 3 Jan 2020 06:45:59 +0000 (+0000) Subject: Merge "CadiAuthFilter-junits" X-Git-Tag: 3.2.38~32 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=music.git;a=commitdiff_plain;h=18f6d5b85e603a7a5f2ebf402edfdcd6ae3fd514;hp=1722238727e632602583cfe2ad2c6d1a8eaf6e1f Merge "CadiAuthFilter-junits" --- diff --git a/WebContent/WEB-INF/music.properties.sample b/WebContent/WEB-INF/music.properties.sample index 871b0c3f..41b919fe 100644 --- a/WebContent/WEB-INF/music.properties.sample +++ b/WebContent/WEB-INF/music.properties.sample @@ -14,5 +14,7 @@ all.public.ips=12.13.14.45:12.13.14.46:12.13.14.47 # Cassandra Username and password cassandra.user=cassandra cassandra.password=cassandra +cassandra.connecttimeoutms=5000 +cassandra.readtimeoutms=5000 # AAF Endpoint aaf.endpoint.url=http://aafendpoint/proxy/authz/nss/ \ No newline at end of file diff --git a/music-core/src/main/java/org/onap/music/datastore/MusicDataStore.java b/music-core/src/main/java/org/onap/music/datastore/MusicDataStore.java index 9ccff828..7f6c42ca 100755 --- a/music-core/src/main/java/org/onap/music/datastore/MusicDataStore.java +++ b/music-core/src/main/java/org/onap/music/datastore/MusicDataStore.java @@ -51,6 +51,7 @@ 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.SocketOptions; import com.datastax.driver.core.TableMetadata; import com.datastax.driver.core.exceptions.AlreadyExistsException; import com.datastax.driver.core.exceptions.InvalidQueryException; @@ -90,7 +91,7 @@ public class MusicDataStore { setCluster(cluster); } - + /** * @param session */ @@ -111,10 +112,10 @@ public class MusicDataStore { public void setCluster(Cluster cluster) { EnumNameCodec lockTypeCodec = new EnumNameCodec(LockType.class); cluster.getConfiguration().getCodecRegistry().register(lockTypeCodec); - + this.cluster = cluster; } - + public Cluster getCluster() { return this.cluster; } @@ -122,7 +123,7 @@ public class MusicDataStore { private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicDataStore.class); - + /** * * @param remoteIp @@ -155,38 +156,44 @@ public class MusicDataStore { poolingOptions .setConnectionsPerHost(HostDistance.LOCAL, 4, 10) .setConnectionsPerHost(HostDistance.REMOTE, 2, 4); - + Cluster cluster; if(MusicUtil.getCassName() != null && MusicUtil.getCassPwd() != null) { String cassPwd = CipherUtil.decryptPKC(MusicUtil.getCassPwd()); logger.info(EELFLoggerDelegate.applicationLogger, "Building with credentials "+MusicUtil.getCassName()+" & "+ MusicUtil.getCassPwd()); cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort()) - .withCredentials(MusicUtil.getCassName(), cassPwd) - //.withLoadBalancingPolicy(new RoundRobinPolicy()) - .withoutJMXReporting() - .withPoolingOptions(poolingOptions) - .addContactPoints(addresses).build(); + .withCredentials(MusicUtil.getCassName(), cassPwd) + //.withLoadBalancingPolicy(new RoundRobinPolicy()) + .withoutJMXReporting() + .withPoolingOptions(poolingOptions) + .withSocketOptions( + new SocketOptions().setConnectTimeoutMillis(MusicUtil.getCassandraConnectTimeOutMS()) + .setReadTimeoutMillis(MusicUtil.getCassandraReadTimeOutMS())) + .addContactPoints(addresses).build(); } else { cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort()) - .withoutJMXReporting() - .withPoolingOptions(poolingOptions) - .addContactPoints(addresses) - .build(); + .withoutJMXReporting() + .withPoolingOptions(poolingOptions) + .withSocketOptions(new SocketOptions() + .setConnectTimeoutMillis(MusicUtil.getCassandraConnectTimeOutMS()) + .setReadTimeoutMillis(MusicUtil.getCassandraReadTimeOutMS())) + .addContactPoints(addresses) + .build(); } - + this.setCluster(cluster); Metadata metadata = this.cluster.getMetadata(); logger.info(EELFLoggerDelegate.applicationLogger, "Connected to cassa cluster " - + metadata.getClusterName() + " at " + address); + + metadata.getClusterName() + " at " + address); try { session = this.cluster.connect(); } catch (Exception ex) { logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.CASSANDRACONNECTIVITY, - ErrorSeverity.ERROR, ErrorTypes.SERVICEUNAVAILABLE, ex); + ErrorSeverity.ERROR, ErrorTypes.SERVICEUNAVAILABLE, ex); throw new MusicServiceException( - "Error while connecting to Cassandra cluster.. " + ex.getMessage()); + "Error while connecting to Cassandra cluster.. " + ex.getMessage()); } } @@ -214,16 +221,16 @@ public class MusicDataStore { KeyspaceMetadata ks = cluster.getMetadata().getKeyspace(keyspace); return ks.getTable(tableName); } - + /** - * - * @param keyspace - * @param tableName - * @return TableMetadata - */ - public KeyspaceMetadata returnKeyspaceMetadata(String keyspace) { - return cluster.getMetadata().getKeyspace(keyspace); - } + * + * @param keyspace + * @param tableName + * @return TableMetadata + */ + public KeyspaceMetadata returnKeyspaceMetadata(String keyspace) { + return cluster.getMetadata().getKeyspace(keyspace); + } /** @@ -289,7 +296,7 @@ public class MusicDataStore { */ public Map> marshalData(ResultSet results) { Map> resultMap = - new HashMap<>(); + new HashMap<>(); int counter = 0; for (Row row : results) { ColumnDefinitions colInfo = row.getColumnDefinitions(); @@ -301,7 +308,7 @@ public class MusicDataStore { getBlobValue(row, definition.getName(), definition.getType())); } else { resultOutput.put(definition.getName(), - getColValue(row, definition.getName(), definition.getType())); + getColValue(row, definition.getName(), definition.getType())); } } } @@ -313,7 +320,7 @@ public class MusicDataStore { // Prepared Statements 1802 additions - + public boolean executePut(PreparedQueryObject queryObject, String consistency) throws MusicServiceException, MusicQueryException { return executePut(queryObject, consistency, 0); @@ -329,19 +336,19 @@ public class MusicDataStore { * @throws MusicQueryException */ public boolean executePut(PreparedQueryObject queryObject, String consistency,long timeSlot) - throws MusicServiceException, MusicQueryException { + throws MusicServiceException, MusicQueryException { boolean result = false; long timeOfWrite = System.currentTimeMillis(); if (!MusicUtil.isValidQueryObject(!queryObject.getValues().isEmpty(), queryObject)) { logger.error(EELFLoggerDelegate.errorLogger, queryObject.getQuery(),AppMessages.QUERYERROR, ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); throw new MusicQueryException("Ill formed queryObject for the request = " + "[" - + queryObject.getQuery() + "]"); + + queryObject.getQuery() + "]"); } logger.debug(EELFLoggerDelegate.applicationLogger, - "In preprared Execute Put: the actual insert query:" - + queryObject.getQuery() + "; the values" - + queryObject.getValues()); + "In preprared Execute Put: the actual insert query:" + + queryObject.getQuery() + "; the values" + + queryObject.getValues()); SimpleStatement preparedInsert = null; try { @@ -381,12 +388,12 @@ public class MusicDataStore { // logger.error(EELFLoggerDelegate.errorLogger,e.getClass().toString() + ":" + e.getMessage(),AppMessages.SESSIONFAILED + " [" // + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR, e); throw new MusicServiceException("Executing Session Failure for Request = " + "[" - + queryObject.getQuery() + "]" + " Reason = " + e.getMessage(),e); + + queryObject.getQuery() + "]" + " Reason = " + e.getMessage(),e); } return result; } - /* *//** + /* *//** * This method performs DDL operations on Cassandra using consistency level ONE. * * @param queryObject Object containing cassandra prepared query and values. @@ -429,15 +436,15 @@ public class MusicDataStore { return results; } - *//** - * - * This method performs DDL operation on Cassandra using consistency level QUORUM. - * - * @param queryObject Object containing cassandra prepared query and values. - * @return ResultSet - * @throws MusicServiceException - * @throws MusicQueryException - *//* + *//** + * + * This method performs DDL operation on Cassandra using consistency level QUORUM. + * + * @param queryObject Object containing cassandra prepared query and values. + * @return ResultSet + * @throws MusicServiceException + * @throws MusicQueryException + *//* public ResultSet executeCriticalGet(PreparedQueryObject queryObject) throws MusicServiceException, MusicQueryException { if (!MusicUtil.isValidQueryObject(!queryObject.getValues().isEmpty(), queryObject)) { @@ -460,50 +467,46 @@ public class MusicDataStore { return results; } - */ + */ public ResultSet executeGet(PreparedQueryObject queryObject,String consistencyLevel) throws MusicQueryException, MusicServiceException { if (!MusicUtil.isValidQueryObject(!queryObject.getValues().isEmpty(), queryObject)) { logger.error(EELFLoggerDelegate.errorLogger, "",AppMessages.QUERYERROR+ " [" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); throw new MusicQueryException("Error processing Prepared Query Object for the request = " + "[" - + queryObject.getQuery() + "]"); + + queryObject.getQuery() + "]"); } ResultSet results = null; try { SimpleStatement statement = new SimpleStatement(queryObject.getQuery(), queryObject.getValues().toArray()); - if (consistencyLevel.equalsIgnoreCase(CONSISTENCY_LEVEL_ONE)) { - if(queryObject.getConsistency() == null) { statement.setConsistencyLevel(ConsistencyLevel.ONE); - } else { - statement.setConsistencyLevel(MusicUtil.getConsistencyLevel(queryObject.getConsistency())); - } - } - else if (consistencyLevel.equalsIgnoreCase(CONSISTENCY_LEVEL_QUORUM)) { + } else if (consistencyLevel.equalsIgnoreCase(CONSISTENCY_LEVEL_QUORUM)) { statement.setConsistencyLevel(ConsistencyLevel.QUORUM); + } else if (consistencyLevel.equalsIgnoreCase(CONSISTENCY_LEVEL_LOCAL_QUORUM)) { + statement.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM); } results = session.execute(statement); } catch (Exception ex) { logger.error(EELFLoggerDelegate.errorLogger, "Execute Get Error" + ex.getMessage(),AppMessages.UNKNOWNERROR+ "[" + queryObject - .getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR, ex); + .getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR, ex); throw new MusicServiceException("Execute Get Error" + ex.getMessage()); } - + return results; - + } - + /** * This method performs DDL operations on Cassandra using consistency level ONE. * * @param queryObject Object containing cassandra prepared query and values. */ public ResultSet executeOneConsistencyGet(PreparedQueryObject queryObject) - throws MusicServiceException, MusicQueryException { + throws MusicServiceException, MusicQueryException { return executeGet(queryObject, CONSISTENCY_LEVEL_ONE); } - + /** * * This method performs DDL operation on Cassandra using consistency level LOCAL_QUORUM. @@ -511,10 +514,10 @@ public class MusicDataStore { * @param queryObject Object containing cassandra prepared query and values. */ public ResultSet executeLocalQuorumConsistencyGet(PreparedQueryObject queryObject) - throws MusicServiceException, MusicQueryException { + throws MusicServiceException, MusicQueryException { return executeGet(queryObject, CONSISTENCY_LEVEL_LOCAL_QUORUM); } - + /** * * This method performs DDL operation on Cassandra using consistency level QUORUM. @@ -522,8 +525,8 @@ public class MusicDataStore { * @param queryObject Object containing cassandra prepared query and values. */ public ResultSet executeQuorumConsistencyGet(PreparedQueryObject queryObject) - throws MusicServiceException, MusicQueryException { + throws MusicServiceException, MusicQueryException { return executeGet(queryObject, CONSISTENCY_LEVEL_QUORUM); } - + } diff --git a/music-core/src/main/java/org/onap/music/datastore/jsonobjects/JsonInsert.java b/music-core/src/main/java/org/onap/music/datastore/jsonobjects/JsonInsert.java index 57ff245a..2f685cfe 100644 --- a/music-core/src/main/java/org/onap/music/datastore/jsonobjects/JsonInsert.java +++ b/music-core/src/main/java/org/onap/music/datastore/jsonobjects/JsonInsert.java @@ -173,11 +173,6 @@ public class JsonInsert implements Serializable { * @throws MusicQueryException */ public PreparedQueryObject genInsertPreparedQueryObj() throws MusicQueryException { - if (logger.isDebugEnabled()) { - logger.debug("Coming inside genTableInsertQuery method " + this.getKeyspaceName()); - logger.debug("Coming inside genTableInsertQuery method " + this.getTableName()); - } - PreparedQueryObject queryObject = new PreparedQueryObject(); TableMetadata tableInfo = null; try { diff --git a/music-core/src/main/java/org/onap/music/datastore/jsonobjects/JsonTable.java b/music-core/src/main/java/org/onap/music/datastore/jsonobjects/JsonTable.java index ef560144..0a277e08 100644 --- a/music-core/src/main/java/org/onap/music/datastore/jsonobjects/JsonTable.java +++ b/music-core/src/main/java/org/onap/music/datastore/jsonobjects/JsonTable.java @@ -144,11 +144,6 @@ public class JsonTable { } public PreparedQueryObject genCreateTableQuery() throws MusicQueryException { - if (logger.isDebugEnabled()) { - logger.debug("Coming inside genCreateTableQuery method " + this.getKeyspaceName()); - logger.debug("Coming inside genCreateTableQuery method " + this.getTableName()); - } - String primaryKey = null; String partitionKey = this.getPartitionKey(); String clusterKey = this.getClusteringKey(); @@ -340,36 +335,12 @@ public class JsonTable { return queryObject; } - /** - * - * @return - */ - public PreparedQueryObject genCreateShadowLockingTableQuery() { - if (logger.isDebugEnabled()) { - logger.debug("Coming inside genCreateShadowLockingTableQuery method " + this.getKeyspaceName()); - logger.debug("Coming inside genCreateShadowLockingTableQuery method " + this.getTableName()); - } - - String tableName = "unsyncedKeys_" + this.getTableName(); - String tabQuery = "CREATE TABLE IF NOT EXISTS " + this.getKeyspaceName() + "." + tableName - + " ( key text,PRIMARY KEY (key) );"; - PreparedQueryObject queryObject = new PreparedQueryObject(); - queryObject.appendQueryString(tabQuery); - - return queryObject; - } - /** * genDropTableQuery * * @return PreparedQueryObject */ public PreparedQueryObject genDropTableQuery() { - if (logger.isDebugEnabled()) { - logger.debug("Coming inside genDropTableQuery method " + this.getKeyspaceName()); - logger.debug("Coming inside genDropTableQuery method " + this.getTableName()); - } - PreparedQueryObject query = new PreparedQueryObject(); query.appendQueryString("DROP TABLE " + this.getKeyspaceName() + "." + this.getTableName() + ";"); logger.info("Delete Query ::::: " + query.getQuery()); diff --git a/music-core/src/main/java/org/onap/music/datastore/jsonobjects/JsonUpdate.java b/music-core/src/main/java/org/onap/music/datastore/jsonobjects/JsonUpdate.java index 12508de0..cd767a44 100644 --- a/music-core/src/main/java/org/onap/music/datastore/jsonobjects/JsonUpdate.java +++ b/music-core/src/main/java/org/onap/music/datastore/jsonobjects/JsonUpdate.java @@ -65,7 +65,7 @@ public class JsonUpdate implements Serializable { private Map consistencyInfo; private transient Map conditions; private transient Map rowSpecification; - private StringBuilder rowIdString; + private String rowIdString; private String primarKeyValue; private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JsonUpdate.class); @@ -142,11 +142,11 @@ public class JsonUpdate implements Serializable { this.values = values; } - public StringBuilder getRowIdString() { + public String getRowIdString() { return rowIdString; } - public void setRowIdString(StringBuilder rowIdString) { + public void setRowIdString(String rowIdString) { this.rowIdString = rowIdString; } @@ -176,11 +176,6 @@ public class JsonUpdate implements Serializable { * @throws MusicQueryException */ public PreparedQueryObject genUpdatePreparedQueryObj(MultivaluedMap rowParams) throws MusicQueryException { - if (logger.isDebugEnabled()) { - logger.debug("Coming inside genUpdatePreparedQueryObj method " + this.getKeyspaceName()); - logger.debug("Coming inside genUpdatePreparedQueryObj method " + this.getTableName()); - } - PreparedQueryObject queryObject = new PreparedQueryObject(); if((this.getKeyspaceName() == null || this.getKeyspaceName().isEmpty()) || @@ -205,20 +200,7 @@ public class JsonUpdate implements Serializable { Map valuesMap = this.getValues(); - TableMetadata tableInfo; - - try { - tableInfo = MusicDataStoreHandle.returnColumnMetadata(this.getKeyspaceName(), this.getTableName()); - } catch (MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger,e, AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes - .GENERALSERVICEERROR, e); - /*return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build();*/ - throw new MusicQueryException(e.getMessage(), Status.BAD_REQUEST.getStatusCode()); - }catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, e, AppMessages.UNKNOWNERROR, ErrorSeverity.CRITICAL, - ErrorTypes.GENERALSERVICEERROR); - throw new MusicQueryException(e.getMessage(), Status.BAD_REQUEST.getStatusCode()); - } + TableMetadata tableInfo = getColumnMetadata(this.getKeyspaceName(), this.getTableName()); if (tableInfo == null) { logger.error(EELFLoggerDelegate.errorLogger,"Table information not found. Please check input for table name= "+this.getTableName(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); @@ -289,7 +271,7 @@ public class JsonUpdate implements Serializable { rowId = getRowIdentifier(this.getKeyspaceName(), this.getTableName(), rowParams, queryObject); this.setRowIdString(rowId.rowIdString); this.setPrimarKeyValue(rowId.primarKeyValue); - if(rowId == null || rowId.primarKeyValue.isEmpty()) { + if(rowId == null || rowId.getPrimaryKeyValue().isEmpty()) { /*return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE) .setError("Mandatory WHERE clause is missing. Please check the input request.").toMap()).build();*/ @@ -312,7 +294,7 @@ public class JsonUpdate implements Serializable { queryObject.appendQueryString( - " SET " + fieldValueString + " WHERE " + rowId.rowIdString + ";"); + " SET " + fieldValueString + " WHERE " + rowId.getRowIdString() + ";"); @@ -324,7 +306,7 @@ public class JsonUpdate implements Serializable { // to avoid parsing repeatedly, just send the select query to obtain row PreparedQueryObject selectQuery = new PreparedQueryObject(); selectQuery.appendQueryString("SELECT * FROM " + this.getKeyspaceName() + "." + this.getTableName() + " WHERE " - + rowId.rowIdString + ";"); + + rowId.getRowIdString() + ";"); selectQuery.addValue(rowId.primarKeyValue); conditionInfo = new Condition(this.getConditions(), selectQuery); } @@ -348,20 +330,62 @@ public class JsonUpdate implements Serializable { return queryObject; } + + TableMetadata getColumnMetadata(String keyspaceName, String tableName) throws MusicQueryException { + TableMetadata tableInfo; + try { + tableInfo = returnColumnMetadata(keyspaceName, tableName); + } catch (MusicServiceException e) { + logger.error(EELFLoggerDelegate.errorLogger,e, AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes + .GENERALSERVICEERROR, e); + /*return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build();*/ + throw new MusicQueryException(e.getMessage(), Status.BAD_REQUEST.getStatusCode()); + }catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, e, AppMessages.UNKNOWNERROR, ErrorSeverity.CRITICAL, + ErrorTypes.GENERALSERVICEERROR); + throw new MusicQueryException(e.getMessage(), Status.BAD_REQUEST.getStatusCode()); + } + return tableInfo; + } + + /** wrapper around static method for testing */ + TableMetadata returnColumnMetadata(String keyspace, String tableName) throws MusicServiceException { + return MusicDataStoreHandle.returnColumnMetadata(keyspace, tableName); + } - private class RowIdentifier { - public String primarKeyValue; - public StringBuilder rowIdString; + class RowIdentifier { + private String primarKeyValue; + private String rowIdString; @SuppressWarnings("unused") public PreparedQueryObject queryObject; // the string with all the row // identifiers separated by AND - public RowIdentifier(String primaryKeyValue, StringBuilder rowIdString, + public RowIdentifier(String primaryKeyValue, String rowIdString, PreparedQueryObject queryObject) { this.primarKeyValue = primaryKeyValue; this.rowIdString = rowIdString; this.queryObject = queryObject; } + + public String getPrimaryKeyValue() { + return this.primarKeyValue; + } + + public void setPrimaryKeyValue(String primaryKeyValue) { + this.primarKeyValue = primaryKeyValue; + } + + public String getRowIdString() { + return this.rowIdString.toString(); + } + + public void setRowIdString(String rowIdString) { + this.rowIdString = rowIdString; + } + + public PreparedQueryObject getQueryObject() { + return this.queryObject; + } } /** @@ -373,12 +397,12 @@ public class JsonUpdate implements Serializable { * @return * @throws MusicServiceException */ - private RowIdentifier getRowIdentifier(String keyspace, String tablename, + RowIdentifier getRowIdentifier(String keyspace, String tablename, MultivaluedMap rowParams, PreparedQueryObject queryObject) throws MusicServiceException { StringBuilder rowSpec = new StringBuilder(); int counter = 0; - TableMetadata tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename); + TableMetadata tableInfo = returnColumnMetadata(keyspace, tablename); if (tableInfo == null) { logger.error(EELFLoggerDelegate.errorLogger, "Table information not found. Please check input for table name= " @@ -410,7 +434,7 @@ public class JsonUpdate implements Serializable { } counter = counter + 1; } - return new RowIdentifier(primaryKey.toString(), rowSpec, queryObject); + return new RowIdentifier(primaryKey.toString(), rowSpec.toString(), queryObject); } } 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..e9533344 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 @@ -129,7 +129,7 @@ public class CassaLockStore { table = table_prepend_name+table; String tabQuery = "CREATE TABLE IF NOT EXISTS "+keyspace+"."+table + " ( key text, lockReference bigint, createTime text, acquireTime text, guard bigint static, " - + "lockType text, owner text, PRIMARY KEY ((key), lockReference) ) " + + "lockType text, leasePeriodTime bigint, owner text, PRIMARY KEY ((key), lockReference) ) " + "WITH CLUSTERING ORDER BY (lockReference ASC);"; PreparedQueryObject queryObject = new PreparedQueryObject(); @@ -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 getAllLocksForOwner(String ownerId, String keyspace, String table) throws MusicServiceException, MusicQueryException { List toRet = new ArrayList(); String lockTable = table_prepend_name + table; diff --git a/music-core/src/main/java/org/onap/music/main/CipherUtil.java b/music-core/src/main/java/org/onap/music/main/CipherUtil.java index 327022d5..3278dc12 100644 --- a/music-core/src/main/java/org/onap/music/main/CipherUtil.java +++ b/music-core/src/main/java/org/onap/music/main/CipherUtil.java @@ -259,13 +259,20 @@ public class CipherUtil { } } - /*public static void main(String[] args) { + public static void main(String[] args) { - System.out.println("Encrypted password: "+encryptPKC("cassandra")); + if (args.length < 2) { + System.out.println("Usage: java -jar CipherUtil "); + return; + } + + keyString = args[0]; + String password = args[1]; + + String enc = encryptPKC(password); + System.out.println("Encrypted password: " + enc); - System.out.println("Decrypted password: "+decryptPKC("dDhqAp5/RwZbl9yRSZg15fN7Qul9eiE/JFkKemtTib0=")); - System.out.println("Decrypted password: "+decryptPKC("I/dOtD/YYzBStbtOYhKuUUyPHSW2G9ZzdSyB8bJp4vk=")); - System.out.println("Decrypted password: "+decryptPKC("g7zJqg74dLsH/fyL7I75b4eySy3pbMS2xVqkrB5lDl8=")); - }*/ + System.out.println("Decrypted password (to verify): " + decryptPKC(enc)); + } } diff --git a/music-core/src/main/java/org/onap/music/main/MusicUtil.java b/music-core/src/main/java/org/onap/music/main/MusicUtil.java index d46e770e..78d17c60 100644 --- a/music-core/src/main/java/org/onap/music/main/MusicUtil.java +++ b/music-core/src/main/java/org/onap/music/main/MusicUtil.java @@ -73,6 +73,7 @@ public class MusicUtil { public static final String EVENTUAL_NB = "eventual_nb"; public static final String ALL = "all"; public static final String QUORUM = "quorum"; + public static final String LOCAL_QUORUM = "local_quorum"; public static final String ONE = "one"; public static final String ATOMICDELETELOCK = "atomic_delete_lock"; @@ -117,8 +118,8 @@ public class MusicUtil { // "clientId.header.prefix","messageId.header.prefix"}; // Consistency Constants and variables. private static final String[] cosistencyLevel = new String[] { - "ALL","EACH_QUORUM","QUORUM","LOCAL_QUORUM","ONE","TWO", - "THREE","LOCAL_ONE","ANY","SERIAL","LOCAL_SERIAL"}; + "ALL","EACH_QUORUM","QUORUM","LOCAL_QUORUM","ONE","TWO", + "THREE","LOCAL_ONE","ANY","SERIAL","LOCAL_SERIAL"}; private static final Map consistencyName = new HashMap<>(); static { consistencyName.put("ONE",ConsistencyLevel.ONE); @@ -138,6 +139,8 @@ public class MusicUtil { private static String cassPwd; private static String myCassaHost = LOCALHOST; private static int cassandraPort = 9042; + private static int cassandraConnectTimeOutMS; + private static int cassandraReadTimeOutMS; // AAF private static String musicAafNs = "org.onap.music.cadi"; @@ -149,11 +152,11 @@ public class MusicUtil { // Response/Request tracking headers private static String transIdPrefix = "false"; - private static String conversationIdPrefix = "false"; + private static String conversationIdPrefix = "false"; private static String clientIdPrefix = "false"; private static String messageIdPrefix = "false"; - private static Boolean transIdRequired = false; - private static Boolean conversationIdRequired = false; + private static Boolean transIdRequired = false; + private static Boolean conversationIdRequired = false; private static Boolean clientIdRequired = false; private static Boolean messageIdRequired = false; private static String cipherEncKey = ""; @@ -161,7 +164,7 @@ public class MusicUtil { public MusicUtil() { throw new IllegalStateException("Utility Class"); } - + public static String getLockUsing() { return lockUsing; } @@ -199,15 +202,31 @@ public class MusicUtil { return cassPwd; } + public static int getCassandraConnectTimeOutMS() { + return cassandraConnectTimeOutMS; + } + + public static void setCassandraConnectTimeOutMS(int cassandraConnectTimeOutMS) { + MusicUtil.cassandraConnectTimeOutMS = cassandraConnectTimeOutMS; + } + + public static int getCassandraReadTimeOutMS() { + return cassandraReadTimeOutMS; + } + + public static void setCassandraReadTimeOutMS(int cassandraReadTimeOutMS) { + MusicUtil.cassandraReadTimeOutMS = cassandraReadTimeOutMS; + } + /** * Returns An array of property names that should be in the Properties * files. * // * @return // */ -// public static String[] getPropkeys() { -// return propKeys.clone(); -// } + // public static String[] getPropkeys() { + // return propKeys.clone(); + // } /** * Get MusicPropertiesFilePath - Default = /opt/music/music.properties @@ -318,7 +337,7 @@ public class MusicUtil { public static void setMyCassaHost(String myCassaHost) { MusicUtil.myCassaHost = myCassaHost; } - + /** * Gey default retry count * @return @@ -424,23 +443,23 @@ public class MusicUtil { String value = ""; switch (type.getName()) { - case UUID: - value = valueObj + ""; - break; - case TEXT: - case VARCHAR: - String valueString = valueObj + ""; - valueString = valueString.replace("'", "''"); - value = "'" + valueString + "'"; - break; - case MAP: { - Map otMap = (Map) valueObj; - value = "{" + jsonMaptoSqlString(otMap, ",") + "}"; - break; - } - default: - value = valueObj + ""; - break; + case UUID: + value = valueObj + ""; + break; + case TEXT: + case VARCHAR: + String valueString = valueObj + ""; + valueString = valueString.replace("'", "''"); + value = "'" + valueString + "'"; + break; + case MAP: { + Map otMap = (Map) valueObj; + value = "{" + jsonMaptoSqlString(otMap, ",") + "}"; + break; + } + default: + value = valueObj + ""; + break; } return value; } @@ -584,14 +603,14 @@ public class MusicUtil { return ts; } - + public static MusicCoreService getMusicCoreService() { if(getLockUsing().equals(MusicUtil.CASSANDRA)) return MusicCassaCore.getInstance(); else return MusicCassaCore.getInstance(); } - + /** * @param lockName * @return @@ -616,9 +635,9 @@ public class MusicUtil { } public static void writeBackToQuorum(PreparedQueryObject selectQuery, String primaryKeyName, - PreparedQueryObject updateQuery, String keyspace, String table, - Object cqlFormattedPrimaryKeyValue) - throws Exception { + PreparedQueryObject updateQuery, String keyspace, String table, + Object cqlFormattedPrimaryKeyValue) + throws Exception { try { ResultSet results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(selectQuery); // write it back to a quorum @@ -641,16 +660,16 @@ public class MusicUtil { counter = counter + 1; } updateQuery.appendQueryString("UPDATE " + keyspace + "." + table + " SET " - + fieldValueString + " WHERE " + primaryKeyName + "= ? " + ";"); + + fieldValueString + " WHERE " + primaryKeyName + "= ? " + ";"); updateQuery.addValue(cqlFormattedPrimaryKeyValue); MusicDataStoreHandle.getDSHandle().executePut(updateQuery, "critical"); } catch (MusicServiceException | MusicQueryException e) { logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.QUERYERROR +""+updateQuery , - ErrorSeverity.MAJOR, ErrorTypes.QUERYERROR, e); + ErrorSeverity.MAJOR, ErrorTypes.QUERYERROR, e); } } - + public static boolean getIsCadi() { return MusicUtil.isCadi; } @@ -729,64 +748,64 @@ public class MusicUtil { MusicUtil.messageIdPrefix = checkPrefix(messageIdPrefix); } - /** - * @return the transIdRequired - */ + /** + * @return the transIdRequired + */ public static Boolean getTransIdRequired() { return transIdRequired; } /** - * @param transIdRequired the transIdRequired to set - */ + * @param transIdRequired the transIdRequired to set + */ public static void setTransIdRequired(Boolean transIdRequired) { MusicUtil.transIdRequired = transIdRequired; } /** - * @return the conversationIdRequired - */ + * @return the conversationIdRequired + */ public static Boolean getConversationIdRequired() { return conversationIdRequired; } /** - * @param conversationIdRequired the conversationIdRequired to set - */ + * @param conversationIdRequired the conversationIdRequired to set + */ public static void setConversationIdRequired(Boolean conversationIdRequired) { MusicUtil.conversationIdRequired = conversationIdRequired; } /** - * @return the clientIdRequired - */ + * @return the clientIdRequired + */ public static Boolean getClientIdRequired() { return clientIdRequired; } /** - * @param clientIdRequired the clientIdRequired to set - */ + * @param clientIdRequired the clientIdRequired to set + */ public static void setClientIdRequired(Boolean clientIdRequired) { MusicUtil.clientIdRequired = clientIdRequired; } /** - * @return the messageIdRequired - */ + * @return the messageIdRequired + */ public static Boolean getMessageIdRequired() { return messageIdRequired; } /** - * @param messageIdRequired the messageIdRequired to set - */ + * @param messageIdRequired the messageIdRequired to set + */ public static void setMessageIdRequired(Boolean messageIdRequired) { MusicUtil.messageIdRequired = messageIdRequired; } @@ -800,7 +819,7 @@ public class MusicUtil { public static void setCipherEncKey(String cipherEncKey) { MusicUtil.cipherEncKey = cipherEncKey; if ( null == cipherEncKey || cipherEncKey.equals("") || - cipherEncKey.equals("nothing to see here")) { + cipherEncKey.equals("nothing to see here")) { logger.error(EELFLoggerDelegate.errorLogger, "Missing Cipher Encryption Key."); } } 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 similarity index 96% rename from music-core/src/test/java/org/onap/music/unittests/MusicUtilTest.java rename to 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()); + Mockito.when(row.getList("columnName", String.class)).thenReturn(new ArrayList()); + + + 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) dataStore.getColValue(row, "columnName", + DataType.map(DataType.varchar(), DataType.varchar()))).size()); + assertEquals(0, + ((List) 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 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> 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 sessionExecuteResponse() { + ResultSet rs = Mockito.mock(ResultSet.class); + Mockito.when(session.execute(Mockito.any(Statement.class))).thenReturn(rs); + + ArgumentCaptor argument = ArgumentCaptor.forClass(SimpleStatement.class); + return argument; + } + + @Test + public void testExecutePutPreparedQueryObjectString() throws Exception { + ArgumentCaptor 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 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 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 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 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 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 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 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 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 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 similarity index 96% rename from music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonDeleteTest.java rename to 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 similarity index 53% rename from music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonInsertTest.java rename to 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 rowSpec = new HashMap<>(); + rowSpec.put("val1","one"); + rowSpec.put("val2","two"); + ji.setRowSpecification(rowSpec); + Map vals = new HashMap<>(); + vals.put("val1","one"); + vals.put("val2","two"); + ji.setValues(vals); + + Map 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 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 similarity index 95% rename from music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonKeySpaceTest.java rename to 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 similarity index 94% rename from music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonSelectTest.java rename to 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 similarity index 59% rename from music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonTableTest.java rename to 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 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 fields = new HashMap<>(); + fields.put("k1", "one"); + jt2.setFields(fields); + jt2.setPrimaryKey("k1"); + Map 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 fields = new HashMap<>(); + fields.put("k1", "one"); + jt2.setFields(fields); + jt2.setPrimaryKey("k1"); + Map 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 similarity index 58% rename from music-core/src/test/java/org/onap/music/unittests/jsonobjects/JsonUpdateTest.java rename to 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 rowParams = Mockito.mock(MultivaluedMap.class); + + ju.setKeyspaceName("keyspace"); + ju.setTableName("table"); + ju.setPrimarKeyValue("primaryKeyValue"); + Map consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "critical"); + ju.setConsistencyInfo(consistencyInfo); + Map 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 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/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 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 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 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 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 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 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 similarity index 95% rename from music-core/src/test/java/org/onap/music/unittests/ResultTypeTest.java rename to 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 similarity index 95% rename from music-core/src/test/java/org/onap/music/unittests/ReturnTypeTest.java rename to 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 currentHolders = new ArrayList<>(); + currentHolders.add("$"+fullyQualifiedKey+"$1"); + currentHolders.add("$"+fullyQualifiedKey+"$2"); + Mockito.when(mLockHandle.getCurrentLockHolders("keyspace", "table", "lockName")) + .thenReturn(currentHolders); + + List 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 ownersLocks = new ArrayList<>(); + ownersLocks.add("lockName$1"); + ownersLocks.add("lockName$2"); + Mockito.when(mLockHandle.getAllLocksForOwner("ownerId", "keyspace", "table")) + .thenReturn(ownersLocks); + + List 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 myList = new ArrayList<>(); + Mockito.when(mLockHandle.getLockQueue("keyspace", "table", "lockName")) + .thenReturn(myList); + List 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,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 setPreparedInsertValues1() { - - List 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 setPreparedInsertValues2() { - - List 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 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 setPreparedUpdateValues() { - - List preparedUpdateValues = new ArrayList<>(); - String vectorTs = - String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); - Map 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 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 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 values = setPreparedUpdateValues(); - if (!values.isEmpty() || values != null) { - for (Object o : values) { - queryobject.addValue(o); - } - } - return queryobject; - - } - - private static ArrayList getAllPossibleLocalIps() { - ArrayList allPossibleIps = new ArrayList(); - try { - Enumeration en = NetworkInterface.getNetworkInterfaces(); - while (en.hasMoreElements()) { - NetworkInterface ni = (NetworkInterface) en.nextElement(); - Enumeration 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); - } - -} diff --git a/music-rest/src/main/java/org/onap/music/main/PropertiesLoader.java b/music-rest/src/main/java/org/onap/music/main/PropertiesLoader.java index 7ef36f23..6fbc76a7 100644 --- a/music-rest/src/main/java/org/onap/music/main/PropertiesLoader.java +++ b/music-rest/src/main/java/org/onap/music/main/PropertiesLoader.java @@ -41,37 +41,43 @@ public class PropertiesLoader implements InitializingBean { @Value("${debug}") public String debug; - + @Value("${version}") public String version; @Value("${build}") public String build; - + @Value("${music.properties}") public String musicProperties; - + @Value("${lock.lease.period}") public String lockLeasePeriod; - + @Value("${cassandra.user}") public String cassandraUser; - + @Value("${cassandra.password}") public String cassandraPassword; - + @Value("${cassandra.port}") public String cassandraPort; - + + @Value("${cassandra.connecttimeoutms}") + public String cassandraConnectTimeOutMS; + + @Value("${cassandra.readtimeoutms}") + public String cassandraReadTimeOutMS; + @Value("${cadi}") public String isCadi; - + @Value("${keyspace.active}") public String isKeyspaceActive; @Value("${retry.count}") public String rertryCount; - + @Value("${transId.header.prefix}") private String transIdPrefix; @@ -83,7 +89,7 @@ public class PropertiesLoader implements InitializingBean { @Value("${messageId.header.prefix}") private String messageIdPrefix; - + @Value("${transId.header.required}") private Boolean transIdRequired; @@ -101,19 +107,19 @@ public class PropertiesLoader implements InitializingBean { @Value("${cipher.enc.key}") private String cipherEncKey; - + @SuppressWarnings("unused") - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PropertiesLoader.class); - + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PropertiesLoader.class); + @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { - + PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer(); pspc.setIgnoreResourceNotFound(true); pspc.setIgnoreUnresolvablePlaceholders(true); return pspc; } - + /** * . */ @@ -133,6 +139,12 @@ public class PropertiesLoader implements InitializingBean { if (cassandraPassword != null && !cassandraPassword.equals("${cassandra.password}")) { MusicUtil.setCassPwd(cassandraPassword); } + if (cassandraConnectTimeOutMS != null && !cassandraConnectTimeOutMS.equals("${cassandra.connecttimeoutms}")) { + MusicUtil.setCassandraConnectTimeOutMS(Integer.parseInt(cassandraConnectTimeOutMS)); + } + if (cassandraReadTimeOutMS != null && !cassandraReadTimeOutMS.equals("${cassandra.readtimeoutms}")) { + MusicUtil.setCassandraReadTimeOutMS(Integer.parseInt(cassandraReadTimeOutMS)); + } if (debug != null && !debug.equals("${debug}")) { MusicUtil.setDebug(Boolean.parseBoolean(debug)); } @@ -209,7 +221,15 @@ public class PropertiesLoader implements InitializingBean { if (properties.getProperty("cassandra.password")!=null) { MusicUtil.setCassPwd(properties.getProperty("cassandra.password")); } - + + if(properties.getProperty("cassandra.connectTimeOutMS")!=null) { + MusicUtil.setCassandraConnectTimeOutMS(Integer.parseInt(properties.getProperty("cassandra.connecttimeoutms"))); + } + + if(properties.getProperty("cassandra.readTimeOutMS")!=null) { + MusicUtil.setCassandraReadTimeOutMS(Integer.parseInt(properties.getProperty("cassandra.readtimeoutms"))); + } + if (properties.getProperty("music.properties")!=null) { MusicUtil.setMusicPropertiesFilePath(properties.getProperty("music.properties")); } @@ -283,11 +303,11 @@ public class PropertiesLoader implements InitializingBean { } } - + @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub - + } }