From ecdc4859a0eaf97db959b1064b1060e392fe1bf4 Mon Sep 17 00:00:00 2001 From: "mark.j.leonard" Date: Tue, 27 Nov 2018 17:44:59 +0000 Subject: [PATCH] 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 --- .gitignore | 3 +- .../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 ++++++++--------- .../aai/modelloader/util/ArtifactTestUtils.java | 9 ++ .../aai/modelloader/util/GizmoTranslatorTest.java | 116 --------------------- .../aai/modelloader/util/TestGizmoTranslator.java | 66 ++++++++++++ 13 files changed, 184 insertions(+), 241 deletions(-) delete mode 100644 src/test/java/org/onap/aai/modelloader/util/GizmoTranslatorTest.java create mode 100644 src/test/java/org/onap/aai/modelloader/util/TestGizmoTranslator.java diff --git a/.gitignore b/.gitignore index f402f8c..363b131 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ .settings/ target/ logs/ -debug-logs/ \ No newline at end of file +debug-logs/ +.checkstyle 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; } } diff --git a/src/test/java/org/onap/aai/modelloader/util/ArtifactTestUtils.java b/src/test/java/org/onap/aai/modelloader/util/ArtifactTestUtils.java index 752f389..50074f1 100644 --- a/src/test/java/org/onap/aai/modelloader/util/ArtifactTestUtils.java +++ b/src/test/java/org/onap/aai/modelloader/util/ArtifactTestUtils.java @@ -34,6 +34,15 @@ public class ArtifactTestUtils { ArtifactTestUtils.loadResourceAsString(resource)); } + /** + * Finds the resource with the given name and returns it as an array of bytes. + * + * @param resourceName + * the /-separated path to the resource + * @return the requested resource contents as a byte array + * @throws IOException + * if the resource could not be found (using current privileges) + */ public byte[] loadResource(String resourceName) throws IOException { URL resource = getResource(resourceName); if (resource != null) { diff --git a/src/test/java/org/onap/aai/modelloader/util/GizmoTranslatorTest.java b/src/test/java/org/onap/aai/modelloader/util/GizmoTranslatorTest.java deleted file mode 100644 index 5891931..0000000 --- a/src/test/java/org/onap/aai/modelloader/util/GizmoTranslatorTest.java +++ /dev/null @@ -1,116 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 European Software Marketing Ltd. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.aai.modelloader.util; - -import static org.junit.Assert.assertTrue; - -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.List; - -import org.junit.Test; -import org.onap.aai.modelloader.gizmo.GizmoBulkPayload; -import org.onap.aai.modelloader.gizmo.GizmoEdgeOperation; -import org.onap.aai.modelloader.gizmo.GizmoVertexOperation; - -public class GizmoTranslatorTest { - - @Test - public void translateXmlModel1() throws Exception { - final String XML_MODEL_FILE = "src/test/resources/models/AAI-stellService-service-1.xml"; - - try { - byte[] encoded = Files.readAllBytes(Paths.get(XML_MODEL_FILE)); - String originalXml = new String(encoded); - - String output = GizmoTranslator.translate(originalXml); - System.out.println("Test1 Outgoing:\n" + output); - - GizmoBulkPayload request = GizmoBulkPayload.fromJson(output); - - List ops = request.getVertexOperations(GizmoBulkPayload.ADD_OP); - assertTrue(ops.size() == 5); - - ops = request.getVertexOperations(GizmoBulkPayload.EXISTS_OP); - assertTrue(ops.size() == 3); - - List edgeOps = request.getEdgeOperations(GizmoBulkPayload.ADD_OP); - assertTrue(edgeOps.size() == 7); - } catch (Exception e) { - e.printStackTrace(); - assertTrue(false); - } - } - - @Test - public void translateXmlModel2() throws Exception { - final String XML_MODEL_FILE2 = "src/test/resources/models/l3-network-widget.xml"; - - try { - byte[] encoded = Files.readAllBytes(Paths.get(XML_MODEL_FILE2)); - String originalXml = new String(encoded); - - String output = GizmoTranslator.translate(originalXml); - System.out.println("Test2 Outgoing:\n" + output); - - GizmoBulkPayload request = GizmoBulkPayload.fromJson(output); - - List ops = request.getVertexOperations(GizmoBulkPayload.ADD_OP); - assertTrue(ops.size() == 2); - - ops = request.getVertexOperations(GizmoBulkPayload.EXISTS_OP); - assertTrue(ops.size() == 0); - - List edgeOps = request.getEdgeOperations(GizmoBulkPayload.ADD_OP); - assertTrue(edgeOps.size() == 1); - } catch (Exception e) { - e.printStackTrace(); - assertTrue(false); - } - } - - @Test - public void translateXmlNamedQuery() throws Exception { - final String XML_MODEL_FILE3 = "src/test/resources/models/named-query-wan-connector.xml"; - - try { - byte[] encoded = Files.readAllBytes(Paths.get(XML_MODEL_FILE3)); - String originalXml = new String(encoded); - - String output = GizmoTranslator.translate(originalXml); - System.out.println("Test3 Outgoing:\n" + output); - - GizmoBulkPayload request = GizmoBulkPayload.fromJson(output); - - List ops = request.getVertexOperations(GizmoBulkPayload.ADD_OP); - assertTrue(ops.size() == 5); - - ops = request.getVertexOperations(GizmoBulkPayload.EXISTS_OP); - assertTrue(ops.size() == 4); - - List edgeOps = request.getEdgeOperations(GizmoBulkPayload.ADD_OP); - assertTrue(edgeOps.size() == 8); - } catch (Exception e) { - e.printStackTrace(); - assertTrue(false); - } - } -} diff --git a/src/test/java/org/onap/aai/modelloader/util/TestGizmoTranslator.java b/src/test/java/org/onap/aai/modelloader/util/TestGizmoTranslator.java new file mode 100644 index 0000000..e82af47 --- /dev/null +++ b/src/test/java/org/onap/aai/modelloader/util/TestGizmoTranslator.java @@ -0,0 +1,66 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 European Software Marketing Ltd. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.modelloader.util; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import javax.xml.parsers.ParserConfigurationException; +import org.junit.Test; +import org.onap.aai.modelloader.gizmo.GizmoBulkPayload; +import org.xml.sax.SAXException; + +public class TestGizmoTranslator { + + @Test + public void translateXmlModel1() throws Exception { + GizmoBulkPayload request = createBulkRequest("src/test/resources/models/AAI-stellService-service-1.xml"); + assertThat(request.getVertexOperations(GizmoBulkPayload.ADD_OP).size(), is(5)); + assertThat(request.getVertexOperations(GizmoBulkPayload.EXISTS_OP).size(), is(3)); + assertThat(request.getEdgeOperations(GizmoBulkPayload.ADD_OP).size(), is(7)); + } + + @Test + public void translateXmlModel2() throws Exception { + GizmoBulkPayload request = createBulkRequest("src/test/resources/models/l3-network-widget.xml"); + assertThat(request.getVertexOperations(GizmoBulkPayload.ADD_OP).size(), is(2)); + assertThat(request.getVertexOperations(GizmoBulkPayload.EXISTS_OP).size(), is(0)); + assertThat(request.getEdgeOperations(GizmoBulkPayload.ADD_OP).size(), is(1)); + } + + @Test + public void translateXmlNamedQuery() throws Exception { + GizmoBulkPayload request = createBulkRequest("src/test/resources/models/named-query-wan-connector.xml"); + assertThat(request.getVertexOperations(GizmoBulkPayload.ADD_OP).size(), is(5)); + assertThat(request.getVertexOperations(GizmoBulkPayload.EXISTS_OP).size(), is(4)); + assertThat(request.getEdgeOperations(GizmoBulkPayload.ADD_OP).size(), is(8)); + } + + private GizmoBulkPayload createBulkRequest(String filePath) + throws IOException, ParserConfigurationException, SAXException { + final String xmlPayload = new String(Files.readAllBytes(Paths.get(filePath))); + return GizmoBulkPayload.fromJson(GizmoTranslator.translate(xmlPayload)); + } + +} -- 2.16.6