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 static org.junit.Assert.fail;
27 import org.apache.commons.io.IOUtils;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
32 import org.openecomp.core.utilities.file.FileContentHandler;
33 import org.openecomp.sdc.common.errors.CoreException;
34 import org.openecomp.sdc.logging.api.Logger;
35 import org.openecomp.sdc.logging.api.LoggerFactory;
36 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.nio.charset.StandardCharsets;
43 public class ToscaSolConverterVnfTest {
44 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaSolConverterVnfTest.class);
46 private AbstractToscaSolConverter toscaSolConverter;
47 private FileContentHandler fileContentHandler;
51 toscaSolConverter = new ToscaSolConverterVnf();
52 fileContentHandler = new FileContentHandler();
56 public void testGivenSOL004WithMetadataDirectoryPackage_whenToscaSolConverterIsCalled_validToscaServiceModelIsReturned() {
57 fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta",
58 ("TOSCA-Meta-File-Version: 1.0\n " +
59 "CSAR-Version: 1.1\n" +
60 "Created-By: Ericsson\n" +
61 "Entry-Definitions: Definitions/Main.yaml\n" +
62 "Entry-Manifest: Main.mf\n" +
63 "Entry-Change-Log: Artifacts/ChangeLog.txt")
64 .getBytes(StandardCharsets.UTF_8));
66 final String mainServiceTemplate = "Main.yaml";
67 final String mainManifest = "Main.mf";
69 fileContentHandler.addFile("Definitions/" + mainServiceTemplate, getFileResource("/toscaSOlConverter/Main.yaml"));
70 fileContentHandler.addFile(mainManifest, "".getBytes());
71 fileContentHandler.addFile("Definitions/sample_import1.yaml", getFileResource("/toscaSOlConverter/sample_import1.yaml"));
72 fileContentHandler.addFile("Definitions/sample_import2.yaml", getFileResource("/toscaSOlConverter/sample_import2.yaml"));
73 fileContentHandler.addFile("Artifacts/sample_import3.yaml", getFileResource("/toscaSOlConverter/sample_import3.yaml"));
74 fileContentHandler.addFile("Artifacts/sample_import4.yaml", getFileResource("/toscaSOlConverter/sample_import4.yaml"));
75 fileContentHandler.addFile("sample_import5.yaml", getFileResource("/toscaSOlConverter/sample_import3.yaml"));
77 final ToscaServiceModel toscaServiceModel = convertToscaSol();
78 final FileContentHandler contentHandler = toscaServiceModel.getArtifactFiles();
79 final Map<String, ServiceTemplate> serviceTemplateMap = toscaServiceModel.getServiceTemplates();
80 final String entryDefinitionTemplateName = toscaServiceModel.getEntryDefinitionServiceTemplate();
81 Assert.assertTrue("Artifacts should contain external files", contentHandler.containsFile(mainManifest));
82 Assert.assertTrue("Main service template should exist", serviceTemplateMap.containsKey(mainServiceTemplate));
83 Assert.assertEquals("Entry Definition name should be same as passed in TOSCA.meta",
84 mainServiceTemplate, entryDefinitionTemplateName);
87 @Test(expected = RuntimeException.class)
88 public void testGivenSOL004InvalidDirectoryPackage_whenToscaSolConverterIsCalled_exceptionIsExpected() {
89 fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta",
90 ("TOSCA-Meta-File-Version: 1.0\n " +
91 "CSAR-Version: 1.1\n" +
92 "Created-by: Ericsson\n" +
93 "Entry-Definitions: Definitions/Main.yaml\n" +
94 "Entry-Manifest: Main.mf\n" +
95 "Entry-Change-Log: Artifacts/ChangeLog.txt")
96 .getBytes(StandardCharsets.UTF_8));
98 fileContentHandler.addFile("Definitions/Main.yaml", getFileResource("/toscaSOlConverter/Main.yaml"));
99 fileContentHandler.addFile("Main.mf", "".getBytes());
100 fileContentHandler.addFile("Definitions/sample_import1.yaml", getFileResource("/toscaSOlConverter/sample_import3.yaml"));
105 @Test(expected = IOException.class)
106 public void testGivenMetaFileDoesNotExist_thenAnExceptionIsThrown() throws IOException {
107 toscaSolConverter.convert(fileContentHandler);
110 @Test(expected = CoreException.class)
111 public void testGivenInvalidServiceTemplate_thenAnExceptionIsThrown() {
113 fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta",
114 ("TOSCA-Meta-File-Version: 1.0\n " +
115 "CSAR-Version: 1.1\n" +
116 "Created-By: Ericsson\n" +
117 "Entry-Definitions: Definitions/Main.yaml\n" +
118 "Entry-Manifest: Main.mf\n" +
119 "Entry-Change-Log: Artifacts/ChangeLog.txt")
120 .getBytes(StandardCharsets.UTF_8));
122 fileContentHandler.addFile("Definitions/Main.yaml", getFileResource("/toscaSOlConverter/invalidMainService.yaml"));
126 private ToscaServiceModel convertToscaSol() {
128 return toscaSolConverter.convert(fileContentHandler);
129 } catch (final IOException e) {
130 final String errorMsg = "Could convert file content handler";
131 LOGGER.error(errorMsg, e);
137 private byte[] getFileResource(final String filePath) {
138 try (final InputStream inputStream = this.getClass().getResourceAsStream(filePath)) {
139 return IOUtils.toByteArray(inputStream);
140 } catch (final IOException ex) {
141 fail(String.format("Could not load file: %s", filePath));