Sonar Issues in Apex plugins-context 57/122157/1
authorlapentafd <francesco.lapenta@est.tech>
Tue, 22 Jun 2021 11:29:11 +0000 (12:29 +0100)
committerlapentafd <francesco.lapenta@est.tech>
Wed, 23 Jun 2021 15:04:10 +0000 (16:04 +0100)
Replacing local-variable type inference, and checkstyle fix

Refactor lambda expression to have one invocation to throw exception

Issue-ID: POLICY-3093
Change-Id: I6f89793a3652c50897d7f8e0fda35fffba903acc
Signed-off-by: lapentafd <francesco.lapenta@est.tech>
plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroBytesObjectMapper.java
plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroDirectObjectMapper.java
plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjectMapperFactory.java
plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java
plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaKeyTranslationUtilities.java
plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroStringObjectMapper.java
plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaMapTest.java

index 9e0ab62..c575eea 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -87,7 +87,7 @@ public class AvroBytesObjectMapper implements AvroObjectMapper {
     public Object mapFromAvro(final Object avroObject) {
         // The Avro object should be a Utf8 object
         if (!(avroObject instanceof ByteBuffer)) {
-            final String returnString = userKey.getId() + ": object \"" + avroObject + "\" of class \""
+            final var returnString = userKey.getId() + ": object \"" + avroObject + "\" of class \""
                             + avroObject.getClass() + "\" cannot be decoded to an object of class \""
                             + schemaClass.getName() + "\"";
             LOGGER.warn(returnString);
@@ -95,10 +95,10 @@ public class AvroBytesObjectMapper implements AvroObjectMapper {
         }
 
         // Cast the byte buffer object so we get access to its methods
-        final ByteBuffer byteBufferAvroObject = (ByteBuffer) avroObject;
+        final var byteBufferAvroObject = (ByteBuffer) avroObject;
 
         // read the byte buffer into a byte array
-        final byte[] byteArray = new byte[byteBufferAvroObject.remaining()];
+        final var byteArray = new byte[byteBufferAvroObject.remaining()];
         byteBufferAvroObject.get(byteArray);
 
         return byteArray;
@@ -110,7 +110,7 @@ public class AvroBytesObjectMapper implements AvroObjectMapper {
     @Override
     public Object mapToAvro(final Object object) {
         if (object == null) {
-            final String returnString = userKey.getId() + ": cannot encode a null object of class \""
+            final var returnString = userKey.getId() + ": cannot encode a null object of class \""
                             + schemaClass.getName() + "\"";
             LOGGER.warn(returnString);
             throw new ContextRuntimeException(returnString);
@@ -118,7 +118,7 @@ public class AvroBytesObjectMapper implements AvroObjectMapper {
 
         // The incoming object should be a byte array
         if (!(object instanceof byte[])) {
-            final String returnString = userKey.getId() + ": object \"" + object + "\" of class \"" + object.getClass()
+            final var returnString = userKey.getId() + ": object \"" + object + "\" of class \"" + object.getClass()
                             + "\" cannot be decoded to an object of class \"" + schemaClass.getName() + "\"";
             LOGGER.warn(returnString);
             throw new ContextRuntimeException(returnString);
index 5d72c62..2c24833 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -117,7 +117,7 @@ public class AvroDirectObjectMapper implements AvroObjectMapper {
         // It is legal for the schema class to be null, if the Avro schema has a "null" type then
         // the decoded object is always returned as a null
         if (!schemaClass.isAssignableFrom(avroObject.getClass())) {
-            final String returnString = userKey.getId() + ": object \"" + avroObject + "\" of class \""
+            final var returnString = userKey.getId() + ": object \"" + avroObject + "\" of class \""
                             + avroObject.getClass() + "\" cannot be decoded to an object of class \""
                             + schemaClass.getName() + "\"";
             LOGGER.warn(returnString);
@@ -134,7 +134,7 @@ public class AvroDirectObjectMapper implements AvroObjectMapper {
     public Object mapToAvro(final Object object) {
         // Null values are only allowed if the schema class is null
         if (object == null && schemaClass != null) {
-            final String returnString = userKey.getId() + ": cannot encode a null object of class \""
+            final var returnString = userKey.getId() + ": cannot encode a null object of class \""
                             + schemaClass.getName() + "\"";
             LOGGER.warn(returnString);
             throw new ContextRuntimeException(returnString);
index 13feae3..4a4d92d 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2020-2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -70,14 +70,14 @@ public class AvroObjectMapperFactory {
      * @return the avro object mapper
      */
     public AvroObjectMapper get(final AxKey userKey, final Schema incomingSchema) {
-        Schema schema = incomingSchema;
-        boolean isnullable = false;
+        var schema = incomingSchema;
+        var isnullable = false;
         if (Schema.Type.UNION.equals(schema.getType())) {
 
             final List<Schema> types = schema.getTypes();
 
             // currently only support unions with 2 types, one of which is NULL
-            final Schema nullschema = Schema.create(Schema.Type.NULL);
+            final var nullschema = Schema.create(Schema.Type.NULL);
             if (types.size() != 2 || !types.contains(nullschema)) {
                 final String resultSting = userKey.getId()
                         + ": Apex currently only supports UNION schemas with 2 options, one must be NULL";
@@ -99,7 +99,7 @@ public class AvroObjectMapperFactory {
             }
         }
 
-        final Schema.Type avroType = schema.getType();
+        final var avroType = schema.getType();
 
         // Check that there is a definition for the mapper for this type
         if (!AVRO_OBJECT_MAPPER_MAP.containsKey(avroType) || AVRO_OBJECT_MAPPER_MAP.get(avroType) == null) {
index 020cc15..fe67798 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2021 Nordix Foundation.
  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -37,8 +37,6 @@ import org.apache.avro.generic.GenericRecord;
 import org.apache.avro.io.DatumWriter;
 import org.apache.avro.io.DecoderFactory;
 import org.apache.avro.io.EncoderFactory;
-import org.apache.avro.io.JsonDecoder;
-import org.apache.avro.io.JsonEncoder;
 import org.onap.policy.apex.context.ContextRuntimeException;
 import org.onap.policy.apex.context.impl.schema.AbstractSchemaHelper;
 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
@@ -100,7 +98,7 @@ public class AvroSchemaHelper extends AbstractSchemaHelper {
     @Override
     public Object createNewInstance() {
         // Create a new instance using the Avro object mapper
-        final Object newInstance = avroObjectMapper.createNewInstance(avroSchema);
+        final var newInstance = avroObjectMapper.createNewInstance(avroSchema);
 
         // If no new instance is created, use default schema handler behaviour
         if (newInstance != null) {
@@ -118,12 +116,12 @@ public class AvroSchemaHelper extends AbstractSchemaHelper {
     @Override
     public Object createNewInstance(final Object incomingObject) {
         if (incomingObject instanceof JsonElement) {
-            final Gson gson = new GsonBuilder().serializeNulls().create();
-            final String elementJsonString = gson.toJson((JsonElement) incomingObject);
+            final var gson = new GsonBuilder().serializeNulls().create();
+            final var elementJsonString = gson.toJson((JsonElement) incomingObject);
 
             return createNewInstance(elementJsonString);
         } else {
-            final String returnString =
+            final var returnString =
                     getUserKey().getId() + ": the object \"" + incomingObject + "\" is not an instance of JsonObject";
             throw new ContextRuntimeException(returnString);
         }
@@ -138,7 +136,7 @@ public class AvroSchemaHelper extends AbstractSchemaHelper {
         if (subInstance != null) {
             return subInstance;
         } else {
-            final String returnString = getUserKey().getId() + ": the schema \"" + avroSchema.getName()
+            final var returnString = getUserKey().getId() + ": the schema \"" + avroSchema.getName()
                     + "\" does not have a subtype of type \"" + subInstanceType + "\"";
             throw new ContextRuntimeException(returnString);
         }
@@ -216,7 +214,7 @@ public class AvroSchemaHelper extends AbstractSchemaHelper {
             return object;
         }
 
-        String objectString = getStringObject(object);
+        var objectString = getStringObject(object);
 
         // Translate illegal characters in incoming JSON keys to legal Avro values
         objectString = AvroSchemaKeyTranslationUtilities.translateIllegalKeys(objectString, false);
@@ -224,10 +222,10 @@ public class AvroSchemaHelper extends AbstractSchemaHelper {
         // Decode the object
         Object decodedObject;
         try {
-            final JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(avroSchema, objectString);
+            final var jsonDecoder = DecoderFactory.get().jsonDecoder(avroSchema, objectString);
             decodedObject = new GenericDatumReader<GenericRecord>(avroSchema).read(null, jsonDecoder);
         } catch (final Exception e) {
-            final String returnString =
+            final var returnString =
                 getUserKey().getId() + OBJECT_TAG + objectString + "\" Avro unmarshalling failed.";
             throw new ContextRuntimeException(returnString, e);
         }
@@ -250,7 +248,7 @@ public class AvroSchemaHelper extends AbstractSchemaHelper {
                 return (String) object;
             }
         } catch (final ClassCastException e) {
-            final String returnString = getUserKey().getId() + OBJECT_TAG + object + "\" of type \""
+            final var returnString = getUserKey().getId() + OBJECT_TAG + object + "\" of type \""
                     + (object != null ? object.getClass().getName() : "null") + "\" must be assignable to \""
                     + getSchemaClass().getName() + "\" or be a Json string representation of it for Avro unmarshalling";
             throw new ContextRuntimeException(returnString, e);
@@ -264,7 +262,7 @@ public class AvroSchemaHelper extends AbstractSchemaHelper {
      * @return the string
      */
     private String getObjectString(final Object object) {
-        String objectString = object.toString().trim();
+        var objectString = object.toString().trim();
         if (objectString.length() == 0) {
             return "\"\"";
         } else if (objectString.length() == 1) {
@@ -288,23 +286,23 @@ public class AvroSchemaHelper extends AbstractSchemaHelper {
     @Override
     public String marshal2String(final Object object) {
         // Condition the object for Avro encoding
-        final Object conditionedObject = avroObjectMapper.mapToAvro(object);
+        final var conditionedObject = avroObjectMapper.mapToAvro(object);
 
-        final String jsonString = getJsonString(object, conditionedObject);
+        final var jsonString = getJsonString(object, conditionedObject);
 
         return AvroSchemaKeyTranslationUtilities.translateIllegalKeys(jsonString, true);
     }
 
     private String getJsonString(final Object object, final Object conditionedObject) {
 
-        try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
+        try (final var output = new ByteArrayOutputStream()) {
             final DatumWriter<Object> writer = new GenericDatumWriter<>(avroSchema);
-            final JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(avroSchema, output, true);
+            final var jsonEncoder = EncoderFactory.get().jsonEncoder(avroSchema, output, true);
             writer.write(conditionedObject, jsonEncoder);
             jsonEncoder.flush();
             return new String(output.toByteArray());
         } catch (final Exception e) {
-            final String returnString = getUserKey().getId() + OBJECT_TAG + object + "\" Avro marshalling failed.";
+            final var returnString = getUserKey().getId() + OBJECT_TAG + object + "\" Avro marshalling failed.";
             throw new ContextRuntimeException(returnString, e);
         }
     }
@@ -312,13 +310,13 @@ public class AvroSchemaHelper extends AbstractSchemaHelper {
     @Override
     public JsonElement marshal2Object(final Object schemaObject) {
         // Get the object as a Json string
-        final String schemaObjectAsString = marshal2String(schemaObject);
+        final var schemaObjectAsString = marshal2String(schemaObject);
 
         // Get a Gson instance to convert the Json string to an object created by Json
-        final Gson gson = new Gson();
+        final var gson = new Gson();
 
         // Convert the Json string into an object
-        final Object schemaObjectAsObject = gson.fromJson(schemaObjectAsString, Object.class);
+        final var schemaObjectAsObject = gson.fromJson(schemaObjectAsString, Object.class);
 
         return gson.toJsonTree(schemaObjectAsObject);
     }
index 6229c06..34a4ded 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2020-2021 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,10 +62,10 @@ public final class AvroSchemaKeyTranslationUtilities {
         }
 
         // Create a JSON element for the incoming JSON string
-        final JsonElement jsonElement = new GsonBuilder().serializeNulls().create().fromJson(jsonString,
+        final var jsonElement = new GsonBuilder().serializeNulls().create().fromJson(jsonString,
                 JsonElement.class);
 
-        final JsonElement translatedJsonElement = translateIllegalKeys(jsonElement, revert);
+        final var translatedJsonElement = translateIllegalKeys(jsonElement, revert);
 
         return new GsonBuilder().serializeNulls().create().toJson(translatedJsonElement);
     }
@@ -98,7 +98,7 @@ public final class AvroSchemaKeyTranslationUtilities {
      * @return the translated JSON element
      */
     public static JsonElement translateIllegalKeys(final JsonObject jsonObject, final boolean revert) {
-        final JsonObject newJsonObject = new JsonObject();
+        final var newJsonObject = new JsonObject();
 
         for (final Entry<String, JsonElement> jsonObjectEntry : jsonObject.entrySet()) {
             newJsonObject.add(translateIllegalKey(jsonObjectEntry.getKey(), revert),
@@ -117,9 +117,9 @@ public final class AvroSchemaKeyTranslationUtilities {
      * @return the translated JSON element
      */
     public static JsonElement translateIllegalKeys(final JsonArray jsonArray, final boolean revert) {
-        final JsonArray newJsonArray = new JsonArray();
+        final var newJsonArray = new JsonArray();
 
-        for (int i = 0; i < jsonArray.size(); i++) {
+        for (var i = 0; i < jsonArray.size(); i++) {
             newJsonArray.add(translateIllegalKeys(jsonArray.get(i), revert));
         }
 
index f664006..27737b5 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -87,7 +87,7 @@ public class AvroStringObjectMapper implements AvroObjectMapper {
     public Object mapFromAvro(final Object avroObject) {
         // The Avro object should be a Utf8 object
         if (!(avroObject instanceof Utf8)) {
-            final String returnString = userKey.getId() + ": object \"" + avroObject + "\" of class \""
+            final var returnString = userKey.getId() + ": object \"" + avroObject + "\" of class \""
                             + avroObject.getClass() + "\" cannot be decoded to an object of class \""
                             + schemaClass.getName() + "\"";
             LOGGER.warn(returnString);
@@ -103,7 +103,7 @@ public class AvroStringObjectMapper implements AvroObjectMapper {
     @Override
     public Object mapToAvro(final Object object) {
         if (object == null) {
-            final String returnString = userKey.getId() + ": cannot encode a null object of class \""
+            final var returnString = userKey.getId() + ": cannot encode a null object of class \""
                             + schemaClass.getName() + "\"";
             LOGGER.warn(returnString);
             throw new ContextRuntimeException(returnString);
index abc9335..f4d2024 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2020-2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -113,7 +113,7 @@ public class AvroSchemaMapTest {
         assertEquals(new Utf8("foo"), subst1A.get("A_DasH_B"));
         assertNull(subst1A.get("A-B"));
         final Throwable exception1 = assertThrows(ContextRuntimeException.class,
-                () -> schemaHelperSubst1.unmarshal("{\"A-B\":123}"));
+            () -> schemaHelperSubst1.unmarshal("{\"A-B\":123}"));
         assertNotNull(exception1.getCause());
         assertEquals("Expected string. Got VALUE_NUMBER_INT", exception1.getCause().getMessage());
 
@@ -129,7 +129,7 @@ public class AvroSchemaMapTest {
         assertEquals(123, subst2A.get("C_DoT_D"));
         assertNull(subst2A.get("C.D"));
         final Throwable exception2 = assertThrows(ContextRuntimeException.class,
-                () -> schemaHelperSubst2.unmarshal("{\"C_DoT_D\":\"bar\"}"));
+            () -> schemaHelperSubst2.unmarshal("{\"C_DoT_D\":\"bar\"}"));
         assertNotNull(exception2.getCause());
         assertEquals("Expected int. Got VALUE_STRING", exception2.getCause().getMessage());
 
@@ -145,7 +145,7 @@ public class AvroSchemaMapTest {
         assertEquals(true, subst3A.get("E_ColoN_F"));
         assertNull(subst3A.get("E:F"));
         final Throwable exception3 = assertThrows(ContextRuntimeException.class,
-                () -> schemaHelperSubst3.unmarshal("{\"E_ColoN_F\":\"gaz\"}"));
+            () -> schemaHelperSubst3.unmarshal("{\"E_ColoN_F\":\"gaz\"}"));
         assertNotNull(exception3.getCause());
         assertEquals("Expected boolean. Got VALUE_STRING", exception3.getCause().getMessage());
     }
@@ -162,8 +162,11 @@ public class AvroSchemaMapTest {
         final AxContextSchema avroFailSchema1 = new AxContextSchema(
                 new AxArtifactKey("AvroFail1", "0.0.1"), "AVRO", fail1);
         schemas.getSchemasMap().put(avroFailSchema1.getKey(), avroFailSchema1);
+
+        SchemaHelperFactory sh = new SchemaHelperFactory();
+        AxArtifactKey ak = avroFailSchema1.getKey();
         final Throwable exception1 = assertThrows(ContextRuntimeException.class,
-                () -> new SchemaHelperFactory().createSchemaHelper(testKey, avroFailSchema1.getKey()));
+            () -> sh.createSchemaHelper(testKey, ak));
         assertNotNull(exception1.getCause());
         assertEquals("Illegal character in: A-B", exception1.getCause().getMessage());
 
@@ -172,8 +175,10 @@ public class AvroSchemaMapTest {
         final AxContextSchema avroFailSchema2 = new AxContextSchema(
                 new AxArtifactKey("AvroFail2", "0.0.1"), "AVRO", fail2);
         schemas.getSchemasMap().put(avroFailSchema2.getKey(), avroFailSchema2);
+
+        AxArtifactKey ak2 = avroFailSchema2.getKey();
         final Throwable exception2 = assertThrows(ContextRuntimeException.class,
-                () -> new SchemaHelperFactory().createSchemaHelper(testKey, avroFailSchema2.getKey()));
+            () -> sh.createSchemaHelper(testKey, ak2));
         assertNotNull(exception2.getCause());
         assertEquals("Illegal character in: C.D", exception2.getCause().getMessage());
 
@@ -182,8 +187,9 @@ public class AvroSchemaMapTest {
         final AxContextSchema avroFailSchema3 = new AxContextSchema(
                 new AxArtifactKey("AvroFail3", "0.0.1"), "AVRO", fail3);
         schemas.getSchemasMap().put(avroFailSchema3.getKey(), avroFailSchema3);
+        AxArtifactKey ak3 = avroFailSchema3.getKey();
         final Throwable exception3 = assertThrows(ContextRuntimeException.class,
-                () -> new SchemaHelperFactory().createSchemaHelper(testKey, avroFailSchema3.getKey()));
+            () -> sh.createSchemaHelper(testKey, ak3));
         assertNotNull(exception3.getCause());
         assertEquals("Illegal character in: E:F", exception3.getCause().getMessage());
     }