Added maven-checkstyle-plugin to parent POM for code style checks 51/139251/2
authorsourabh_sourabh <sourabh.sourabh@est.tech>
Tue, 22 Oct 2024 13:00:29 +0000 (14:00 +0100)
committersourabh_sourabh <sourabh.sourabh@est.tech>
Wed, 23 Oct 2024 10:29:07 +0000 (11:29 +0100)
- Integrate Checkstyle to enforce coding standards across modules.
- Ensure Checkstyle applies to both dmi-service and dmi-stub modules.
- Fixed all code check style issues.

Issue-ID: CPS-2461
Change-Id: Ie84928c1f2e3de549cc8fab2001974369884a831
Signed-off-by: sourabh_sourabh <sourabh.sourabh@est.tech>
dmi-service/pom.xml
dmi-stub/dmi-plugin-demo-and-csit-stub-service/pom.xml
dmi-stub/dmi-plugin-demo-and-csit-stub-service/src/main/java/org/onap/cps/ncmp/dmi/rest/stub/controller/DmiRestStubController.java
dmi-stub/dmi-plugin-demo-and-csit-stub-service/src/main/java/org/onap/cps/ncmp/dmi/rest/stub/controller/aop/ModuleInitialProcessAspect.java
pom.xml

index 7b2c54c..fccdfaa 100644 (file)
@@ -1,4 +1,21 @@
 <?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ============LICENSE_START=======================================================
+  Copyright (C) 2024 Nordix Foundation
+  ================================================================================
+  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.
+  SPDX-License-Identifier: Apache-2.0
+  ============LICENSE_END=========================================================
+-->
+
 <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -14,7 +31,6 @@
     <properties>
         <app>org.onap.cps.ncmp.dmi.Application</app>
         <base.image>${docker.pull.registry}/onap/integration-java17:12.0.0</base.image>
-        <cps.version>3.5.2</cps.version>
         <image.tag>${project.version}-${maven.build.timestamp}</image.tag>
         <jacoco.minimum.coverage>0.98</jacoco.minimum.coverage>
         <maven.build.timestamp.format>yyyyMMdd'T'HHmmss'Z'</maven.build.timestamp.format>
                     </environmentVariables>
                 </configuration>
             </plugin>
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-checkstyle-plugin</artifactId>
-                <executions>
-                    <execution>
-                        <id>onap-license</id>
-                        <goals>
-                            <goal>check</goal>
-                        </goals>
-                        <phase>process-sources</phase>
-                        <configuration>
-                            <configLocation>onap-checkstyle/check-license.xml</configLocation>
-                            <includeResources>false</includeResources>
-                            <includeTestSourceDirectory>true</includeTestSourceDirectory>
-                            <includeTestResources>false</includeTestResources>
-                            <sourceDirectories>
-                                <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
-                            </sourceDirectories>
-                            <consoleOutput>false</consoleOutput>
-                            <violationSeverity>warning</violationSeverity>
-                            <failOnViolation>true</failOnViolation>
-                        </configuration>
-                    </execution>
-                    <execution>
-                        <id>onap-java-style</id>
-                        <goals>
-                            <goal>check</goal>
-                        </goals>
-                        <phase>process-sources</phase>
-                        <configuration>
-                            <configLocation>onap-checkstyle/onap-java-style.xml</configLocation>
-                            <sourceDirectories>
-                                <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
-                            </sourceDirectories>
-                            <includeResources>true</includeResources>
-                            <includeTestSourceDirectory>true</includeTestSourceDirectory>
-                            <includeTestResources>true</includeTestResources>
-                            <consoleOutput>false</consoleOutput>
-                            <violationSeverity>warning</violationSeverity>
-                            <failOnViolation>true</failOnViolation>
-                        </configuration>
-                    </execution>
-                    <execution>
-                        <id>cps-java-style</id>
-                        <goals>
-                            <goal>check</goal>
-                        </goals>
-                        <phase>process-sources</phase>
-                        <configuration>
-                            <configLocation>cps-java-style.xml</configLocation>
-                            <sourceDirectories>
-                                <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
-                            </sourceDirectories>
-                            <includeResources>true</includeResources>
-                            <includeTestSourceDirectory>true</includeTestSourceDirectory>
-                            <includeTestResources>true</includeTestResources>
-                            <consoleOutput>true</consoleOutput>
-                            <violationSeverity>warning</violationSeverity>
-                            <failOnViolation>true</failOnViolation>
-                        </configuration>
-                    </execution>
-                </executions>
-                <dependencies>
-                    <dependency>
-                        <groupId>${project.groupId}</groupId>
-                        <artifactId>checkstyle</artifactId>
-                        <version>${cps.version}</version>
-                    </dependency>
-                </dependencies>
-            </plugin>
             <plugin>
                 <groupId>com.github.spotbugs</groupId>
                 <artifactId>spotbugs-maven-plugin</artifactId>
index e8d88dd..3bb8324 100644 (file)
@@ -1,3 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
 <!--
   ============LICENSE_START=======================================================
   Copyright (C) 2023-2024 Nordix Foundation
index 2ba56fa..85d454b 100644 (file)
@@ -294,9 +294,9 @@ public class DmiRestStubController {
     public ResponseEntity<Object> retrieveDataJobResult(
             @PathVariable("dataProducerId") final String dataProducerId,
             @PathVariable("dataProducerJobId") final String dataProducerJobId,
-            @RequestParam(name = "destination") String destination) {
-        log.debug("Received request to retrieve data job result. Data Producer ID: {}, " +
-                        "Data Producer Job ID: {}, Destination: {}",
+            @RequestParam(name = "destination") final String destination) {
+        log.debug("Received request to retrieve data job result. Data Producer ID: {}, "
+                        "Data Producer Job ID: {}, Destination: {}",
                 dataProducerId, dataProducerJobId, destination);
         return ResponseEntity.ok(Map.of("result", "some status"));
     }
@@ -350,24 +350,25 @@ public class DmiRestStubController {
         return dataOperationEvent;
     }
 
-    private ResponseEntity<String> processModuleRequest(Object moduleRequest, String responseFileName, long simulatedResponseDelay) {
-        String moduleSetTag = extractModuleSetTagFromRequest(moduleRequest);
+    private ResponseEntity<String> processModuleRequest(final Object moduleRequest, final String responseFileName,
+                                                        final long simulatedResponseDelay) {
+        final String moduleSetTag = extractModuleSetTagFromRequest(moduleRequest);
         logRequestBody(moduleRequest);
-        String moduleResponseContent = getModuleResponseContent(moduleSetTag, responseFileName);
+        final String moduleResponseContent = getModuleResponseContent(moduleSetTag, responseFileName);
         delay(simulatedResponseDelay);
         return ResponseEntity.ok(moduleResponseContent);
     }
 
-    private String extractModuleSetTagFromRequest(Object moduleReferencesRequest) {
-        JsonNode rootNode = objectMapper.valueToTree(moduleReferencesRequest);
+    private String extractModuleSetTagFromRequest(final Object moduleReferencesRequest) {
+        final JsonNode rootNode = objectMapper.valueToTree(moduleReferencesRequest);
         return rootNode.path("moduleSetTag").asText(null);
     }
 
-    private boolean isModuleSetTagNullOrEmpty(String moduleSetTag) {
+    private boolean isModuleSetTagNullOrEmpty(final String moduleSetTag) {
         return moduleSetTag == null || moduleSetTag.trim().isEmpty();
     }
 
-    private void logRequestBody(Object request) {
+    private void logRequestBody(final Object request) {
         try {
             log.info("Incoming DMI request body: {}", objectMapper.writeValueAsString(request));
         } catch (final JsonProcessingException jsonProcessingException) {
@@ -376,12 +377,12 @@ public class DmiRestStubController {
     }
 
     private String getModuleResponseContent(final String moduleSetTag, final String responseFileName) {
-        String moduleResponseFilePath = isModuleSetTagNullOrEmpty(moduleSetTag)
+        final String moduleResponseFilePath = isModuleSetTagNullOrEmpty(moduleSetTag)
                 ? String.format("module/ietfYang-%s", responseFileName)
                 : String.format("module/%s-%s", moduleSetTag, responseFileName);
         log.info("Using module responses from : {}", moduleResponseFilePath);
 
-        Resource moduleResponseResource = applicationContext.getResource(
+        final Resource moduleResponseResource = applicationContext.getResource(
                 ResourceLoader.CLASSPATH_URL_PREFIX + moduleResponseFilePath);
         return ResourceFileReaderUtil.getResourceFileContent(moduleResponseResource);
     }
index 32dfeb8..2cecc14 100644 (file)
@@ -22,6 +22,8 @@ package org.onap.cps.ncmp.dmi.rest.stub.controller.aop;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.aspectj.lang.ProceedingJoinPoint;
@@ -29,10 +31,9 @@ import org.aspectj.lang.annotation.Around;
 import org.aspectj.lang.annotation.Aspect;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.HttpStatus;
-import org.springframework.stereotype.Component;
 import org.springframework.http.ResponseEntity;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
+import org.springframework.stereotype.Component;
+
 
 /**
  * Aspect to handle initial processing for methods annotated with @ModuleInitialProcess.
@@ -57,47 +58,50 @@ public class ModuleInitialProcessAspect {
      * @return the result of the method execution or a ResponseEntity indicating that the service is unavailable
      */
     @Around("@annotation(moduleInitialProcess)")
-    public Object handleModuleInitialProcess(ProceedingJoinPoint proceedingJoinPoint, ModuleInitialProcess moduleInitialProcess) throws Throwable {
+    public Object handleModuleInitialProcess(final ProceedingJoinPoint proceedingJoinPoint,
+                                             final ModuleInitialProcess moduleInitialProcess) throws Throwable {
         log.debug("Aspect invoked for method: {}", proceedingJoinPoint.getSignature());
-        Object moduleRequest = proceedingJoinPoint.getArgs()[1];
-        String moduleSetTag = extractModuleSetTagFromRequest(moduleRequest);
+        final Object moduleRequest = proceedingJoinPoint.getArgs()[1];
+        final String moduleSetTag = extractModuleSetTagFromRequest(moduleRequest);
 
         if (isModuleSetTagEmptyOrInvalid(moduleSetTag)) {
             log.debug("Received request with an empty or null moduleSetTag. Returning default processing.");
             return proceedingJoinPoint.proceed();
         }
 
-        long firstRequestTimestamp = getFirstRequestTimestamp(moduleSetTag);
-        long currentTimestamp = System.currentTimeMillis();
+        final long firstRequestTimestamp = getFirstRequestTimestamp(moduleSetTag);
+        final long currentTimestamp = System.currentTimeMillis();
 
         if (isInitialProcessingCompleted(currentTimestamp, firstRequestTimestamp)) {
             log.debug("Initial processing for moduleSetTag '{}' is completed.", moduleSetTag);
             return proceedingJoinPoint.proceed();
         }
 
-        long remainingProcessingTime = calculateRemainingProcessingTime(currentTimestamp, firstRequestTimestamp);
-        log.info("Initial processing for moduleSetTag '{}' is still active. Returning HTTP 503. Remaining time: {} ms.", moduleSetTag, remainingProcessingTime);
+        final long remainingProcessingTime = calculateRemainingProcessingTime(currentTimestamp, firstRequestTimestamp);
+        log.info("Initial processing for moduleSetTag '{}' is still active. Returning HTTP 503. Remaining time: {} ms.",
+                moduleSetTag, remainingProcessingTime);
         return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build();
     }
 
-    private String extractModuleSetTagFromRequest(Object moduleRequest) {
-        JsonNode rootNode = objectMapper.valueToTree(moduleRequest);
+    private String extractModuleSetTagFromRequest(final Object moduleRequest) {
+        final JsonNode rootNode = objectMapper.valueToTree(moduleRequest);
         return rootNode.path("moduleSetTag").asText(null);
     }
 
-    private boolean isModuleSetTagEmptyOrInvalid(String moduleSetTag) {
+    private boolean isModuleSetTagEmptyOrInvalid(final String moduleSetTag) {
         return moduleSetTag == null || moduleSetTag.trim().isEmpty();
     }
 
-    private long getFirstRequestTimestamp(String moduleSetTag) {
-        return firstRequestTimePerModuleSetTag.computeIfAbsent(moduleSetTag, firstRequestTime -> System.currentTimeMillis());
+    private long getFirstRequestTimestamp(final String moduleSetTag) {
+        return firstRequestTimePerModuleSetTag
+                .computeIfAbsent(moduleSetTag, firstRequestTime -> System.currentTimeMillis());
     }
 
-    private boolean isInitialProcessingCompleted(long currentTimestamp, long firstRequestTimestamp) {
+    private boolean isInitialProcessingCompleted(final long currentTimestamp, final long firstRequestTimestamp) {
         return currentTimestamp - firstRequestTimestamp >= moduleInitialProcessingDelayMs;
     }
 
-    private long calculateRemainingProcessingTime(long currentTimestamp, long firstRequestTimestamp) {
+    private long calculateRemainingProcessingTime(final long currentTimestamp, final long firstRequestTimestamp) {
         return moduleInitialProcessingDelayMs - (currentTimestamp - firstRequestTimestamp);
     }
 }
diff --git a/pom.xml b/pom.xml
index b457e23..f8d34c5 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -1,3 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
 <!--
   ============LICENSE_START=======================================================
   Copyright (c) 2021-2024 Nordix Foundation.
@@ -18,6 +19,7 @@
   SPDX-License-Identifier: Apache-2.0
   ============LICENSE_END=========================================================
 -->
+
 <project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
     <name>ncmp-dmi-plugin</name>
     <description>DMI Plugin Service</description>
 
+    <properties>
+        <cps.version>3.5.2</cps.version>
+    </properties>
+
+    <build>
+        <plugins>
+            <!-- Add the Maven Checkstyle Plugin here -->
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-checkstyle-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>onap-license</id>
+                        <goals>
+                            <goal>check</goal>
+                        </goals>
+                        <phase>process-sources</phase>
+                        <configuration>
+                            <configLocation>onap-checkstyle/check-license.xml</configLocation>
+                            <includeResources>false</includeResources>
+                            <includeTestSourceDirectory>true</includeTestSourceDirectory>
+                            <includeTestResources>false</includeTestResources>
+                            <sourceDirectories>
+                                <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
+                            </sourceDirectories>
+                            <consoleOutput>false</consoleOutput>
+                            <violationSeverity>warning</violationSeverity>
+                            <failOnViolation>true</failOnViolation>
+                        </configuration>
+                    </execution>
+                    <execution>
+                        <id>onap-java-style</id>
+                        <goals>
+                            <goal>check</goal>
+                        </goals>
+                        <phase>process-sources</phase>
+                        <configuration>
+                            <configLocation>onap-checkstyle/onap-java-style.xml</configLocation>
+                            <sourceDirectories>
+                                <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
+                            </sourceDirectories>
+                            <includeResources>true</includeResources>
+                            <includeTestSourceDirectory>true</includeTestSourceDirectory>
+                            <includeTestResources>true</includeTestResources>
+                            <consoleOutput>false</consoleOutput>
+                            <violationSeverity>warning</violationSeverity>
+                            <failOnViolation>true</failOnViolation>
+                        </configuration>
+                    </execution>
+                    <execution>
+                        <id>cps-java-style</id>
+                        <goals>
+                            <goal>check</goal>
+                        </goals>
+                        <phase>process-sources</phase>
+                        <configuration>
+                            <configLocation>cps-java-style.xml</configLocation>
+                            <sourceDirectories>
+                                <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
+                            </sourceDirectories>
+                            <includeResources>true</includeResources>
+                            <includeTestSourceDirectory>true</includeTestSourceDirectory>
+                            <includeTestResources>true</includeTestResources>
+                            <consoleOutput>true</consoleOutput>
+                            <violationSeverity>warning</violationSeverity>
+                            <failOnViolation>true</failOnViolation>
+                        </configuration>
+                    </execution>
+                </executions>
+                <dependencies>
+                    <dependency>
+                        <groupId>${project.groupId}</groupId>
+                        <artifactId>checkstyle</artifactId>
+                        <version>${cps.version}</version>
+                    </dependency>
+                </dependencies>
+            </plugin>
+        </plugins>
+    </build>
+
 </project>