Add new SOL004 ETSI Validator
[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 static org.junit.Assert.assertEquals;
24 import static org.openecomp.sdc.be.test.util.TestResourcesHandler.getResourceBytesOrFail;
25
26 import java.io.IOException;
27 import java.util.List;
28 import java.util.Map;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.openecomp.core.utilities.file.FileContentHandler;
32 import org.openecomp.sdc.common.utils.SdcCommon;
33 import org.openecomp.sdc.datatypes.error.ErrorMessage;
34
35
36 public class ONAPCsarValidatorTest {
37
38     private ONAPCsarValidator onapCsarValidator;
39     private FileContentHandler contentHandler;
40
41     @BeforeEach
42     public void setUp() throws IOException{
43         onapCsarValidator = new ONAPCsarValidator();
44         contentHandler = new FileContentHandler();
45         contentHandler.addFile("TOSCA-Metadata/TOSCA.meta",
46             getResourceBytesOrFail("validation.files/metafile/nonSOL004WithMetaDirectoryCompliantMetaFile.meta"));
47         contentHandler
48             .addFile("MainServiceTemplate.mf", getResourceBytesOrFail("validation.files/manifest/sampleManifest.mf"));
49         contentHandler.addFile(TestConstants.TOSCA_DEFINITION_FILEPATH,
50             getResourceBytesOrFail(TestConstants.SAMPLE_DEFINITION_FILE_PATH));
51     }
52
53     @Test
54     public void testGivenCSARPackage_withValidContent_thenNoErrorsReturned() {
55         assertExpectedErrors("Valid CSAR Package should have 0 errors",
56                 onapCsarValidator.validateContent(contentHandler), 0);
57     }
58
59     @Test
60     public void testGivenCSARPackage_withInvalidManifestFile_thenErrorsReturned() throws IOException{
61         contentHandler = new FileContentHandler();
62         contentHandler.addFile("TOSCA-Metadata/TOSCA.meta",
63             getResourceBytesOrFail("validation.files/metafile/nonSOL004WithMetaDirectoryCompliantMetaFile.meta"));
64         contentHandler
65             .addFile("MainServiceTemplate.mf", getResourceBytesOrFail("validation.files/manifest/invalidManifest.mf"));
66         contentHandler.addFile(TestConstants.TOSCA_DEFINITION_FILEPATH,
67             getResourceBytesOrFail(TestConstants.SAMPLE_DEFINITION_FILE_PATH));
68
69         assertExpectedErrors("CSAR package with invalid manifest file should have errors", onapCsarValidator.validateContent(contentHandler), 1);
70
71     }
72
73     @Test
74     public void testGivenCSARPackage_withUnwantedFolders_thenErrorsReturned(){
75         contentHandler.addFolder("Files/");
76         assertExpectedErrors("CSAR package with unwanted folders should fail with errors", onapCsarValidator.validateContent(contentHandler), 1);
77     }
78
79     @Test
80     public void testGivenCSARPackage_withUnwantedFiles_thenErrorsReturned(){
81         contentHandler.addFile("ExtraFile.text", "".getBytes());
82         assertExpectedErrors("CSAR package with unwanted files should fail with errors",
83                 onapCsarValidator.validateContent(contentHandler), 1);
84     }
85
86     private void assertExpectedErrors( String testCase, Map<String, List<ErrorMessage>> errors, int expectedErrors){
87         if(expectedErrors > 0){
88             List<ErrorMessage> errorMessages = errors.get(SdcCommon.UPLOAD_FILE);
89             assertEquals(testCase, expectedErrors, errorMessages.size());
90         }else{
91             assertEquals(testCase, expectedErrors, errors.size());
92         }
93     }
94 }