diff options
author | JulienBe <julien.bertozzi@intl.att.com> | 2020-06-18 19:32:33 +0200 |
---|---|---|
committer | Sébastien Determe <sebastien.determe@intl.att.com> | 2020-07-29 12:29:41 +0000 |
commit | 04b7ecf58e76a1364c46a9ef8ac59d730d658dcb (patch) | |
tree | ce58713551c2a8f16e690d860ea1a9992dc08101 /asdctool/src/main/java/org/openecomp | |
parent | 5e8464585f07210ad4b20ddfee2c23b1cf2d8b2a (diff) |
Improve Utils coverage and improve Sonar score
Issue-ID: SDC-3131
Signed-off-by: JulienBe <julien.bertozzi@intl.att.com>
Change-Id: I784a0ad563141dfb546ec1d227b3eb03e08faef3
Diffstat (limited to 'asdctool/src/main/java/org/openecomp')
-rw-r--r-- | asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java | 196 | ||||
-rw-r--r-- | asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportJanusGraphServlet.java | 21 |
2 files changed, 99 insertions, 118 deletions
diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java index 4c52647f65..0f44968e6e 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java @@ -7,9 +7,9 @@ * 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. @@ -34,111 +34,95 @@ import javax.ws.rs.core.Response.ResponseBuilder; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; public class Utils { - private static Logger log = Logger.getLogger(Utils.class.getName()); - - public static final String NEW_LINE = System.getProperty("line.separator"); - - private Utils() { - } - - public static Response buildOkResponse( - /* - * ResponseFormat errorResponseWrapper, - */int status, Object entity, Map<String, String> additionalHeaders) { - ResponseBuilder responseBuilder = Response.status(status); - if (entity != null) { - log.trace("returned entity is {}", entity.toString()); - responseBuilder = responseBuilder.entity(entity); - } - if (additionalHeaders != null) { - for (Entry<String, String> additionalHeader : additionalHeaders.entrySet()) { - String headerName = additionalHeader.getKey(); - String headerValue = additionalHeader.getValue(); - log.trace("Adding header {} with value {} to the response", headerName, headerValue); - responseBuilder.header(headerName, headerValue); - } - } - return responseBuilder.build(); - } - - public static JanusGraph openGraph(Configuration conf) { - - JanusGraph graph = null; - try { - - graph = JanusGraphFactory.open(conf); - - } catch (Exception e) { - log.error("Failed to start open graph", e); - } - - return graph; - - } - - public static boolean vertexLeftContainsRightProps(Map<String, Object> leftProps, Map<String, Object> rightProps) { - - if (rightProps != null) { - - for (Entry<String, Object> entry : rightProps.entrySet()) { - String key = entry.getKey(); - Object leftValue = leftProps.get(key); - Object rightValue = entry.getValue(); - if (leftValue == null) { - if (rightValue == null) { - continue; - } else { - log.debug("The key {} cannot be found in the properties {}",key,leftProps); - return false; - } - } - - if (!leftValue.equals(rightValue)) { - log.trace("The value of key {} is differnet between properties. {} vs {}",key,leftValue,rightValue); - return false; - } - } - - } - - return true; - } - - public static void setProperties(Element element, Map<String, Object> properties) { - - if (properties != null && !properties.isEmpty()) { - - Object[] propertyKeyValues = new Object[properties.size() * 2]; - int i = 0; - for (Entry<String, Object> entry : properties.entrySet()) { - propertyKeyValues[i++] = entry.getKey(); - propertyKeyValues[i++] = entry.getValue(); - } - - ElementHelper.attachProperties(element, propertyKeyValues); - - } - - } - - public static Map<String, Object> getProperties(Element element) { - - Map<String, Object> result = new HashMap<>(); - - if (CollectionUtils.isNotEmpty(element.keys())) { - Map<String, Property> propertyMap = ElementHelper.propertyMap(element, - element.keys().toArray(new String[element.keys().size()])); - - for (Entry<String, Property> entry : propertyMap.entrySet()) { - String key = entry.getKey(); - Object value = entry.getValue().value(); - - result.put(key, value); - } - } - return result; - } + private static final Logger log = Logger.getLogger(Utils.class.getName()); + + public static final String NEW_LINE = System.getProperty("line.separator"); + + private Utils() { + } + + /** + * ResponseFormat errorResponseWrapper, + */ + public static Response buildOkResponse(int status, Object entity, Map<String, String> additionalHeaders) { + ResponseBuilder responseBuilder = Response.status(status); + if (entity != null) { + log.trace("returned entity is {}", entity.toString()); + responseBuilder = responseBuilder.entity(entity); + } + if (additionalHeaders != null) { + for (Entry<String, String> additionalHeader : additionalHeaders.entrySet()) { + String headerName = additionalHeader.getKey(); + String headerValue = additionalHeader.getValue(); + log.trace("Adding header {} with value {} to the response", headerName, headerValue); + responseBuilder.header(headerName, headerValue); + } + } + return responseBuilder.build(); + } + + public static Optional<JanusGraph> openGraph(Configuration conf) { + try { + return Optional.ofNullable(JanusGraphFactory.open(conf)); + } catch (Exception e) { + log.error("Failed to start open graph", e); + } + return Optional.empty(); + } + + public static boolean vertexLeftContainsRightProps(Map<String, Object> leftProps, Map<String, Object> rightProps) { + if (rightProps == null) + return true; + for (Entry<String, Object> entry : rightProps.entrySet()) { + String key = entry.getKey(); + Object leftValue = leftProps.get(key); + Object rightValue = entry.getValue(); + if (leftValue == null) { + if (rightValue == null) { + continue; + } else { + log.debug("The key {} cannot be found in the properties {}", key, leftProps); + return false; + } + } + if (!leftValue.equals(rightValue)) { + log.trace("The value of key {} is differnet between properties. {} vs {}", key, leftValue, rightValue); + return false; + } + } + return true; + } + + public static void setProperties(Element element, Map<String, Object> properties) { + if (properties != null && !properties.isEmpty()) { + Object[] propertyKeyValues = new Object[properties.size() * 2]; + int i = 0; + for (Entry<String, Object> entry : properties.entrySet()) { + propertyKeyValues[i++] = entry.getKey(); + propertyKeyValues[i++] = entry.getValue(); + } + ElementHelper.attachProperties(element, propertyKeyValues); + } + } + + public static Map<String, Object> getProperties(Element element) { + Map<String, Object> result = new HashMap<>(); + + if (element != null && CollectionUtils.isNotEmpty(element.keys())) { + Map<String, Property> propertyMap = ElementHelper.propertyMap(element, + element.keys().toArray(new String[element.keys().size()])); + + for (Entry<String, Property> entry : propertyMap.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue().value(); + + result.put(key, value); + } + } + return result; + } } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportJanusGraphServlet.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportJanusGraphServlet.java index 412926fa6c..4e9428d397 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportJanusGraphServlet.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportJanusGraphServlet.java @@ -42,8 +42,8 @@ import java.io.FileReader; import java.io.IOException; import java.io.OutputStream; import java.util.Map.Entry; +import java.util.Optional; import java.util.Properties; -//import com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter; @Path("/janusgraph") public class ExportImportJanusGraphServlet { @@ -76,18 +76,15 @@ public class ExportImportJanusGraphServlet { conf.setProperty("storage.machine-id-appendix", System.currentTimeMillis() % 1000); - try(JanusGraph openGraph = Utils.openGraph(conf)){ - - if (openGraph == null) { - Response buildErrorResponse = Utils.buildOkResponse(500, "failed to open graph", null); - return buildErrorResponse; + Optional<JanusGraph> openGraph = Utils.openGraph(conf); + if (openGraph.isPresent()) { + try { + return Utils.buildOkResponse(200, "ok man", null); + } finally { + openGraph.get().close(); } - - // Open JanusGraph Graph - - Response buildOkResponse = Utils.buildOkResponse(200, "ok man", null); - - return buildOkResponse; + } else { + return Utils.buildOkResponse(500, "failed to open graph", null); } } |