summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorburdziak <olaf.burdziakowski@nokia.com>2019-04-11 15:29:58 +0200
committerburdziak <olaf.burdziakowski@nokia.com>2019-04-19 12:36:54 +0200
commit122a5255adf6255584a8eef860424e3ecffcfd47 (patch)
tree5e868c94d923c90d790a8fd28f2615a12d88a3bf
parentfa5f20d91c416a855f0d6afe157db8250574617c (diff)
Fix sonar issues (part) in MusicService
Change-Id: I043cb7e3ea4c3f1ad450e07966d8c2a098e0936e Issue-ID: PORTAL-523 Signed-off-by: burdziak <olaf.burdziakowski@nokia.com>
-rw-r--r--ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/conf/MusicSession.java4
-rw-r--r--ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/conf/MusicSessionRepositoryHandler.java2
-rw-r--r--ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/service/MusicService.java135
-rw-r--r--ecomp-sdk/epsdk-music/src/test/java/org/onap/portalapp/music/conf/MusicSessionTest.java9
4 files changed, 71 insertions, 79 deletions
diff --git a/ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/conf/MusicSession.java b/ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/conf/MusicSession.java
index 1f68bda0..322e1b41 100644
--- a/ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/conf/MusicSession.java
+++ b/ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/conf/MusicSession.java
@@ -283,8 +283,6 @@ public final class MusicSession implements Session, Serializable {
}
} catch (MusicLockingException e) {
logger.error(EELFLoggerDelegate.errorLogger, "isExpired locking failed with id:" + id, e);
- } catch (MusicServiceException e) {
- logger.error(EELFLoggerDelegate.errorLogger, "isExpired failed with id:" + id, e);
}
return isExpired;
}
@@ -331,8 +329,6 @@ public final class MusicSession implements Session, Serializable {
MusicService.removeAttribute(attributeName, this.id);
}catch(MusicLockingException e){
logger.error(EELFLoggerDelegate.errorLogger, " removeAttribute locking failed id:" + this.id + " attribute name "+ attributeName , e);
- }catch(MusicServiceException e){
- logger.error(EELFLoggerDelegate.errorLogger, " removeAttribute failed id:" + this.id + " attribute name "+ attributeName , e);
}
}
diff --git a/ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/conf/MusicSessionRepositoryHandler.java b/ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/conf/MusicSessionRepositoryHandler.java
index 0e8551b9..167ed955 100644
--- a/ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/conf/MusicSessionRepositoryHandler.java
+++ b/ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/conf/MusicSessionRepositoryHandler.java
@@ -80,8 +80,6 @@ public class MusicSessionRepositoryHandler {
MusicService.removeSession(id);
} catch (MusicLockingException e) {
logger.error(EELFLoggerDelegate.errorLogger, "removeSession locking failed with id " + id, e);
- } catch (MusicServiceException e) {
- logger.error(EELFLoggerDelegate.errorLogger, "removeSession failed with id " + id, e);
}
}
}
diff --git a/ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/service/MusicService.java b/ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/service/MusicService.java
index 3861b2dd..c8d15884 100644
--- a/ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/service/MusicService.java
+++ b/ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/service/MusicService.java
@@ -80,9 +80,17 @@ public class MusicService {
private static String musicMetaTable = MusicProperties.getProperty(MusicProperties.MUSIC_SESSION_META_TABLES);
private static String musicAttrTable = MusicProperties.getProperty(MusicProperties.MUSIC_SESSION_ATTR_TABLES);
+ private static final String WITH_SESSION_ID = " with session id: ";
+ private static final String RESULT = "result:";
+ private static final String WHERE = " WHERE ";
+ private static final String FROM = " FROM ";
+ private static final String DELETE = "DELETE ";
+ private static final String REMOVE_SESSION = "removeSession: ";
+ private static final String SUCCESS = "success";
+
/**
* Store session attribute name and values into Cassandra via Music
- *
+ *
* @param attributeName
* @param value
* @param sessionId
@@ -116,18 +124,18 @@ public class MusicService {
else
result = MusicCore.eventualPut(queryObject);
logger.debug(EELFLoggerDelegate.debugLogger, "setAttribute: attributeName: " + attributeName
- + " with session id: " + sessionId + "result:" + result.getMessage());
+ + WITH_SESSION_ID + sessionId + RESULT + result.getMessage());
return result;
}
/**
* Store session meta data values into Cassandra via Music
- *
+ *
* @param session
* @return ReturnType that includes required body information for Music api
* @throws Exception
*/
- public static ReturnType setMetaAttribute(Session session) throws Exception {
+ public static ReturnType setMetaAttribute(Session session) throws MusicLockingException {
logger.debug(EELFLoggerDelegate.debugLogger, "setMetaAttribute: start with session id: " + session.getId());
ReturnType result = null;
PreparedQueryObject queryObject = new PreparedQueryObject();
@@ -148,14 +156,14 @@ public class MusicService {
else
result = MusicCore.eventualPut(queryObject);
logger.debug(EELFLoggerDelegate.debugLogger,
- "setMetaAttribute: with session id: " + session + "result:" + result.getMessage());
+ "setMetaAttribute: with session id: " + session + RESULT + result.getMessage());
return result;
}
/**
* Retrieve session meta data from Cassandra via Music
- *
+ *
* @param sessionId
* @return MusicSession
* @throws Exception
@@ -165,7 +173,7 @@ public class MusicService {
ResultSet result = null;
PreparedQueryObject queryObject = new PreparedQueryObject();
StringBuilder querySB = new StringBuilder();
- querySB.append("SELECT * FROM ").append(musicKeySpace).append(".").append(musicMetaTable).append(" WHERE ")
+ querySB.append("SELECT * FROM ").append(musicKeySpace).append(".").append(musicMetaTable).append(WHERE)
.append(MusicProperties.PRIMARY_ID).append("=?;");
queryObject.appendQueryString(querySB.toString());
queryObject.addValue(sessionId);
@@ -179,7 +187,7 @@ public class MusicService {
/**
* Get proper column names (from meta or attribute table) base on isMeta
- *
+ *
* @param isMeta
* @param attributeName
* @return String
@@ -199,7 +207,7 @@ public class MusicService {
/**
* Retrieve session attribute data from Cassandra via Music
- *
+ *
* @param attributeName
* @param sessionId
* @return attribute value with T type
@@ -212,8 +220,8 @@ public class MusicService {
boolean isMeta = MusicUtil.isSessionMetaAttr(attributeName);
PreparedQueryObject queryObject = new PreparedQueryObject();
StringBuilder querySB = new StringBuilder();
- querySB.append("SELECT ").append(getColumn(attributeName, isMeta)).append(" FROM ").append(musicKeySpace)
- .append(".").append(getTableName(isMeta)).append(" WHERE ").append(MusicProperties.PRIMARY_ID)
+ querySB.append("SELECT ").append(getColumn(attributeName, isMeta)).append(FROM).append(musicKeySpace)
+ .append(".").append(getTableName(isMeta)).append(WHERE).append(MusicProperties.PRIMARY_ID)
.append("= ?");
queryObject.addValue(sessionId);
@@ -235,22 +243,22 @@ public class MusicService {
/**
* Remove session attribute data from Cassandra via Music
- *
+ *
* @param attributeName
* @param sessionId
* @return ReturnType
* @throws MusicServiceException
- * @throws MusicLockingException
+ * @throws MusicLockingException
*/
- public static ReturnType removeAttribute(String attributeName, String sessionId) throws MusicServiceException, MusicLockingException {
+ public static ReturnType removeAttribute(String attributeName, String sessionId) throws MusicLockingException {
logger.debug(EELFLoggerDelegate.debugLogger, "removeAttribute: start with session id: " + sessionId);
boolean isMeta = MusicUtil.isSessionMetaAttr(attributeName);
ReturnType result = null;
String tableName = null;
PreparedQueryObject queryObject = new PreparedQueryObject();
StringBuilder querySB = new StringBuilder();
- querySB.append("DELETE ").append(getDelColumn(isMeta, attributeName)).append(" FROM ").append(musicKeySpace)
- .append(".").append(getTableName(isMeta)).append(" WHERE ").append(MusicProperties.PRIMARY_ID)
+ querySB.append(DELETE).append(getDelColumn(isMeta, attributeName)).append(FROM).append(musicKeySpace)
+ .append(".").append(getTableName(isMeta)).append(WHERE).append(MusicProperties.PRIMARY_ID)
.append("= ? ");
queryObject.addValue(sessionId);
@@ -266,40 +274,40 @@ public class MusicService {
else
result = MusicCore.eventualPut(queryObject);
logger.debug(EELFLoggerDelegate.debugLogger,
- "removeSession: " + attributeName + " with session id: " + sessionId + "result:" + result.getMessage());
+ REMOVE_SESSION + attributeName + WITH_SESSION_ID + sessionId + RESULT + result.getMessage());
return result;
}
/**
* Remove entire session from Cassandra via Music
- *
+ *
* @param sessionId
* @return ReturnType
* @throws MusicServiceException
- * @throws MusicLockingException
+ * @throws MusicLockingException
*/
- public static ReturnType removeSession(String sessionId) throws MusicServiceException, MusicLockingException {
+ public static ReturnType removeSession(String sessionId) throws MusicLockingException {
ReturnType result = null;
boolean isAtomic = isAtomicPut;
logger.debug(EELFLoggerDelegate.debugLogger, "removeSession: start with session id: " + sessionId);
PreparedQueryObject queryObject = new PreparedQueryObject();
StringBuilder querySB = new StringBuilder();
- querySB.append("DELETE ").append(" FROM ").append(musicKeySpace).append(".").append(musicMetaTable)
- .append(" WHERE ").append(MusicProperties.PRIMARY_ID).append("= ? ");
+ querySB.append(DELETE).append(FROM).append(musicKeySpace).append(".").append(musicMetaTable)
+ .append(WHERE).append(MusicProperties.PRIMARY_ID).append("= ? ");
queryObject.appendQueryString(querySB.toString());
queryObject.addValue(sessionId);
if (isAtomic)
result = MusicCore.atomicPutWithDeleteLock(musicKeySpace, musicMetaTable, sessionId, queryObject, null);
else
result = MusicCore.eventualPut(queryObject);
- logger.debug(EELFLoggerDelegate.debugLogger, "removeSession: " + musicMetaTable + " with session id: "
- + sessionId + "result:" + result.getMessage());
+ logger.debug(EELFLoggerDelegate.debugLogger, REMOVE_SESSION + musicMetaTable + WITH_SESSION_ID
+ + sessionId + RESULT + result.getMessage());
queryObject = new PreparedQueryObject();
querySB = new StringBuilder();
- querySB.append("DELETE ").append(" FROM ").append(musicKeySpace).append(".").append(musicAttrTable)
- .append(" WHERE ").append(MusicProperties.PRIMARY_ID).append("= ? ");
+ querySB.append(DELETE).append(FROM).append(musicKeySpace).append(".").append(musicAttrTable)
+ .append(WHERE).append(MusicProperties.PRIMARY_ID).append("= ? ");
queryObject.appendQueryString(querySB.toString());
queryObject.addValue(sessionId);
if (isAtomic)
@@ -307,15 +315,15 @@ public class MusicService {
else
result = MusicCore.eventualPut(queryObject);
- logger.debug(EELFLoggerDelegate.debugLogger, "removeSession: " + musicAttrTable + " with session id: "
- + sessionId + "result:" + result.getMessage());
+ logger.debug(EELFLoggerDelegate.debugLogger, REMOVE_SESSION + musicAttrTable + WITH_SESSION_ID
+ + sessionId + RESULT + result.getMessage());
return result;
}
/**
* Get proper table name (Meta or Attribute) base on isMeta.
- *
+ *
* @param isMeta
* @return String
*/
@@ -330,7 +338,7 @@ public class MusicService {
/**
* Get proper column name (Meta or Attribute) base on isMeta.
- *
+ *
* @param attributeName
* @param isMeta
* @return String
@@ -346,7 +354,7 @@ public class MusicService {
/**
* Get proper column name (Meta or Attribute) base on isMeta for removing.
- *
+ *
* @param attributeName
* @param isMeta
* @return String
@@ -360,7 +368,7 @@ public class MusicService {
/**
* To set session attributes in Music
- *
+ *
* @param attributeName
* @param value
* @param session
@@ -374,22 +382,22 @@ public class MusicService {
String sessionId, String className, boolean isMeta) throws JsonProcessingException {
System.out.println("setAttribute: " + attributeName);
RestResponse<String> portalRestResponse = null;
- HttpEntity<Map<String, Object>> entity = new HttpEntity<Map<String, Object>>(
+ HttpEntity<Map<String, Object>> entity = new HttpEntity<>(
getMusicRestBody(attributeName, value, sessionId, session, className, isMeta), getMusicHeader());
String url = getInsertUrl(isMeta);
ResponseEntity<String> response = null;
try {
response = template.exchange(url, HttpMethod.POST, entity, String.class);
- portalRestResponse = new RestResponse<String>(RestStatusEnum.OK, "success", response.getBody());
+ portalRestResponse = new RestResponse<>(RestStatusEnum.OK, SUCCESS, response.getBody());
} catch (Exception e) {
- portalRestResponse = new RestResponse<String>(RestStatusEnum.ERROR, e.getMessage(), null);
+ portalRestResponse = new RestResponse<>(RestStatusEnum.ERROR, e.getMessage(), null);
}
return portalRestResponse;
}
/**
* To get session attribute in Music
- *
+ *
* @param attributeName
* @param value
* @param sessionId
@@ -401,23 +409,22 @@ public class MusicService {
boolean isMeta) {
System.out.println("getAttribute: " + attributeName);
RestResponse<String> portalRestResponse = null;
- HttpEntity<String> entity = new HttpEntity<String>(null, getMusicHeader());
+ HttpEntity<String> entity = new HttpEntity<>(null, getMusicHeader());
ResponseEntity<String> response = null;
String url = getSelectSessionIdUrl(attributeName, sessionId, isMeta);
try {
response = template.exchange(url, HttpMethod.GET, entity, String.class);
- portalRestResponse = new RestResponse<String>(RestStatusEnum.OK, "success", response.getBody());
+ portalRestResponse = new RestResponse<>(RestStatusEnum.OK, SUCCESS, response.getBody());
} catch (Exception e) {
- portalRestResponse = new RestResponse<String>(RestStatusEnum.ERROR, e.getMessage(), null);
+ portalRestResponse = new RestResponse<>(RestStatusEnum.ERROR, e.getMessage(), null);
}
return portalRestResponse;
}
/**
* To remove session attribute or session meta in Music
- *
+ *
* @param attributeName
- * @param value
* @param sessionId
* @param isMeta
* @return RestResponse<String>
@@ -428,20 +435,18 @@ public class MusicService {
ResponseEntity<String> response = null;
String url = getSelectSessionIdUrl(attributeName, sessionId, true);
try {
- response = template.exchange(url, HttpMethod.DELETE, entity, String.class);
- portalRestResponse = new RestResponse<String>(RestStatusEnum.OK, "success", response.getBody());
url = getSelectSessionIdUrl(attributeName, sessionId, false);
response = template.exchange(url, HttpMethod.DELETE, entity, String.class);
- portalRestResponse = new RestResponse<String>(RestStatusEnum.OK, "success", response.getBody());
+ portalRestResponse = new RestResponse<>(RestStatusEnum.OK, SUCCESS, response.getBody());
} catch (Exception e) {
- portalRestResponse = new RestResponse<String>(RestStatusEnum.ERROR, e.getMessage(), null);
+ portalRestResponse = new RestResponse<>(RestStatusEnum.ERROR, e.getMessage(), null);
}
return portalRestResponse;
}
/**
* Generate body for Music api calls
- *
+ *
* @return String that includes required body information for Music api
* calls
* @throws JsonProcessingException
@@ -475,7 +480,7 @@ public class MusicService {
/**
* Generate body for Music delete api calls
- *
+ *
* @return String that includes required body information for Music api
* calls
* @throws JsonProcessingException
@@ -512,7 +517,7 @@ public class MusicService {
/**
* Generate header for Music api calls
- *
+ *
* @return header that contains required header information for Music api
* calls
*/
@@ -529,7 +534,7 @@ public class MusicService {
/**
* Construct URL for Music api calls
- *
+ *
* @return path
*/
private static String constructPath(boolean isMeta) {
@@ -545,10 +550,10 @@ public class MusicService {
path.append("/");
return path.toString();
}
-
+
/**
* Get a list of sessions that need to be cleaned up
- *
+ *
* @return List<String>
*/
private static List<String> getSessionToBeDeleted(){
@@ -559,15 +564,15 @@ public class MusicService {
List<String> sessionIDList = new ArrayList<>();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
StringBuilder querySB = new StringBuilder();
- String cleanUpThreshold = MusicProperties.getProperty(MusicProperties.MUSIC_CLEAN_UP_THRESHOLD); //Clean up sessions that's cleanUpThreshold hours ago
- Date dateForCleanup = new Date(System.currentTimeMillis() - 3600 * 1000 * Integer.valueOf(cleanUpThreshold)); // Get the threshold date that needs to be clean up
+ String cleanUpThreshold = MusicProperties.getProperty(MusicProperties.MUSIC_CLEAN_UP_THRESHOLD); //Clean up sessions that's cleanUpThreshold hours ago
+ Date dateForCleanup = new Date(System.currentTimeMillis() - 3600 * 1000 * Integer.valueOf(cleanUpThreshold)); // Get the threshold date that needs to be clean up
String dateForCleanupCondition = dateFormat.format(dateForCleanup);
- querySB.append("SELECT ").append(MusicProperties.PRIMARY_ID).append(" FROM ").append(musicKeySpace)
- .append(".").append(getTableName(true)).append(" WHERE ").append(MusicProperties.LAST_ACCESS_TIME)
+ querySB.append("SELECT ").append(MusicProperties.PRIMARY_ID).append(FROM).append(musicKeySpace)
+ .append(".").append(getTableName(true)).append(WHERE).append(MusicProperties.LAST_ACCESS_TIME)
.append("< ? ").append(" ALLOW FILTERING");
queryObject.appendQueryString(querySB.toString());
queryObject.addValue(dateForCleanupCondition);
-
+
try{
if (isAtomicGet)
result = MusicCore.atomicGetWithDeleteLock(musicKeySpace, musicMetaTable, null, queryObject);
@@ -583,10 +588,10 @@ public class MusicService {
}
return sessionIDList;
}
-
+
/**
* Remove session data in music base on the defined frequency
- *
+ *
* @return List<String>
*/
public static void cleanUpMusic() {
@@ -605,13 +610,13 @@ public class MusicService {
sessionIDListCondition.deleteCharAt(sessionIDListCondition.length()-1);
sessionIDListCondition.deleteCharAt(sessionIDListCondition.length()-1);
sessionIDListCondition.append(")");
- StringBuilder querySB = new StringBuilder();
+ StringBuilder querySB = new StringBuilder();
PreparedQueryObject queryObject = new PreparedQueryObject();
/**Deleting attributes table**/
querySB = new StringBuilder();
queryObject = new PreparedQueryObject();
querySB.append("DELETE FROM ").append(musicKeySpace)
- .append(".").append(getTableName(false)).append(" WHERE ").append(MusicProperties.PRIMARY_ID)
+ .append(".").append(getTableName(false)).append(WHERE).append(MusicProperties.PRIMARY_ID)
.append(" in ").append(sessionIDListCondition);
queryObject.appendQueryString(querySB.toString());
try{
@@ -623,13 +628,13 @@ public class MusicService {
logger.error(EELFLoggerDelegate.errorLogger, "Error while cleaning up music attributes tables" , e);
}
logger.debug(EELFLoggerDelegate.debugLogger, "Music sessions have been cleaned up !");
-
+
/**Deleting meta table**/
logger.debug(EELFLoggerDelegate.debugLogger, "Cleaning up meta table ...");
querySB = new StringBuilder();
queryObject = new PreparedQueryObject();
querySB.append("DELETE FROM ").append(musicKeySpace)
- .append(".").append(getTableName(true)).append(" WHERE ").append(MusicProperties.PRIMARY_ID)
+ .append(".").append(getTableName(true)).append(WHERE).append(MusicProperties.PRIMARY_ID)
.append(" in ").append(sessionIDListCondition);
queryObject.appendQueryString(querySB.toString());
try{
@@ -640,13 +645,13 @@ public class MusicService {
}catch(Exception e){
logger.error(EELFLoggerDelegate.errorLogger, "Error while cleaning up music meta tables" , e);
}
-
+
logger.debug(EELFLoggerDelegate.debugLogger, "Cleaned up attributes table ... ");
}else{
logger.debug(EELFLoggerDelegate.debugLogger, "No Session needs to be cleaned up");
}
-
- }
+
+ }
}
}
diff --git a/ecomp-sdk/epsdk-music/src/test/java/org/onap/portalapp/music/conf/MusicSessionTest.java b/ecomp-sdk/epsdk-music/src/test/java/org/onap/portalapp/music/conf/MusicSessionTest.java
index 65290d48..fab1ba7b 100644
--- a/ecomp-sdk/epsdk-music/src/test/java/org/onap/portalapp/music/conf/MusicSessionTest.java
+++ b/ecomp-sdk/epsdk-music/src/test/java/org/onap/portalapp/music/conf/MusicSessionTest.java
@@ -233,14 +233,7 @@ public class MusicSessionTest {
}
@Test
- public void removeAttributeMusicServiceExceptionTest() throws MusicServiceException, MusicLockingException {
- Mockito.when(MusicService.removeAttribute(Matchers.anyString(), Matchers.anyString()))
- .thenThrow(new MusicServiceException());
- musicSession.removeAttribute("test_attr_name");
- }
-
- @Test
- public void removeAttributeMusicLockingExceptionTest() throws MusicServiceException, MusicLockingException {
+ public void removeAttributeMusicLockingExceptionTest() throws MusicLockingException {
Mockito.when(MusicService.removeAttribute(Matchers.anyString(), Matchers.anyString()))
.thenThrow(new MusicLockingException());
musicSession.removeAttribute("test_attr_name");