Add a CSAR validator for model ETSI SOL001 2.5.1
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / impl / orchestration / csar / validation / ValidatorFactoryTest.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.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.ToscaMetaEntryVersion261.CREATED_BY_ENTRY;
30 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.CSAR_VERSION_ENTRY;
31 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ENTRY_DEFINITIONS;
32 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_CHANGE_LOG;
33 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_MANIFEST;
34 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.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;
39
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;
46
47 class ValidatorFactoryTest {
48
49     private String metaFile;
50     private FileContentHandler handler;
51     private ValidatorFactory validatorFactory;
52
53     @BeforeEach
54     void setUp(){
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")
64             .toString();
65     }
66
67     @Test
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));
73
74         assertThrows(IOException.class, () -> validatorFactory.getValidator(handler));
75     }
76
77     @Test
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));
83
84         assertEquals(ONAPCsarValidator.class, validatorFactory.getValidator(handler).getClass());
85     }
86
87
88     @Test
89     void testGivenNonSOL004MetaDirectoryCompliantMetaFile_thenONAPCSARValidatorIsReturned() throws IOException {
90         metaFile = metaFile +
91                 ENTRY_DEFINITIONS.getName() + ATTRIBUTE_VALUE_SEPARATOR.getToken() + TOSCA_DEFINITION_FILEPATH;
92         handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8));
93
94         assertEquals(ONAPCsarValidator.class, validatorFactory.getValidator(handler).getClass());
95     }
96
97     @Test
98     void testGivenSOL004MetaDirectoryCompliantMetafile_thenONAPCsarValidatorIsReturned() throws IOException {
99         metaFile = metaFile +
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));
104
105        assertEquals(SOL004MetaDirectoryValidator.class, validatorFactory.getValidator(handler).getClass());
106     }
107
108     @Test
109     void testGivenMultiBlockMetadataWithSOL00CompliantMetaFile_thenSOL004MetaDirectoryValidatorReturned()
110             throws IOException {
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));
116
117         assertEquals(SOL004MetaDirectoryValidator.class, validatorFactory.getValidator(handler).getClass());
118
119     }
120
121     @Test
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);
128
129         final List<Validator> validatorList1 = validatorFactory.getValidators("test1");
130         assertTrue(validatorList1.isEmpty());
131     }
132
133 }