4 * Copyright © 2018 European Support Limited
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org.openecomp.sdc.heat.datatypes.model;
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.onap.sdc.tosca.services.YamlUtil;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
32 public class HeatOrchestrationTemplateTest {
34 public void testYamlToServiceTemplateObj() throws IOException {
35 YamlUtil yamlUtil = new YamlUtil();
36 try (InputStream yamlFile = yamlUtil.loadYamlFileIs("/mock/model/testHeat.yml")) {
37 HeatOrchestrationTemplate heatOrchestrationTemplate =
38 yamlUtil.yamlToObject(yamlFile, HeatOrchestrationTemplate.class);
39 heatOrchestrationTemplate.toString();
44 public void createHotTemplate() throws Exception{
45 HeatOrchestrationTemplate template = new HeatOrchestrationTemplate();
46 template.setHeat_template_version("2016-04-14");
47 template.setDescription("test description for hot template");
48 Map<String, Parameter> params = createParameters();
49 template.setParameters(params);
50 List<ParameterGroup> parameterGroup = new ArrayList<>();
51 ParameterGroup paramsGroup = new ParameterGroup();
52 paramsGroup.setDescription("params group test description");
53 paramsGroup.setLabel("params group test label");
54 String paramName = params.get("param1").getLabel();
55 List<String> paramsNames = new ArrayList<>();
56 paramsNames.add(paramName);
57 paramsGroup.setParameters(paramsNames);
58 parameterGroup.add(paramsGroup);
59 template.setParameter_groups(parameterGroup);
60 Map<String, Object> conditions = new HashMap<>();
61 conditions.put("key1", "val1");
62 HashMap<String, Object> mapValue = new HashMap<>();
63 mapValue.put("innerKey", "innerVal");
64 conditions.put("key2", mapValue);
65 template.setConditions(conditions);
67 Map<String, Resource> resources = new HashMap<>();
68 Resource resource = new Resource();
69 resource.setMetadata("resource metadata");
70 resource.setType("resource type");
71 //Map<String, String> resourceProps = new ;
72 Map<String, Object> resourceProps = new HashMap<>();
73 resourceProps.put("aaa", "bbb");
74 //resourceProps.add(resourceProp);
75 resource.setProperties(resourceProps);
76 resources.put("R1", resource);
77 resource = new Resource();
78 resource.setMetadata("resource2 metadata");
79 resource.setType("resource2 type");
80 //resourceProps = new ArrayList<>();
81 resourceProps = new HashMap<>();
82 resourceProps.put("aaa2", "bbb2");
83 //resourceProps.add(resourceProp);
84 resource.setProperties(resourceProps);
85 List<String> dependsOn = new ArrayList<>();
87 resource.setDepends_on(dependsOn);
88 resource.setDeletion_policy("all");
89 resource.setUpdate_policy("once");
90 resources.put("R2", resource);
91 template.setResources(resources);
93 YamlUtil yamlUtil = new YamlUtil();
94 String yml = yamlUtil.objectToYaml(template);
95 Assert.assertNotNull(yml);
96 HeatOrchestrationTemplate heatOrchestrationTemplate;
97 heatOrchestrationTemplate =
98 yamlUtil.yamlToObject(yml, HeatOrchestrationTemplate.class);
99 Assert.assertNotNull(heatOrchestrationTemplate);
102 private Map<String, Parameter> createParameters() {
103 Map<String, Parameter> params = new HashMap<>();
105 for (int i = 0; i < 2; i++) {
106 param = new Parameter();
107 param.setDescription("param " + i + " desc");
108 param.setLabel("param " + i + " label");
109 param.set_default("PARAM " + i + " default");
110 param.setHidden(i % 2 == 0);
111 param.setImmutable(i % 2 == 0);
112 param.setType(i % 2 == 0 ? ParameterType.STRING.getDisplayName()
113 : ParameterType.BOOLEAN.getDisplayName());
114 params.put("param" + i, param);
120 private List<Constraint> createConstraints() {
121 List<Constraint> constraints = new ArrayList<>();
122 Constraint constraint = new Constraint();
123 constraint.setLength(new Integer[]{2, 4});
124 constraints.add(constraint);
125 constraint = new Constraint();
126 constraint.setPattern("some regex");
127 constraints.add(constraint);
128 constraint = new Constraint();
129 constraint.setRange(new Integer[]{5, 8});
130 constraints.add(constraint);
131 constraint = new Constraint();
132 List<Object> validValues = new ArrayList<>();
133 validValues.add("abc");
134 validValues.add("def");
135 constraint.setValidValues(validValues);
136 constraints.add(constraint);