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
10 * * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * * SPDX-License-Identifier: Apache-2.0
19 * * ============LICENSE_END=========================================================
23 package org.openecomp.core.impl;
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;
39 public class ToscaSolConverterImplTest {
41 private ToscaSolConverterImpl toscaSolConverter;
42 private FileContentHandler fileContentHandler;
46 toscaSolConverter = new ToscaSolConverterImpl();
47 fileContentHandler = new FileContentHandler();
52 public void testGivenSOL004WithMetadataDirectoryPackage_whenToscaSolConverterIsCalled_validToscaServiceModelIsReturned()
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));
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("Definitions/Main.yaml"));
75 Assert.assertEquals("Entry Definition name should be same as passed in TOSCA.meta",
76 "Definitions/Main.yaml", entryDefinitionTemplateName);
79 @Test(expected = IOException.class)
80 public void testGivenMetaFileDoesNotExist_thenAnExceptionIsThrown() throws IOException{
81 toscaSolConverter.convert(fileContentHandler);
84 @Test(expected = CoreException.class)
85 public void testGivenInvalidServiceTemplate_thenAnExceptionIsThrown() throws IOException{
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));
96 fileContentHandler.addFile("Definitions/Main.yaml", getFileResource("/toscaSOlConverter/invalidMainService.yaml"));
97 toscaSolConverter.convert(fileContentHandler);
100 private byte[] getFileResource(String filePath) throws IOException {
101 InputStream inputStream = ClassLoader.class.getClass().getResourceAsStream(filePath);
102 return IOUtils.toByteArray(inputStream);