287662283cb2ac86aecfe5c229012191135fad47
[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
7 import java.io.IOException;
8 import java.nio.charset.StandardCharsets;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.ENTRY_CHANGE_LOG;
12 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.ENTRY_DEFINITIONS;
13 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.ENTRY_MANIFEST;
14 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.ENTRY_SEPARATOR;
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 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_METADATA_FILEPATH;
19
20 public class ValidatorFactoryTest {
21
22     private String metaFile;
23     private FileContentHandler handler;
24
25     @Before
26     public void setUp(){
27         handler = new FileContentHandler();
28         metaFile =
29                 "TOSCA-Meta-File-Version: 1.0\n" +
30                 "CSAR-Version: 1.1\n" +
31                 "Created-by: Bilal Iqbal\n";
32     }
33
34     @Test(expected = IOException.class)
35     public void testGivenEmptyMetaFile_thenIOExceptionIsThrown() throws IOException{
36         handler.addFile(TOSCA_METADATA_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
37         handler.addFile(TOSCA_DEFINITION_FILEPATH, "".getBytes());
38         handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
39         handler.addFile(TOSCA_MANIFEST_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
40
41         ValidatorFactory.getValidator(handler);
42     }
43
44     @Test
45     public void testGivenEmptyBlock0_thenONAPCsarValidatorIsReturned() throws IOException{
46         handler.addFile(TOSCA_METADATA_FILEPATH, " ".getBytes(StandardCharsets.UTF_8));
47         handler.addFile(TOSCA_DEFINITION_FILEPATH, "".getBytes());
48         handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
49         handler.addFile(TOSCA_MANIFEST_FILEPATH, "".getBytes(StandardCharsets.UTF_8));
50
51         assertEquals(ONAPCsarValidator.class, ValidatorFactory.getValidator(handler).getClass());
52     }
53
54
55     @Test
56     public void testGivenNonSOL004MetaDirectoryCompliantMetaFile_thenONAPCSARValidatorIsReturned() throws IOException{
57         metaFile = metaFile +
58                 ENTRY_DEFINITIONS + ENTRY_SEPARATOR + TOSCA_DEFINITION_FILEPATH;
59         handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8));
60
61         assertEquals(ONAPCsarValidator.class, ValidatorFactory.getValidator(handler).getClass());
62     }
63
64     @Test
65     public void testGivenSOL004MetaDirectoryCompliantMetafile_thenONAPCsarValidatorIsReturned() throws IOException{
66
67         metaFile = metaFile +
68                 ENTRY_DEFINITIONS + ENTRY_SEPARATOR + TOSCA_DEFINITION_FILEPATH + "\n"
69                 + ENTRY_MANIFEST + ENTRY_SEPARATOR + TOSCA_MANIFEST_FILEPATH + "\n"
70                 + ENTRY_CHANGE_LOG + ENTRY_SEPARATOR + TOSCA_CHANGELOG_FILEPATH + "\n";
71         handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8));
72
73        assertEquals(SOL004MetaDirectoryValidator.class, ValidatorFactory.getValidator(handler).getClass());
74     }
75
76     @Test
77     public void testGivenMultiBlockMetadataWithSOL00CompliantMetaFile_thenSOL004MetaDirectoryValidatorReturned() throws IOException {
78
79         handler.addFile(TOSCA_METADATA_FILEPATH, ValidatorUtil.getFileResource("/validation.files/metafile/metaFileWithMultipleBlocks.meta"));
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(SOL004MetaDirectoryValidator.class, ValidatorFactory.getValidator(handler).getClass());
85
86     }
87
88 }