3715b0f99999a2546f250c00940dd9f53ca44319
[sdc.git] /
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.heat.datatypes.model;
22
23 import org.junit.Assert;
24 import org.junit.Test;
25 import org.openecomp.sdc.logging.api.Logger;
26 import org.openecomp.sdc.logging.api.LoggerFactory;
27 import org.openecomp.sdc.tosca.services.YamlUtil;
28
29 import java.io.InputStream;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 public class HeatOrchestrationTemplateTest {
36
37   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
38
39   @Test
40   public void testYamlToServiceTemplateObj() {
41     YamlUtil yamlUtil = new YamlUtil();
42     InputStream yamlFile = yamlUtil.loadYamlFileIs("/mock/model/testHeat.yml");
43     HeatOrchestrationTemplate heatOrchestrationTemplate =
44         yamlUtil.yamlToObject(yamlFile, HeatOrchestrationTemplate.class);
45     heatOrchestrationTemplate.toString();
46   }
47
48   @Test
49   public void createHotTemplate() {
50     HeatOrchestrationTemplate template = new HeatOrchestrationTemplate();
51     template.setHeat_template_version("2016-04-14");
52     template.setDescription("test description for hot template");
53     Map<String, Parameter> params = createParameters();
54     template.setParameters(params);
55     List<ParameterGroup> parameterGroup = new ArrayList<>();
56     ParameterGroup paramsGroup = new ParameterGroup();
57     paramsGroup.setDescription("params group test description");
58     paramsGroup.setLabel("params group test label");
59     String paramName = params.get("param1").getLabel();
60     List<String> paramsNames = new ArrayList<>();
61     paramsNames.add(paramName);
62     paramsGroup.setParameters(paramsNames);
63     parameterGroup.add(paramsGroup);
64     template.setParameter_groups(parameterGroup);
65     Map<String, Object> conditions = new HashMap<>();
66     conditions.put("key1", "val1");
67     HashMap<String, Object> mapValue = new HashMap<>();
68     mapValue.put("innerKey", "innerVal");
69     conditions.put("key2", mapValue);
70     template.setConditions(conditions);
71
72     Map<String, Resource> resources = new HashMap<>();
73     Resource resource = new Resource();
74     resource.setMetadata("resource metadata");
75     resource.setType("resource type");
76     //Map<String, String> resourceProps = new ;
77     Map<String, Object> resourceProps = new HashMap<>();
78     resourceProps.put("aaa", "bbb");
79     //resourceProps.add(resourceProp);
80     resource.setProperties(resourceProps);
81     resources.put("R1", resource);
82     resource = new Resource();
83     resource.setMetadata("resource2 metadata");
84     resource.setType("resource2 type");
85     //resourceProps = new ArrayList<>();
86     resourceProps = new HashMap<>();
87     resourceProps.put("aaa2", "bbb2");
88     //resourceProps.add(resourceProp);
89     resource.setProperties(resourceProps);
90     List<String> dependsOn = new ArrayList<>();
91     dependsOn.add("R1");
92     resource.setDepends_on(dependsOn);
93     resource.setDeletion_policy("all");
94     resource.setUpdate_policy("once");
95     resources.put("R2", resource);
96     template.setResources(resources);
97
98     YamlUtil yamlUtil = new YamlUtil();
99     String yml = yamlUtil.objectToYaml(template);
100     Assert.assertNotNull(yml);
101     try {
102       HeatOrchestrationTemplate heatOrchestrationTemplate =
103           yamlUtil.yamlToObject(yml, HeatOrchestrationTemplate.class);
104       Assert.assertNotNull(heatOrchestrationTemplate);
105     } catch (Exception ignored) {
106       log.debug("",ignored);
107     }
108   }
109
110   private Map<String, Parameter> createParameters() {
111     Map<String, Parameter> params = new HashMap<>();
112     Parameter param;
113     for (int i = 0; i < 2; i++) {
114       param = new Parameter();
115       param.setDescription("param " + i + " desc");
116       param.setLabel("param " + i + " label");
117       param.set_default("PARAM " + i + " default");
118       param.setHidden(i % 2 == 0);
119       param.setImmutable(i % 2 == 0);
120       param.setType(i % 2 == 0 ? ParameterType.STRING.getDisplayName()
121           : ParameterType.BOOLEAN.getDisplayName());
122       params.put("param" + i, param);
123     }
124
125     return params;
126   }
127
128   private List<Constraint> createConstraints() {
129     List<Constraint> constraints = new ArrayList<>();
130     Constraint constraint = new Constraint();
131     constraint.setLength(new Integer[]{2, 4});
132     constraints.add(constraint);
133     constraint = new Constraint();
134     constraint.setPattern("some regex");
135     constraints.add(constraint);
136     constraint = new Constraint();
137     constraint.setRange(new Integer[]{5, 8});
138     constraints.add(constraint);
139     constraint = new Constraint();
140     List<Object> validValues = new ArrayList<>();
141     validValues.add("abc");
142     validValues.add("def");
143     constraint.setValidValues(validValues);
144     constraints.add(constraint);
145     return constraints;
146   }
147 }