aboutsummaryrefslogtreecommitdiffstats
path: root/sdc-distribution-client/src/test/java/org/openecomp/sdc/impl/HeatParserTest.java
blob: 3b5b1a191078418e8e423d3ee42210adea5617ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*-
 * ============LICENSE_START=======================================================
 * sdc-distribution-client
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.impl;

import static org.junit.Assert.*;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.openecomp.sdc.utils.heat.HeatParameter;
import org.openecomp.sdc.utils.heat.HeatParameterConstraint;
import org.openecomp.sdc.utils.heat.HeatParser;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;

public class HeatParserTest {

	@Test
	public void testParametersParsing() throws IOException{
		String resourceName = "heatExample.yaml";
		URL url = Resources.getResource(resourceName);
		String heatFileContents = Resources.toString(url, Charsets.UTF_8);
		assertNotNull("Didn't find "+resourceName, heatFileContents);

		HeatParser heatParser = new HeatParser();
		//Flat parameter entry
		Map<String, HeatParameter> parameters = heatParser.getHeatParameters(heatFileContents);
		HeatParameter heatParameter1 = parameters.get("image_name_1");
		validateField("string", heatParameter1.getType(), "type");
		validateField("Image Name", heatParameter1.getLabel(), "label");
		validateField("SCOIMAGE Specify an image name for instance1", heatParameter1.getDescription(), "description");
		validateField("cirros-0.3.1-x86_64", heatParameter1.getDefault(), "default");
		validateField(null, heatParameter1.getConstraints(), "constraints");
		validateField("false", heatParameter1.getHidden(), "hidden");


		//Flat parameter entry with constraints
		heatParameter1 = parameters.get("network_id");
		validateField("string", heatParameter1.getType(), "type");
		validateField("Network ID", heatParameter1.getLabel(), "label");
		validateField("SCONETWORK Network to be used for the compute instance", heatParameter1.getDescription(), "description");
		validateField(null, heatParameter1.getDefault(), "default");
		validateField("true", heatParameter1.getHidden(), "hidden");

		//Constraints
		List<HeatParameterConstraint> constraints = heatParameter1.getConstraints();
		assertEquals("Number of constraints", 6, constraints.size());

		//Length
		HeatParameterConstraint lengthConstraint = heatParameter1.getLengthConstraint();
		assertNotNull(lengthConstraint);
		Map<String, String> expectedMap = new HashMap<>();
		expectedMap.put("min", "6");
		expectedMap.put("max", "8");
		validateField(expectedMap, lengthConstraint.getLength(), "length");
		validateField("Password length must be between 6 and 8 characters.", lengthConstraint.getDescription(), "length description");

		//Range
		HeatParameterConstraint rangeConstraint = heatParameter1.getRangeConstraint();
		assertNotNull(rangeConstraint);
		validateField(expectedMap, rangeConstraint.getRange(), "range");
		validateField("Range description", rangeConstraint.getDescription(), "range description");

		//Allowed values
		HeatParameterConstraint allowedValues = heatParameter1.getAllowedValuesConstraint();
		assertNotNull(allowedValues);
		List<String> expectedValues = new ArrayList<>();
		expectedValues.add("m1.small");
		expectedValues.add("m1.medium");
		expectedValues.add("m1.large");
		validateField(expectedValues, allowedValues.getAllowed_values(), "allowed_values");	
		validateField("Allowed values description", allowedValues.getDescription(), "allowed_values description");

		//Allowed pattern
		List<HeatParameterConstraint> allowedPatternList = heatParameter1.getAllowedPatternConstraint();
		assertNotNull(allowedPatternList);
		assertEquals("Allowed pattern list", 2, allowedPatternList.size());
		HeatParameterConstraint allowedPattern = allowedPatternList.get(0);
		validateField("[a-zA-Z0-9]+", allowedPattern.getAllowed_pattern(), "allowed_pattern");	
		validateField("Password must consist of characters and numbers only.", allowedPattern.getDescription(), "allowed_pattern description");
		allowedPattern = allowedPatternList.get(1);
		validateField("[A-Z]+[a-zA-Z0-9]*", allowedPattern.getAllowed_pattern(), "allowed_pattern");	
		validateField("Password must start with an uppercase character.", allowedPattern.getDescription(), "allowed_pattern description");

		//Custom constraint
		List<HeatParameterConstraint> customConstraintList = heatParameter1.getCustomConstraintConstraint();
		assertNotNull(customConstraintList);
		assertEquals("Custom constraint list", 1, customConstraintList.size());
		HeatParameterConstraint customConstraint = customConstraintList.get(0);
		validateField("nova.keypair", customConstraint.getCustom_constraint(), "custom_constraint");	
		validateField("Custom description", customConstraint.getDescription(), "custom_constraint description");
	}
	
	@Test
	public void testParametersParsingInvalidYaml() throws IOException{
		String invalidHeatFileContents = "just text";
		HeatParser heatParser = new HeatParser();
		//Flat parameter entry
		Map<String, HeatParameter> parameters = heatParser.getHeatParameters(invalidHeatFileContents);
		assertNull(parameters);
	}
	
	@Test
	public void testParametersParsingNoParamteresSection() throws IOException{
		String heatFileContentsNoParams = "heat_template_version: 2013-05-23\r\n\r\ndescription: Simple template to deploy a stack with two virtual machine instances";
		HeatParser heatParser = new HeatParser();
		//Flat parameter entry
		Map<String, HeatParameter> parameters = heatParser.getHeatParameters(heatFileContentsNoParams);
		assertNull(parameters);
	}

	private void validateField(Object expected, Object actual, String type){
		assertEquals("Field of type "+type+":", expected, actual);
	}
}