diff options
Diffstat (limited to 'catalog-be/src')
6 files changed, 726 insertions, 192 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java index 361f6c610f..428dd1a751 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java @@ -32,14 +32,16 @@ import org.apache.commons.lang3.tuple.ImmutablePair; import org.elasticsearch.common.Strings; import org.openecomp.sdc.be.components.ArtifactsResolver; import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum; +import org.openecomp.sdc.be.components.impl.artifact.ArtifactTypeToPayloadTypeSelector; +import org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum; import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum; +import org.openecomp.sdc.be.components.utils.InterfaceOperationUtils; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.config.Configuration.ArtifactTypeConfig; import org.openecomp.sdc.be.config.ConfigurationManager; -import org.openecomp.sdc.be.config.validation.DeploymentArtifactHeatConfiguration; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao; import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus; @@ -52,11 +54,28 @@ import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.info.ArtifactTemplateInfo; -import org.openecomp.sdc.be.model.*; +import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.ArtifactType; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.GroupDefinition; +import org.openecomp.sdc.be.model.GroupInstance; +import org.openecomp.sdc.be.model.HeatParameterDefinition; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.model.LifeCycleTransitionEnum; +import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.Operation; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.heat.HeatParameterType; import org.openecomp.sdc.be.model.jsontitan.operations.NodeTemplateOperation; -import org.openecomp.sdc.be.components.utils.InterfaceOperationUtils; -import org.openecomp.sdc.be.model.operations.api.*; +import org.openecomp.sdc.be.model.operations.api.IElementOperation; +import org.openecomp.sdc.be.model.operations.api.IHeatParametersOperation; +import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; +import org.openecomp.sdc.be.model.operations.api.IUserAdminOperation; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; import org.openecomp.sdc.be.resources.data.ComponentMetadataData; @@ -82,9 +101,12 @@ import org.openecomp.sdc.common.util.GeneralUtility; import org.openecomp.sdc.common.util.ValidationUtils; import org.openecomp.sdc.common.util.YamlToObjectConverter; import org.openecomp.sdc.exception.ResponseFormat; - import org.springframework.beans.factory.annotation.Autowired; -import org.xml.sax.*; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; +import org.xml.sax.XMLReader; import org.yaml.snakeyaml.Yaml; import javax.servlet.http.HttpServletRequest; @@ -94,8 +116,17 @@ import javax.xml.parsers.SAXParserFactory; import java.io.ByteArrayInputStream; import java.io.IOException; import java.math.BigDecimal; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; import java.util.function.Predicate; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -2406,18 +2437,6 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { } } - private boolean isValidJson(byte[] jsonToParse) { - String parsed = new String(jsonToParse); - try { - gson.fromJson(parsed, Object.class); - } - catch (Exception e) { - log.debug("Json is invalid : {}", e.getMessage(), e); - return false; - } - return true; - } - private void validateSingleDeploymentArtifactName(Wrapper<ResponseFormat> errorWrapper, String artifactName, Component parentComponent, NodeTypeEnum parentType) { boolean artifactNameFound = false; Iterator<ArtifactDefinition> parentDeploymentArtifactsItr = getDeploymentArtifacts(parentComponent, parentType, null) @@ -3143,65 +3162,25 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { log.trace("Calculated checksum, base64 payload: {}, checksum: {}", payload, checkSum); // Specific payload validations of different types - Either<Boolean, ResponseFormat> isValidPayload = Either.left(true); + Either<Boolean, ResponseFormat> result = Either.left(true); if (isDeploymentArtifact(artifactInfo)) { log.trace("Starting deployment artifacts payload validation"); String artifactType = artifactInfo.getArtifactType(); - if (ArtifactTypeEnum.HEAT.getType() - .equalsIgnoreCase(artifactType) || ArtifactTypeEnum.HEAT_VOL.getType() - .equalsIgnoreCase(artifactType) || ArtifactTypeEnum.HEAT_NET - .getType() - .equalsIgnoreCase(artifactType) - || ArtifactTypeEnum.HEAT_ENV.getType().equalsIgnoreCase(artifactType)) { - isValidPayload = validateDeploymentHeatPayload(decodedPayload, artifactType); - if (isValidPayload.isLeft()) { - isValidPayload = extractHeatParameters(artifactInfo); - } - } - else if (ArtifactTypeEnum.YANG_XML.getType() - .equalsIgnoreCase(artifactType) || ArtifactTypeEnum.VNF_CATALOG.getType() - .equalsIgnoreCase(artifactType) || ArtifactTypeEnum.VF_LICENSE - .getType() - .equalsIgnoreCase(artifactType) - || ArtifactTypeEnum.VENDOR_LICENSE.getType() - .equalsIgnoreCase(artifactType) || ArtifactTypeEnum.MODEL_INVENTORY_PROFILE - .getType() - .equalsIgnoreCase(artifactType) - || ArtifactTypeEnum.MODEL_QUERY_SPEC.getType() - .equalsIgnoreCase(artifactType) || ArtifactTypeEnum.UCPE_LAYER_2_CONFIGURATION - .getType() - .equalsIgnoreCase(artifactType)) { - isValidPayload = validateXmlPayload(decodedPayload, artifactType); + String fileExtension = GeneralUtility.getFilenameExtension(artifactInfo.getArtifactName()); + PayloadTypeEnum payloadType = ArtifactTypeToPayloadTypeSelector.getPayloadType(artifactType, fileExtension); + Either<Boolean, ActionStatus> isPayloadValid = payloadType.isValid(decodedPayload); + if (isPayloadValid.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(isPayloadValid.right().value(), artifactType); + return Either.right(responseFormat); } - else if (ArtifactTypeEnum.DCAE_INVENTORY_JSON.getType() - .equalsIgnoreCase(artifactType) || ArtifactTypeEnum.DCAE_INVENTORY_TOSCA - .getType() - .equalsIgnoreCase(artifactType) - || ArtifactTypeEnum.VES_EVENTS.getType() - .equalsIgnoreCase(artifactType) || ArtifactTypeEnum.LIFECYCLE_OPERATIONS - .getType() - .equalsIgnoreCase(artifactType)) { - String artifactFileName = artifactInfo.getArtifactName(); - String fileExtension = GeneralUtility.getFilenameExtension(artifactFileName).toLowerCase(); - switch (fileExtension) { - case "xml": - isValidPayload = validateXmlPayload(decodedPayload, artifactType); - break; - case "json": - isValidPayload = validateJsonPayload(decodedPayload, artifactType); - break; - case "yml": - case "yaml": - isValidPayload = validateYmlPayload(decodedPayload, artifactType); - break; - default: - break; - } + + if (payloadType.isHeatRelated()) { + log.trace("Payload is heat related so going to extract heat parameters for artifact type {}", artifactType); + result = extractHeatParameters(artifactInfo); } } - if (isValidPayload.isRight()) { - ResponseFormat responseFormat = isValidPayload.right().value(); - return Either.right(responseFormat); + if (result.isRight()) { + return Either.right(result.right().value()); } } // null/empty payload is normal if called from metadata update ONLY. @@ -3218,61 +3197,6 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { return Either.left(decodedPayload); } - private Either<Boolean, ResponseFormat> validateDeploymentHeatPayload(byte[] decodedPayload, String artifactType) { - // Basic YAML validation - YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); - if (!yamlToObjectConverter.isValidYaml(decodedPayload)) { - log.debug("Invalid YAML format"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_YAML, artifactType); - return Either.right(responseFormat); - } - if (!ArtifactTypeEnum.HEAT_ENV.getType().equalsIgnoreCase(artifactType)) { - // HEAT specific YAML validation - DeploymentArtifactHeatConfiguration heatConfiguration = yamlToObjectConverter.convert(decodedPayload, DeploymentArtifactHeatConfiguration.class); - if (heatConfiguration == null || heatConfiguration.getHeat_template_version() == null) { - log.debug("HEAT doesn't contain required \"heat_template_version\" section."); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_DEPLOYMENT_ARTIFACT_HEAT, artifactType); - return Either.right(responseFormat); - } - } - - return Either.left(true); - } - - private Either<Boolean, ResponseFormat> validateYmlPayload(byte[] decodedPayload, String artifactType) { - Either<Boolean, ResponseFormat> res = Either.left(true); - YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); - if (!yamlToObjectConverter.isValidYaml(decodedPayload)) { - log.debug("Invalid YAML format"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_YAML, artifactType); - res = Either.right(responseFormat); - } - - return res; - } - - private Either<Boolean, ResponseFormat> validateXmlPayload(byte[] payload, String artifactType) { - boolean isXmlValid = isValidXml(payload); - if (!isXmlValid) { - ResponseFormat responseFormat = ResponseFormatManager.getInstance() - .getResponseFormat(ActionStatus.INVALID_XML, artifactType); - log.debug("Invalid XML content"); - return Either.right(responseFormat); - } - return Either.left(true); - } - - private Either<Boolean, ResponseFormat> validateJsonPayload(byte[] payload, String type) { - boolean isJsonValid = isValidJson(payload); - if (!isJsonValid) { - ResponseFormat responseFormat = ResponseFormatManager.getInstance() - .getResponseFormat(ActionStatus.INVALID_JSON, type); - log.debug("Invalid JSON content"); - return Either.right(responseFormat); - } - return Either.left(true); - } - public Either<Operation, ResponseFormat> deleteArtifactByInterface(String resourceId, String userUserId, String artifactId, boolean inTransaction) { User user = new User(); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/ArtifactTypeToPayloadTypeSelector.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/ArtifactTypeToPayloadTypeSelector.java new file mode 100644 index 0000000000..3e3b9e260f --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/ArtifactTypeToPayloadTypeSelector.java @@ -0,0 +1,163 @@ +/* + * - + * * ============LICENSE_START======================================================= + * * Copyright (C) 2019 Nordix Foundation. + * * ================================================================================ + * * 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. + * * + * * SPDX-License-Identifier: Apache-2.0 + * * ============LICENSE_END========================================================= + * + */ + +package org.openecomp.sdc.be.components.impl.artifact; + +import org.openecomp.sdc.common.api.ArtifactTypeEnum; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +import static org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum.HEAT_YAML; +import static org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum.NOT_DEFINED; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.DCAE_INVENTORY_JSON; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.DCAE_INVENTORY_TOSCA; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.HEAT; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.HEAT_ENV; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.HEAT_NET; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.HEAT_VOL; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.LIFECYCLE_OPERATIONS; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.MODEL_INVENTORY_PROFILE; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.MODEL_QUERY_SPEC; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.PM_DICTIONARY; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.UCPE_LAYER_2_CONFIGURATION; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.VENDOR_LICENSE; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.VES_EVENTS; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.VF_LICENSE; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.VNF_CATALOG; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.YANG_XML; + +public class ArtifactTypeToPayloadTypeSelector { + + private static final Map<ArtifactTypeWithExtension, PayloadTypeEnum> artifactTypeWithExtension2PayloadType = new HashMap<>(); + private static final Map<String, PayloadTypeEnum> artifactType2PayloadType = new HashMap<>(); + private static final String XML = "xml"; + private static final String JSON = "json"; + private static final String YML = "yml"; + private static final String YAML = "yaml"; + + static { + populateArtifactTypeWithExtensionMap(); + populateArtifactsTypeOnlyMap(); + } + + private static void populateArtifactTypeWithExtensionMap() { + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(DCAE_INVENTORY_JSON, XML), PayloadTypeEnum.XML); + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(DCAE_INVENTORY_JSON, JSON), PayloadTypeEnum.JSON); + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(DCAE_INVENTORY_JSON, YML), PayloadTypeEnum.YAML); + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(DCAE_INVENTORY_JSON, YAML), PayloadTypeEnum.YAML); + + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(DCAE_INVENTORY_TOSCA, XML), PayloadTypeEnum.XML); + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(DCAE_INVENTORY_TOSCA, JSON), PayloadTypeEnum.JSON); + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(DCAE_INVENTORY_TOSCA, YML), PayloadTypeEnum.YAML); + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(DCAE_INVENTORY_TOSCA, YAML), PayloadTypeEnum.YAML); + + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(VES_EVENTS, XML), PayloadTypeEnum.XML); + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(VES_EVENTS, JSON), PayloadTypeEnum.JSON); + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(VES_EVENTS, YML), PayloadTypeEnum.YAML); + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(VES_EVENTS, YAML), PayloadTypeEnum.YAML); + + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(LIFECYCLE_OPERATIONS, XML), PayloadTypeEnum.XML); + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(LIFECYCLE_OPERATIONS, JSON), PayloadTypeEnum.JSON); + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(LIFECYCLE_OPERATIONS, YML), PayloadTypeEnum.YAML); + artifactTypeWithExtension2PayloadType.put(createArtifactTypeWithExtension(LIFECYCLE_OPERATIONS, YAML), PayloadTypeEnum.YAML); + } + + private static void populateArtifactsTypeOnlyMap() { + artifactType2PayloadType.put(HEAT.getType().toLowerCase(), HEAT_YAML); + artifactType2PayloadType.put(HEAT_VOL.getType().toLowerCase(), HEAT_YAML); + artifactType2PayloadType.put(HEAT_NET.getType().toLowerCase(), HEAT_YAML); + artifactType2PayloadType.put(HEAT_ENV.getType().toLowerCase(), PayloadTypeEnum.HEAT_ENV); + + artifactType2PayloadType.put(YANG_XML.getType().toLowerCase(), PayloadTypeEnum.XML); + artifactType2PayloadType.put(VNF_CATALOG.getType().toLowerCase(), PayloadTypeEnum.XML); + artifactType2PayloadType.put(VF_LICENSE.getType().toLowerCase(), PayloadTypeEnum.XML); + artifactType2PayloadType.put(VENDOR_LICENSE.getType().toLowerCase(), PayloadTypeEnum.XML); + artifactType2PayloadType.put(MODEL_INVENTORY_PROFILE.getType().toLowerCase(), PayloadTypeEnum.XML); + artifactType2PayloadType.put(MODEL_QUERY_SPEC.getType().toLowerCase(), PayloadTypeEnum.XML); + artifactType2PayloadType.put(UCPE_LAYER_2_CONFIGURATION.getType().toLowerCase(), PayloadTypeEnum.XML); + artifactType2PayloadType.put(PM_DICTIONARY.getType().toLowerCase(), PayloadTypeEnum.YAML); + } + + private static ArtifactTypeWithExtension createArtifactTypeWithExtension(ArtifactTypeEnum artifactTypeEnum, String extension) { + return createArtifactTypeWithExtension(artifactTypeEnum.getType(), extension); + } + + private static ArtifactTypeWithExtension createArtifactTypeWithExtension(String artifactType, String extension) { + return new ArtifactTypeToPayloadTypeSelector().new ArtifactTypeWithExtension(artifactType.toLowerCase(), extension.toLowerCase()); + } + + public static PayloadTypeEnum getPayloadType(String artifactType, String fileExtension) { + PayloadTypeEnum payloadType = artifactTypeWithExtension2PayloadType.get(createArtifactTypeWithExtension(artifactType, fileExtension)); + payloadType = payloadType != null ? payloadType : artifactType2PayloadType.get(artifactType.toLowerCase()); + return payloadType != null ? payloadType : NOT_DEFINED; + } + + private class ArtifactTypeWithExtension { + private String artifactType; + private String fileExtension; + + public ArtifactTypeWithExtension(String artifactType, String fileExtension) { + this.artifactType = artifactType; + this.fileExtension = fileExtension; + } + + public String getArtifactType() { + return artifactType; + } + + public String getFileExtension() { + return fileExtension; + } + + @Override + public boolean equals(Object other) { + if (!(other instanceof ArtifactTypeWithExtension)) { + return false; + } + ArtifactTypeWithExtension otherArtifactTypeWithExtension = (ArtifactTypeWithExtension) other; + + return isArtifactTypeEqual(otherArtifactTypeWithExtension.getArtifactType()) && + isFileExtensionEqual(otherArtifactTypeWithExtension.getFileExtension()); + } + + @Override + public int hashCode() { + return Objects.hash(artifactType, fileExtension); + } + + private boolean isArtifactTypeEqual(String otherArtifactType) { + if (artifactType == null) { + return otherArtifactType == null; + } + return artifactType.equalsIgnoreCase(otherArtifactType); + } + + private boolean isFileExtensionEqual(String otherFileExtension) { + if (fileExtension == null) { + return otherFileExtension == null; + } + return fileExtension.equalsIgnoreCase(otherFileExtension); + } + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnum.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnum.java new file mode 100644 index 0000000000..57afb8743c --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnum.java @@ -0,0 +1,149 @@ +/* + * - + * * ============LICENSE_START======================================================= + * * Copyright (C) 2019 Nordix Foundation. + * * ================================================================================ + * * 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. + * * + * * SPDX-License-Identifier: Apache-2.0 + * * ============LICENSE_END========================================================= + * + */ + +package org.openecomp.sdc.be.components.impl.artifact; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import fj.data.Either; +import org.openecomp.sdc.be.config.validation.DeploymentArtifactHeatConfiguration; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.common.log.wrappers.Logger; +import org.openecomp.sdc.common.util.YamlToObjectConverter; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; +import org.xml.sax.XMLReader; + +import javax.xml.XMLConstants; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParserFactory; +import java.io.ByteArrayInputStream; +import java.io.IOException; + +public enum PayloadTypeEnum { + HEAT_YAML { + @Override + public Either<Boolean, ActionStatus> isValid(byte[] payload) { + YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); + if (isNotValidYaml(payload, yamlToObjectConverter)) { + return Either.right(ActionStatus.INVALID_YAML); + } + + DeploymentArtifactHeatConfiguration heatConfiguration = + yamlToObjectConverter.convert(payload, DeploymentArtifactHeatConfiguration.class); + if (heatConfiguration == null || heatConfiguration.getHeat_template_version() == null) { + log.debug("HEAT doesn't contain required \"heat_template_version\" section."); + return Either.right(ActionStatus.INVALID_DEPLOYMENT_ARTIFACT_HEAT); + } + return Either.left(true); + } + + @Override + public boolean isHeatRelated() { + return true; + } + + private boolean isNotValidYaml(byte[] payload, YamlToObjectConverter yamlToObjectConverter) { + return !yamlToObjectConverter.isValidYaml(payload); + } + }, + HEAT_ENV { + @Override + public Either<Boolean, ActionStatus> isValid(byte[] payload) { + return isValidYaml(payload); + } + + @Override + public boolean isHeatRelated() { + return true; + } + }, + XML { + @Override + public Either<Boolean, ActionStatus> isValid(byte[] payload) { + try { + XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); + setFeatures(reader); + reader.parse(new InputSource(new ByteArrayInputStream(payload))); + } catch (ParserConfigurationException | IOException | SAXException exception) { + log.debug("Xml is invalid : {}", exception.getMessage(), exception); + return Either.right(ActionStatus.INVALID_XML); + } + return Either.left(true); + } + + private void setFeatures(XMLReader reader) throws SAXNotSupportedException { + try { + reader.setFeature("http://apache.org/xml/features/validation/schema", false); + reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + } catch (SAXNotRecognizedException exception) { + log.debug("Xml parser couldn't set feature: \"http://apache.org/xml/features/validation/schema\", false", + exception.getMessage(), exception); + } + } + }, + JSON { + @Override + public Either<Boolean, ActionStatus> isValid(byte[] payload) { + try { + gson.fromJson(new String(payload), Object.class); + } catch (Exception e) { + log.debug("Json is invalid : {}", e.getMessage(), e); + return Either.right(ActionStatus.INVALID_JSON); + } + return Either.left(true); + } + }, + YAML { + @Override + public Either<Boolean, ActionStatus> isValid(byte[] payload) { + return isValidYaml(payload); + } + }, + NOT_DEFINED { + @Override + public Either<Boolean, ActionStatus> isValid(byte[] payload) { + return Either.left(true); + } + }; + + private static final Logger log = Logger.getLogger(PayloadTypeEnum.class); + private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + public abstract Either<Boolean, ActionStatus> isValid(byte[] payload); + + public boolean isHeatRelated() { + return false; + } + + private static Either<Boolean, ActionStatus> isValidYaml(byte[] payload) { + YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); + if (yamlToObjectConverter.isValidYaml(payload)) { + log.debug("Invalid YAML format"); + return Either.left(true); + } + return Either.right(ActionStatus.INVALID_YAML); + } + +} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogicTest.java index f35de43613..ccba26461a 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogicTest.java @@ -29,6 +29,7 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import fj.data.Either; import mockit.Deencapsulation; +import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.tuple.ImmutablePair; import org.junit.Before; import org.junit.Test; @@ -55,13 +56,29 @@ import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.info.ArtifactTemplateInfo; -import org.openecomp.sdc.be.model.*; +import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.ArtifactType; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.GroupDefinition; +import org.openecomp.sdc.be.model.GroupInstance; +import org.openecomp.sdc.be.model.HeatParameterDefinition; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.model.LifecycleStateEnum; import org.openecomp.sdc.be.model.Operation; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache; import org.openecomp.sdc.be.model.jsontitan.operations.ArtifactsOperations; import org.openecomp.sdc.be.model.jsontitan.operations.NodeTemplateOperation; import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; -import org.openecomp.sdc.be.model.operations.api.*; +import org.openecomp.sdc.be.model.operations.api.IElementOperation; +import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation; +import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; +import org.openecomp.sdc.be.model.operations.api.IUserAdminOperation; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.ArtifactOperation; import org.openecomp.sdc.be.resources.data.ESArtifactData; import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; @@ -72,22 +89,28 @@ import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum; import org.openecomp.sdc.common.api.ArtifactTypeEnum; import org.openecomp.sdc.common.api.ConfigurationSource; import org.openecomp.sdc.common.datastructure.Wrapper; -import org.openecomp.sdc.common.datastructure.Wrapper; import org.openecomp.sdc.common.impl.ExternalConfiguration; import org.openecomp.sdc.common.impl.FSConfigurationSource; import org.openecomp.sdc.exception.ResponseFormat; import java.io.IOException; -import java.util.*; - +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; - -import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.HEAT_ENV_NAME; import static org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.HEAT_VF_ENV_NAME; @@ -981,21 +1004,6 @@ public class ArtifactsBusinessLogicTest { } - - - - @Test - public void testIsValidJson() throws Exception { - ArtifactsBusinessLogic testSubject; - byte[] jsonToParse = new byte[] { ' ' }; - boolean result; - - // default test - testSubject = createTestSubject(); - result = Deencapsulation.invoke(testSubject, "isValidJson", new Object[] { jsonToParse }); - } - - @Test public void testValidateSingleDeploymentArtifactName() throws Exception { ArtifactsBusinessLogic testSubject;Wrapper<ResponseFormat> errorWrapper = new Wrapper<>(); @@ -1160,71 +1168,96 @@ public class ArtifactsBusinessLogicTest { } + @Test + public void testGivenValidVesEventsArtifactPayload_WhenHandlePayload_ThenResultIsDecodedPayload() { + final byte[] payload = "validYaml: yes".getBytes(); + ArtifactDefinition artifactInfo = createArtifactInfo(payload, "ves_events_file.yaml", ArtifactTypeEnum.VES_EVENTS); + final boolean isArtifactMetadataUpdate = false; + ArtifactsBusinessLogic testSubject = new ArtifactsBusinessLogic(); + Either<byte[], ResponseFormat> result = Deencapsulation.invoke(testSubject, "handlePayload", + new Object[] { artifactInfo, isArtifactMetadataUpdate }); + assertArrayEquals(payload, result.left().value()); + } - @Test - public void testHandlePayload() throws Exception { - ArtifactsBusinessLogic testSubject; - ArtifactDefinition artifactInfo = buildArtifactPayload(); - boolean isArtifactMetadataUpdate = false; - Either<byte[], ResponseFormat> result; + public void testGivenInValidVesEventsArtifactPayload_WhenHandlePayload_ThenResultIsInvalidYaml() { + final int expectedStatus = 100; + when(componentsUtils.getResponseFormat(eq(ActionStatus.INVALID_YAML), any(String.class))).thenReturn(new ResponseFormat(expectedStatus)); + final byte[] payload = "invalidYaml".getBytes(); + ArtifactDefinition artifactInfo = createArtifactInfo(payload, "ves_events_file.yaml", ArtifactTypeEnum.VES_EVENTS); - // default test - testSubject = createTestSubject(); - result = Deencapsulation.invoke(testSubject, "handlePayload", + final boolean isArtifactMetadataUpdate = false; + ArtifactsBusinessLogic testSubject = new ArtifactsBusinessLogic(); + testSubject.setComponentsUtils(componentsUtils); + + Either<byte[], ResponseFormat> result = Deencapsulation.invoke(testSubject, "handlePayload", new Object[] { artifactInfo, isArtifactMetadataUpdate }); + + int status = result.right().value().getStatus(); + assertEquals(expectedStatus, status); } - + @Test + public void testGivenEmptyVesEventsArtifactPayload_WhenHandlePayload_ThenResultIsMissingData() { + final int expectedStatus = 101; + when(componentsUtils.getResponseFormat(eq(ActionStatus.MISSING_DATA), any(String.class))).thenReturn(new ResponseFormat(expectedStatus)); + final byte[] payload = "".getBytes(); + ArtifactDefinition artifactInfo = createArtifactInfo(payload, "ves_events_file.yaml", ArtifactTypeEnum.VES_EVENTS); + final boolean isArtifactMetadataUpdate = false; + ArtifactsBusinessLogic testSubject = new ArtifactsBusinessLogic(); + testSubject.setComponentsUtils(componentsUtils); - - @Test - public void testValidateYmlPayload() throws Exception { - ArtifactsBusinessLogic testSubject; - byte[] decodedPayload = new byte[] { ' ' }; - String artifactType = ""; - Either<Boolean, ResponseFormat> result; + Either<byte[], ResponseFormat> result = Deencapsulation.invoke(testSubject, "handlePayload", + new Object[] { artifactInfo, isArtifactMetadataUpdate }); - // default test - testSubject = createTestSubject(); - result = Deencapsulation.invoke(testSubject, "validateYmlPayload", - new Object[] { decodedPayload, artifactType }); + int status = result.right().value().getStatus(); + assertEquals(expectedStatus, status); } - @Test - public void testValidateXmlPayload() throws Exception { - ArtifactsBusinessLogic testSubject; - byte[] payload = new byte[] { ' ' }; - String artifactType = ""; - Either<Boolean, ResponseFormat> result; + public void testGivenValidHeatArtifactPayload_WhenHandlePayload_ThenResultIsDecodedPayload() { + final byte[] payload = "heat_template_version: 1.0".getBytes(); + ArtifactDefinition artifactInfo = createArtifactInfo(payload, "heat_template.yaml", ArtifactTypeEnum.HEAT); - // default test - testSubject = createTestSubject(); - result = Deencapsulation.invoke(testSubject, "validateXmlPayload", new Object[] { payload, artifactType }); + final boolean isArtifactMetadataUpdate = false; + ArtifactsBusinessLogic testSubject = new ArtifactsBusinessLogic(); + + Either<byte[], ResponseFormat> result = Deencapsulation.invoke(testSubject, "handlePayload", + new Object[] { artifactInfo, isArtifactMetadataUpdate }); + assertArrayEquals(payload, result.left().value()); } - @Test - public void testValidateJsonPayload() throws Exception { - ArtifactsBusinessLogic testSubject; - byte[] payload = new byte[] { ' ' }; - String type = ""; - Either<Boolean, ResponseFormat> result; + public void testGivenInValidHeatArtifactPayload_WhenHandlePayload_ThenResultIsInvalidYaml() { + final int expectedStatus = 1000; + when(componentsUtils.getResponseFormat(eq(ActionStatus.INVALID_DEPLOYMENT_ARTIFACT_HEAT), any(String.class))).thenReturn(new ResponseFormat(expectedStatus)); + final byte[] payload = "validYaml: butNoHeatTemplateVersion".getBytes(); + ArtifactDefinition artifactInfo = createArtifactInfo(payload, "heat_template.yaml", ArtifactTypeEnum.HEAT); - // default test - testSubject = createTestSubject(); - result = Deencapsulation.invoke(testSubject, "validateJsonPayload", new Object[] { payload, type }); - } - + final boolean isArtifactMetadataUpdate = false; + ArtifactsBusinessLogic testSubject = new ArtifactsBusinessLogic(); + testSubject.setComponentsUtils(componentsUtils); + + Either<byte[], ResponseFormat> result = Deencapsulation.invoke(testSubject, "handlePayload", + new Object[] { artifactInfo, isArtifactMetadataUpdate }); + int status = result.right().value().getStatus(); + assertEquals(expectedStatus, status); + } + private ArtifactDefinition createArtifactInfo(byte[] payload, String artifactName, ArtifactTypeEnum artifactType) { + ArtifactDefinition artifactInfo = new ArtifactDefinition(); + artifactInfo.setArtifactName(artifactName); + artifactInfo.setArtifactType(artifactType.getType()); + artifactInfo.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT); + artifactInfo.setPayload(Base64.encodeBase64(payload)); + return artifactInfo; + } - @Test public void testValidateUserRole() throws Exception { ArtifactsBusinessLogic testSubject; diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/artifact/ArtifactTypeToPayloadTypeSelectorTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/artifact/ArtifactTypeToPayloadTypeSelectorTest.java new file mode 100644 index 0000000000..4f772ea7de --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/artifact/ArtifactTypeToPayloadTypeSelectorTest.java @@ -0,0 +1,112 @@ +/* + * - + * * ============LICENSE_START======================================================= + * * Copyright (C) 2019 Nordix Foundation. + * * ================================================================================ + * * 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. + * * + * * SPDX-License-Identifier: Apache-2.0 + * * ============LICENSE_END========================================================= + * + */ + +package org.openecomp.sdc.be.components.impl.artifact; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.openecomp.sdc.common.api.ArtifactTypeEnum; + +import java.util.Arrays; +import java.util.Collection; + +import static org.junit.Assert.assertEquals; +import static org.openecomp.sdc.be.components.impl.artifact.ArtifactTypeToPayloadTypeSelector.getPayloadType; +import static org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum.HEAT_ENV; +import static org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum.HEAT_YAML; +import static org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum.NOT_DEFINED; +import static org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum.XML; + +@RunWith(Parameterized.class) +public class ArtifactTypeToPayloadTypeSelectorTest { + + private static final String ANY_EXTENSION = "anyExtension"; + private static final String JSON = "json"; + private static final String YAML = "yaml"; + private static final String YML = "yml"; + private String artifactType; + private String extension; + private PayloadTypeEnum expectedPayloadTypeEnum; + + public ArtifactTypeToPayloadTypeSelectorTest(String artifactType, String extension, PayloadTypeEnum expectedPayloadTypeEnum) { + this.artifactType = artifactType; + this.extension = extension; + this.expectedPayloadTypeEnum = expectedPayloadTypeEnum; + } + + @Parameters(name = "{index}: {0}, {1}") + public static Collection<Object[]> data() { + return Arrays.asList(new Object[][] { + { ArtifactTypeEnum.HEAT.getType(), ANY_EXTENSION, HEAT_YAML }, + { ArtifactTypeEnum.HEAT.getType().toLowerCase(), ANY_EXTENSION, HEAT_YAML }, + { ArtifactTypeEnum.HEAT_VOL.getType(), ANY_EXTENSION, HEAT_YAML }, + { ArtifactTypeEnum.HEAT_VOL.getType().toLowerCase(), ANY_EXTENSION, HEAT_YAML }, + { ArtifactTypeEnum.HEAT_NET.getType(), ANY_EXTENSION, HEAT_YAML }, + { ArtifactTypeEnum.HEAT_NET.getType().toLowerCase(), ANY_EXTENSION, HEAT_YAML }, + { ArtifactTypeEnum.HEAT_ENV.getType(), ANY_EXTENSION, HEAT_ENV }, + { ArtifactTypeEnum.HEAT_ENV.getType().toLowerCase(), ANY_EXTENSION, HEAT_ENV }, + { ArtifactTypeEnum.YANG_XML.getType(), ANY_EXTENSION, XML }, + { ArtifactTypeEnum.YANG_XML.getType().toLowerCase(), ANY_EXTENSION, XML }, + { ArtifactTypeEnum.VNF_CATALOG.getType(), ANY_EXTENSION, XML }, + { ArtifactTypeEnum.VNF_CATALOG.getType().toLowerCase(), ANY_EXTENSION, XML }, + { ArtifactTypeEnum.VF_LICENSE.getType(), ANY_EXTENSION, XML }, + { ArtifactTypeEnum.VF_LICENSE.getType().toLowerCase(), ANY_EXTENSION, XML }, + { ArtifactTypeEnum.VENDOR_LICENSE.getType(), ANY_EXTENSION, XML }, + { ArtifactTypeEnum.VENDOR_LICENSE.getType().toLowerCase(), ANY_EXTENSION, XML }, + { ArtifactTypeEnum.MODEL_INVENTORY_PROFILE.getType(), ANY_EXTENSION, XML }, + { ArtifactTypeEnum.MODEL_INVENTORY_PROFILE.getType().toLowerCase(), ANY_EXTENSION, XML }, + { ArtifactTypeEnum.MODEL_QUERY_SPEC.getType(), ANY_EXTENSION, XML }, + { ArtifactTypeEnum.MODEL_QUERY_SPEC.getType().toLowerCase(), ANY_EXTENSION, XML }, + { ArtifactTypeEnum.UCPE_LAYER_2_CONFIGURATION.getType(), ANY_EXTENSION, XML }, + { ArtifactTypeEnum.UCPE_LAYER_2_CONFIGURATION.getType().toLowerCase(), ANY_EXTENSION, XML }, + { ArtifactTypeEnum.DCAE_INVENTORY_JSON.getType(), JSON, PayloadTypeEnum.JSON }, + { ArtifactTypeEnum.DCAE_INVENTORY_JSON.getType().toLowerCase(), JSON, PayloadTypeEnum.JSON }, + { ArtifactTypeEnum.DCAE_INVENTORY_TOSCA.getType(), YAML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.DCAE_INVENTORY_TOSCA.getType().toLowerCase(), YAML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.DCAE_INVENTORY_TOSCA.getType(), YML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.DCAE_INVENTORY_TOSCA.getType().toLowerCase(), YML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.VES_EVENTS.getType(), YAML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.VES_EVENTS.getType().toLowerCase(), YAML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.VES_EVENTS.getType(), YML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.VES_EVENTS.getType().toLowerCase(), YML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.LIFECYCLE_OPERATIONS.getType(), YAML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.LIFECYCLE_OPERATIONS.getType().toLowerCase(), YAML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.LIFECYCLE_OPERATIONS.getType(), YML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.LIFECYCLE_OPERATIONS.getType().toLowerCase(), YML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.PM_DICTIONARY.getType(), YAML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.PM_DICTIONARY.getType().toLowerCase(), YAML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.PM_DICTIONARY.getType(), YML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.PM_DICTIONARY.getType().toLowerCase(), YML, PayloadTypeEnum.YAML }, + { ArtifactTypeEnum.ANSIBLE_PLAYBOOK.getType(), ANY_EXTENSION, NOT_DEFINED }, + { ArtifactTypeEnum.ANSIBLE_PLAYBOOK.getType().toLowerCase(), ANY_EXTENSION, NOT_DEFINED } + }); + } + + @Test + public void testCorrectPayloadTypeEnumSelectedForArtifactTypeAndExtension() { + PayloadTypeEnum payloadType = getPayloadType(artifactType, extension); + assertEquals(expectedPayloadTypeEnum, payloadType); + } + +} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnumTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnumTest.java new file mode 100644 index 0000000000..0456a83690 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnumTest.java @@ -0,0 +1,153 @@ +/* + * - + * * ============LICENSE_START======================================================= + * * Copyright (C) 2019 Nordix Foundation. + * * ================================================================================ + * * 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. + * * + * * SPDX-License-Identifier: Apache-2.0 + * * ============LICENSE_END========================================================= + * + */ + +package org.openecomp.sdc.be.components.impl.artifact; + +import fj.data.Either; +import org.junit.Test; +import org.openecomp.sdc.be.dao.api.ActionStatus; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum.HEAT_ENV; +import static org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum.HEAT_YAML; +import static org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum.JSON; +import static org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum.NOT_DEFINED; +import static org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum.XML; +import static org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum.YAML; + +public class PayloadTypeEnumTest { + + private static final byte[] INVALID_YAML = "invalidYaml".getBytes(); + + @Test + public void testGivenValidContentForHeatYamlType_WhenCheckValid_ThenResultTrue() { + byte[] input = "heat_template_version: 1".getBytes(); + Either<Boolean, ActionStatus> result = HEAT_YAML.isValid(input); + assertTrue(result.left().value()); + } + + @Test + public void testGivenInvalidYamlContentForHeatYamlType_WhenCheckValid_ThenInvalidYamlResultReturned() { + Either<Boolean, ActionStatus> result = HEAT_YAML.isValid(INVALID_YAML); + assertEquals(ActionStatus.INVALID_YAML, result.right().value()); + } + + @Test + public void testGivenValidYamlButInvalidHeatTempleContentForHeatYamlType_WhenCheckValid_ThenInvalidHeatReturned() { + byte[] input = "validYaml: butNotTopologyTemplate".getBytes(); + Either<Boolean, ActionStatus> result = HEAT_YAML.isValid(input); + assertEquals(ActionStatus.INVALID_DEPLOYMENT_ARTIFACT_HEAT, result.right().value()); + } + + @Test + public void testGivenHeatYamlType_WhenCheckIfHeatRelated_ThenResultTrue() { + assertTrue(HEAT_YAML.isHeatRelated()); + } + + @Test + public void testGivenValidYamlForHeatEnvType_WhenCheckValid_ThenResultTrue() { + byte[] input = "validYaml: yes".getBytes(); + Either<Boolean, ActionStatus> result = HEAT_ENV.isValid(input); + assertTrue(result.left().value()); + } + + @Test + public void testGivenInvalidYamlContentForHeatEnvType_WhenCheckValid_ThenInvalidYamlResultReturned() { + Either<Boolean, ActionStatus> result = HEAT_ENV.isValid(INVALID_YAML); + assertEquals(ActionStatus.INVALID_YAML, result.right().value()); + } + + @Test + public void testGivenHeatEnvType_WhenCheckIfHeatRelated_ThenResultTrue() { + assertTrue(HEAT_ENV.isHeatRelated()); + } + + @Test + public void testGivenValidYamlForYamlType_WhenCheckValid_ThenResultTrue() { + byte[] input = "validYaml: yes".getBytes(); + Either<Boolean, ActionStatus> result = YAML.isValid(input); + assertTrue(result.left().value()); + } + + @Test + public void testGivenInvalidYamlContentForYamlType_WhenCheckValid_ThenInvalidYamlResultReturned() { + Either<Boolean, ActionStatus> result = YAML.isValid(INVALID_YAML); + assertEquals(ActionStatus.INVALID_YAML, result.right().value()); + } + + @Test + public void testGivenYamlType_WhenCheckIfHeatRelated_ThenResultFalse() { + assertFalse(YAML.isHeatRelated()); + } + + @Test + public void testGivenValidJsonForJsonType_WhenCheckValid_ThenResultTrue() { + byte[] input = "{\"validYaml\": \"yes\"}".getBytes(); + Either<Boolean, ActionStatus> result = JSON.isValid(input); + assertTrue(result.left().value()); + } + + @Test + public void testGivenInvalidJsonContentForJsonType_WhenCheckValid_ThenInvalidJsonResultReturned() { + byte[] input = "invalidJson,with,extraCommas".getBytes(); + Either<Boolean, ActionStatus> result = JSON.isValid(input); + assertEquals(ActionStatus.INVALID_JSON, result.right().value()); + } + + @Test + public void testGivenJsonType_WhenCheckIfHeatRelated_ThenResultFalse() { + assertFalse(JSON.isHeatRelated()); + } + + @Test + public void testGivenValidXmlForXmlType_WhenCheckValid_ThenResultTrue() { + byte[] input = "<root>validXml</root>".getBytes(); + Either<Boolean, ActionStatus> result = XML.isValid(input); + assertTrue(result.left().value()); + } + + @Test + public void testGivenInvalidXmlContentForXmlType_WhenCheckValid_ThenInvalidXmlResultReturned() { + byte[] input = "<root>inValidXmlWithNoClosingTag".getBytes(); + Either<Boolean, ActionStatus> result = XML.isValid(input); + assertEquals(ActionStatus.INVALID_XML, result.right().value()); + } + + @Test + public void testGivenXmlType_WhenCheckIfHeatRelated_ThenResultFalse() { + assertFalse(XML.isHeatRelated()); + } + + @Test + public void testGivenAnyInputForNotDefinedType_WhenCheckValid_ThenResultTrue() { + byte[] input = "Any input can be defined here <<<<<:::::::////////".getBytes(); + Either<Boolean, ActionStatus> result = NOT_DEFINED.isValid(input); + assertTrue(result.left().value()); + } + + @Test + public void testGivenNotDefinedType_WhenCheckIfHeatRelated_ThenResultFalse() { + assertFalse(NOT_DEFINED.isHeatRelated()); + } +}
\ No newline at end of file |