From ecdc4859a0eaf97db959b1064b1060e392fe1bf4 Mon Sep 17 00:00:00 2001 From: "mark.j.leonard" Date: Tue, 27 Nov 2018 17:44:59 +0000 Subject: Fix Sonar code smells Reformat to ONAP Java standards. Rename GizmoTranslatorTest.java to Test*.java to match the existing naming conventions. Remove duplicated test scaffolding from this file. Change-Id: Ie8d5f7ea113d3d60d6222f66cd6740df7f9afca0 Issue-ID: AAI-1957 Signed-off-by: mark.j.leonard --- .../aai/modelloader/config/ModelLoaderConfig.java | 13 ++- .../entity/model/AbstractModelArtifact.java | 21 +++-- .../modelloader/entity/model/ModelArtifact.java | 2 +- .../entity/model/NamedQueryArtifact.java | 2 +- .../aai/modelloader/gizmo/GizmoBulkPayload.java | 31 ++++--- .../aai/modelloader/gizmo/GizmoEdgeOperation.java | 33 ++++---- .../onap/aai/modelloader/gizmo/GizmoVertex.java | 7 +- .../modelloader/gizmo/GizmoVertexOperation.java | 25 +++--- .../onap/aai/modelloader/util/GizmoTranslator.java | 97 ++++++++++------------ 9 files changed, 107 insertions(+), 124 deletions(-) (limited to 'src/main/java/org/onap') diff --git a/src/main/java/org/onap/aai/modelloader/config/ModelLoaderConfig.java b/src/main/java/org/onap/aai/modelloader/config/ModelLoaderConfig.java index 6a45f28..c81f0dc 100644 --- a/src/main/java/org/onap/aai/modelloader/config/ModelLoaderConfig.java +++ b/src/main/java/org/onap/aai/modelloader/config/ModelLoaderConfig.java @@ -108,8 +108,10 @@ public class ModelLoaderConfig implements IConfiguration { /** * Original constructor * - * @param modelLoaderProperties properties needed to be configured for the model loader - * @param certLocation location of the certificate + * @param modelLoaderProperties + * properties needed to be configured for the model loader + * @param certLocation + * location of the certificate */ public ModelLoaderConfig(Properties modelLoaderProperties, String certLocation) { this.modelLoaderProperties = modelLoaderProperties; @@ -292,12 +294,7 @@ public class ModelLoaderConfig implements IConfiguration { public boolean useGizmo() { String useGizmo = modelLoaderProperties.getProperty(PROP_AAI_USE_GIZMO); - - if ( (useGizmo == null) || (!useGizmo.equalsIgnoreCase("true")) ) { - return false; - } - - return true; + return useGizmo != null && useGizmo.equalsIgnoreCase("true"); } /** diff --git a/src/main/java/org/onap/aai/modelloader/entity/model/AbstractModelArtifact.java b/src/main/java/org/onap/aai/modelloader/entity/model/AbstractModelArtifact.java index f310ae3..c21a285 100644 --- a/src/main/java/org/onap/aai/modelloader/entity/model/AbstractModelArtifact.java +++ b/src/main/java/org/onap/aai/modelloader/entity/model/AbstractModelArtifact.java @@ -23,9 +23,7 @@ package org.onap.aai.modelloader.entity.model; import java.util.HashSet; import java.util.List; import java.util.Set; - import javax.ws.rs.core.MediaType; - import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.modelloader.config.ModelLoaderConfig; @@ -41,7 +39,7 @@ import org.springframework.http.HttpStatus; public abstract class AbstractModelArtifact extends Artifact implements IModelArtifact { private static Logger logger = LoggerFactory.getInstance().getLogger(AbstractModelArtifact.class.getName()); - + private String modelNamespace; private String modelNamespaceVersion; private Set referencedModelIds = new HashSet<>(); @@ -83,24 +81,25 @@ public abstract class AbstractModelArtifact extends Artifact implements IModelAr public abstract void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId); - protected boolean pushToGizmo(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List completedArtifacts) { + protected boolean pushToGizmo(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) { try { String gizmoPayload = GizmoTranslator.translate(getPayload()); - OperationResult postResponse = - aaiClient.postResource(config.getAaiBaseUrl().trim(), gizmoPayload, distId, MediaType.APPLICATION_JSON_TYPE); - + OperationResult postResponse = aaiClient.postResource(config.getAaiBaseUrl().trim(), gizmoPayload, distId, + MediaType.APPLICATION_JSON_TYPE); + if (postResponse.getResultCode() != HttpStatus.OK.value()) { return false; } - + } catch (Exception e) { - logErrorMsg("Ingest failed for " + getType().toString() + " " + getUniqueIdentifier() + ": " + e.getMessage()); + logErrorMsg( + "Ingest failed for " + getType().toString() + " " + getUniqueIdentifier() + ": " + e.getMessage()); return false; } return true; } - + protected void logInfoMsg(String infoMsg) { logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, infoMsg); } @@ -108,7 +107,7 @@ public abstract class AbstractModelArtifact extends Artifact implements IModelAr protected void logErrorMsg(String errorMsg) { logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, errorMsg); } - + @Override public String toString() { StringBuilder sb = new StringBuilder(); diff --git a/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifact.java b/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifact.java index 175f858..6974625 100644 --- a/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifact.java +++ b/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifact.java @@ -120,7 +120,7 @@ public class ModelArtifact extends AbstractModelArtifact { public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List completedArtifacts) { if (config.useGizmo()) { - return pushToGizmo(aaiClient, config, distId, completedArtifacts); + return pushToGizmo(aaiClient, config, distId); } return pushToResources(aaiClient, config, distId, completedArtifacts); diff --git a/src/main/java/org/onap/aai/modelloader/entity/model/NamedQueryArtifact.java b/src/main/java/org/onap/aai/modelloader/entity/model/NamedQueryArtifact.java index ba5d12b..04a17fa 100644 --- a/src/main/java/org/onap/aai/modelloader/entity/model/NamedQueryArtifact.java +++ b/src/main/java/org/onap/aai/modelloader/entity/model/NamedQueryArtifact.java @@ -56,7 +56,7 @@ public class NamedQueryArtifact extends AbstractModelArtifact { @Override public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List completedArtifacts) { if (config.useGizmo()) { - return pushToGizmo(aaiClient, config, distId, completedArtifacts); + return pushToGizmo(aaiClient, config, distId); } return pushToResources(aaiClient, config, distId, completedArtifacts); diff --git a/src/main/java/org/onap/aai/modelloader/gizmo/GizmoBulkPayload.java b/src/main/java/org/onap/aai/modelloader/gizmo/GizmoBulkPayload.java index 11aa35d..bbc1276 100644 --- a/src/main/java/org/onap/aai/modelloader/gizmo/GizmoBulkPayload.java +++ b/src/main/java/org/onap/aai/modelloader/gizmo/GizmoBulkPayload.java @@ -23,7 +23,6 @@ package org.onap.aai.modelloader.gizmo; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; - import java.util.ArrayList; import java.util.List; @@ -38,8 +37,8 @@ public class GizmoBulkPayload { public static final String OP_KEY = "operation"; - private List objects = new ArrayList(); - private List relationships = new ArrayList(); + private List objects = new ArrayList<>(); + private List relationships = new ArrayList<>(); private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create(); @@ -70,42 +69,42 @@ public class GizmoBulkPayload { public List getVertexOperations() { return getVertexOperations(ALL_OPS); } - + public List getVertexOperations(String opType) { - List ops = new ArrayList(); + List ops = new ArrayList<>(); for (JsonElement v : getObjects()) { GizmoVertexOperation op = GizmoVertexOperation.fromJsonElement(v); - - if ( (opType.equalsIgnoreCase(ALL_OPS)) || (op.getOperation().equalsIgnoreCase(opType)) ) { + + if ((opType.equalsIgnoreCase(ALL_OPS)) || (op.getOperation().equalsIgnoreCase(opType))) { ops.add(op); } } - + return ops; } - + public void addVertexOperation(GizmoVertexOperation newOp) { objects.add(newOp.toJsonElement()); } - + public List getEdgeOperations() { return getEdgeOperations(ALL_OPS); } - + public List getEdgeOperations(String opType) { - List ops = new ArrayList(); + List ops = new ArrayList<>(); for (JsonElement v : getRelationships()) { GizmoEdgeOperation op = GizmoEdgeOperation.fromJsonElement(v); - - if ( (opType.equalsIgnoreCase(ALL_OPS)) || (op.getOperation().equalsIgnoreCase(opType)) ) { + + if ((opType.equalsIgnoreCase(ALL_OPS)) || (op.getOperation().equalsIgnoreCase(opType))) { ops.add(op); } } - + return ops; } - + public void addEdgeOperation(GizmoEdgeOperation newOp) { relationships.add(newOp.toJsonElement()); } diff --git a/src/main/java/org/onap/aai/modelloader/gizmo/GizmoEdgeOperation.java b/src/main/java/org/onap/aai/modelloader/gizmo/GizmoEdgeOperation.java index eb578b5..68384c2 100644 --- a/src/main/java/org/onap/aai/modelloader/gizmo/GizmoEdgeOperation.java +++ b/src/main/java/org/onap/aai/modelloader/gizmo/GizmoEdgeOperation.java @@ -20,60 +20,57 @@ */ package org.onap.aai.modelloader.gizmo; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; public class GizmoEdgeOperation { private String operation; private String internalId; private GizmoEdge edge; - + public GizmoEdgeOperation(String op, String id, GizmoEdge edge) { this.operation = op; this.internalId = id; this.edge = edge; } - + public JsonElement toJsonElement() { JsonObject opObj = new JsonObject(); JsonParser parser = new JsonParser(); JsonObject edgeObj = parser.parse(edge.toJson()).getAsJsonObject(); - + opObj.addProperty(GizmoBulkPayload.OP_KEY, operation); opObj.add(internalId, edgeObj); - + return opObj; } - + public static GizmoEdgeOperation fromJsonElement(JsonElement element) { - List> entries = - new ArrayList>(element.getAsJsonObject().entrySet()); - + List> entries = new ArrayList<>(element.getAsJsonObject().entrySet()); + String op = null; String id = null; GizmoEdge edge = null; - + for (Map.Entry entry : entries) { if (entry.getKey().equalsIgnoreCase(GizmoBulkPayload.OP_KEY)) { op = entry.getValue().getAsString(); - } - else { + } else { id = entry.getKey(); edge = GizmoEdge.fromJson(entry.getValue().getAsJsonObject().toString()); } } - + if (op == null) { // Use default op = GizmoBulkPayload.EXISTS_OP; } - - return new GizmoEdgeOperation(op, id, edge); + + return new GizmoEdgeOperation(op, id, edge); } public String getOperation() { diff --git a/src/main/java/org/onap/aai/modelloader/gizmo/GizmoVertex.java b/src/main/java/org/onap/aai/modelloader/gizmo/GizmoVertex.java index 9de94cd..6a6ade8 100644 --- a/src/main/java/org/onap/aai/modelloader/gizmo/GizmoVertex.java +++ b/src/main/java/org/onap/aai/modelloader/gizmo/GizmoVertex.java @@ -20,11 +20,10 @@ */ package org.onap.aai.modelloader.gizmo; -import java.util.HashMap; -import java.util.Map; - import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import java.util.HashMap; +import java.util.Map; public class GizmoVertex { private static final Gson gson = new GsonBuilder().create(); @@ -62,7 +61,7 @@ public class GizmoVertex { public void setProperty(String key, String value) { if (properties == null) { - properties = new HashMap(); + properties = new HashMap<>(); } properties.put(key, value); diff --git a/src/main/java/org/onap/aai/modelloader/gizmo/GizmoVertexOperation.java b/src/main/java/org/onap/aai/modelloader/gizmo/GizmoVertexOperation.java index 3adc284..79e8869 100644 --- a/src/main/java/org/onap/aai/modelloader/gizmo/GizmoVertexOperation.java +++ b/src/main/java/org/onap/aai/modelloader/gizmo/GizmoVertexOperation.java @@ -20,39 +20,37 @@ */ package org.onap.aai.modelloader.gizmo; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; public class GizmoVertexOperation { private String operation; private String internalId; private GizmoVertex vertex; - + public GizmoVertexOperation(String op, String id, GizmoVertex vertex) { this.operation = op; this.internalId = id; this.vertex = vertex; } - + public JsonElement toJsonElement() { JsonObject opObj = new JsonObject(); JsonParser parser = new JsonParser(); JsonObject vertexObj = parser.parse(vertex.toJson()).getAsJsonObject(); - + opObj.addProperty(GizmoBulkPayload.OP_KEY, getOperation()); opObj.add(internalId, vertexObj); - + return opObj; } - + public static GizmoVertexOperation fromJsonElement(JsonElement element) { - List> entries = - new ArrayList>(element.getAsJsonObject().entrySet()); + List> entries = new ArrayList<>(element.getAsJsonObject().entrySet()); String op = null; String id = null; @@ -61,8 +59,7 @@ public class GizmoVertexOperation { for (Map.Entry entry : entries) { if (entry.getKey().equalsIgnoreCase(GizmoBulkPayload.OP_KEY)) { op = entry.getValue().getAsString(); - } - else { + } else { id = entry.getKey(); vertex = GizmoVertex.fromJson(entry.getValue().getAsJsonObject().toString()); } @@ -79,7 +76,7 @@ public class GizmoVertexOperation { public String getOperation() { return operation; } - + public void setOperation(String op) { operation = op; } diff --git a/src/main/java/org/onap/aai/modelloader/util/GizmoTranslator.java b/src/main/java/org/onap/aai/modelloader/util/GizmoTranslator.java index 69e5971..cb08a25 100644 --- a/src/main/java/org/onap/aai/modelloader/util/GizmoTranslator.java +++ b/src/main/java/org/onap/aai/modelloader/util/GizmoTranslator.java @@ -25,11 +25,9 @@ import java.io.StringReader; import java.util.HashSet; import java.util.Map; import java.util.Set; - import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; - import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.modelloader.gizmo.GizmoBulkPayload; @@ -46,7 +44,7 @@ import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class GizmoTranslator { - + private enum NodeType { VERTEX, ATTRIBUTE, @@ -61,20 +59,20 @@ public class GizmoTranslator { NQ_ELEMENT_VERTEX, UNKNOWN } - + private static Logger logger = LoggerFactory.getInstance().getLogger(GizmoTranslator.class.getName()); - + public static String translate(String xmlPayload) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xmlPayload)); Document doc = builder.parse(is); - + GizmoBulkPayload gizmoPayload = new GizmoBulkPayload(); - + processNode(doc.getDocumentElement(), null, null, gizmoPayload); - + return gizmoPayload.toJson(); } @@ -85,9 +83,9 @@ public class GizmoTranslator { Node newParent = null; NodeType nodeType = getNodeType(node); - + switch (nodeType) { - case VERTEX: + case VERTEX: case MODEL_ELEMENT_VERTEX: case NQ_ELEMENT_VERTEX: parentVertexOp = createGizmoVertexOp(node, GizmoBulkPayload.ADD_OP); @@ -105,7 +103,7 @@ public class GizmoTranslator { newParent = parentNode; break; } - + NodeList childNodes = node.getChildNodes(); for (int ix = 0; ix < childNodes.getLength(); ix++) { processNode(childNodes.item(ix), newParent, parentVertexOp, gizmoPayload); @@ -118,54 +116,51 @@ public class GizmoTranslator { logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Unable to resolve relationship"); return; } - + GizmoVertex targetVertex = new GizmoVertex(); targetVertex.setType(relatedToList.item(0).getTextContent().trim()); - + NodeList relationData = relationshipNode.getElementsByTagName("relationship-data"); for (int ix = 0; ix < relationData.getLength(); ix++) { Element relationNode = (Element)relationData.item(ix); NodeList keyList = relationNode.getElementsByTagName("relationship-key"); NodeList valueList = relationNode.getElementsByTagName("relationship-value"); - + if ( (keyList.getLength() != 1) || (valueList.getLength() != 1) ) { logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Unable to resolve relationship. Missing key/value."); return; } - + String[] keyBits = keyList.item(0).getTextContent().trim().split("\\."); String value = valueList.item(0).getTextContent().trim(); - + if (keyBits[0].equalsIgnoreCase(targetVertex.getType())) { targetVertex.setProperty(keyBits[1], value); } } - + gizmoPayload.addVertexOperation(new GizmoVertexOperation(GizmoBulkPayload.EXISTS_OP, getVertexId(targetVertex), targetVertex)); - + GizmoEdge edge = new GizmoEdge(); - + edge.setSource("$" + getVertexId(sourceNode.getVertex())); edge.setTarget("$" + getVertexId(targetVertex)); - + gizmoPayload.addEdgeOperation(new GizmoEdgeOperation(GizmoBulkPayload.ADD_OP, edge.getSource() + "_" + edge.getTarget(), edge)); } private static GizmoEdgeOperation createGizmoEdgeOp(Node node, Node parentNode) { GizmoEdge edge = new GizmoEdge(); - + edge.setSource("$" + getVertexId(createGizmoVertex(node))); edge.setTarget("$" + getVertexId(createGizmoVertex(parentNode))); - - GizmoEdgeOperation edgeOp = new GizmoEdgeOperation(GizmoBulkPayload.ADD_OP, edge.getSource() + "_" + edge.getTarget(), edge); - - return edgeOp; + + return new GizmoEdgeOperation(GizmoBulkPayload.ADD_OP, edge.getSource() + "_" + edge.getTarget(), edge); } private static GizmoVertexOperation createGizmoVertexOp(Node node, String operationType) { - GizmoVertex vertex = createGizmoVertex(node); - GizmoVertexOperation addOp = new GizmoVertexOperation(operationType, getVertexId(vertex), vertex); - return addOp; + GizmoVertex vertex = createGizmoVertex(node); + return new GizmoVertexOperation(operationType, getVertexId(vertex), vertex); } private static String getVertexId(GizmoVertex vertex) { @@ -174,7 +169,7 @@ public class GizmoTranslator { for (Map.Entry entry : vertex.getProperties().entrySet()) { sb.append("-" + entry.getValue()); } - + return sb.toString(); } @@ -189,12 +184,12 @@ public class GizmoTranslator { vertex.setProperty(childNodes.item(ix).getNodeName().trim(), childNodes.item(ix).getTextContent().trim()); } } - + // Special case for model-element, where we need to generate an id field if (getNodeType(node).equals(NodeType.MODEL_ELEMENT_VERTEX)) { vertex.setProperty("model-element-uuid", generateModelElementId((Element)node)); } - + // Special case for nq-element, where we need to generate an id field if (getNodeType(node).equals(NodeType.NQ_ELEMENT_VERTEX)) { vertex.setProperty("named-query-element-uuid", generateModelElementId((Element)node)); @@ -202,11 +197,11 @@ public class GizmoTranslator { return vertex; } - + // Generate a unique hash to store as the id for this node private static String generateModelElementId(Element node) { - Set elemSet = new HashSet(); - + Set elemSet = new HashSet<>(); + NodeList childNodes = node.getElementsByTagName("*"); for (int ix = 0; ix < childNodes.getLength(); ix++) { NodeType nt = getNodeType(childNodes.item(ix)); @@ -214,7 +209,7 @@ public class GizmoTranslator { elemSet.add(childNodes.item(ix).getTextContent().trim()); } } - + return Integer.toString(elemSet.hashCode()); } @@ -222,59 +217,59 @@ public class GizmoTranslator { if (!(node instanceof Element)) { return NodeType.UNKNOWN; } - + if (node.getNodeName().equalsIgnoreCase("relationship-list")) { return NodeType.RELATIONSHIP_LIST; } - + if (node.getNodeName().equalsIgnoreCase("relationship")) { return NodeType.RELATIONSHIP; } - + if (node.getNodeName().equalsIgnoreCase("relationship-data")) { return NodeType.RELATIONSHIP_DATA; } - + if (node.getNodeName().equalsIgnoreCase("related-to")) { return NodeType.RELATED_TO; } - + if (node.getNodeName().equalsIgnoreCase("relationship-key")) { return NodeType.RELATIONSHIP_KEY; } - + if (node.getNodeName().equalsIgnoreCase("relationship-value")) { return NodeType.RELATIONSHIP_VALUE; } - + if (node.getNodeName().equalsIgnoreCase("model-element")) { return NodeType.MODEL_ELEMENT_VERTEX; } - + if (node.getNodeName().equalsIgnoreCase("named-query-element")) { return NodeType.NQ_ELEMENT_VERTEX; } - + NodeList childNodes = node.getChildNodes(); int childElements = countChildElements(childNodes); - + if ( (childElements == 0) && (node.getTextContent() != null) && (!node.getTextContent().trim().isEmpty()) ) { return NodeType.ATTRIBUTE; } - + for (int ix = 0; ix < childNodes.getLength(); ix++) { if (getNodeType(childNodes.item(ix)) == NodeType.ATTRIBUTE) { return NodeType.VERTEX; } } - + if (childElements > 0) { return NodeType.CONTAINER; } - + return NodeType.UNKNOWN; } - + static int countChildElements(NodeList nodes) { int count = 0; for (int ix = 0; ix < nodes.getLength(); ix++) { @@ -282,7 +277,7 @@ public class GizmoTranslator { count++; } } - - return count; + + return count; } } -- cgit 1.2.3-korg