aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org
diff options
context:
space:
mode:
authorlapentafd <francesco.lapenta@est.tech>2021-06-22 12:29:11 +0100
committerlapentafd <francesco.lapenta@est.tech>2021-06-23 16:04:10 +0100
commit93c3d0aa9be2d5cd8ce935730a23f268fa2d2069 (patch)
tree05f61022270e506c1a96a05a31dda93aa90ebcfe /plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org
parent782b1a82328a1ba95d370a014c587e1ba13ca464 (diff)
Sonar Issues in Apex plugins-context
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>
Diffstat (limited to 'plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org')
-rw-r--r--plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroBytesObjectMapper.java12
-rw-r--r--plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroDirectObjectMapper.java6
-rw-r--r--plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjectMapperFactory.java10
-rw-r--r--plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java40
-rw-r--r--plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaKeyTranslationUtilities.java12
-rw-r--r--plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroStringObjectMapper.java6
6 files changed, 42 insertions, 44 deletions
diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroBytesObjectMapper.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroBytesObjectMapper.java
index 9e0ab6252..c575eea02 100644
--- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroBytesObjectMapper.java
+++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroBytesObjectMapper.java
@@ -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);
diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroDirectObjectMapper.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroDirectObjectMapper.java
index 5d72c62d4..2c2483384 100644
--- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroDirectObjectMapper.java
+++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroDirectObjectMapper.java
@@ -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);
diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjectMapperFactory.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjectMapperFactory.java
index 13feae3f4..4a4d92dea 100644
--- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjectMapperFactory.java
+++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjectMapperFactory.java
@@ -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) {
diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java
index 020cc1520..fe6779891 100644
--- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java
+++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java
@@ -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);
}
diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaKeyTranslationUtilities.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaKeyTranslationUtilities.java
index 6229c066e..34a4dedb0 100644
--- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaKeyTranslationUtilities.java
+++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaKeyTranslationUtilities.java
@@ -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));
}
diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroStringObjectMapper.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroStringObjectMapper.java
index f664006e2..27737b500 100644
--- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroStringObjectMapper.java
+++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroStringObjectMapper.java
@@ -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);