Fix critical and major sonar violations 17/57817/1
authorBogumil Zebek <bogumil.zebek@nokia.com>
Fri, 27 Jul 2018 10:45:04 +0000 (12:45 +0200)
committerbogumil_zebek <bogumil.zebek@nokia.com>
Fri, 27 Jul 2018 10:45:04 +0000 (12:45 +0200)
Change-Id: Iada703c9606fc4958430bc2ab967528333d419a0
Issue-ID: AAI-342
Signed-off-by: bogumil_zebek <bogumil.zebek@nokia.com>
sparkybe-onap-service/src/main/java/org/onap/aai/sparky/aggregation/sync/HistoricalEntitySyncController.java
sparkybe-onap-service/src/main/java/org/onap/aai/sparky/analytics/HistogramSampler.java
sparkybe-onap-service/src/main/java/org/onap/aai/sparky/analytics/HistoricalCounter.java
sparkybe-onap-service/src/main/java/org/onap/aai/sparky/dal/ActiveInventoryAdapter.java
sparkybe-onap-service/src/main/java/org/onap/aai/sparky/sync/SyncControllerImpl.java
sparkybe-onap-service/src/main/java/org/onap/aai/sparky/sync/SynchronizerConstants.java
sparkybe-onap-service/src/main/java/org/onap/aai/sparky/sync/TransactionRateMonitor.java
sparkybe-onap-service/src/main/java/org/onap/aai/sparky/sync/config/SyncControllerConfig.java
sparkybe-onap-service/src/main/java/org/onap/aai/sparky/util/NodeUtils.java
sparkybe-onap-service/src/main/java/org/onap/aai/sparky/viewandinspect/services/BaseVisualizationService.java
sparkybe-onap-service/src/test/java/org/onap/aai/sparky/aggregation/sync/HistoricalEntitySyncControllerTest.java [new file with mode: 0644]

index 03534e0..da1f290 100644 (file)
@@ -33,6 +33,8 @@ import org.onap.aai.sparky.sync.config.ElasticSearchSchemaConfig;
 import org.onap.aai.sparky.sync.config.NetworkStatisticsConfig;
 import org.onap.aai.sparky.sync.config.SyncControllerConfig;
 
+import java.util.concurrent.TimeUnit;
+
 public class HistoricalEntitySyncController extends SyncControllerImpl
     implements SyncControllerRegistrar {
 
@@ -48,7 +50,7 @@ public class HistoricalEntitySyncController extends SyncControllerImpl
 
     // final String controllerName = "Historical Entity Count Synchronizer";
 
-    long taskFrequencyInMs = syncFrequencyInMinutes * 60 * 1000;
+    long taskFrequencyInMs = getTaskFrequencyInMs(syncFrequencyInMinutes);
 
     setDelayInMs(taskFrequencyInMs);
     setSyncFrequencyInMs(taskFrequencyInMs);
@@ -70,6 +72,10 @@ public class HistoricalEntitySyncController extends SyncControllerImpl
 
   }
 
+  static long getTaskFrequencyInMs(int syncFrequencyInMinutes) {
+    return TimeUnit.MINUTES.toMillis(Integer.toUnsignedLong(syncFrequencyInMinutes));
+  }
+
   public SyncControllerRegistry getSyncControllerRegistry() {
     return syncControllerRegistry;
   }
index c1fa9f1..1350d28 100644 (file)
@@ -173,7 +173,7 @@ public final class HistogramSampler {
     this.minValue = -1;
     this.maxValue = 0;
     initializeHistogramBins(numBins);
-    this.stepSize = (maxValue / numBins);
+    this.stepSize = ((double) maxValue / (double) numBins);
     clear();
   }
 
index 68305f8..b53c1e8 100644 (file)
@@ -75,7 +75,7 @@ public class HistoricalCounter {
 
     } else {
 
-      if (min == -1) {
+      if (Double.valueOf(min).equals(-1.0)) {
         min = value;
       }
 
index 33d2ff4..e168f43 100644 (file)
@@ -143,9 +143,11 @@ public class ActiveInventoryAdapter {
 
   protected String getResourceBasePath() {
 
-    String versionStr = null;
+    String versionStr;
     if (oxmModelLoader != null) {
       versionStr = String.valueOf(oxmModelLoader.getLatestVersionNum());
+    } else {
+      throw new RuntimeException("Unable to resolve aai version.");
     }
 
     return "/aai/" + versionStr.toLowerCase();
index 8b008de..7c81074 100644 (file)
@@ -22,6 +22,7 @@ package org.onap.aai.sparky.sync;
 
 import static java.util.concurrent.CompletableFuture.supplyAsync;
 
+import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Collection;
 import java.util.Date;
@@ -58,6 +59,8 @@ public class SyncControllerImpl implements SyncController {
     TEST_INDEX_INTEGRITY, GENERATE_FINAL_REPORT
   }
 
+  private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
+
   /**
    * The Enum SyncActions.
    */
@@ -93,7 +96,6 @@ public class SyncControllerImpl implements SyncController {
   /**
    * Instantiates a new sync controller.
    *
-   * @param name the name
    * @throws Exception the exception
    */
   public SyncControllerImpl(SyncControllerConfig syncControllerConfig) throws Exception {
@@ -251,7 +253,7 @@ public class SyncControllerImpl implements SyncController {
 
                 LOG.info(AaiUiMsgs.INFO_GENERIC,
                     getControllerName() + " started synchronization at "
-                        + SynchronizerConstants.SIMPLE_DATE_FORMAT.format(opStartTime).replaceAll(
+                        + this.simpleDateFormat.format(opStartTime).replaceAll(
                             SynchronizerConstants.TIME_STD, SynchronizerConstants.TIME_CONFIG_STD));
 
                 runCount.incrementAndGet();
index 24279c6..ccc9934 100644 (file)
@@ -20,8 +20,8 @@
  */
 package org.onap.aai.sparky.sync;
 
-import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.concurrent.TimeUnit;
 
 /**
  * The Class SynchronizerConstants.
@@ -30,8 +30,6 @@ public final class SynchronizerConstants {
   // Error values for invalid user input
   public static final int DEFAULT_CONFIG_ERROR_INT_VALUE = Integer.MAX_VALUE;
   public static final Date DEFAULT_CONFIG_ERROR_DATE_VALUE = new Date(Long.MAX_VALUE);
-  public static final SimpleDateFormat SIMPLE_DATE_FORMAT =
-      new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
 
   public static final String DEPTH_MODIFIER = "?depth=0";
   public static final String DEPTH_ALL_MODIFIER = "?depth=all";
@@ -48,7 +46,7 @@ public final class SynchronizerConstants {
   public static final int IDX_TIME_IN_TIMESTAMP = 0;
   public static final int IDX_TIMEZONE_IN_TIMESTAMP = 1;
   public static final long MILLISEC_IN_A_MIN = 60000;
-  public static final long MILLISEC_IN_A_DAY = 24 * 60 * 60 * 1000;
+  public static final long MILLISEC_IN_A_DAY = TimeUnit.HOURS.toMillis(24);
   public static final String TIME_STD = "GMT";
   public static final String TIME_CONFIG_STD = "UTC";
   public static final String TIMESTAMP24HOURS_PATTERN =
index 07d5e84..464875c 100644 (file)
@@ -60,7 +60,7 @@ public class TransactionRateMonitor {
 
   public double getCurrentTps() {
     if (numTransactions.get() > 0) {
-      double timeDelta = System.currentTimeMillis() - startTimeInMs;
+      long timeDelta = System.currentTimeMillis() - startTimeInMs;
       double numTxns = numTransactions.get();
       return (numTxns / timeDelta) * 1000.0;
     }
index f71c660..555f8cc 100644 (file)
@@ -36,6 +36,7 @@ import org.onap.aai.sparky.sync.SynchronizerConstants;
 public class SyncControllerConfig {
 
   private static final String UNKNOWN_CONTROLLER_NAME = "UnknownControllerName";
+  private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
 
   private String controllerName;
   private boolean enabled;
@@ -242,12 +243,12 @@ public class SyncControllerConfig {
       Calendar targetSyncTime = Calendar.getInstance(tz);
       targetSyncTime.add(Calendar.SECOND, taskFrequencyInSeconds);
 
-      return SynchronizerConstants.SIMPLE_DATE_FORMAT.format(targetSyncTime.getTimeInMillis())
+      return this.simpleDateFormat.format(targetSyncTime.getTimeInMillis())
           .replaceAll(SynchronizerConstants.TIME_STD, SynchronizerConstants.TIME_CONFIG_STD);
 
     } else {
 
-      return SynchronizerConstants.SIMPLE_DATE_FORMAT
+      return this.simpleDateFormat
           .format(getNextSyncTime(getTargetSyncTime(), taskFrequencyInSeconds))
           .replaceAll(SynchronizerConstants.TIME_STD, SynchronizerConstants.TIME_CONFIG_STD);
 
index 7a9d0e0..b75fa91 100644 (file)
@@ -880,11 +880,7 @@ public class NodeUtils {
       throw ex;
     } finally {
       if (bufferedReader != null) {
-        try {
           bufferedReader.close();
-        } catch (IOException ex) {
-          throw ex;
-        }
       }
     }
 
index dffcbdf..33819e2 100644 (file)
@@ -193,7 +193,7 @@ public class BaseVisualizationService implements VisualizationService {
   public OperationResult buildVisualizationUsingGenericQuery(QueryRequest queryRequest) {
 
     OperationResult returnValue = new OperationResult();
-    OperationResult dataCollectionResult = null;
+    OperationResult dataCollectionResult = new OperationResult();
     QueryParams queryParams = null;
     SearchableEntity sourceEntity = null;
 
diff --git a/sparkybe-onap-service/src/test/java/org/onap/aai/sparky/aggregation/sync/HistoricalEntitySyncControllerTest.java b/sparkybe-onap-service/src/test/java/org/onap/aai/sparky/aggregation/sync/HistoricalEntitySyncControllerTest.java
new file mode 100644 (file)
index 0000000..9527520
--- /dev/null
@@ -0,0 +1,16 @@
+package org.onap.aai.sparky.aggregation.sync;
+
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class HistoricalEntitySyncControllerTest {
+
+    @Test
+    public void shouldConvertMinutesToMs() {
+        assertEquals(60000, HistoricalEntitySyncController.getTaskFrequencyInMs(1));
+        assertEquals(120000, HistoricalEntitySyncController.getTaskFrequencyInMs(2));
+    }
+
+}
\ No newline at end of file