Onboard PNF software version
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / impl / orchestration / csar / validation / ONAPCsarValidatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
22
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.List;
30 import java.util.Map;
31
32 import static org.junit.Assert.assertEquals;
33
34
35 public class ONAPCsarValidatorTest {
36
37     private ONAPCsarValidator onapCsarValidator;
38     private FileContentHandler contentHandler;
39
40     @Before
41     public void setUp() throws IOException{
42         onapCsarValidator = new ONAPCsarValidator();
43         contentHandler = new FileContentHandler();
44         contentHandler.addFile("TOSCA-Metadata/TOSCA.meta", ValidatorUtil.getFileResource("/validation.files/metafile/nonSOL004WithMetaDirectoryCompliantMetaFile.meta"));
45         contentHandler.addFile("MainServiceTemplate.mf", ValidatorUtil.getFileResource("/validation.files/manifest/sampleManifest.mf"));
46         contentHandler.addFile(TestConstants.TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(TestConstants.SAMPLE_DEFINITION_FILE_PATH));
47     }
48
49     @Test
50     public void testGivenCSARPackage_withValidContent_thenNoErrorsReturned() {
51         assertExpectedErrors("Valid CSAR Package should have 0 errors",
52                 onapCsarValidator.validateContent(contentHandler), 0);
53     }
54
55     @Test
56     public void testGivenCSARPackage_withInvalidManifestFile_thenErrorsReturned() throws IOException{
57         contentHandler = new FileContentHandler();
58         contentHandler.addFile("TOSCA-Metadata/TOSCA.meta", ValidatorUtil.getFileResource("/validation.files/metafile/nonSOL004WithMetaDirectoryCompliantMetaFile.meta"));
59         contentHandler.addFile("MainServiceTemplate.mf", ValidatorUtil.getFileResource("/validation.files/manifest/invalidManifest.mf"));
60         contentHandler.addFile(TestConstants.TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(TestConstants.SAMPLE_DEFINITION_FILE_PATH));
61
62         assertExpectedErrors("CSAR package with invalid manifest file should have errors", onapCsarValidator.validateContent(contentHandler), 1);
63
64     }
65
66     @Test
67     public void testGivenCSARPackage_withUnwantedFolders_thenErrorsReturned(){
68         contentHandler.addFolder("Files/");
69         assertExpectedErrors("CSAR package with unwanted folders should fail with errors", onapCsarValidator.validateContent(contentHandler), 1);
70     }
71
72     @Test
73     public void testGivenCSARPackage_withUnwantedFiles_thenErrorsReturned(){
74         contentHandler.addFile("ExtraFile.text", "".getBytes());
75         assertExpectedErrors("CSAR package with unwanted files should fail with errors",
76                 onapCsarValidator.validateContent(contentHandler), 1);
77     }
78
79     private void assertExpectedErrors( String testCase, Map<String, List<ErrorMessage>> errors, int expectedErrors){
80         if(expectedErrors > 0){
81             List<ErrorMessage> errorMessages = errors.get(SdcCommon.UPLOAD_FILE);
82             assertEquals(testCase, expectedErrors, errorMessages.size());
83         }else{
84             assertEquals(testCase, expectedErrors, errors.size());
85         }
86     }
87 }