2 * Copyright © 2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.heat.datatypes.model;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.onap.sdc.tosca.services.YamlUtil;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
30 public class HeatOrchestrationTemplateTest {
32 public void testYamlToServiceTemplateObj() throws IOException {
33 YamlUtil yamlUtil = new YamlUtil();
34 try (InputStream yamlFile = yamlUtil.loadYamlFileIs("/mock/model/testHeat.yml")) {
35 HeatOrchestrationTemplate heatOrchestrationTemplate =
36 yamlUtil.yamlToObject(yamlFile, HeatOrchestrationTemplate.class);
37 heatOrchestrationTemplate.toString();
42 public void createHotTemplate() throws Exception{
43 HeatOrchestrationTemplate template = new HeatOrchestrationTemplate();
44 template.setHeat_template_version("2016-04-14");
45 template.setDescription("test description for hot template");
46 Map<String, Parameter> params = createParameters();
47 template.setParameters(params);
48 List<ParameterGroup> parameterGroup = new ArrayList<>();
49 ParameterGroup paramsGroup = new ParameterGroup();
50 paramsGroup.setDescription("params group test description");
51 paramsGroup.setLabel("params group test label");
52 String paramName = params.get("param1").getLabel();
53 List<String> paramsNames = new ArrayList<>();
54 paramsNames.add(paramName);
55 paramsGroup.setParameters(paramsNames);
56 parameterGroup.add(paramsGroup);
57 template.setParameter_groups(parameterGroup);
58 Map<String, Object> conditions = new HashMap<>();
59 conditions.put("key1", "val1");
60 HashMap<String, Object> mapValue = new HashMap<>();
61 mapValue.put("innerKey", "innerVal");
62 conditions.put("key2", mapValue);
63 template.setConditions(conditions);
65 Map<String, Resource> resources = new HashMap<>();
66 Resource resource = new Resource();
67 resource.setMetadata("resource metadata");
68 resource.setType("resource type");
69 //Map<String, String> resourceProps = new ;
70 Map<String, Object> resourceProps = new HashMap<>();
71 resourceProps.put("aaa", "bbb");
72 //resourceProps.add(resourceProp);
73 resource.setProperties(resourceProps);
74 resources.put("R1", resource);
75 resource = new Resource();
76 resource.setMetadata("resource2 metadata");
77 resource.setType("resource2 type");
78 //resourceProps = new ArrayList<>();
79 resourceProps = new HashMap<>();
80 resourceProps.put("aaa2", "bbb2");
81 //resourceProps.add(resourceProp);
82 resource.setProperties(resourceProps);
83 List<String> dependsOn = new ArrayList<>();
85 resource.setDepends_on(dependsOn);
86 resource.setDeletion_policy("all");
87 resource.setUpdate_policy("once");
88 resources.put("R2", resource);
89 template.setResources(resources);
91 YamlUtil yamlUtil = new YamlUtil();
92 String yml = yamlUtil.objectToYaml(template);
93 Assert.assertNotNull(yml);
94 HeatOrchestrationTemplate heatOrchestrationTemplate;
95 heatOrchestrationTemplate =
96 yamlUtil.yamlToObject(yml, HeatOrchestrationTemplate.class);
97 Assert.assertNotNull(heatOrchestrationTemplate);
100 private Map<String, Parameter> createParameters() {
101 Map<String, Parameter> params = new HashMap<>();
103 for (int i = 0; i < 2; i++) {
104 param = new Parameter();
105 param.setDescription("param " + i + " desc");
106 param.setLabel("param " + i + " label");
107 param.set_default("PARAM " + i + " default");
108 param.setHidden(i % 2 == 0);
109 param.setImmutable(i % 2 == 0);
110 param.setType(i % 2 == 0 ? ParameterType.STRING.getDisplayName()
111 : ParameterType.BOOLEAN.getDisplayName());
112 params.put("param" + i, param);
118 private List<Constraint> createConstraints() {
119 List<Constraint> constraints = new ArrayList<>();
120 Constraint constraint = new Constraint();
121 constraint.setLength(new Integer[]{2, 4});
122 constraints.add(constraint);
123 constraint = new Constraint();
124 constraint.setPattern("some regex");
125 constraints.add(constraint);
126 constraint = new Constraint();
127 constraint.setRange(new Integer[]{5, 8});
128 constraints.add(constraint);
129 constraint = new Constraint();
130 List<Object> validValues = new ArrayList<>();
131 validValues.add("abc");
132 validValues.add("def");
133 constraint.setValidValues(validValues);
134 constraints.add(constraint);