Fix sonar violation 38/75738/4
authorZlatko Murgoski <zlatko.murgoski@nokia.com>
Mon, 14 Jan 2019 10:24:22 +0000 (11:24 +0100)
committerZlatko Murgoski <zlatko.murgoski@nokia.com>
Tue, 29 Jan 2019 08:50:05 +0000 (09:50 +0100)
Fix sonar violation

Issue-ID: DCAEGEN2-1016
Signed-off-by: Zlatko Murgoski <zlatko.murgoski@nokia.com>
Change-Id: I0db722972aeeb57ebb4b61b11f1e302f613890d5

src/main/java/org/onap/dcae/ApplicationException.java [new file with mode: 0644]
src/main/java/org/onap/dcae/ApplicationSettings.java
src/main/java/org/onap/dcae/VesApplication.java
src/main/java/org/onap/dcae/common/AnyNode.java
src/main/java/org/onap/dcae/common/publishing/DMaaPEventPublisher.java
src/main/java/org/onap/dcae/controller/ConfigFilesFacade.java
src/main/java/org/onap/dcae/controller/ConfigLoader.java
src/main/java/org/onap/dcae/controller/ConfigSource.java
src/main/java/org/onap/dcae/restapi/ServletConfig.java
src/main/java/org/onap/dcae/restapi/VesRestController.java

diff --git a/src/main/java/org/onap/dcae/ApplicationException.java b/src/main/java/org/onap/dcae/ApplicationException.java
new file mode 100644 (file)
index 0000000..2079d86
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * ============LICENSE_START=======================================================
+ * PROJECT
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018 Nokia. All rights reserved.s
+ * ================================================================================
+ * 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.dcae;
+
+import java.io.IOException;
+import org.apache.commons.configuration.ConfigurationException;
+
+public class ApplicationException extends RuntimeException {
+
+  public ApplicationException(ConfigurationException ex) {
+    super(ex);
+  }
+
+  public ApplicationException(String message, Exception ex) {
+    super(message,ex);
+  }
+
+  public ApplicationException(IOException ex) {
+    super(ex);
+  }
+}
index f140def..7d52c5e 100644 (file)
 
 package org.onap.dcae;
 
+import static io.vavr.API.Tuple;
+import static java.lang.String.format;
+import static java.nio.file.Files.readAllBytes;
+
 import com.fasterxml.jackson.databind.JsonNode;
 import com.github.fge.jackson.JsonLoader;
 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
@@ -32,23 +36,16 @@ import io.vavr.Tuple2;
 import io.vavr.collection.HashMap;
 import io.vavr.collection.List;
 import io.vavr.collection.Map;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import javax.annotation.Nullable;
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.PropertiesConfiguration;
 import org.json.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.annotation.Nullable;
-import java.io.IOException;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.util.Base64;
-
-import static io.vavr.API.Tuple;
-import static java.lang.String.format;
-import static java.nio.file.Files.readAllBytes;
-import static java.util.Arrays.stream;
-
 /**
  * Abstraction over application configuration.
  * Its job is to provide easily discoverable (by method names lookup) and type safe access to configuration properties.
@@ -72,7 +69,7 @@ public class ApplicationSettings {
         Map<String, String> parsedArgs = argsParser.apply(args);
         configurationFileLocation = findOutConfigurationFileLocation(parsedArgs);
         loadPropertiesFromFile();
-        parsedArgs.filterKeys(k -> !k.equals("c")).forEach(this::updateProperty);
+        parsedArgs.filterKeys(k -> !"c".equals(k)).forEach(this::updateProperty);
         loadedJsonSchemas = loadJsonSchemas();
     }
 
@@ -81,7 +78,7 @@ public class ApplicationSettings {
             properties.load(configurationFileLocation);
         } catch (ConfigurationException ex) {
             log.error("Cannot load properties cause:", ex);
-            throw new RuntimeException(ex);
+            throw new ApplicationException(ex);
         }
     }
 
@@ -124,7 +121,7 @@ public class ApplicationSettings {
 
     private Map<String, JsonSchema> loadJsonSchemas() {
         return jsonSchema().toMap().entrySet().stream()
-                .map(versionToFilePath -> readSchemaForVersion(versionToFilePath))
+                .map(this::readSchemaForVersion)
                 .collect(HashMap.collector());
     }
 
@@ -136,7 +133,7 @@ public class ApplicationSettings {
             JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema(schemaNode);
             return Tuple(versionToFilePath.getKey(), schema);
         } catch (IOException | ProcessingException e) {
-            throw new RuntimeException("Could not read schema from path: " + versionToFilePath.getValue(), e);
+            throw new ApplicationException("Could not read schema from path: " + versionToFilePath.getValue(), e);
         }
     }
 
index 48ac6b8..2dcd8fa 100644 (file)
@@ -86,7 +86,7 @@ public class VesApplication {
                         Paths.get(properties.dMaaPConfigurationFileLocation()),
                         properties.configurationFileLocation());
         scheduledThreadPoolExecutor
-                .scheduleAtFixedRate(() -> configLoader.updateConfig(),
+                .scheduleAtFixedRate(configLoader::updateConfig,
                         properties.configurationUpdateFrequency(),
                         properties.configurationUpdateFrequency(),
                         TimeUnit.MINUTES);
index a68e629..d55cb1e 100644 (file)
@@ -82,7 +82,7 @@ public class AnyNode {
     public Option<AnyNode> getAsOption(String key) {
         try {
             AnyNode value = get(key);
-            if (value.toString().equals("null")) {
+            if ("null".equals(value.toString())) {
                 return Option.none();
             }
             return Option.some(value);
index aa3dc7a..b00b274 100644 (file)
@@ -46,9 +46,9 @@ class DMaaPEventPublisher implements EventPublisher {
     private final DMaaPPublishersCache publishersCache;
     private final Logger outputLogger;
 
-    DMaaPEventPublisher(DMaaPPublishersCache DMaaPPublishersCache,
+    DMaaPEventPublisher(DMaaPPublishersCache publishersCache,
                         Logger outputLogger) {
-        this.publishersCache = DMaaPPublishersCache;
+        this.publishersCache = publishersCache;
         this.outputLogger = outputLogger;
     }
 
index c83db2d..0b2c197 100644 (file)
@@ -53,7 +53,7 @@ class ConfigFilesFacade {
 
     Try<Map<String, String>> readCollectorProperties() {
         log.info(f("Reading collector properties from path: '%s'", propertiesPath));
-        return Try(() -> readProperties())
+        return Try(this::readProperties)
             .map(prop -> toList(prop.getKeys()).toMap(k -> k, k -> (String) prop.getProperty(k)))
             .mapFailure(enhanceError("Unable to read properties configuration from path '%s'", propertiesPath))
             .onFailure(logError(log))
index c1ac65d..e11c2b8 100644 (file)
@@ -68,7 +68,7 @@ public class ConfigLoader {
         log.info("Trying to dynamically update config from Config Binding Service");
         readEnvProps(envVariablesSupplier.get())
             .onEmpty(() -> log.warn(SKIP_MSG))
-            .forEach(props -> updateConfig(props));
+            .forEach(this::updateConfig);
     }
 
     private void updateConfig(EnvProps props) {
index 68dc250..a9e439e 100644 (file)
@@ -26,6 +26,7 @@ import static org.onap.dcae.common.publishing.VavrUtils.f;
 import static org.onap.dcae.controller.Conversions.toJson;
 import static org.onap.dcae.controller.Conversions.toJsonArray;
 
+import com.mashape.unirest.http.HttpResponse;
 import com.mashape.unirest.http.Unirest;
 import io.vavr.control.Try;
 import org.json.JSONArray;
@@ -41,11 +42,11 @@ final class ConfigSource {
         log.info("Fetching app configuration from CBS");
         return callConsulForCBSConfiguration(envProps)
             .peek(strBody -> log.info(f("Received following CBS configuration from Consul '%s'", strBody)))
-            .flatMap(strBody -> toJsonArray(strBody))
-            .flatMap(json -> withdrawCatalog(json))
+            .flatMap(Conversions::toJsonArray)
+            .flatMap(ConfigSource::withdrawCatalog)
             .flatMap(json -> constructFullCBSUrl(envProps, json))
             .flatMap(cbsUrl -> callCBSForAppConfig(envProps, cbsUrl))
-            .flatMap(strBody -> toJson(strBody))
+            .flatMap(Conversions::toJson)
             .peek(jsonNode -> log.info(f("Received app configuration: '%s'", jsonNode)))
             .onFailure(exc -> log.error("Could not fetch application config", exc));
     }
@@ -83,7 +84,7 @@ final class ConfigSource {
                 res -> res.getStatus() == 200,
                 res -> new RuntimeException(f("HTTP call (GET '%s') failed with status %s and body '%s'",
                     url, res.getStatus(), res.getBody())))
-            .map(res -> res.getBody())
+            .map(HttpResponse::getBody)
             .peek(body -> log.info(f("HTTP GET on '%s' returned body '%s'", url, body)));
     }
 
index 2ba8a28..35616ac 100644 (file)
@@ -21,6 +21,7 @@
 
 package org.onap.dcae.restapi;
 
+import org.onap.dcae.ApplicationException;
 import org.onap.dcae.ApplicationSettings;
 import org.onap.dcae.common.SSLContextCreator;
 import org.slf4j.Logger;
@@ -100,7 +101,7 @@ public class ServletConfig implements WebServerFactoryCustomizer<ConfigurableSer
             return new String(readAllBytes(location));
         } catch (IOException e) {
             log.error("Could not read keystore password from: '" + location + "'.", e);
-            throw new RuntimeException(e);
+            throw new ApplicationException(e);
         }
     }
 }
\ No newline at end of file
index 68aecce..510031d 100644 (file)
@@ -37,6 +37,7 @@ import javax.servlet.http.HttpServletRequest;
 
 import org.json.JSONArray;
 import org.json.JSONObject;
+import org.onap.dcae.ApplicationException;
 import org.onap.dcae.ApplicationSettings;
 import org.onap.dcae.common.VESLogger;
 import org.slf4j.Logger;
@@ -165,7 +166,7 @@ public class VesRestController {
             }
             return report.isSuccess();
         } catch (Exception e) {
-            throw new RuntimeException("Unable to validate against schema", e);
+            throw new ApplicationException("Unable to validate against schema", e);
         }
     }