Copy entry_defintion_type to TOSCA.meta
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / test / java / org / openecomp / sdc / translator / services / heattotosca / impl / functiontranslation / FunctionTranslationGetFileImplTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation;
18
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
22 import org.openecomp.core.utilities.file.FileUtils;
23 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
24 import org.openecomp.sdc.tosca.csar.AsdPackageHelper;
25 import org.openecomp.sdc.tosca.csar.ManifestUtils;
26 import org.openecomp.sdc.tosca.services.ToscaConstants;
27 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
28 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
29 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
30 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory;
31
32 import java.util.HashMap;
33 import java.util.List;
34
35 import static org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation.FunctionTranslator.getFunctionTranslateTo;
36
37 public class FunctionTranslationGetFileImplTest {
38   @Test
39   public void testGetFileWithExtensionFunction() {
40     String functionName = "get_file";
41     Object function = "scripFileName.sh";
42     String heatFileName = "heatFileName";
43     HeatOrchestrationTemplate heatOrchestrationTemplate = new HeatOrchestrationTemplate();
44     NodeTemplate nodeTemplate = new NodeTemplate();
45     TranslationContext context = new TranslationContext();
46
47     testGetToscaFunctionForGetFile(functionName, function, heatFileName, heatOrchestrationTemplate,
48         nodeTemplate, context);
49   }
50
51   @Test
52   public void testGetFileWithoutExtensionFunction() {
53     String functionName = "get_file";
54     Object function = "scripFileName";
55     String heatFileName = "heatFileName";
56     HeatOrchestrationTemplate heatOrchestrationTemplate = new HeatOrchestrationTemplate();
57     NodeTemplate nodeTemplate = new NodeTemplate();
58     TranslationContext context = new TranslationContext();
59
60     //#      route_targets: { "Fn::Split" : [ ",", Ref: route_targets ] }
61     testGetToscaFunctionForGetFile(functionName, function, heatFileName, heatOrchestrationTemplate,
62         nodeTemplate, context);
63   }
64
65   private void testGetToscaFunctionForGetFile(String functionName, Object function,
66                                               String heatFileName,
67                                               HeatOrchestrationTemplate heatOrchestrationTemplate,
68                                               NodeTemplate nodeTemplate,
69                                               TranslationContext context) {
70     Assert.assertTrue(FunctionTranslationFactory.getInstance(functionName).isPresent());
71     if(FunctionTranslationFactory.getInstance(functionName).isPresent()) {
72       FunctionTranslator functionTranslator = new FunctionTranslator(getFunctionTranslateTo(null, null, heatFileName,
73               heatOrchestrationTemplate, context), null, function, nodeTemplate);
74       Object result = FunctionTranslationFactory.getInstance(functionName).get()
75           .translateFunction(functionTranslator);
76       Assert.assertNotNull(((HashMap) result).get("get_artifact"));
77       List artifactParameters = (List) ((HashMap) result).get("get_artifact");
78       Assert.assertNotNull(artifactParameters);
79       Assert.assertEquals(2, artifactParameters.size());
80       Assert.assertEquals(ToscaConstants.MODELABLE_ENTITY_NAME_SELF, artifactParameters.get(0));
81       Assert.assertEquals(((String) function).split("\\.")[0], artifactParameters.get(1));
82
83       Assert.assertNotNull(nodeTemplate.getArtifacts());
84       Assert.assertNotNull(
85           nodeTemplate.getArtifacts().get(FileUtils.getFileWithoutExtention((String) function)));
86       ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl(new AsdPackageHelper(new ManifestUtils()));
87       Assert.assertEquals(
88           nodeTemplate.getArtifacts().get(FileUtils.getFileWithoutExtention((String) function))
89               .getFile(), "../" + toscaFileOutputService.getArtifactsFolderName() + "/" + function);
90     }
91   }
92
93 }