[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / test / java / org / openecomp / sdc / translator / services / heattotosca / helper / ContrailTranslationHelperTest.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.helper;
22
23 import org.junit.Assert;
24 import org.junit.Test;
25 import org.openecomp.sdc.heat.datatypes.model.Resource;
26 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Optional;
33
34 import static org.junit.Assert.*;
35
36 /**
37  * @author SHIRIA
38  * @since December 21, 2016.
39  */
40 public class ContrailTranslationHelperTest {
41
42   @Test
43   public void testgetComputeNodeTypeId()
44       throws Exception {
45     Resource serviceTemplate = new Resource();
46     serviceTemplate.setProperties(new HashMap<>());
47     serviceTemplate.getProperties().put("image_name", "aaaa");
48     Map flavor = new HashMap<>();
49     flavor.put("get_param", "bbb_flavor_name");
50     serviceTemplate.getProperties().put("flavor", flavor);
51     String computeNodeTypeId =
52         new ContrailTranslationHelper()
53             .getComputeNodeTypeId(serviceTemplate, "123", "123", new TranslationContext());
54     Assert.assertEquals("org.openecomp.resource.vfc.nodes.heat.bbb", computeNodeTypeId);
55   }
56
57   @Test
58   public void testTranslateFnSplitFunctionExp1() {
59     // property value = { "Fn::Split" : [ ",", "management,left,right,other" ] }
60     Map propertyValue = new HashMap();
61     List funcListVal = new ArrayList<>();
62     funcListVal.add(",");
63     funcListVal.add("management,left,right,other");
64     propertyValue.put("Fn::Split", funcListVal);
65     Optional<List<Map<String, List>>> translatedFun =
66         new ContrailTranslationHelper().translateFnSplitFunction(propertyValue, 4, false);
67
68     assertEquals(true, translatedFun.isPresent());
69     if (translatedFun.isPresent()) {
70       assertEquals(4, translatedFun.get().size());
71       for (int i = 0; i < translatedFun.get().size(); i++) {
72         assertEquals("management,left,right,other", translatedFun.get().get(i).get("token").get(0));
73         assertEquals(",", translatedFun.get().get(i).get("token").get(1));
74         assertEquals(i, translatedFun.get().get(i).get("token").get(2));
75       }
76     }
77   }
78
79   @Test
80   public void testTranslateFnSplitFunctionBoolean() {
81     // property value = { "Fn::Split" : [ ";", "n;false;false;false" ] }
82     Map propertyValue = new HashMap();
83     List funcListVal = new ArrayList<>();
84     funcListVal.add(";");
85     funcListVal.add("n;false;false;false");
86     propertyValue.put("Fn::Split", funcListVal);
87     Optional<List<Map<String, List>>> translatedFun =
88         new ContrailTranslationHelper().translateFnSplitFunction(propertyValue, 4, true);
89
90     assertEquals(true, translatedFun.isPresent());
91     if (translatedFun.isPresent()) {
92       assertEquals(4, translatedFun.get().size());
93       for (int i = 0; i < translatedFun.get().size(); i++) {
94         assertEquals("false;false;false;false", translatedFun.get().get(i).get("token").get(0));
95         assertEquals(";", translatedFun.get().get(i).get("token").get(1));
96         assertEquals(i, translatedFun.get().get(i).get("token").get(2));
97       }
98     }
99   }
100
101   @Test
102   public void testTranslateFnSplitFunctionExp2() {
103     // property value =  { "Fn::Split" : [ ";", "n;false;false;false" ] }
104     Map propertyValue = new HashMap();
105     List funcListVal = new ArrayList<>();
106     funcListVal.add(";");
107     funcListVal.add("n;false;false;false");
108     propertyValue.put("Fn::Split", funcListVal);
109     Optional<List<Map<String, List>>> translatedFun =
110         new ContrailTranslationHelper().translateFnSplitFunction(propertyValue, 4, false);
111
112     assertEquals(true, translatedFun.isPresent());
113     if (translatedFun.isPresent()) {
114       assertEquals(4, translatedFun.get().size());
115       for (int i = 0; i < translatedFun.get().size(); i++) {
116         assertEquals("n;false;false;false", translatedFun.get().get(i).get("token").get(0));
117         assertEquals(";", translatedFun.get().get(i).get("token").get(1));
118         assertEquals(i, translatedFun.get().get(i).get("token").get(2));
119       }
120     }
121   }
122
123   @Test
124   public void testTranslateFnSplitFunctionWithParam() {
125     // property value = { "Fn::Split" : [ ",", Ref: st_shared_ip_list ] }
126     Map propertyValue = new HashMap();
127     List funcListVal = new ArrayList<>();
128     funcListVal.add(",");
129     Map innerMap = new HashMap();
130     innerMap.put("Ref", "st_shared_ip_list");
131     funcListVal.add(innerMap);
132     propertyValue.put("Fn::Split", funcListVal);
133     Optional<List<Map<String, List>>> translatedFun =
134         new ContrailTranslationHelper().translateFnSplitFunction(propertyValue, 5, false);
135
136     assertEquals(true, translatedFun.isPresent());
137     if (translatedFun.isPresent()) {
138       assertEquals(5, translatedFun.get().size());
139       for (int i = 0; i < translatedFun.get().size(); i++) {
140         assertEquals("st_shared_ip_list",
141
142             ((Map) translatedFun.get().get(i).get("token").get(0)).get("get_input"));
143         assertEquals(",", translatedFun.get().get(i).get("token").get(1));
144         assertEquals(i, translatedFun.get().get(i).get("token").get(2));
145       }
146     }
147   }
148 }
149