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