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 /openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test | |
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>
Diffstat (limited to 'openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test')
5 files changed, 126 insertions, 20 deletions
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 |