From 04b7ecf58e76a1364c46a9ef8ac59d730d658dcb Mon Sep 17 00:00:00 2001 From: JulienBe Date: Thu, 18 Jun 2020 19:32:33 +0200 Subject: Improve Utils coverage and improve Sonar score Issue-ID: SDC-3131 Signed-off-by: JulienBe Change-Id: I784a0ad563141dfb546ec1d227b3eb03e08faef3 --- .../java/org/openecomp/sdc/asdctool/Utils.java | 196 ++++++++++----------- .../servlets/ExportImportJanusGraphServlet.java | 21 +-- .../java/org/openecomp/sdc/asdctool/UtilsTest.java | 150 +++++++--------- 3 files changed, 165 insertions(+), 202 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 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 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 leftProps, Map rightProps) { - - if (rightProps != null) { - - for (Entry 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 properties) { - - if (properties != null && !properties.isEmpty()) { - - Object[] propertyKeyValues = new Object[properties.size() * 2]; - int i = 0; - for (Entry entry : properties.entrySet()) { - propertyKeyValues[i++] = entry.getKey(); - propertyKeyValues[i++] = entry.getValue(); - } - - ElementHelper.attachProperties(element, propertyKeyValues); - - } - - } - - public static Map getProperties(Element element) { - - Map result = new HashMap<>(); - - if (CollectionUtils.isNotEmpty(element.keys())) { - Map propertyMap = ElementHelper.propertyMap(element, - element.keys().toArray(new String[element.keys().size()])); - - for (Entry 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 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 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 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 leftProps, Map rightProps) { + if (rightProps == null) + return true; + for (Entry 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 properties) { + if (properties != null && !properties.isEmpty()) { + Object[] propertyKeyValues = new Object[properties.size() * 2]; + int i = 0; + for (Entry entry : properties.entrySet()) { + propertyKeyValues[i++] = entry.getKey(); + propertyKeyValues[i++] = entry.getValue(); + } + ElementHelper.attachProperties(element, propertyKeyValues); + } + } + + public static Map getProperties(Element element) { + Map result = new HashMap<>(); + + if (element != null && CollectionUtils.isNotEmpty(element.keys())) { + Map propertyMap = ElementHelper.propertyMap(element, + element.keys().toArray(new String[element.keys().size()])); + + for (Entry 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 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); } } diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/UtilsTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/UtilsTest.java index 8ea53003e9..636bc54c95 100644 --- a/asdctool/src/test/java/org/openecomp/sdc/asdctool/UtilsTest.java +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/UtilsTest.java @@ -20,96 +20,78 @@ package org.openecomp.sdc.asdctool; -import org.apache.commons.configuration.Configuration; import org.apache.tinkerpop.gremlin.structure.Element; -import org.janusgraph.core.JanusGraph; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + import javax.ws.rs.core.Response; import java.util.HashMap; import java.util.Map; -public class UtilsTest { - - @Test - public void testBuildOkResponse() throws Exception { - int status = 200; - Object entity = null; - Map additionalHeaders = null; - Response result; - - // test with mock entity - Object mockEntity = new Object(); - result = Utils.buildOkResponse(status, entity, additionalHeaders); - Assert.assertNotNull(result); - - // test with mock headers - Map mockAdditionalHeaders = new HashMap<>(); - mockAdditionalHeaders.put("stam", "stam"); - result = Utils.buildOkResponse(status, mockEntity, mockAdditionalHeaders); - Assert.assertNotNull(result); - } - - @Test - public void testOpenGraph() throws Exception { - Configuration conf = null; - JanusGraph result; - - // default test with null - result = Utils.openGraph(conf); - } +import static org.junit.jupiter.api.Assertions.*; - @Test - public void testVertexLeftContainsRightProps() throws Exception { - Map leftProps = new HashMap<>(); - Map rightProps = null; - boolean result; - - // test 1 with null - rightProps = null; - result = Utils.vertexLeftContainsRightProps(leftProps, rightProps); - Assert.assertEquals(true, result); - - // test 2 with mocks - Map mockLeftProps = new HashMap<>(); - mockLeftProps.put("stam", new Object()); - Map mockRightProps = new HashMap<>(); - mockRightProps.put("stam", new Object()); - result = Utils.vertexLeftContainsRightProps(mockLeftProps, mockRightProps); - Assert.assertEquals(false, result); - - // test 3 with mocks - Object mockObject = new Object(); - mockLeftProps = new HashMap<>(); - mockLeftProps.put("stam", mockObject); - mockRightProps = new HashMap<>(); - mockRightProps.put("stam", mockObject); - result = Utils.vertexLeftContainsRightProps(mockLeftProps, mockRightProps); - Assert.assertEquals(true, result); - } - - @Test(expected=IllegalArgumentException.class) - public void testSetProperties() throws Exception { - Element element = null; - Map properties = null; - - // test 1 - properties = null; - Utils.setProperties(element, properties); - - // test 2 - properties = new HashMap<>(); - properties.put("stam", new Object()); - Utils.setProperties(element, properties); - } - - @Test(expected=NullPointerException.class) - public void testGetProperties() throws Exception { - Element element = null; - Map result; +public class UtilsTest { - // default test - result = Utils.getProperties(element); - } + @Test + public void testBuildOkResponse() { + int status = 200; + Response result; + + Response responseToNulls = Utils.buildOkResponse(105, null, null); + assertNotNull(responseToNulls); + assertEquals(105, responseToNulls.getStatus()); + assertFalse(responseToNulls.hasEntity()); + + // test with mock headers + Map mockAdditionalHeaders = new HashMap<>(); + mockAdditionalHeaders.put("stam", "stam"); + result = Utils.buildOkResponse(status, "entity", mockAdditionalHeaders); + assertNotNull(result); + assertEquals(200, result.getStatus()); + assertTrue(result.hasEntity()); + } + + @Test + public void testOpenGraph() { + assertNotNull(Utils.openGraph(null)); + } + + @Test + public void testVertexLeftContainsRightProps() { + assertTrue(Utils.vertexLeftContainsRightProps(new HashMap<>(), null)); + assertTrue(Utils.vertexLeftContainsRightProps(null, null)); + assertTrue(Utils.vertexLeftContainsRightProps(null, new HashMap<>())); + + // test 2 with mocks + Map mockLeftProps = new HashMap<>(); + mockLeftProps.put("stam", new Object()); + Map mockRightProps = new HashMap<>(); + mockRightProps.put("stam", new Object()); + assertFalse(Utils.vertexLeftContainsRightProps(mockLeftProps, mockRightProps)); + + // test 3 with mocks + Object mockObject = new Object(); + mockLeftProps.put("stam", mockObject); + mockRightProps.put("stam", mockObject); + assertTrue(Utils.vertexLeftContainsRightProps(mockLeftProps, mockRightProps)); + mockLeftProps.put("stam", null); + assertFalse(Utils.vertexLeftContainsRightProps(mockLeftProps, mockRightProps)); + mockLeftProps.put("stam", "Woops"); + assertFalse(Utils.vertexLeftContainsRightProps(mockLeftProps, mockRightProps)); + } + + @Test + public void testSetProperties() { + assertDoesNotThrow(() -> Utils.setProperties(null, null)); + assertDoesNotThrow(() -> Utils.setProperties(Mockito.mock(Element.class), null)); + Map properties = new HashMap<>(); + properties.put("stam", new Object()); + assertThrows(IllegalArgumentException.class, () -> Utils.setProperties(null, properties)); + } + + @Test + public void testGetProperties() { + assertDoesNotThrow(() -> Utils.getProperties(null)); + } } -- cgit 1.2.3-korg