bfc1fa9d85d67d8ee2c20188f84d1d980eab6be0
[sdc.git] /
1 /*
2  * -
3  *  * ============LICENSE_START=======================================================
4  *  *  Copyright (C) 2019  Nordix Foundation.
5  *  * ================================================================================
6  *  * Licensed under the Apache License, Version 2.0 (the "License");
7  *  * you may not use this file except in compliance with the License.
8  *  * You may obtain a copy of the License at
9  *  *
10  *  *      http://www.apache.org/licenses/LICENSE-2.0
11  *  *
12  *  * Unless required by applicable law or agreed to in writing, software
13  *  * distributed under the License is distributed on an "AS IS" BASIS,
14  *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  * See the License for the specific language governing permissions and
16  *  * limitations under the License.
17  *  *
18  *  * SPDX-License-Identifier: Apache-2.0
19  *  * ============LICENSE_END=========================================================
20  *
21  */
22
23 package org.openecomp.core.impl;
24
25 import org.apache.commons.io.IOUtils;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
30 import org.openecomp.core.utilities.file.FileContentHandler;
31 import org.openecomp.sdc.common.errors.CoreException;
32 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.nio.charset.StandardCharsets;
36 import java.util.Map;
37
38
39 public class ToscaSolConverterVnfTest {
40
41     private AbstractToscaSolConverter toscaSolConverter;
42     private FileContentHandler fileContentHandler;
43
44     @Before
45     public void setUp(){
46         toscaSolConverter = new ToscaSolConverterVnf();
47         fileContentHandler = new FileContentHandler();
48     }
49
50
51     @Test
52     public void testGivenSOL004WithMetadataDirectoryPackage_whenToscaSolConverterIsCalled_validToscaServiceModelIsReturned()
53             throws IOException{
54         fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta",
55                 ("TOSCA-Meta-File-Version: 1.0\n " +
56                 "CSAR-Version: 1.1\n" +
57                 "Created-by: Ericsson\n" +
58                 "Entry-Definitions: Definitions/Main.yaml\n" +
59                 "Entry-Manifest: Main.mf\n" +
60                 "Entry-Change-Log: Artifacts/ChangeLog.txt")
61                         .getBytes(StandardCharsets.UTF_8));
62
63         fileContentHandler.addFile("Definitions/Main.yaml", getFileResource("/toscaSOlConverter/Main.yaml"));
64         fileContentHandler.addFile("Main.mf", "".getBytes());
65         fileContentHandler.addFile("Definitions/sample_import1.yaml", getFileResource("/toscaSOlConverter/sample_import1.yaml"));
66         fileContentHandler.addFile("Definitions/sample_import2.yaml", getFileResource("/toscaSOlConverter/sample_import2.yaml"));
67         fileContentHandler.addFile("Artifacts/sample_import3.yaml", getFileResource("/toscaSOlConverter/sample_import3.yaml"));
68         fileContentHandler.addFile("Artifacts/sample_import4.yaml", getFileResource("/toscaSOlConverter/sample_import4.yaml"));
69         ToscaServiceModel toscaServiceModel = toscaSolConverter.convert(fileContentHandler);
70         FileContentHandler contentHandler = toscaServiceModel.getArtifactFiles();
71         Map<String, ServiceTemplate> serviceTemplateMap = toscaServiceModel.getServiceTemplates();
72         String entryDefinitionTemplateName = toscaServiceModel.getEntryDefinitionServiceTemplate();
73         Assert.assertTrue("Artifacts should contain external files", contentHandler.containsFile("Main.mf"));
74         Assert.assertTrue("Main service template should exist", serviceTemplateMap.containsKey("Main.yaml"));
75         Assert.assertEquals("Entry Definition name should be same as passed in TOSCA.meta",
76                 "Main.yaml", entryDefinitionTemplateName);
77     }
78
79     @Test(expected = IOException.class)
80     public void testGivenMetaFileDoesNotExist_thenAnExceptionIsThrown() throws IOException{
81         toscaSolConverter.convert(fileContentHandler);
82     }
83
84     @Test(expected = CoreException.class)
85     public void testGivenInvalidServiceTemplate_thenAnExceptionIsThrown() throws IOException{
86
87         fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta",
88                 ("TOSCA-Meta-File-Version: 1.0\n " +
89                         "CSAR-Version: 1.1\n" +
90                         "Created-by: Ericsson\n" +
91                         "Entry-Definitions: Definitions/Main.yaml\n" +
92                         "Entry-Manifest: Main.mf\n" +
93                         "Entry-Change-Log: Artifacts/ChangeLog.txt")
94                         .getBytes(StandardCharsets.UTF_8));
95
96         fileContentHandler.addFile("Definitions/Main.yaml", getFileResource("/toscaSOlConverter/invalidMainService.yaml"));
97         toscaSolConverter.convert(fileContentHandler);
98     }
99
100     private byte[] getFileResource(String filePath) throws IOException {
101         InputStream inputStream = ClassLoader.class.getClass().getResourceAsStream(filePath);
102         return IOUtils.toByteArray(inputStream);
103     }
104
105 }