diff options
author | David Brilla <david.brilla@tieto.com> | 2020-02-12 11:00:39 +0100 |
---|---|---|
committer | James Forsyth <jf2512@att.com> | 2020-02-21 19:55:35 +0000 |
commit | e654645a50a0d028d8e67ea997f84efe8d28a6a0 (patch) | |
tree | ce460c6da06fc6e33a204b8a68e1bcd471c956df | |
parent | 4f436e1378f7ee4533701f5b1766fe04d123cd1c (diff) |
AAI-common sonar fixes
Fixing bugs, code smells of aai-common repo found on sonar.
Issue-ID: AAI-2786
Change-Id: I421f8b46f385c7334b0f3cd80c07cb2f833f127f
Signed-off-by: David Brilla <david.brilla@tieto.com>
9 files changed, 81 insertions, 92 deletions
diff --git a/aai-core/src/main/java/org/onap/aai/exceptions/AAIException.java b/aai-core/src/main/java/org/onap/aai/exceptions/AAIException.java index bca4b48c..320d5630 100644 --- a/aai-core/src/main/java/org/onap/aai/exceptions/AAIException.java +++ b/aai-core/src/main/java/org/onap/aai/exceptions/AAIException.java @@ -48,7 +48,7 @@ public class AAIException extends Exception { public AAIException() { super(); this.code = DEFAULT_EXCEPTION_CODE; - this.templateVars = new LinkedList<String>(); + this.templateVars = new LinkedList<>(); try { this.errorObject = ErrorLogHelper.getErrorObject(getCode()); @@ -67,7 +67,7 @@ public class AAIException extends Exception { super(); this.code = code; - this.templateVars = new LinkedList<String>(); + this.templateVars = new LinkedList<>(); try { this.errorObject = ErrorLogHelper.getErrorObject(getCode()); @@ -87,7 +87,7 @@ public class AAIException extends Exception { super(details); this.code = code; - this.templateVars = new LinkedList<String>(); + this.templateVars = new LinkedList<>(); try { this.errorObject = ErrorLogHelper.getErrorObject(getCode()); @@ -108,7 +108,7 @@ public class AAIException extends Exception { super(cause); this.code = code; - this.templateVars = new LinkedList<String>(); + this.templateVars = new LinkedList<>(); try { this.errorObject = ErrorLogHelper.getErrorObject(getCode()); @@ -129,7 +129,7 @@ public class AAIException extends Exception { super(details, cause); this.code = code; - this.templateVars = new LinkedList<String>(); + this.templateVars = new LinkedList<>(); try { this.errorObject = ErrorLogHelper.getErrorObject(getCode()); diff --git a/aai-core/src/main/java/org/onap/aai/logging/EcompResponseDescription.java b/aai-core/src/main/java/org/onap/aai/logging/EcompResponseDescription.java index 4b2b3b46..b72cc100 100644 --- a/aai-core/src/main/java/org/onap/aai/logging/EcompResponseDescription.java +++ b/aai-core/src/main/java/org/onap/aai/logging/EcompResponseDescription.java @@ -22,25 +22,26 @@ package org.onap.aai.logging; import ch.qos.logback.classic.pattern.ClassicConverter; import ch.qos.logback.classic.spi.ILoggingEvent; - import org.onap.aai.logging.LoggingContext.LoggingField; public class EcompResponseDescription extends ClassicConverter { - public final static String DefaultDescription = "Unknown response/error description"; + public static final String DEFAULT_DESCRIPTION = "Unknown response/error description"; @Override public String convert(ILoggingEvent event) { if (!event.getMDCPropertyMap().containsKey(LoggingField.RESPONSE_DESCRIPTION.toString())) { - return (DefaultDescription); + return (DEFAULT_DESCRIPTION); } // Replace pipes and new lines String currentDesc = event.getMDCPropertyMap().get(LoggingField.RESPONSE_DESCRIPTION.toString()); if ((currentDesc == null) || (currentDesc.length() == 0)) { - return (DefaultDescription); + return (DEFAULT_DESCRIPTION); } - currentDesc = currentDesc.replaceAll("|", "!"); + currentDesc = currentDesc.replaceAll("\\|", "!"); currentDesc = currentDesc.replaceAll("[\\r\\n]+", "^"); - return event.getMDCPropertyMap().get(LoggingField.RESPONSE_DESCRIPTION.toString()); + return event.getMDCPropertyMap().get(currentDesc); } } + + diff --git a/aai-core/src/main/java/org/onap/aai/logging/LoggingContext.java b/aai-core/src/main/java/org/onap/aai/logging/LoggingContext.java index f79025a2..5630fd10 100644 --- a/aai-core/src/main/java/org/onap/aai/logging/LoggingContext.java +++ b/aai-core/src/main/java/org/onap/aai/logging/LoggingContext.java @@ -34,7 +34,6 @@ import java.util.concurrent.TimeUnit; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import org.onap.aai.exceptions.AAIException; import org.slf4j.MDC; public class LoggingContext { @@ -56,7 +55,7 @@ public class LoggingContext { public static final String BUSINESS_PROCESS_ERROR = "500"; public static final String UNKNOWN_ERROR = "900"; - public static final Map<String, String> responseMap = new HashMap(); + protected static final Map<String, String> responseMap = new HashMap(); static { responseMap.put(SUCCESS, "Success"); @@ -64,7 +63,7 @@ public class LoggingContext { } // Specific Log Event Fields - public static enum LoggingField { + public enum LoggingField { START_TIME("startTime"), REQUEST_ID("requestId"), SERVICE_INSTANCE_ID("serviceInstanceId"), SERVER_NAME( "serverName"), SERVICE_NAME("serviceName"), PARTNER_NAME("partnerName"), STATUS_CODE( "statusCode"), RESPONSE_CODE("responseCode"), RESPONSE_DESCRIPTION( @@ -89,6 +88,7 @@ public class LoggingContext { this.text = text; } + @Override public String toString() { return text; } @@ -162,7 +162,7 @@ public class LoggingContext { } public static String responseCode() { - return (String) MDC.get(LoggingField.RESPONSE_CODE.toString()); + return MDC.get(LoggingField.RESPONSE_CODE.toString()); } public static void responseCode(String responseCode) { @@ -268,10 +268,7 @@ public class LoggingContext { public static boolean isStopWatchStarted() { final String rawStopWatchStart = MDC.get(LoggingField.STOP_WATCH_START.toString()); - if (rawStopWatchStart == null) { - return false; - } - return true; + return rawStopWatchStart != null; } public static void stopWatchStart() { @@ -406,7 +403,7 @@ public class LoggingContext { } public static Map<String, String> getCopy() { - final Map<String, String> copy = new HashMap<String, String>(); + final Map<String, String> copy = new HashMap<>(); for (LoggingField field : LoggingField.values()) { final String value = MDC.get(field.toString()); diff --git a/aai-core/src/main/java/org/onap/aai/serialization/tinkerpop/TreeBackedVertex.java b/aai-core/src/main/java/org/onap/aai/serialization/tinkerpop/TreeBackedVertex.java index c2856a77..700005ac 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/tinkerpop/TreeBackedVertex.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/tinkerpop/TreeBackedVertex.java @@ -39,8 +39,8 @@ import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex; public class TreeBackedVertex extends DetachedVertex implements Vertex { private static final long serialVersionUID = -976854460992756953L; - private final Tree<Element> tree; - private final Vertex self; + private final transient Tree<Element> tree; + private final transient Vertex self; public TreeBackedVertex(Vertex v, Tree<Element> tree) { super(v, true); diff --git a/aai-core/src/main/java/org/onap/aai/util/HbaseSaltPrefixer.java b/aai-core/src/main/java/org/onap/aai/util/HbaseSaltPrefixer.java index fef10a77..a41ad27b 100644 --- a/aai-core/src/main/java/org/onap/aai/util/HbaseSaltPrefixer.java +++ b/aai-core/src/main/java/org/onap/aai/util/HbaseSaltPrefixer.java @@ -55,7 +55,7 @@ public class HbaseSaltPrefixer { * @return the string */ public String prependSalt(String key) { - int salt = Math.abs(key.hashCode()) % NUM_REGION_BUCKETS; + int salt = key.hashCode() % NUM_REGION_BUCKETS; return salt + "-" + key; } } diff --git a/aai-core/src/test/java/org/onap/aai/logging/EcompResponseDescriptionTest.java b/aai-core/src/test/java/org/onap/aai/logging/EcompResponseDescriptionTest.java index deec8828..4bcf79cd 100644 --- a/aai-core/src/test/java/org/onap/aai/logging/EcompResponseDescriptionTest.java +++ b/aai-core/src/test/java/org/onap/aai/logging/EcompResponseDescriptionTest.java @@ -44,7 +44,7 @@ public class EcompResponseDescriptionTest { @Test public void getDefaultDesc() { - assertEquals(_ecompResponseDescription.convert(mockEvent), _ecompResponseDescription.DefaultDescription); + assertEquals(_ecompResponseDescription.convert(mockEvent), _ecompResponseDescription.DEFAULT_DESCRIPTION); } } diff --git a/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/definitions/EdgeSchema.java b/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/definitions/EdgeSchema.java index 07dd65d1..5d259128 100644 --- a/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/definitions/EdgeSchema.java +++ b/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/definitions/EdgeSchema.java @@ -27,25 +27,25 @@ public class EdgeSchema { protected String name; protected String source; protected String target; - protected Multiplicity multiplicity; + protected Multiplicity multiplicity; protected Map<String,String> annotations; protected Map<String,PropertySchema> properties; - + public enum Multiplicity { - MANY_2_MANY, + MANY_2_MANY, MANY_2_ONE, ONE_2_MANY, ONE_2_ONE } - + public String getName() { return name; } - + public String getSource() { return source; } - + public String getTarget() { return target; } @@ -61,55 +61,53 @@ public class EdgeSchema { public Map<String,PropertySchema> getPropertySchemaList() { return properties; } - + public String getAnnotationValue(String annotation) { return annotations.get(annotation.toLowerCase()); } - + public Map<String,String> getAnnotations() { return annotations; } - + @Override public int hashCode() { String key = source + target + name; return key.hashCode(); } - + @Override public boolean equals(Object obj) { if (this == obj) { return true; } - - if (getClass() != obj.getClass()) { + + if (obj == null || getClass() != obj.getClass()) { return false; } - + EdgeSchema other = (EdgeSchema) obj; - - if ( (!source.equals(other.getSource())) || (!target.equals(other.getTarget())) || (!name.equals(other.getName())) ) { - return false; - } - - return true; + + return (source.equals(other.getSource())) + && (target.equals(other.getTarget())) + && (name.equals(other.getName())); } - + public String toString() { StringBuilder sb = new StringBuilder(); sb.append("edge: " + getSource() + " -> " + getTarget() + "\n"); sb.append(" type: " + getName() + "\n"); sb.append(" multiplicity: " + getMultiplicity() + "\n"); - + sb.append(" annotations: " + "\n"); - for (String annotation : annotations.keySet()) { - sb.append(" " + annotation + ": " + annotations.get(annotation) + "\n"); + for (Map.Entry<String, String> entry : annotations.entrySet()) { + sb.append(" " + entry.getKey() + ": " + entry.getValue() + "\n"); } sb.append(" properties: " + "\n"); for (PropertySchema attrSchema : getPropertySchemaList().values()) { sb.append(attrSchema.toString()); } - + return sb.toString(); } diff --git a/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/JsonSchemaProvider.java b/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/JsonSchemaProvider.java index a947eac4..47cb2727 100644 --- a/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/JsonSchemaProvider.java +++ b/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/JsonSchemaProvider.java @@ -30,7 +30,7 @@ import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import java.util.zip.ZipEntry; +import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import org.onap.aai.cl.api.Logger; @@ -53,25 +53,25 @@ import org.springframework.web.client.RestTemplate; public class JsonSchemaProvider implements SchemaProvider { Logger logger = LoggerFactory.getInstance().getLogger(JsonSchemaProvider.class.getName()); - + private JsonSchemaProviderConfig config; private Map<String,SchemaInstance> schemaCache = new ConcurrentHashMap<>(); private RestTemplate restTemplate = null; - + public JsonSchemaProvider(JsonSchemaProviderConfig config) { this.config = config; - SecureClientHttpRequestFactory fac = new SecureClientHttpRequestFactory(config); + SecureClientHttpRequestFactory fac = new SecureClientHttpRequestFactory(config); fac.setBufferRequestBody(false); this.restTemplate = new RestTemplate(fac); } - + @Override public void loadSchema() throws SchemaProviderException { // Load the latest schema version fetchSchemaVersion(getLatestSchemaVersion()); } - + @Override public String getLatestSchemaVersion() throws SchemaProviderException { return "v0"; @@ -79,7 +79,7 @@ public class JsonSchemaProvider implements SchemaProvider { @Override public VertexSchema getVertexSchema(String vertexName, String schemaVersion) throws SchemaProviderException { - SchemaInstance inst = getSchemaVersion(schemaVersion); + SchemaInstance inst = getSchemaVersion(schemaVersion); return inst.getVertexSchema(vertexName); } @@ -89,16 +89,16 @@ public class JsonSchemaProvider implements SchemaProvider { SchemaInstance inst = getSchemaVersion(version); return inst.getEdgeSchema(sourceVertex, targetVertex, edgeType); } - + @Override public Set<EdgeSchema> getAdjacentEdgeSchema(String vertexType, String version) throws SchemaProviderException { SchemaInstance inst = getSchemaVersion(version); - + Set<EdgeSchema> edgeList = inst.getEdgeSchema(vertexType); if (edgeList == null) { - edgeList = new HashSet<EdgeSchema>(); + edgeList = new HashSet<>(); } - + return edgeList; } @@ -107,55 +107,51 @@ public class JsonSchemaProvider implements SchemaProvider { throws SchemaProviderException { SchemaInstance inst = getSchemaVersion(version); - if (inst == null) { - throw new SchemaProviderException("Unable to find schema version " + version); - } - Set<EdgeSchema> edgeList = inst.getEdgeSchemas(sourceType, targetType); if (edgeList == null) { - edgeList = new HashSet<EdgeSchema>(); + edgeList = new HashSet<>(); } - + return edgeList; } - + public void loadSchema(String payload, String version) throws SchemaProviderException { JsonSchema jsonSchema = JsonSchema.fromJson(payload); SchemaInstance schemaInst = new SchemaInstance(); - + for (JsonVertexSchema jsonVertex : jsonSchema.getNodeTypes()) { FromJsonVertexSchema vSchema = new FromJsonVertexSchema(); vSchema.fromJson(jsonVertex, jsonSchema.getDataTypes(), jsonSchema.getCommonProperties()); schemaInst.addVertex(vSchema); } - + for (JsonEdgeSchema jsonEdge : jsonSchema.getRelationshipTypes()) { FromJsonEdgeSchema eSchema = new FromJsonEdgeSchema(); eSchema.fromJson(jsonEdge); schemaInst.addEdge(eSchema); } - + schemaCache.put(version, schemaInst); } - + private synchronized void fetchSchemaVersion(String version) throws SchemaProviderException { if (schemaCache.get(version) != null) { return; } - + String url = config.getSchemaServiceBaseUrl() + "/" + version; - + HttpHeaders headers = new HttpHeaders(); headers.put("X-FromAppId", Arrays.asList(config.getServiceName())); headers.put("X-TransactionId", Arrays.asList(java.util.UUID.randomUUID().toString())); headers.setAccept(Arrays.asList(org.springframework.http.MediaType.APPLICATION_OCTET_STREAM)); - - HttpEntity <String> entity = new HttpEntity<String>(headers); - + + HttpEntity <String> entity = new HttpEntity<>(headers); + ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class); - - + + if (response.getStatusCodeValue() == HttpStatus.NOT_FOUND.value()) { logger.warn(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, "version " + version + " not found"); throw new SchemaProviderException("Schema version " + version + " not found"); @@ -173,7 +169,7 @@ public class JsonSchemaProvider implements SchemaProvider { StringWriter writer = new StringWriter(); PrintWriter printWriter = new PrintWriter(writer); ex.printStackTrace(printWriter); - logger.error(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, "failed to load version " + version + ": " + logger.error(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, "failed to load version " + version + ": " + response.getBody() + "\n" + writer.toString()); throw new SchemaProviderException("Error loading schema version " + version + ":" + ex.getMessage()); @@ -181,7 +177,7 @@ public class JsonSchemaProvider implements SchemaProvider { logger.info(SchemaProviderMsgs.LOADED_SCHEMA_FILE, version); } - + private String unzipAndGetJSONString(ResponseEntity<byte[]> response) throws IOException { StringBuffer sb = new StringBuffer(""); @@ -199,8 +195,10 @@ public class JsonSchemaProvider implements SchemaProvider { } } finally { try { - zipStream.closeEntry(); - zipStream.close(); + if (zipStream != null) { + zipStream.closeEntry(); + zipStream.close(); + } } catch (Exception e) { logger.warn(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, e.toString()); @@ -223,7 +221,7 @@ public class JsonSchemaProvider implements SchemaProvider { throw new SchemaProviderException("Unable to find schema version " + versionToLoad); } } - + return inst; } @@ -231,5 +229,5 @@ public class JsonSchemaProvider implements SchemaProvider { public Map<String, VertexSchema> getVertexMap(String schemaVersion) throws SchemaProviderException { return getSchemaVersion(schemaVersion).getVertexMap(); } - + } diff --git a/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/SecureClientHttpRequestFactory.java b/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/SecureClientHttpRequestFactory.java index 0b48a36c..5be74c39 100644 --- a/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/SecureClientHttpRequestFactory.java +++ b/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/SecureClientHttpRequestFactory.java @@ -45,7 +45,7 @@ public class SecureClientHttpRequestFactory extends SimpleClientHttpRequestFacto private static final String KEYSTORE_ALGORITHM = "SunX509"; private static final String KEYSTORE_TYPE = "PKCS12"; private JsonSchemaProviderConfig config; - + public SecureClientHttpRequestFactory(JsonSchemaProviderConfig config) { super(); @@ -100,18 +100,13 @@ public class SecureClientHttpRequestFactory extends SimpleClientHttpRequestFacto } if (config.getSchemaServiceCertFile() != null) { - FileInputStream fin =null; - try { - fin = new FileInputStream(config.getSchemaServiceCertFile()); - + try (FileInputStream fin = new FileInputStream(config.getSchemaServiceCertFile())) { // Load the keystore and initialize the key manager factory. ks.load(fin, pwd); kmf.init(ks, pwd); - + ctx.init(kmf.getKeyManagers(), trustAllCerts, null); - }finally { - fin.close(); - } + } } else { ctx.init(null, trustAllCerts, null); } |