Fix sonar issues in PM Mapper 64/123264/2
authorStanislav Marszalek <s.marszalek2@partner.samsung.com>
Fri, 13 Aug 2021 10:24:19 +0000 (12:24 +0200)
committerStanislav Marszalek <s.marszalek2@partner.samsung.com>
Wed, 25 Aug 2021 05:43:33 +0000 (07:43 +0200)
Issue-ID: DCAEGEN2-2887

Signed-off-by: Stanislav Marszalek <s.marszalek2@partner.samsung.com>
Change-Id: I02d3684c98d563d7f386de2fdf032e930ac18b9f

src/main/java/org/onap/dcaegen2/services/pmmapper/App.java
src/main/java/org/onap/dcaegen2/services/pmmapper/config/FilesProcessingConfig.java
src/main/java/org/onap/dcaegen2/services/pmmapper/model/ServerResource.java
src/main/java/org/onap/dcaegen2/services/pmmapper/ssl/SSLContextFactory.java
src/main/java/org/onap/dcaegen2/services/pmmapper/utils/MeasFilterConfigAdapter.java
src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DeliveryHandlerTest.java
src/test/java/org/onap/dcaegen2/services/pmmapper/mapping/MapperTest.java
src/test/java/org/onap/dcaegen2/services/pmmapper/utils/MeasFilterConfigAdapterTest.java
src/test/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSenderTests.java

index b020837..9cfddf0 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019-2020 Nordix Foundation.
  *  Copyright (C) 2021 Nokia.
+ *  Copyright (C) 2021 Samsung Electronics.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -211,7 +212,7 @@ public class App {
     private boolean isCached(String id) {
         boolean isPresent = eventsCache.contains(id);
         if(isPresent) {
-            logger.unwrap().info("Skipping. This event is already waiting in cache to be processed: " + id);
+            logger.unwrap().info("Skipping. This event is already waiting in cache to be processed: {}", id);
         }
         return isPresent;
     }
index c79f059..e4fd0b2 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2021 Nokia.
+ *  Copyright (C) 2021 Samsung Electronics.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -35,6 +36,7 @@ public class FilesProcessingConfig {
     private static final int DEFAULT_LIMIT_RATE = 1;
     private static final String ENV_THREADS_MULTIPLIER = "THREADS_MULTIPLIER";
     private static final String ENV_PROCESSING_THREADS_COUNT = "PROCESSING_THREADS_COUNT";
+    private static final String ENV_VARIABLE_INCORRECT_MSG = " environment variable has incorrect value.\n";
     private static final int DEFAULT_MULTIPLIER = 1;
 
     private static final ONAPLogAdapter logger = new ONAPLogAdapter(
@@ -61,7 +63,7 @@ public class FilesProcessingConfig {
                 .orElseGet(this::getDefaultLimitRate);
         } catch (NumberFormatException exception) {
             throw new EnvironmentConfigException(
-                ENV_LIMIT_RATE + " environment variable has incorrect value.\n"
+                    ENV_LIMIT_RATE + ENV_VARIABLE_INCORRECT_MSG
                     , exception);
         }
     }
@@ -88,7 +90,7 @@ public class FilesProcessingConfig {
 
     private Integer parseIntegerValue(String val) throws NumberFormatException {
         Integer value = Integer.valueOf(val);
-        logger.unwrap().info(ENV_LIMIT_RATE + " value is: " + value);
+        logger.unwrap().info(ENV_LIMIT_RATE + " value is: {}", value);
         return value;
     }
 
@@ -104,7 +106,7 @@ public class FilesProcessingConfig {
                 .orElseGet(this::getDefaultMultiplier);
         } catch (NumberFormatException exception) {
             throw new EnvironmentConfigException(
-                ENV_THREADS_MULTIPLIER + " environment variable has incorrect value.\n", exception);
+                    ENV_THREADS_MULTIPLIER + ENV_VARIABLE_INCORRECT_MSG, exception);
         }
     }
 
@@ -121,14 +123,14 @@ public class FilesProcessingConfig {
                 .orElseGet(this::getDefaultThreadsCount);
         } catch (NumberFormatException exception) {
             throw new EnvironmentConfigException(
-                ENV_PROCESSING_THREADS_COUNT + " environment variable has incorrect value.\n", exception);
+                    ENV_PROCESSING_THREADS_COUNT + ENV_VARIABLE_INCORRECT_MSG, exception);
         }
     }
 
     private int getDefaultThreadsCount() {
         int defaultThreadsCount = Runtime.getRuntime().availableProcessors();
         logger.unwrap().info(ENV_PROCESSING_THREADS_COUNT +
-                " env not present. Setting threads count to available cores: " + defaultThreadsCount);
+                " env not present. Setting threads count to available cores: {}", defaultThreadsCount);
         return defaultThreadsCount;
     }
 }
index 5c15280..ec71bdc 100644 (file)
@@ -2,6 +2,7 @@
  * -
  *  * ============LICENSE_START=======================================================
  *  *  Copyright (C) 2019 Nordix Foundation.
+ *  *  Copyright (C) 2021 Samsung Electronics.
  *  * ================================================================================
  *  * Licensed under the Apache License, Version 2.0 (the "License");
  *  * you may not use this file except in compliance with the License.
@@ -32,19 +33,21 @@ public abstract class ServerResource implements HttpHandler {
 
     /**
      * Creates a new server resource with a custom method and endpoint.
+     *
      * @param httpMethod Method that the resource should be accessible by.
      * @param endpointTemplate Endpoint that the resource should be accessible by.
      */
-    public ServerResource(String httpMethod, String endpointTemplate) {
+    protected ServerResource(String httpMethod, String endpointTemplate) {
         this.httpMethod = httpMethod;
         this.endpointTemplate = endpointTemplate;
     }
 
     /**
      * Creates a new server resource with a custom endpoint and method 'get'.
+     *
      * @param endpointTemplate Endpoint that the resource should be accessible by.
      */
-    public ServerResource(String endpointTemplate) {
+    protected ServerResource(String endpointTemplate) {
         this.httpMethod = GET_STRING;
         this.endpointTemplate = endpointTemplate;
     }
index 2b81c0a..e7c317d 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019-2020 Nordix Foundation.
+ *  Copyright (C) 2021 Samsung Electronics.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -42,7 +43,6 @@ import java.security.KeyStoreException;
 import java.security.NoSuchAlgorithmException;
 import java.security.UnrecoverableKeyException;
 import java.security.cert.CertificateException;
-import java.util.Base64;
 
 import static java.nio.file.Files.readAllBytes;
 
index 1826707..a7dfab1 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2021 Samsung Electronics.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -42,7 +43,7 @@ public class MeasFilterConfigAdapter extends TypeAdapter<MeasFilterConfig> {
 
     @Override
     public MeasFilterConfig read(JsonReader jsonReader) throws IOException {
-        JsonElement rootElement = new JsonParser().parse(jsonReader);
+        JsonElement rootElement = JsonParser.parseReader(jsonReader);
         if (rootElement.isJsonObject()) {
             logger.unwrap().debug("Reading filter as an object.");
             return new Gson().fromJson(rootElement, MeasFilterConfig.class);
index 9b7bc4d..180da4d 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2021 Samsung Electronics.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -125,10 +126,10 @@ class DeliveryHandlerTest {
         objUnderTest.handleRequest(httpServerExchange);
         verify(eventReceiver, times(1)).receive(any(Event.class));
 
-        assertEquals(logAppender.list.get(0).getMarker().getName(), "ENTRY");
+        assertEquals("ENTRY", logAppender.list.get(0).getMarker().getName());
         assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("InvocationID"));
         assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("RequestID"));
-        assertEquals(logAppender.list.get(1).getMarker().getName(), "EXIT");
+        assertEquals("EXIT", logAppender.list.get(1).getMarker().getName());
         logAppender.stop();
     }
 }
\ No newline at end of file
index 26cb648..9c0d67c 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019-2020 Nordix Foundation.
  *  Copyright (C) 2021 Nokia.
+ *  Copyright (C) 2021 Samsung Electronics.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -30,6 +31,7 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import com.google.gson.Gson;
+
 import freemarker.core.Environment;
 import freemarker.template.Template;
 import freemarker.template.TemplateException;
@@ -59,6 +61,7 @@ import org.onap.dcaegen2.services.pmmapper.model.Event;
 import org.onap.dcaegen2.services.pmmapper.model.EventMetadata;
 import org.onap.dcaegen2.services.pmmapper.utils.MeasConverter;
 import org.powermock.reflect.Whitebox;
+
 import utils.ArgumentCreator;
 import utils.EventUtils;
 
@@ -74,8 +77,8 @@ class MapperTest {
 
     private static final Path schema = Paths.get("src/test/resources/mapper_test/CommonEventFormat_30.1-ONAP.json");
     private static final Path METADATA_DIRECTORY = Paths.get("src/test/resources/metadata/");
-    private static final Path FIFTH_GENERATION_METADATA = Paths.get(METADATA_DIRECTORY.toString()+"/valid_5g_metadata.json");
-    private static final Path FOURTH_GENERATION_METADATA = Paths.get(METADATA_DIRECTORY.toString()+"/valid_4g_metadata.json");
+    private static final Path FIFTH_GENERATION_METADATA = Paths.get(METADATA_DIRECTORY.toString() + "/valid_5g_metadata.json");
+    private static final Path FOURTH_GENERATION_METADATA = Paths.get(METADATA_DIRECTORY.toString() + "/valid_4g_metadata.json");
     private static final Path TEMPLATES_DIRECTORY = Paths.get("src/main/resources/templates/");
     private static final Path dataDirectory = Paths.get("src/test/resources/mapper_test/mapping_data/");
 
@@ -88,7 +91,7 @@ class MapperTest {
         String fourthGenMetadataFileContents = new String(Files.readAllBytes(FOURTH_GENERATION_METADATA));
         fourthGenerationMetadata = new Gson().fromJson(fourthGenMetadataFileContents, EventMetadata.class);
         String fifthGenMetadataFileContents = new String(Files.readAllBytes(FIFTH_GENERATION_METADATA));
-        fifthGenerationMetadata =  new Gson().fromJson(fifthGenMetadataFileContents, EventMetadata.class);
+        fifthGenerationMetadata = new Gson().fromJson(fifthGenMetadataFileContents, EventMetadata.class);
         converter = new MeasConverter();
     }
 
@@ -131,25 +134,27 @@ class MapperTest {
 
     @Test
     void testFailureToParseLte() {
-        assertThrows(MappingException.class, () ->
-                objUnderTest.map(EventUtils.makeMockEvent("not xml", fourthGenerationMetadata)));
+        assertThrows(MappingException.class, () -> EventUtils.makeMockEvent("not xml", fourthGenerationMetadata));
     }
 
     @Test
     void testFailureToParseNr() {
-        assertThrows(MappingException.class, () ->
-                 objUnderTest.map(EventUtils.makeMockEvent("not xml", fifthGenerationMetadata)));
+        assertThrows(MappingException.class, () -> EventUtils.makeMockEvent("not xml", fifthGenerationMetadata));
     }
 
     @Test
     void testInvalidPath() {
-        assertThrows(IllegalArgumentException.class, () -> new Mapper(Paths.get("not a path"),converter));
+        Path path = Paths.get("not a path");
+        assertThrows(IllegalArgumentException.class, () -> new Mapper(path, converter));
     }
 
     @Test
     void testInvalidTemplateDirectory() {
-        assertThrows(IllegalArgumentException.class, () -> new Mapper(Paths.get("fake dir"), new MeasConverter()));
+        Path path = Paths.get("fake dir");
+        MeasConverter measConverter = new MeasConverter();
+        assertThrows(IllegalArgumentException.class, () -> new Mapper(path, measConverter));
     }
+
     @Test
     void testTemplateNotFound() {
         EventMetadata testMetadata = mock(EventMetadata.class);
@@ -161,7 +166,7 @@ class MapperTest {
 
     @Test
     void testNullPath() {
-        assertThrows(NullPointerException.class, () -> new Mapper(null,converter));
+        assertThrows(NullPointerException.class, () -> new Mapper(null, converter));
     }
 
     @Test
@@ -176,8 +181,8 @@ class MapperTest {
 
     static List<Arguments> getValidEvents() {
         ArgumentCreator creator = (Path path, EventMetadata metadata) -> {
-            Path testEventPath = Paths.get(path.toString()+"/test.xml");
-            Path metadataPath = Paths.get(path.toString()+"/metadata.json");
+            Path testEventPath = Paths.get(path.toString() + "/test.xml");
+            Path metadataPath = Paths.get(path.toString() + "/metadata.json");
             EventMetadata eventMetadata = null;
             try {
                 eventMetadata = new Gson().fromJson(new String(Files.readAllBytes(metadataPath)), EventMetadata.class);
index a2d53de..0fda499 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2021 Samsung Electronics.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -63,20 +64,23 @@ class MeasFilterConfigAdapterTest {
     @Test
     void testInvalidConfigObject() throws IOException {
         String filter = "{\"filters\": \"invalid\"}";
-        assertThrows(JsonSyntaxException.class, () -> objUnderTest.read(new JsonReader(new StringReader(filter))));
+        JsonReader jsonReader = new JsonReader(new StringReader(filter));
+        assertThrows(JsonSyntaxException.class, () -> objUnderTest.read(jsonReader));
     }
 
     @Test
     void testInvalidConfigString() throws IOException {
-        String filter ="\"{\\\"filters\\\": [{\"pmDefVsn\":\\\"V9\\\", \\\"nfType\\\": \\\"NrRadio\\\"," +
+        String filter = "\"{\\\"filters\\\": [{\"pmDefVsn\":\\\"V9\\\", \\\"nfType\\\": \\\"NrRadio\\\"," +
                 "\\\"vendor\\\": \\\"Ericsson\\\", \\\"measTypes\\\": [\\\"A\\\", \\\"B\\\"]}]}\"";
-        assertThrows(JsonSyntaxException.class, () -> objUnderTest.read(new JsonReader(new StringReader(filter))));
+        JsonReader jsonReader = new JsonReader(new StringReader(filter));
+        assertThrows(JsonSyntaxException.class, () -> objUnderTest.read(jsonReader));
     }
 
     @Test
     void testUnsupportedJSONType() throws IOException {
         String filter = "[]";
-        assertThrows(UnsupportedOperationException.class, () -> objUnderTest.read(new JsonReader(new StringReader(filter))));
+        JsonReader jsonReader = new JsonReader(new StringReader(filter));
+        assertThrows(UnsupportedOperationException.class, () -> objUnderTest.read(jsonReader));
     }
 
     @Test
index 8541824..091794e 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019-2020 Nordix Foundation.
  *  Copyright (C) 2021 Nokia.
+ *  Copyright (C) 2021 Samsung Electronics.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -86,7 +87,7 @@ public class RequestSenderTests {
         String result = new RequestSender().send(url);
 
         client.verify(req, VerificationTimes.atLeast(1));
-        assertEquals(result, "ResponseBody");
+        assertEquals("ResponseBody", result);
         assertTrue(logAppender.list.get(1).getMessage().contains("Sending"));
         assertTrue(logAppender.list.get(2).getMessage().contains("Received"));
         logAppender.stop();