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