From f3bf5dd360b93fd7ef6cc8d871a9903de9f27e5a Mon Sep 17 00:00:00 2001 From: "mark.j.leonard" Date: Tue, 26 Jun 2018 11:37:52 +0100 Subject: Fix remaining Sonar code smells Code reformatting to address Sonar code smells, excluding the cyclomatic complexity errors which will require some heavier refactoring. Issue-ID: AAI-1242 Change-Id: I0f2502c8038b1e70f53f3b82b9d4d0761d9137cc Signed-off-by: mark.j.leonard --- .../onap/aai/babel/csar/CsarToXmlConverter.java | 45 +++++++++--------- .../aai/babel/csar/extractor/YamlExtractor.java | 54 ++++++++++++---------- .../service/GenerateArtifactsServiceImpl.java | 2 +- .../org/onap/aai/babel/util/RequestValidator.java | 18 ++++---- .../babel/xml/generator/api/ArtifactGenerator.java | 1 + 5 files changed, 64 insertions(+), 56 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/onap/aai/babel/csar/CsarToXmlConverter.java b/src/main/java/org/onap/aai/babel/csar/CsarToXmlConverter.java index be6ef58..98db8ae 100644 --- a/src/main/java/org/onap/aai/babel/csar/CsarToXmlConverter.java +++ b/src/main/java/org/onap/aai/babel/csar/CsarToXmlConverter.java @@ -1,5 +1,5 @@ /** - * ============LICENSE_START======================================================= + * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. @@ -22,7 +22,6 @@ package org.onap.aai.babel.csar; import java.util.List; import java.util.Objects; - import org.apache.commons.lang3.time.StopWatch; import org.onap.aai.babel.csar.extractor.InvalidArchiveException; import org.onap.aai.babel.csar.extractor.YamlExtractor; @@ -34,47 +33,51 @@ import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException; import org.onap.aai.babel.xml.generator.data.Artifact; /** - * This class is responsible for converting content in a csar archive into one or more xml artifacts. + * This class is responsible for converting CSAR content into one or more XML artifacts. */ public class CsarToXmlConverter { private static final LogHelper logger = LogHelper.INSTANCE; + private final YamlExtractor yamlExtractor; + + public CsarToXmlConverter() { + yamlExtractor = new YamlExtractor(); + } + /** - * This method is responsible for extracting one or more yaml files from the given csarArtifact and then using them - * to generate xml artifacts. + * This method is responsible for generating Artifacts from YAML files within CSAR content. * - * @param csarArchive the artifact that contains the csar archive to generate xml artifacts from - * @param name the name of the archive file - * @param version the version of the archive file - * @return List a list of generated xml artifacts - * @throws CsarConverterException if there is an error either extracting the yaml files or generating xml artifacts + * @param csarArchive + * the artifact that contains the csar archive to generate XML artifacts from + * @param name + * the name of the archive file + * @param version + * the version of the archive file + * @return List a list of generated XML artifacts + * @throws CsarConverterException + * if there is an error either extracting the YAML files or generating XML artifacts */ public List generateXmlFromCsar(byte[] csarArchive, String name, String version) throws CsarConverterException { + validateArguments(csarArchive, name, version); StopWatch stopwatch = new StopWatch(); stopwatch.start(); - validateArguments(csarArchive, name, version); - logger.info(ApplicationMsgs.DISTRIBUTION_EVENT, - "Starting to process csarArchive to convert contents to xml artifacts"); + "Starting to process csarArchive to convert contents to XML artifacts"); List xmlArtifacts; try { - logger.debug("Calling YamlExtractor to extract ymlFiles"); - List ymlFiles = YamlExtractor.extract(csarArchive, name, version); - - logger.debug("Calling XmlArtifactGenerator to generateXmlArtifacts"); + List ymlFiles = yamlExtractor.extract(csarArchive, name, version); xmlArtifacts = new ModelGenerator().generateArtifacts(csarArchive, ymlFiles); - - logger.debug(xmlArtifacts.size() + " xml artifacts have been generated"); + logger.debug(xmlArtifacts.size() + " XML artifact(s) have been generated"); } catch (InvalidArchiveException e) { throw new CsarConverterException( - "An error occurred trying to extract the yml files from the csar file : " + e); + "An error occurred trying to extract the YMAL files from the csar file : " + e); } catch (XmlArtifactGenerationException e) { throw new CsarConverterException( - "An error occurred trying to generate xml files from a collection of yml files : " + e); + "An error occurred trying to generate XML files from a collection of YAML files : " + e); } finally { logger.logMetrics(stopwatch, LogHelper.getCallerMethodName(0)); } diff --git a/src/main/java/org/onap/aai/babel/csar/extractor/YamlExtractor.java b/src/main/java/org/onap/aai/babel/csar/extractor/YamlExtractor.java index 73d364e..15d77a1 100644 --- a/src/main/java/org/onap/aai/babel/csar/extractor/YamlExtractor.java +++ b/src/main/java/org/onap/aai/babel/csar/extractor/YamlExtractor.java @@ -1,5 +1,5 @@ /** - * ============LICENSE_START======================================================= + * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. @@ -37,35 +37,29 @@ import org.onap.aai.babel.xml.generator.data.Artifact; import org.onap.aai.cl.api.Logger; /** - * The purpose of this class is to process a .csar file in the form of a byte array and extract yaml files from it. + * This class extracts YAML files from CSAR (compressed archive) content. * - * A .csar file is a compressed archive like a zip file and this class will treat the byte array as it if were a zip - * file. */ public class YamlExtractor { private static Logger logger = LogHelper.INSTANCE; private static final Pattern YAMLFILE_EXTENSION_REGEX = Pattern.compile("(?i).*\\.ya?ml$"); - /** Private constructor */ - private YamlExtractor() { - throw new IllegalAccessError("Utility class"); - } - /** * This method is responsible for filtering the contents of the supplied archive and returning a collection of - * {@link Artifact}s that represent the yml files that have been found in the archive.
- *
- * If the archive contains no yml files it will return an empty list.
+ * {@link Artifact}s that represent the YAML files that have been found in the archive. * - * @param archive the zip file in the form of a byte array containing one or more yml files - * @param name the name of the archive file - * @param version the version of the archive file - * @return List collection of yml files found in the archive - * @throws InvalidArchiveException if an error occurs trying to extract the yml files from the archive, if the - * archive is not a zip file or there are no yml files + * @param archive + * the compressed archive in the form of a byte array, expected to contain one or more YAML files + * @param name + * the name of the archive + * @param version + * the version of the archive + * @return List<Artifact> collection of YAML artifacts found in the archive + * @throws InvalidArchiveException + * if an error occurs trying to extract the YAML file(s) from the archive, or no files were found */ - public static List extract(byte[] archive, String name, String version) throws InvalidArchiveException { + public List extract(byte[] archive, String name, String version) throws InvalidArchiveException { validateRequest(archive, name, version); logger.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Extracting CSAR archive: " + name); @@ -81,7 +75,7 @@ public class YamlExtractor { } } if (ymlFiles.isEmpty()) { - throw new InvalidArchiveException("No valid yml files were found in the csar file."); + throw new InvalidArchiveException("No valid YAML files were found in the csar file."); } } catch (IOException e) { throw new InvalidArchiveException( @@ -94,7 +88,15 @@ public class YamlExtractor { return ymlFiles; } - private static void validateRequest(byte[] archive, String name, String version) throws InvalidArchiveException { + /** + * Throw an error if the supplied parameters are not valid. + * + * @param archive + * @param name + * @param version + * @throws InvalidArchiveException + */ + private void validateRequest(byte[] archive, String name, String version) throws InvalidArchiveException { if (archive == null || archive.length == 0) { throw new InvalidArchiveException("An archive must be supplied for processing."); } else if (StringUtils.isBlank(name)) { @@ -105,13 +107,15 @@ public class YamlExtractor { } /** + * Determine whether the file name matches the pattern for YAML content. + * * @param entry - * @return + * the entry + * @return true, if successful */ - private static boolean fileShouldBeExtracted(ZipArchiveEntry entry) { + private boolean fileShouldBeExtracted(ZipArchiveEntry entry) { boolean extractFile = YAMLFILE_EXTENSION_REGEX.matcher(entry.getName()).matches(); - logger.debug(ApplicationMsgs.DISTRIBUTION_EVENT, - "Checking if " + entry.getName() + " should be extracted... " + extractFile); + logger.debug(ApplicationMsgs.DISTRIBUTION_EVENT, "Extraction of " + entry.getName() + "=" + extractFile); return extractFile; } } diff --git a/src/main/java/org/onap/aai/babel/service/GenerateArtifactsServiceImpl.java b/src/main/java/org/onap/aai/babel/service/GenerateArtifactsServiceImpl.java index 532d8c7..25499b8 100644 --- a/src/main/java/org/onap/aai/babel/service/GenerateArtifactsServiceImpl.java +++ b/src/main/java/org/onap/aai/babel/service/GenerateArtifactsServiceImpl.java @@ -144,7 +144,7 @@ public class GenerateArtifactsServiceImpl implements GenerateArtifactsService { Gson gson = new GsonBuilder().disableHtmlEscaping().create(); BabelRequest babelRequest = gson.fromJson(requestBody, BabelRequest.class); - RequestValidator.validateRequest(babelRequest); + new RequestValidator().validateRequest(babelRequest); byte[] csarFile = Base64.getDecoder().decode(babelRequest.getCsar()); List babelArtifacts = new CsarToXmlConverter().generateXmlFromCsar(csarFile, diff --git a/src/main/java/org/onap/aai/babel/util/RequestValidator.java b/src/main/java/org/onap/aai/babel/util/RequestValidator.java index c0d2c98..be45e17 100644 --- a/src/main/java/org/onap/aai/babel/util/RequestValidator.java +++ b/src/main/java/org/onap/aai/babel/util/RequestValidator.java @@ -1,5 +1,5 @@ /** - * ============LICENSE_START======================================================= + * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. @@ -22,20 +22,20 @@ package org.onap.aai.babel.util; import org.onap.aai.babel.service.data.BabelRequest; -/** Utility class */ +/** + * Validator for a Babel request. + * + */ public class RequestValidator { - /** Empty constructor */ - private RequestValidator() { - // Prevent instantiation - } - /** * Validates that the request body contains the required attributes * - * @param request the request body to validate + * @param request + * the request body to validate + * @throws RequestValidationException */ - public static void validateRequest(BabelRequest request) throws RequestValidationException { + public void validateRequest(BabelRequest request) throws RequestValidationException { if (request.getCsar() == null) { throw new RequestValidationException("No csar attribute found in the request body."); } diff --git a/src/main/java/org/onap/aai/babel/xml/generator/api/ArtifactGenerator.java b/src/main/java/org/onap/aai/babel/xml/generator/api/ArtifactGenerator.java index 0dc64bf..bb40182 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/api/ArtifactGenerator.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/api/ArtifactGenerator.java @@ -26,6 +26,7 @@ import org.onap.aai.babel.xml.generator.data.Artifact; import org.onap.aai.babel.xml.generator.data.GenerationData; /** Artifact Generation. Note that there is only one implementation of this interface currently. */ +@FunctionalInterface public interface ArtifactGenerator { /** -- cgit 1.2.3-korg