diff options
author | andre.schmid <andre.schmid@est.tech> | 2021-08-23 19:41:50 +0100 |
---|---|---|
committer | andre.schmid <andre.schmid@est.tech> | 2021-08-26 09:55:34 +0100 |
commit | 5a0703ffad1492ec6b6c78143f63dca83ee030d2 (patch) | |
tree | 202acfe75f4975051bd0b18d55b962ecd148fe7c | |
parent | 0c2661a7dfd1de644c7a3f9f42fc1883d3ceff78 (diff) |
Dynamically load CSAR validators for models
Change-Id: I88ece0936e8a2814ef13dfa23eecda56de3dc6fe
Issue-ID: SDC-3683
Signed-off-by: andre.schmid <andre.schmid@est.tech>
11 files changed, 215 insertions, 40 deletions
diff --git a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/CsarValidationTest.java b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/CsarValidationTest.java index 9500c9b378..7a4ad4da1e 100644 --- a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/CsarValidationTest.java +++ b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/CsarValidationTest.java @@ -39,6 +39,8 @@ import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validatio import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidatorFactory; class CsarValidationTest { + + private final ValidatorFactory validatorFactory = new ValidatorFactory(); @Test void shouldNotReturnErrors_whenPnfCsarIsValid() throws OnboardPackageException, IOException { @@ -46,7 +48,7 @@ class CsarValidationTest { FileContentHandler pnfFileContent = CsarLoader.load("validPnfCompliantWithSOL004.csar", "/Files/PNFs/validation/pmdictionary/validPnfCompliantWithSOL004.csar"); //when - final ValidationResult validationResult = ValidatorFactory.getValidator(pnfFileContent).validate(pnfFileContent); + final ValidationResult validationResult = validatorFactory.getValidator(pnfFileContent).validate(pnfFileContent); //then assertThat(validationResult.getErrors(), is(empty())); @@ -59,7 +61,7 @@ class CsarValidationTest { FileContentHandler pnfFileContent = CsarLoader.load("invalidPnfCompliantWithSOL004.csar", "/Files/PNFs/validation/pmdictionary/invalidPnfCompliantWithSOL004.csar"); //when - final ValidationResult validationResult = ValidatorFactory.getValidator(pnfFileContent).validate(pnfFileContent); + final ValidationResult validationResult = validatorFactory.getValidator(pnfFileContent).validate(pnfFileContent); List<ErrorMessage> errorList = validationResult.getErrors(); //then @@ -77,7 +79,7 @@ class CsarValidationTest { ); //when - final ValidationResult validationResult = ValidatorFactory.getValidator(pnfFileContent).validate(pnfFileContent); + final ValidationResult validationResult = validatorFactory.getValidator(pnfFileContent).validate(pnfFileContent); //then assertThat(validationResult.isValid(), is(true)); @@ -94,7 +96,7 @@ class CsarValidationTest { ); //when - final ValidationResult validationResult = ValidatorFactory.getValidator(pnfFileContent).validate(pnfFileContent); + final ValidationResult validationResult = validatorFactory.getValidator(pnfFileContent).validate(pnfFileContent); List<ErrorMessage> errorList = validationResult.getErrors(); //then diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateImpl.java index eb78bf0059..23930ed640 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateImpl.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateImpl.java @@ -152,7 +152,9 @@ public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplate } @Override - public Response upload(final String vspId, final String versionId, final Attachment fileToUpload, final String user) { + public Response upload(String vspId, String versionId, final Attachment fileToUpload, final String user) { + vspId = ValidationUtils.sanitizeInputString(vspId); + versionId = ValidationUtils.sanitizeInputString(versionId); final byte[] fileToUploadBytes; final var filename = ValidationUtils.sanitizeInputString(fileToUpload.getDataHandler().getName()); ArtifactInfo artifactInfo = null; @@ -193,8 +195,8 @@ public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplate return Response.ok(uploadFileResponseDto) .build(); } - final var version = new Version(ValidationUtils.sanitizeInputString(versionId)); - final var vspDetails = new VspDetails(ValidationUtils.sanitizeInputString(vspId), version); + final var version = new Version(versionId); + final var vspDetails = vendorSoftwareProductManager.getVsp(vspId, version); return processOnboardPackage(onboardPackageInfo, vspDetails, errorMessages); } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/BaseOrchestrationTemplateHandler.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/BaseOrchestrationTemplateHandler.java index 6e810eefbd..ea36e5389f 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/BaseOrchestrationTemplateHandler.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/BaseOrchestrationTemplateHandler.java @@ -50,7 +50,7 @@ public abstract class BaseOrchestrationTemplateHandler implements OrchestrationT if (isInvalidRawZipData(onboardPackage.getFileExtension(), uploadFileResponse, fileContentByteArray, candidateService)) { return uploadFileResponse; } - final UploadFileResponse validateResponse = validate(onboardPackageInfo); + final UploadFileResponse validateResponse = validate(vspDetails, onboardPackageInfo); if (!MapUtils.isEmpty(validateResponse.getErrors())) { uploadFileResponse.addStructureErrors(validateResponse.getErrors()); return uploadFileResponse; @@ -87,7 +87,7 @@ public abstract class BaseOrchestrationTemplateHandler implements OrchestrationT return false; } - public abstract UploadFileResponse validate(final OnboardPackageInfo onboardPackageInfo); + public abstract UploadFileResponse validate(final VspDetails vspDetails, final OnboardPackageInfo onboardPackageInfo); protected abstract OnboardingTypesEnum getHandlerType(); } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateCSARHandler.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateCSARHandler.java index 3005a2df2c..5a3518135a 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateCSARHandler.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateCSARHandler.java @@ -22,6 +22,9 @@ package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration; import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters; import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import org.apache.commons.collections4.CollectionUtils; @@ -36,7 +39,6 @@ import org.openecomp.sdc.datatypes.error.ErrorMessage; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.CsarSecurityValidator; -import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidationResult; import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.Validator; import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidatorFactory; import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManagerException; @@ -48,8 +50,14 @@ import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler { + private final ValidatorFactory validatorFactory; + + public OrchestrationTemplateCSARHandler() { + this.validatorFactory = new ValidatorFactory(); + } + @Override - public UploadFileResponse validate(final OnboardPackageInfo onboardPackageInfo) { + public UploadFileResponse validate(final VspDetails vspDetails, final OnboardPackageInfo onboardPackageInfo) { final UploadFileResponse uploadFileResponse = new UploadFileResponse(); if (onboardPackageInfo.getPackageType() == OnboardingTypesEnum.SIGNED_CSAR) { final OnboardSignedPackage originalOnboardPackage = (OnboardSignedPackage) onboardPackageInfo.getOriginalOnboardPackage(); @@ -66,22 +74,57 @@ public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateH final OnboardPackage onboardPackage = onboardPackageInfo.getOnboardPackage(); final FileContentHandler fileContentHandler = onboardPackage.getFileContentHandler(); try { - final Validator validator = ValidatorFactory.getValidator(fileContentHandler); - final ValidationResult validationResult = validator.validate(fileContentHandler); - if (CollectionUtils.isNotEmpty(validationResult.getErrors())) { - uploadFileResponse.addStructureErrors(Map.of(SdcCommon.UPLOAD_FILE, validationResult.getErrors())); + final List<String> modelIdList = vspDetails.getModelIdList(); + if (CollectionUtils.isEmpty(modelIdList)) { + final Map<String, List<ErrorMessage>> errorResponseMap = validateSdcModel(fileContentHandler); + if (!errorResponseMap.isEmpty()) { + uploadFileResponse.addStructureErrors(errorResponseMap); + } + } else { + final Map<String, List<ErrorMessage>> errorResponseMap = validateModels(modelIdList, fileContentHandler); + if (!errorResponseMap.isEmpty()) { + uploadFileResponse.addStructureErrors(errorResponseMap); + } } - } catch (IOException exception) { + } catch (final IOException exception) { logger.error(exception.getMessage(), exception); uploadFileResponse .addStructureError(SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_CSAR_FILE.getErrorMessage())); - } catch (CoreException coreException) { + } catch (final CoreException coreException) { logger.error(coreException.getMessage(), coreException); uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage())); } return uploadFileResponse; } + private Map<String, List<ErrorMessage>> validateModels(final List<String> modelIdList, final FileContentHandler fileContentHandler) { + final Map<String, List<ErrorMessage>> errorResponseMap = new HashMap<>(); + modelIdList.forEach(model -> { + final List<Validator> validators = validatorFactory.getValidators(model); + validators.forEach(validator -> { + final var validationResult = validator.validate(fileContentHandler); + if (CollectionUtils.isNotEmpty(validationResult.getErrors())) { + if (errorResponseMap.containsKey(SdcCommon.UPLOAD_FILE)) { + errorResponseMap.get(SdcCommon.UPLOAD_FILE).addAll(validationResult.getErrors()); + } else { + errorResponseMap.put(SdcCommon.UPLOAD_FILE, validationResult.getErrors()); + } + } + }); + }); + return errorResponseMap; + } + + private Map<String, List<ErrorMessage>> validateSdcModel(final FileContentHandler fileContentHandler) throws IOException { + final var validator = validatorFactory.getValidator(fileContentHandler); + final var validationResult = validator.validate(fileContentHandler); + if (CollectionUtils.isNotEmpty(validationResult.getErrors())) { + return Map.of(SdcCommon.UPLOAD_FILE, validationResult.getErrors()); + } + + return Collections.emptyMap(); + } + private Optional<UploadFileResponse> validatePackageSecurity(final OnboardSignedPackage signedPackage, final ArtifactInfo artifactInfo) { final UploadFileResponse uploadFileResponseDto = new UploadFileResponse(); try { diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateZipHandler.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateZipHandler.java index e534eded02..c4d7053870 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateZipHandler.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateZipHandler.java @@ -37,7 +37,7 @@ import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; public class OrchestrationTemplateZipHandler extends BaseOrchestrationTemplateHandler { @Override - public UploadFileResponse validate(final OnboardPackageInfo onboardPackageInfo) { + public UploadFileResponse validate(final VspDetails vspDetails, final OnboardPackageInfo onboardPackageInfo) { final UploadFileResponse uploadFileResponse = new UploadFileResponse(); final OnboardPackage onboardPackage = onboardPackageInfo.getOnboardPackage(); OrchestrationUtil.getFileContentMap(OnboardingTypesEnum.ZIP, uploadFileResponse, onboardPackage.getFileContent().array()); diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactory.java index 27a1852644..91d2705693 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactory.java @@ -22,13 +22,21 @@ package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validati import static org.openecomp.sdc.tosca.csar.CSARConstants.ETSI_VERSION_2_7_1; import java.io.IOException; +import java.util.Comparator; +import java.util.List; +import java.util.ServiceLoader; +import java.util.ServiceLoader.Provider; +import java.util.stream.Collectors; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIService; import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIServiceImpl; public class ValidatorFactory { - private ValidatorFactory() { + private final ServiceLoader<Validator> validatorLoader; + + public ValidatorFactory() { + this.validatorLoader = ServiceLoader.load(Validator.class); } /** @@ -38,7 +46,7 @@ public class ValidatorFactory { * @return Validator based on the contents of the csar package provided * @throws IOException when metafile is invalid */ - public static Validator getValidator(final FileContentHandler fileContentHandler) throws IOException { + public Validator getValidator(final FileContentHandler fileContentHandler) throws IOException { final ETSIService etsiService = new ETSIServiceImpl(null); if (!etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)) { return new ONAPCsarValidator(); @@ -51,4 +59,18 @@ public class ValidatorFactory { } return new SOL004MetaDirectoryValidator(); } + + /** + * Get validators based on the given model. + * + * @param model the model + * @return a list containing all validators for the given model, empty otherwise. + */ + public List<Validator> getValidators(final String model) { + return validatorLoader.stream() + .map(Provider::get) + .filter(validator -> validator.appliesTo(model)) + .sorted(Comparator.comparingInt(Validator::getOrder)) + .collect(Collectors.toList()); + } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004Version4MetaDirectoryValidatorTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004Version4MetaDirectoryValidatorTest.java index 9587f59e72..0f6f63bcbf 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004Version4MetaDirectoryValidatorTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004Version4MetaDirectoryValidatorTest.java @@ -41,6 +41,8 @@ import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager; class SOL004Version4MetaDirectoryValidatorTest extends SOL004MetaDirectoryValidatorTest { + private final ValidatorFactory validatorFactory = new ValidatorFactory(); + @Override public SOL004MetaDirectoryValidator getSOL004MetaDirectoryValidator() { return new SOL004Version4MetaDirectoryValidator(); @@ -107,7 +109,7 @@ class SOL004Version4MetaDirectoryValidatorTest extends SOL004MetaDirectoryValida manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); - final Validator validator = ValidatorFactory.getValidator(handler); + final Validator validator = validatorFactory.getValidator(handler); final ValidationResult validationResult = validator.validate(handler); assertTrue(validationResult.getErrors().isEmpty()); } @@ -137,7 +139,7 @@ class SOL004Version4MetaDirectoryValidatorTest extends SOL004MetaDirectoryValida manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); - final Validator validator = ValidatorFactory.getValidator(handler); + final Validator validator = validatorFactory.getValidator(handler); final ValidationResult validationResult = validator.validate(handler); assertExpectedErrors("Non-MANO file does not exist", validationResult.getErrors(), 1); } @@ -167,7 +169,7 @@ class SOL004Version4MetaDirectoryValidatorTest extends SOL004MetaDirectoryValida manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); - final Validator validator = ValidatorFactory.getValidator(handler); + final Validator validator = validatorFactory.getValidator(handler); final ValidationResult validationResult = validator.validate(handler); assertExpectedErrors("Manifest with non existent source files", validationResult.getErrors(), 1); } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/TestModelValidator1.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/TestModelValidator1.java new file mode 100644 index 0000000000..bf76d2ad73 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/TestModelValidator1.java @@ -0,0 +1,42 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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.vendorsoftwareproduct.impl.orchestration.csar.validation; + +import org.openecomp.core.utilities.file.FileContentHandler; + +public class TestModelValidator1 implements Validator { + + @Override + public ValidationResult validate(FileContentHandler csarContent) { + return new CsarValidationResult(); + } + + @Override + public boolean appliesTo(String model) { + return "test".equals(model); + } + + @Override + public int getOrder() { + return 0; + } +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/TestModelValidator2.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/TestModelValidator2.java new file mode 100644 index 0000000000..e38fe907c4 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/TestModelValidator2.java @@ -0,0 +1,42 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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.vendorsoftwareproduct.impl.orchestration.csar.validation; + +import org.openecomp.core.utilities.file.FileContentHandler; + +public class TestModelValidator2 implements Validator { + + @Override + public ValidationResult validate(FileContentHandler csarContent) { + return new CsarValidationResult(); + } + + @Override + public boolean appliesTo(String model) { + return "test".equals(model); + } + + @Override + public int getOrder() { + return 1; + } +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactoryTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactoryTest.java index f2dde27365..9d66d627f9 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactoryTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactoryTest.java @@ -20,7 +20,10 @@ package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.openecomp.sdc.be.test.util.TestResourcesHandler.getResourceBytesOrFail; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ATTRIBUTE_VALUE_SEPARATOR; import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.CREATED_BY_ENTRY; @@ -36,17 +39,20 @@ import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.va import java.io.IOException; import java.nio.charset.StandardCharsets; -import org.junit.Before; -import org.junit.Test; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.openecomp.core.utilities.file.FileContentHandler; -public class ValidatorFactoryTest { +class ValidatorFactoryTest { private String metaFile; private FileContentHandler handler; + private ValidatorFactory validatorFactory; - @Before - public void setUp(){ + @BeforeEach + void setUp(){ + validatorFactory = new ValidatorFactory(); handler = new FileContentHandler(); metaFile = new StringBuilder() .append(TOSCA_META_FILE_VERSION_ENTRY.getName()) @@ -58,49 +64,49 @@ public class ValidatorFactoryTest { .toString(); } - @Test(expected = IOException.class) - public void testGivenEmptyMetaFile_thenIOExceptionIsThrown() throws IOException{ + @Test + void testGivenEmptyMetaFile_thenIOExceptionIsThrown() { handler.addFile(TOSCA_META_PATH_FILE_NAME, "".getBytes(StandardCharsets.UTF_8)); handler.addFile(TOSCA_DEFINITION_FILEPATH, "".getBytes()); handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); handler.addFile(TOSCA_MANIFEST_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); - ValidatorFactory.getValidator(handler); + assertThrows(IOException.class, () -> validatorFactory.getValidator(handler)); } @Test - public void testGivenEmptyBlock0_thenONAPCsarValidatorIsReturned() throws IOException { + void testGivenEmptyBlock0_thenONAPCsarValidatorIsReturned() throws IOException { handler.addFile(TOSCA_META_PATH_FILE_NAME, " ".getBytes(StandardCharsets.UTF_8)); handler.addFile(TOSCA_DEFINITION_FILEPATH, "".getBytes()); handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); handler.addFile(TOSCA_MANIFEST_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); - assertEquals(ONAPCsarValidator.class, ValidatorFactory.getValidator(handler).getClass()); + assertEquals(ONAPCsarValidator.class, validatorFactory.getValidator(handler).getClass()); } @Test - public void testGivenNonSOL004MetaDirectoryCompliantMetaFile_thenONAPCSARValidatorIsReturned() throws IOException { + void testGivenNonSOL004MetaDirectoryCompliantMetaFile_thenONAPCSARValidatorIsReturned() throws IOException { metaFile = metaFile + ENTRY_DEFINITIONS.getName() + ATTRIBUTE_VALUE_SEPARATOR.getToken() + TOSCA_DEFINITION_FILEPATH; handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - assertEquals(ONAPCsarValidator.class, ValidatorFactory.getValidator(handler).getClass()); + assertEquals(ONAPCsarValidator.class, validatorFactory.getValidator(handler).getClass()); } @Test - public void testGivenSOL004MetaDirectoryCompliantMetafile_thenONAPCsarValidatorIsReturned() throws IOException { + void testGivenSOL004MetaDirectoryCompliantMetafile_thenONAPCsarValidatorIsReturned() throws IOException { metaFile = metaFile + ENTRY_DEFINITIONS.getName() + ATTRIBUTE_VALUE_SEPARATOR.getToken() + TOSCA_DEFINITION_FILEPATH + "\n" + ETSI_ENTRY_MANIFEST.getName() + ATTRIBUTE_VALUE_SEPARATOR.getToken() + TOSCA_MANIFEST_FILEPATH + "\n" + ETSI_ENTRY_CHANGE_LOG.getName() + ATTRIBUTE_VALUE_SEPARATOR.getToken() + TOSCA_CHANGELOG_FILEPATH + "\n"; handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - assertEquals(SOL004MetaDirectoryValidator.class, ValidatorFactory.getValidator(handler).getClass()); + assertEquals(SOL004MetaDirectoryValidator.class, validatorFactory.getValidator(handler).getClass()); } @Test - public void testGivenMultiBlockMetadataWithSOL00CompliantMetaFile_thenSOL004MetaDirectoryValidatorReturned() + void testGivenMultiBlockMetadataWithSOL00CompliantMetaFile_thenSOL004MetaDirectoryValidatorReturned() throws IOException { handler.addFile(TOSCA_META_PATH_FILE_NAME, getResourceBytesOrFail("validation.files/metafile/metaFileWithMultipleBlocks.meta")); @@ -108,8 +114,20 @@ public class ValidatorFactoryTest { handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); handler.addFile(TOSCA_MANIFEST_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); - assertEquals(SOL004MetaDirectoryValidator.class, ValidatorFactory.getValidator(handler).getClass()); + assertEquals(SOL004MetaDirectoryValidator.class, validatorFactory.getValidator(handler).getClass()); + + } + @Test + void testGetValidators() { + final List<Validator> validatorList = validatorFactory.getValidators("test"); + assertFalse(validatorList.isEmpty()); + assertEquals(2, validatorList.size()); + assertTrue(validatorList.get(0) instanceof TestModelValidator1); + assertTrue(validatorList.get(1) instanceof TestModelValidator2); + + final List<Validator> validatorList1 = validatorFactory.getValidators("test1"); + assertTrue(validatorList1.isEmpty()); } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/META-INF/services/org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.Validator b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/META-INF/services/org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.Validator new file mode 100644 index 0000000000..16a9298ff3 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/META-INF/services/org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.Validator @@ -0,0 +1,2 @@ +org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestModelValidator1 +org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestModelValidator2
\ No newline at end of file |