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 org.junit.Before;
24 import org.junit.Test;
25 import org.openecomp.core.utilities.file.FileContentHandler;
26 import org.openecomp.sdc.common.utils.SdcCommon;
27 import org.openecomp.sdc.datatypes.error.ErrorMessage;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
33 import static org.junit.Assert.assertTrue;
36 public class ONAPCsarValidatorTest {
38 ONAPCsarValidator onapCsarValidator;
39 private FileContentHandler contentHandler;
40 private List<String> folderList;
43 public void setUp() throws IOException{
44 onapCsarValidator = new ONAPCsarValidator();
45 contentHandler = new FileContentHandler();
46 folderList = new ArrayList<>();
48 contentHandler.addFile("TOSCA-Metadata/TOSCA.meta", ValidatorUtil.getFileResource("/validation.files/metafile/nonSOL004WithMetaDirectoryCompliantMetaFile.meta"));
49 contentHandler.addFile("MainServiceTemplate.mf", ValidatorUtil.getFileResource("/validation.files/manifest/sampleManifest.mf"));
50 contentHandler.addFile(TestConstants.TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(TestConstants.SAMPLE_DEFINITION_FILE_PATH));
54 public void testGivenCSARPackage_withValidContent_thenNoErrorsReturned() {
55 assertExpectedErrors("Valid CSAR Package should have 0 errors",
56 onapCsarValidator.validateContent(contentHandler, folderList), 0);
60 public void testGivenCSARPackage_withInvalidManifestFile_thenErrorsReturned() throws IOException{
61 contentHandler = new FileContentHandler();
62 contentHandler.addFile("TOSCA-Metadata/TOSCA.meta", ValidatorUtil.getFileResource("/validation.files/metafile/nonSOL004WithMetaDirectoryCompliantMetaFile.meta"));
63 contentHandler.addFile("MainServiceTemplate.mf", ValidatorUtil.getFileResource("/validation.files/manifest/invalidManifest.mf"));
64 contentHandler.addFile(TestConstants.TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(TestConstants.SAMPLE_DEFINITION_FILE_PATH));
66 assertExpectedErrors("CSAR package with invalid manifest file should have errors", onapCsarValidator.validateContent(contentHandler, folderList), 1);
71 public void testGivenCSARPackage_withUnwantedFolders_thenErrorsReturned(){
73 folderList.add("Files/");
74 assertExpectedErrors("CSAR package with unwanted folders should fail with errors", onapCsarValidator.validateContent(contentHandler, folderList), 1);
78 public void testGivenCSARPackage_withUnwantedFiles_thenErrorsReturned(){
80 contentHandler.addFile("ExtraFile.text", "".getBytes());
81 assertExpectedErrors("CSAR package with unwanted files should fail with errors",
82 onapCsarValidator.validateContent(contentHandler, folderList), 1);
85 private void assertExpectedErrors( String testCase, Map<String, List<ErrorMessage>> errors, int expectedErrors){
86 if(expectedErrors > 0){
87 List<ErrorMessage> errorMessages = errors.get(SdcCommon.UPLOAD_FILE);
88 assertTrue(testCase, errorMessages.size() == expectedErrors);
90 assertTrue(testCase,errors.size() == expectedErrors);