Fix sonar issues (part) in MusicService 57/85057/4
authorburdziak <olaf.burdziakowski@nokia.com>
Thu, 11 Apr 2019 13:29:58 +0000 (15:29 +0200)
committerburdziak <olaf.burdziakowski@nokia.com>
Fri, 19 Apr 2019 10:36:54 +0000 (12:36 +0200)
Change-Id: I043cb7e3ea4c3f1ad450e07966d8c2a098e0936e
Issue-ID: PORTAL-523
Signed-off-by: burdziak <olaf.burdziakowski@nokia.com>
ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/conf/MusicSession.java
ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/conf/MusicSessionRepositoryHandler.java
ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/service/MusicService.java
ecomp-sdk/epsdk-music/src/test/java/org/onap/portalapp/music/conf/MusicSessionTest.java

index 1f68bda..322e1b4 100644 (file)
@@ -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);
                }
                
        }
index 0e8551b..167ed95 100644 (file)
@@ -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);
                        }
                }
        }
index 3861b2d..c8d1588 100644 (file)
@@ -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");
                        }
-                       
-               }       
+
+               }
        }
 
 }
index 65290d4..fab1ba7 100644 (file)
@@ -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");