Fix sonar issues 81/139881/1
authoradheli.tavares <adheli.tavares@est.tech>
Thu, 9 Jan 2025 10:18:33 +0000 (10:18 +0000)
committeradheli.tavares <adheli.tavares@est.tech>
Thu, 9 Jan 2025 10:19:40 +0000 (10:19 +0000)
- fixing sonar issues on code to generate a new common artifact
with parent new dependencies.

Issue-ID: POLICY-5240
Change-Id: I47b2ace072708e576d5fc5a82c2419925cbed7ed
Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
common-parameters/src/test/java/org/onap/policy/common/parameters/TestValueValidator.java
gson/src/test/java/org/onap/policy/common/gson/GsonMessageBodyHandlerTest.java
gson/src/test/java/org/onap/policy/common/gson/JacksonExclusionStrategyTest.java
message-bus/src/test/java/org/onap/policy/common/message/bus/event/client/BidirectionalTopicClientTest.java
policy-endpoints/src/test/java/org/onap/policy/common/endpoints/rest/CommonTestData.java
utils/src/test/java/org/onap/policy/common/utils/coder/PropertyCoderTest.java
utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderObjectTest.java
utils/src/test/java/org/onap/policy/common/utils/validation/TestParameterValidationUtils.java

index 0cd3776..1b42876 100644 (file)
@@ -3,7 +3,7 @@
  * ONAP
  * ================================================================================
  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2024 Nordix Foundation
+ * Modifications Copyright (C) 2024-2025 Nordix Foundation
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -62,8 +62,7 @@ class TestValueValidator extends ValidatorUtil {
         validator.validateValue(result, MY_FIELD, null);
         assertThat(result.getResult()).isNull();
 
-        validator.addAnnotation(NotNull.class,
-            (result2, fieldName, value) -> result2.validateNotNull(fieldName, value));
+        validator.addAnnotation(NotNull.class, BeanValidationResult::validateNotNull);
         validator.validateValue(result, MY_FIELD, null);
         assertThat(result.getResult()).contains(MY_FIELD, "null");
     }
@@ -75,8 +74,7 @@ class TestValueValidator extends ValidatorUtil {
         validator.validateValue(result, MY_FIELD, HELLO);
         assertThat(result.getResult()).isNull();
 
-        validator.addAnnotation(NotNull.class,
-            (result2, fieldName, value) -> result2.validateNotNull(fieldName, value));
+        validator.addAnnotation(NotNull.class, BeanValidationResult::validateNotNull);
         validator.validateValue(result, MY_FIELD, HELLO);
         assertThat(result.getResult()).isNull();
     }
@@ -93,7 +91,6 @@ class TestValueValidator extends ValidatorUtil {
         assertThat(validator.isNullAllowed()).isTrue();
 
         // "null" flag should become false with this annotation
-        assertThat(validator.isNullAllowed()).isTrue();
         validator.addAnnotation(NotNull.class, (result2, fieldName, value) -> true);
         assertThat(validator.isNullAllowed()).isFalse();
     }
@@ -109,7 +106,7 @@ class TestValueValidator extends ValidatorUtil {
 
         // the field DOES have this annotation
         validator.addAnnotation(NotNull.class, (result2, fieldName, annot, value) -> {
-            wasNull.set(annot instanceof NotNull);
+            wasNull.set(annot != null);
             return result2.validateNotNull(fieldName, value);
         });
         assertThat(validator.isEmpty()).isFalse();
index 00e1caf..579b9fd 100644 (file)
@@ -3,7 +3,7 @@
  * ONAP
  * ================================================================================
  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2023-2024 Nordix Foundation.
+ * Modifications Copyright (C) 2023-2025 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -137,7 +137,7 @@ class GsonMessageBodyHandlerTest {
 
         // use a derived type, but specify the base type when reading
         @SuppressWarnings("rawtypes")
-        Class clazz = new MyObject() {}.getClass();
+        Class clazz = MyObject.class;
 
         @SuppressWarnings("unchecked")
         Class<Object> objclazz = clazz;
index 38f2f32..fc3c667 100644 (file)
@@ -3,7 +3,7 @@
  * ONAP
  * ================================================================================
  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
- * Modificaitons Copyright (C) 2023-2024 Nordix Foundation.
+ * Modificaitons Copyright (C) 2023-2025 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -29,9 +29,12 @@ import com.google.gson.FieldAttributes;
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 import com.google.gson.JsonElement;
+import java.io.Serial;
 import java.lang.reflect.GenericArrayType;
 import java.util.LinkedList;
 import java.util.TreeMap;
+import lombok.Getter;
+import lombok.Setter;
 import lombok.ToString;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
@@ -91,7 +94,7 @@ class JacksonExclusionStrategyTest {
 
         // generic classes should NOT be managed
         Class<?>[] unmanaged = {
-            new Data[0].getClass(), Enum.class, boolean.class, byte.class, short.class, int.class,
+            Data[].class, Enum.class, boolean.class, byte.class, short.class, int.class,
             long.class, float.class, double.class, char.class, Boolean.class, Byte.class, Short.class,
             Integer.class, Long.class, Float.class, Double.class, Character.class, String.class,
             MyMap.class, MyList.class, MyJson.class, GenericArrayType.class};
@@ -104,36 +107,27 @@ class JacksonExclusionStrategyTest {
     /**
      * Used to verify that no fields are exposed.
      */
+    @Getter
+    @Setter
     @ToString
     public static class Data {
         private int id;
         public String text;
 
-        public int getId() {
-            return id;
-        }
-
         void setId(int id) {
             this.id = id;
         }
 
-        public String getText() {
-            return text;
-        }
-
         void setText(String text) {
             this.text = text;
         }
     }
 
+    @Getter
     @ToString(callSuper = true)
     public static class Derived extends Data {
         protected String value;
 
-        public String getValue() {
-            return value;
-        }
-
         void setValue(String value) {
             this.value = value;
         }
@@ -142,34 +136,34 @@ class JacksonExclusionStrategyTest {
     /**
      * Used to verify that enums are not managed.
      */
-    public static enum Enum {
+    public enum Enum {
         UP, DOWN,
     }
 
     /**
      * Used to verify that interfaces <i>are</i> managed.
      */
-    public static interface Intfc {
+    public interface Intfc {
         int getId();
     }
 
     /**
      * Used to verify that Maps are not managed.
      */
+    @Getter
     public static class MyMap extends TreeMap<String, Data> {
+        @Serial
         private static final long serialVersionUID = 1L;
 
         private int mapId;
 
-        public int getMapId() {
-            return mapId;
-        }
     }
 
     /**
      * Used to verify that Collections are not managed.
      */
     public static class MyList extends LinkedList<Data> {
+        @Serial
         private static final long serialVersionUID = 1L;
     }
 
index ba33ff9..3bfd429 100644 (file)
@@ -3,7 +3,7 @@
  * ONAP
  * ================================================================================
  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2024 Nordix Foundation.
+ * Modifications Copyright (C) 2024-2025 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import java.util.Arrays;
+import java.util.List;
 import java.util.Properties;
 import java.util.concurrent.Semaphore;
 import java.util.concurrent.TimeUnit;
@@ -119,11 +120,11 @@ class BidirectionalTopicClientTest {
         lenient().when(source.offer(anyString())).thenReturn(true);
         lenient().when(source.getTopicCommInfrastructure()).thenReturn(SOURCE_INFRA);
 
-        lenient().when(endpoint.getTopicSinks(anyString())).thenReturn(Arrays.asList());
-        lenient().when(endpoint.getTopicSinks(SINK_TOPIC)).thenReturn(Arrays.asList(sink));
+        lenient().when(endpoint.getTopicSinks(anyString())).thenReturn(List.of());
+        lenient().when(endpoint.getTopicSinks(SINK_TOPIC)).thenReturn(List.of(sink));
 
-        lenient().when(endpoint.getTopicSources(any())).thenReturn(Arrays.asList());
-        lenient().when(endpoint.getTopicSources(Arrays.asList(SOURCE_TOPIC))).thenReturn(Arrays.asList(source));
+        lenient().when(endpoint.getTopicSources(any())).thenReturn(List.of());
+        lenient().when(endpoint.getTopicSources(List.of(SOURCE_TOPIC))).thenReturn(List.of(source));
 
         theMessage = new MyMessage(MY_TEXT);
 
@@ -161,7 +162,7 @@ class BidirectionalTopicClientTest {
             .hasMessage("no sources for topic: unknown-source");
 
         // too many sources
-        when(endpoint.getTopicSources(Arrays.asList(SOURCE_TOPIC))).thenReturn(Arrays.asList(source, source));
+        when(endpoint.getTopicSources(List.of(SOURCE_TOPIC))).thenReturn(Arrays.asList(source, source));
 
         assertThatThrownBy(() -> new BidirectionalTopicClient2(SINK_TOPIC, SOURCE_TOPIC))
             .isInstanceOf(BidirectionalTopicClientException.class)
@@ -381,9 +382,9 @@ class BidirectionalTopicClientTest {
 
                 @Override
                 public boolean send(String messageText) {
-                    boolean result = super.send(messageText);
+                    boolean messageSent = super.send(messageText);
                     sendSem.release();
-                    return result;
+                    return messageSent;
                 }
 
                 @Override
index 3e16b8a..8c43e1f 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019, 2024 Nordix Foundation.
+ *  Copyright (C) 2019, 2024-2025 Nordix Foundation.
  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -23,7 +23,6 @@ package org.onap.policy.common.endpoints.rest;
 
 import java.io.File;
 import java.io.IOException;
-import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.util.Map;
 import java.util.TreeMap;
index d45f43b..5559155 100644 (file)
@@ -3,7 +3,7 @@
  * ONAP
  * ================================================================================
  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2024 Nordix Foundation
+ * Modifications Copyright (C) 2024-2025 Nordix Foundation
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@ class PropertyCoderTest {
      * For "pass", the encrypted value was generated via:
      *  java org.onap.policy.common.utils.security.CryptoUtils enc hello abcdefghijklmnopqrstuvwxyzabcdef
      */
-    private static final String json =
+    private static final String JSON =
             ("{'aes_encryption_key':'abcdefghijklmnopqrstuvwxyzabcdef'"
             + ",'xacml.pdp.rest.password':'enc:FSfOhDygtmnX3gkMSfTFMoBFW+AG5k6goNj2KZgQmeF0DqgcMg=='"
             + ",'xacml.pdp.rest.user':'testpdp'"
@@ -72,7 +72,7 @@ class PropertyCoderTest {
 
     @Test
     void testPropertyCoder() {
-        MyClass data = propertyCoder.decode(json, AES_ENCRYPTION_KEY, MyClass.class);
+        MyClass data = propertyCoder.decode(JSON, AES_ENCRYPTION_KEY, MyClass.class);
         assertEquals("alpha", data.getPdpRestPass());
         assertEquals("hello", data.servers.get(0).pass);
         assertEquals("server1", data.servers.get(0).name);
@@ -82,7 +82,7 @@ class PropertyCoderTest {
 
     @Test
     void testPropertyCoderReader() {
-        Reader reader = new StringReader(json);
+        Reader reader = new StringReader(JSON);
         MyClass data = propertyCoder.decode(reader, AES_ENCRYPTION_KEY, MyClass.class);
         assertEquals("alpha", data.getPdpRestPass());
         assertEquals("hello", data.servers.get(0).pass);
index c597b7f..0e4967b 100644 (file)
@@ -3,7 +3,7 @@
  * ONAP
  * ================================================================================
  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2024 Nordix Foundation
+ * Modifications Copyright (C) 2024-2025 Nordix Foundation
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ class StandardCoderObjectTest {
     private static final String PROP1 = "abc";
     private static final String PROP2 = "ghi";
     private static final Integer PROP2_INDEX = 1;
-    private static final String PROP2b = "jkl";
+    private static final String PROP_2_B = "jkl";
     private static final String VAL1 = "def";
     private static final String VAL2 = "mno";
     private static final String JSON = "{'abc':'def','ghi':[{},{'jkl':'mno'}]}".replace('\'', '"');
@@ -71,7 +71,7 @@ class StandardCoderObjectTest {
         assertEquals(VAL1, sco.getString(PROP1));
 
         // multiple fields
-        assertEquals(VAL2, sco.getString(PROP2, PROP2_INDEX, PROP2b));
+        assertEquals(VAL2, sco.getString(PROP2, PROP2_INDEX, PROP_2_B));
 
         // not found
         assertNull(sco.getString("xyz"));
index a85e8ec..9f10ba9 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
  *  Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
- *  Modifications Copyright (C) 2024 Nordix Foundation
+ *  Modifications Copyright (C) 2024-2025 Nordix Foundation
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -44,7 +44,6 @@ class TestParameterValidationUtils {
     @Test
     void testValidateIntParameter() {
         assertTrue(ParameterValidationUtils.validateIntParameter(5555));
-        assertTrue(ParameterValidationUtils.validateIntParameter(Integer.valueOf(7777)));
         assertFalse(ParameterValidationUtils.validateIntParameter(0));
         assertFalse(ParameterValidationUtils.validateIntParameter(-1));
     }
@@ -52,7 +51,6 @@ class TestParameterValidationUtils {
     @Test
     void testValidateLongParameter() {
         assertTrue(ParameterValidationUtils.validateLongParameter(5555L));
-        assertTrue(ParameterValidationUtils.validateLongParameter(Long.valueOf(7777L)));
         assertFalse(ParameterValidationUtils.validateLongParameter(0L));
         assertFalse(ParameterValidationUtils.validateLongParameter(-1L));
     }