2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.openecomp.sdc.be.test.util.TestResourcesHandler.getResourceBytesOrFail;
28 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ATTRIBUTE_VALUE_SEPARATOR;
29 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.CREATED_BY_ENTRY;
30 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.CSAR_VERSION_ENTRY;
31 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ENTRY_DEFINITIONS;
32 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ETSI_ENTRY_CHANGE_LOG;
33 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ETSI_ENTRY_MANIFEST;
34 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.TOSCA_META_FILE_VERSION_ENTRY;
35 import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME;
36 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_CHANGELOG_FILEPATH;
37 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_DEFINITION_FILEPATH;
38 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_MANIFEST_FILEPATH;
40 import java.io.IOException;
41 import java.nio.charset.StandardCharsets;
42 import java.util.List;
43 import org.junit.jupiter.api.BeforeEach;
44 import org.junit.jupiter.api.Test;
45 import org.openecomp.core.utilities.file.FileContentHandler;
47 class ValidatorFactoryTest {
49 private String metaFile;
50 private FileContentHandler handler;
51 private ValidatorFactory validatorFactory;
55 validatorFactory = new ValidatorFactory();
56 handler = new FileContentHandler();
57 metaFile = new StringBuilder()
58 .append(TOSCA_META_FILE_VERSION_ENTRY.getName())
59 .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" 1.0").append("\n")
60 .append(CSAR_VERSION_ENTRY.getName())
61 .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" 1.1").append("\n")
62 .append(CREATED_BY_ENTRY.getName())
63 .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" Vendor").append("\n")
68 void testGivenEmptyMetaFile_thenIOExceptionIsThrown() {
69 handler.addFile(TOSCA_META_PATH_FILE_NAME, "".getBytes(StandardCharsets.UTF_8));
70 handler.addFile(TOSCA_DEFINITION_FILEPATH, "".getBytes());
71 handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
72 handler.addFile(TOSCA_MANIFEST_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
74 assertThrows(IOException.class, () -> validatorFactory.getValidator(handler));
78 void testGivenEmptyBlock0_thenONAPCsarValidatorIsReturned() throws IOException {
79 handler.addFile(TOSCA_META_PATH_FILE_NAME, " ".getBytes(StandardCharsets.UTF_8));
80 handler.addFile(TOSCA_DEFINITION_FILEPATH, "".getBytes());
81 handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
82 handler.addFile(TOSCA_MANIFEST_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
84 assertEquals(ONAPCsarValidator.class, validatorFactory.getValidator(handler).getClass());
89 void testGivenNonSOL004MetaDirectoryCompliantMetaFile_thenONAPCSARValidatorIsReturned() throws IOException {
91 ENTRY_DEFINITIONS.getName() + ATTRIBUTE_VALUE_SEPARATOR.getToken() + TOSCA_DEFINITION_FILEPATH;
92 handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8));
94 assertEquals(ONAPCsarValidator.class, validatorFactory.getValidator(handler).getClass());
98 void testGivenSOL004MetaDirectoryCompliantMetafile_thenONAPCsarValidatorIsReturned() throws IOException {
100 ENTRY_DEFINITIONS.getName() + ATTRIBUTE_VALUE_SEPARATOR.getToken() + TOSCA_DEFINITION_FILEPATH + "\n"
101 + ETSI_ENTRY_MANIFEST.getName() + ATTRIBUTE_VALUE_SEPARATOR.getToken() + TOSCA_MANIFEST_FILEPATH + "\n"
102 + ETSI_ENTRY_CHANGE_LOG.getName() + ATTRIBUTE_VALUE_SEPARATOR.getToken() + TOSCA_CHANGELOG_FILEPATH + "\n";
103 handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8));
105 assertEquals(SOL004MetaDirectoryValidator.class, validatorFactory.getValidator(handler).getClass());
109 void testGivenMultiBlockMetadataWithSOL00CompliantMetaFile_thenSOL004MetaDirectoryValidatorReturned()
111 handler.addFile(TOSCA_META_PATH_FILE_NAME,
112 getResourceBytesOrFail("validation.files/metafile/metaFileWithMultipleBlocks.meta"));
113 handler.addFile(TOSCA_DEFINITION_FILEPATH, "".getBytes());
114 handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
115 handler.addFile(TOSCA_MANIFEST_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
117 assertEquals(SOL004MetaDirectoryValidator.class, validatorFactory.getValidator(handler).getClass());
122 void testGetValidators() {
123 final List<Validator> validatorList = validatorFactory.getValidators("test");
124 assertFalse(validatorList.isEmpty());
125 assertEquals(2, validatorList.size());
126 assertTrue(validatorList.get(0) instanceof TestModelValidator1);
127 assertTrue(validatorList.get(1) instanceof TestModelValidator2);
129 final List<Validator> validatorList1 = validatorFactory.getValidators("test1");
130 assertTrue(validatorList1.isEmpty());