From f327e0cb4a14ff8d4f85e5ae9da5986b8c87ab8c Mon Sep 17 00:00:00 2001 From: "Thomas Nelson Jr (arthurdent3) tn1381@att.com" Date: Sun, 4 Mar 2018 03:28:07 -0500 Subject: Sonar Fixes to increate Coverage on Unit tests Including bug fix that added proper Exception handling on REST Calls. Change-Id: I618faa69ab549e76c148bb7c664a5ffe0eae84ca Issue-ID: MUSIC-44, MUSIC-45 Signed-off-by: Thomas Nelson Jr (arthurdent3) tn1381@att.com --- src/main/java/org/onap/music/main/CachingUtil.java | 25 ++++++++++++---- src/main/java/org/onap/music/main/MusicCore.java | 33 +++++++++++++++++----- src/main/java/org/onap/music/main/MusicUtil.java | 2 +- src/main/java/org/onap/music/main/ResultType.java | 3 +- 4 files changed, 48 insertions(+), 15 deletions(-) (limited to 'src/main/java/org/onap/music/main') diff --git a/src/main/java/org/onap/music/main/CachingUtil.java b/src/main/java/org/onap/music/main/CachingUtil.java index 4b2b4824..aef261f0 100755 --- a/src/main/java/org/onap/music/main/CachingUtil.java +++ b/src/main/java/org/onap/music/main/CachingUtil.java @@ -378,8 +378,7 @@ public class CachingUtil implements Runnable { } - public static Map verifyOnboarding(String ns, String userId, String password) - throws Exception { + public static Map verifyOnboarding(String ns, String userId, String password) { Map resultMap = new HashMap<>(); if (ns == null || userId == null || password == null) { logger.error(EELFLoggerDelegate.errorLogger,"One or more required headers is missing. userId: "+userId+" :: password: "+password); @@ -390,10 +389,24 @@ public class CachingUtil implements Runnable { PreparedQueryObject queryObject = new PreparedQueryObject(); queryObject.appendQueryString( "select * from admin.keyspace_master where application_name = ? and username = ? and password = ? allow filtering"); - queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), ns)); - queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); - queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), password)); - Row rs = MusicCore.get(queryObject).one(); + try { + queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), ns)); + queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); + queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), password)); + } catch(Exception e) { + resultMap.put("Exception", + "Unable to process input data. Invalid input data type. Please check ns, userId and password values. "+e.getMessage()); + return resultMap; + } + Row rs = null; + try { + rs = MusicCore.get(queryObject).one(); + } catch (MusicServiceException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + resultMap.put("Exception", "Unable to process operation. Error is "+e.getMessage()); + return resultMap; + } if (rs == null) { logger.error(EELFLoggerDelegate.errorLogger,"Namespace, UserId and password doesn't match. namespace: "+ns+" and userId: "+userId); diff --git a/src/main/java/org/onap/music/main/MusicCore.java b/src/main/java/org/onap/music/main/MusicCore.java index 3d5cf4e5..04aa60ce 100644 --- a/src/main/java/org/onap/music/main/MusicCore.java +++ b/src/main/java/org/onap/music/main/MusicCore.java @@ -66,7 +66,7 @@ public class MusicCore { this.selectQueryForTheRow = selectQueryForTheRow; } - public boolean testCondition() { + public boolean testCondition() throws Exception { // first generate the row ResultSet results = quorumGet(selectQueryForTheRow); Row row = results.one(); @@ -111,13 +111,20 @@ public class MusicCore { /** * * @return + * @throws MusicServiceException */ - public static MusicDataStore getDSHandle() { + public static MusicDataStore getDSHandle() throws MusicServiceException { logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring data store handle"); long start = System.currentTimeMillis(); if (mDstoreHandle == null) { mDstoreHandle = new MusicDataStore(); } + if(mDstoreHandle.getSession() == null) { + String message = "Connection to Cassandra has not been enstablished." + + " Please check connection properites and reboot."; + logger.info(EELFLoggerDelegate.applicationLogger, message); + throw new MusicServiceException(message); + } long end = System.currentTimeMillis(); logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire data store handle:" + (end - start) + " ms"); return mDstoreHandle; @@ -293,7 +300,11 @@ public class MusicCore { // do syncing if this was a forced lock release if (needToSyncQuorum) { logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: Since there was a forcible release, need to sync quorum!"); - syncQuorum(key); + try { + syncQuorum(key); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger,"Failed to set Lock state " + e); + } } // change status to locked @@ -325,7 +336,7 @@ public class MusicCore { } - private static void syncQuorum(String key) { + private static void syncQuorum(String key) throws Exception { logger.info(EELFLoggerDelegate.applicationLogger,"Performing sync operation---"); String[] splitString = key.split("\\."); String keyspaceName = splitString[0]; @@ -407,8 +418,9 @@ public class MusicCore { * * @param results * @return + * @throws MusicServiceException */ - public static Map> marshallResults(ResultSet results) { + public static Map> marshallResults(ResultSet results) throws MusicServiceException { return getDSHandle().marshalData(results); } @@ -506,8 +518,9 @@ public class MusicCore { * @param keyspace * @param tablename * @return + * @throws MusicServiceException */ - public static TableMetadata returnColumnMetadata(String keyspace, String tablename) { + public static TableMetadata returnColumnMetadata(String keyspace, String tablename) throws MusicServiceException { return getDSHandle().returnColumnMetadata(keyspace, tablename); } @@ -604,10 +617,16 @@ public class MusicCore { MusicLockState mls = getLockingServiceHandle() .getLockState(keyspaceName + "." + tableName + "." + primaryKey); if (mls.getLockHolder().equals(lockId) == true) { - if (conditionInfo != null)// check if condition is true + if (conditionInfo != null) + try { if (conditionInfo.testCondition() == false) return new ReturnType(ResultType.FAILURE, "Lock acquired but the condition is not true"); + } catch (Exception e) { + return new ReturnType(ResultType.FAILURE, + "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n" + + e.getMessage()); + } getDSHandle().executePut(queryObject, MusicUtil.CRITICAL); long end = System.currentTimeMillis(); logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the critical put:" + (end - start) + " ms"); diff --git a/src/main/java/org/onap/music/main/MusicUtil.java b/src/main/java/org/onap/music/main/MusicUtil.java index a31e33f9..3c0d4c9d 100755 --- a/src/main/java/org/onap/music/main/MusicUtil.java +++ b/src/main/java/org/onap/music/main/MusicUtil.java @@ -418,7 +418,7 @@ public class MusicUtil { * @return * @throws Exception */ - public static Object convertToActualDataType(DataType colType, Object valueObj) { + public static Object convertToActualDataType(DataType colType, Object valueObj) throws Exception{ String valueObjString = valueObj + ""; switch (colType.getName()) { case UUID: diff --git a/src/main/java/org/onap/music/main/ResultType.java b/src/main/java/org/onap/music/main/ResultType.java index f19ada4a..6b0c5252 100644 --- a/src/main/java/org/onap/music/main/ResultType.java +++ b/src/main/java/org/onap/music/main/ResultType.java @@ -22,7 +22,8 @@ package org.onap.music.main; public enum ResultType { - SUCCESS("Success"), FAILURE("Failure"); + SUCCESS("Success"), FAILURE("Failure"), + SYNTAXERROR("SyntaxError"), EXCEPTION("Exception"); private String result; -- cgit 1.2.3-korg