5a43ab5e7183901b91a52a4764fd444b52b350b7
[sdc.git] /
1 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
2
3 import org.junit.Before;
4 import org.junit.Test;
5 import org.openecomp.core.utilities.file.FileContentHandler;
6 import java.io.IOException;
7 import java.nio.charset.StandardCharsets;
8
9 import static org.junit.Assert.assertEquals;
10 import static org.openecomp.sdc.tosca.csar.CSARConstants.SEPARATOR_MF_ATTRIBUTE;
11 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS;
12 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_CHANGE_LOG;
13 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_MANIFEST;
14 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME;
15 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_CHANGELOG_FILEPATH;
16 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_DEFINITION_FILEPATH;
17 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_MANIFEST_FILEPATH;
18
19 public class ValidatorFactoryTest {
20
21     private String metaFile;
22     private FileContentHandler handler;
23
24     @Before
25     public void setUp(){
26         handler = new FileContentHandler();
27         metaFile =
28                 "TOSCA-Meta-File-Version: 1.0\n" +
29                 "CSAR-Version: 1.1\n" +
30                 "Created-by: Bilal Iqbal\n";
31     }
32
33     @Test(expected = IOException.class)
34     public void testGivenEmptyMetaFile_thenIOExceptionIsThrown() throws IOException{
35         handler.addFile(TOSCA_META_PATH_FILE_NAME, "".getBytes(StandardCharsets.UTF_8));
36         handler.addFile(TOSCA_DEFINITION_FILEPATH, "".getBytes());
37         handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
38         handler.addFile(TOSCA_MANIFEST_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
39
40         ValidatorFactory.getValidator(handler);
41     }
42
43     @Test
44     public void testGivenEmptyBlock0_thenONAPCsarValidatorIsReturned() throws IOException{
45         handler.addFile(TOSCA_META_PATH_FILE_NAME, " ".getBytes(StandardCharsets.UTF_8));
46         handler.addFile(TOSCA_DEFINITION_FILEPATH, "".getBytes());
47         handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
48         handler.addFile(TOSCA_MANIFEST_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
49
50         assertEquals(ONAPCsarValidator.class, ValidatorFactory.getValidator(handler).getClass());
51     }
52
53
54     @Test
55     public void testGivenNonSOL004MetaDirectoryCompliantMetaFile_thenONAPCSARValidatorIsReturned() throws IOException{
56         metaFile = metaFile +
57                 TOSCA_META_ENTRY_DEFINITIONS + SEPARATOR_MF_ATTRIBUTE + TOSCA_DEFINITION_FILEPATH;
58         handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8));
59
60         assertEquals(ONAPCsarValidator.class, ValidatorFactory.getValidator(handler).getClass());
61     }
62
63     @Test
64     public void testGivenSOL004MetaDirectoryCompliantMetafile_thenONAPCsarValidatorIsReturned() throws IOException{
65
66         metaFile = metaFile +
67                 TOSCA_META_ENTRY_DEFINITIONS + SEPARATOR_MF_ATTRIBUTE + TOSCA_DEFINITION_FILEPATH + "\n"
68                 + TOSCA_META_ETSI_ENTRY_MANIFEST + SEPARATOR_MF_ATTRIBUTE + TOSCA_MANIFEST_FILEPATH + "\n"
69                 + TOSCA_META_ETSI_ENTRY_CHANGE_LOG + SEPARATOR_MF_ATTRIBUTE + TOSCA_CHANGELOG_FILEPATH + "\n";
70         handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8));
71
72        assertEquals(SOL004MetaDirectoryValidator.class, ValidatorFactory.getValidator(handler).getClass());
73     }
74
75     @Test
76     public void testGivenMultiBlockMetadataWithSOL00CompliantMetaFile_thenSOL004MetaDirectoryValidatorReturned() throws IOException {
77
78         handler.addFile(TOSCA_META_PATH_FILE_NAME, ValidatorUtil.getFileResource("/validation.files/metafile/metaFileWithMultipleBlocks.meta"));
79         handler.addFile(TOSCA_DEFINITION_FILEPATH, "".getBytes());
80         handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
81         handler.addFile(TOSCA_MANIFEST_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
82
83         assertEquals(SOL004MetaDirectoryValidator.class, ValidatorFactory.getValidator(handler).getClass());
84
85     }
86
87 }