push addional code
[sdc.git] / openecomp-be / lib / openecomp-heat-lib / src / test / java / org / openecomp / sdc / heat / datatypes / model / HeatOrchestrationTemplateTest.java
1 package org.openecomp.sdc.heat.datatypes.model;
2
3 import org.openecomp.core.utilities.yaml.YamlUtil;
4 import org.junit.Assert;
5 import org.junit.Test;
6
7 import java.io.InputStream;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12
13 public class HeatOrchestrationTemplateTest {
14
15   @Test
16   public void testYamlToServiceTemplateObj() {
17     YamlUtil yamlUtil = new YamlUtil();
18     InputStream yamlFile = yamlUtil.loadYamlFileIs("/mock/model/testHeat.yml");
19     HeatOrchestrationTemplate heatOrchestrationTemplate =
20         yamlUtil.yamlToObject(yamlFile, HeatOrchestrationTemplate.class);
21     heatOrchestrationTemplate.toString();
22   }
23
24   @Test
25   public void createHotTemplate() {
26     HeatOrchestrationTemplate template = new HeatOrchestrationTemplate();
27     template.setHeat_template_version("2016-04-14");
28     template.setDescription("test description for hot template");
29     Map<String, Parameter> params = createParameters();
30     template.setParameters(params);
31     List<ParameterGroup> parameterGroup = new ArrayList<>();
32     ParameterGroup paramsGroup = new ParameterGroup();
33     paramsGroup.setDescription("params group test description");
34     paramsGroup.setLabel("params group test label");
35     String paramName = params.get("param1").getLabel();
36     List<String> paramsNames = new ArrayList<>();
37     paramsNames.add(paramName);
38     paramsGroup.setParameters(paramsNames);
39     parameterGroup.add(paramsGroup);
40     template.setParameter_groups(parameterGroup);
41     Map<String, Object> conditions = new HashMap<>();
42     conditions.put("key1", "val1");
43     HashMap<String, Object> mapValue = new HashMap<>();
44     mapValue.put("innerKey", "innerVal");
45     conditions.put("key2", mapValue);
46     template.setConditions(conditions);
47
48     Map<String, Resource> resources = new HashMap<>();
49     Resource resource = new Resource();
50     resource.setMetadata("resource metadata");
51     resource.setType("resource type");
52     //Map<String, String> resourceProps = new ;
53     Map<String, Object> resourceProps = new HashMap<>();
54     resourceProps.put("aaa", "bbb");
55     //resourceProps.add(resourceProp);
56     resource.setProperties(resourceProps);
57     resources.put("R1", resource);
58     resource = new Resource();
59     resource.setMetadata("resource2 metadata");
60     resource.setType("resource2 type");
61     //resourceProps = new ArrayList<>();
62     resourceProps = new HashMap<>();
63     resourceProps.put("aaa2", "bbb2");
64     //resourceProps.add(resourceProp);
65     resource.setProperties(resourceProps);
66     List<String> dependsOn = new ArrayList<>();
67     dependsOn.add("R1");
68     resource.setDepends_on(dependsOn);
69     resource.setDeletion_policy("all");
70     resource.setUpdate_policy("once");
71     resources.put("R2", resource);
72     template.setResources(resources);
73
74     YamlUtil yamlUtil = new YamlUtil();
75     String yml = yamlUtil.objectToYaml(template);
76     Assert.assertNotNull(yml);
77     try {
78       HeatOrchestrationTemplate heatOrchestrationTemplate =
79           yamlUtil.yamlToObject(yml, HeatOrchestrationTemplate.class);
80       Assert.assertNotNull(heatOrchestrationTemplate);
81     } catch (Exception ignored) {
82     }
83   }
84
85   private Map<String, Parameter> createParameters() {
86     Map<String, Parameter> params = new HashMap<>();
87     Parameter param;
88     for (int i = 0; i < 2; i++) {
89       param = new Parameter();
90       param.setDescription("param " + i + " desc");
91       param.setLabel("param " + i + " label");
92       param.set_default("PARAM " + i + " default");
93       param.setHidden(i % 2 == 0);
94       param.setImmutable(i % 2 == 0);
95       param.setType(i % 2 == 0 ? ParameterType.STRING.getDisplayName()
96           : ParameterType.BOOLEAN.getDisplayName());
97       params.put("param" + i, param);
98     }
99
100     return params;
101   }
102
103   private List<Constraint> createConstraints() {
104     List<Constraint> constraints = new ArrayList<>();
105     Constraint constraint = new Constraint();
106     constraint.setLength(new Integer[]{2, 4});
107     constraints.add(constraint);
108     constraint = new Constraint();
109     constraint.setPattern("some regex");
110     constraints.add(constraint);
111     constraint = new Constraint();
112     constraint.setRange(new Integer[]{5, 8});
113     constraints.add(constraint);
114     constraint = new Constraint();
115     List<Object> validValues = new ArrayList<>();
116     validValues.add("abc");
117     validValues.add("def");
118     constraint.setValid_values(validValues);
119     constraints.add(constraint);
120     return constraints;
121   }
122 }