[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / artifacts / HeatEnvArtifact.java
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.ci.tests.execute.artifacts;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.InputStream;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.commons.codec.binary.Base64;
32 import org.junit.Rule;
33 import org.junit.rules.TestName;
34 import org.openecomp.sdc.be.datatypes.elements.HeatParameterDataDefinition;
35 import org.openecomp.sdc.be.model.ArtifactDefinition;
36 import org.openecomp.sdc.be.model.ArtifactUiDownloadData;
37 import org.openecomp.sdc.be.model.ComponentInstance;
38 import org.openecomp.sdc.be.model.HeatParameterDefinition;
39 import org.openecomp.sdc.be.model.Resource;
40 import org.openecomp.sdc.be.model.Service;
41 import org.openecomp.sdc.be.model.User;
42 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
43 import org.openecomp.sdc.ci.tests.datatypes.ComponentInstanceReqDetails;
44 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
45 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
46 import org.openecomp.sdc.ci.tests.datatypes.enums.ServiceCategoriesEnum;
47 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
48 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
49 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
50 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
51 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
52 import org.openecomp.sdc.ci.tests.utils.rest.ComponentInstanceRestUtils;
53 import org.openecomp.sdc.ci.tests.utils.rest.LifecycleRestUtils;
54 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
55 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
56 import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
57 import org.testng.annotations.Test;
58 import org.yaml.snakeyaml.Yaml;
59
60 public class HeatEnvArtifact extends ComponentBaseTest {
61
62         @Rule
63         public static TestName name = new TestName();
64
65         public HeatEnvArtifact() {
66                 super(name, HeatEnvArtifact.class.getName());
67         }
68
69         @Test(enabled = true)
70         public void heatEnvOnResourceFormatTest() throws Exception {
71
72                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
73
74                 Resource createdResource = createVfFromCSAR(sdncModifierDetails, "csarHeatEnv.csar");
75                 assertNotNull(createdResource);
76
77                 RestResponse certifyState = LifecycleRestUtils.changeComponentState(createdResource, sdncModifierDetails, LifeCycleStatesEnum.CHECKIN);
78                 BaseRestUtils.checkSuccess(certifyState);
79
80                 Resource certifiedResource = ResponseParser.parseToObjectUsingMapper(certifyState.getResponse(), Resource.class);
81
82                 ServiceReqDetails serviceDetails = ElementFactory.getDefaultService("ciNewtestservice1", ServiceCategoriesEnum.MOBILITY, sdncModifierDetails.getUserId());
83
84                 // 2 create service
85                 RestResponse createServiceResponse = ServiceRestUtils.createService(serviceDetails, sdncModifierDetails);
86                 ResourceRestUtils.checkCreateResponse(createServiceResponse);
87                 Service service = ResponseParser.parseToObjectUsingMapper(createServiceResponse.getResponse(), Service.class);
88
89                 // 3 create vf instance in service
90                 ComponentInstanceReqDetails componentInstanceDetails = ElementFactory.getComponentInstance(certifiedResource);
91                 RestResponse createComponentInstance = ComponentInstanceRestUtils.createComponentInstance(componentInstanceDetails, sdncModifierDetails, service);
92                 ResourceRestUtils.checkCreateResponse(createComponentInstance);
93
94                 RestResponse getService = ServiceRestUtils.getService(service.getUniqueId());
95                 BaseRestUtils.checkSuccess(getService);
96                 service = ResponseParser.parseToObjectUsingMapper(getService.getResponse(), Service.class);
97
98                 List<ComponentInstance> componentInstances = service.getComponentInstances();
99                 assertNotNull(componentInstances);
100                 assertEquals(1, componentInstances.size());
101
102                 ComponentInstance vfi = componentInstances.get(0);
103                 Map<String, ArtifactDefinition> deploymentArtifacts = vfi.getDeploymentArtifacts();
104                 assertNotNull(deploymentArtifacts);
105                 assertEquals(4, deploymentArtifacts.size());
106                 ArtifactDefinition heatEnv = deploymentArtifacts.get("heat0env");
107                 assertNotNull(heatEnv);
108
109                 Map<String, Object> yaml = downloadComponentInstanceYamlFile(service.getUniqueId(), vfi.getUniqueId(), sdncModifierDetails, heatEnv.getUniqueId());
110                 assertNotNull(yaml);
111                 Map<String, Object> paramters = (Map<String, Object>)yaml.get("parameters");
112                 assertNotNull(paramters);
113                 assertEquals(8, paramters.size());
114                 assertEquals(null, paramters.get("param8"));
115                 List<HeatParameterDataDefinition> heatParameters = heatEnv.getHeatParameters();
116                 heatParameters.forEach(p -> {
117                         assertEquals(p.getCurrentValue(), paramters.get(p.getName()));
118                 });
119         }
120         @Test(enabled = true)
121         public void noHeatEnvOnResourceFormatTest() throws Exception {
122                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
123
124                 Resource createdResource = createVfFromCSAR(sdncModifierDetails, "csarHeatNoEnv.csar");
125                 assertNotNull(createdResource);
126
127                 RestResponse certifyState = LifecycleRestUtils.changeComponentState(createdResource, sdncModifierDetails, LifeCycleStatesEnum.CHECKIN);
128                 BaseRestUtils.checkSuccess(certifyState);
129
130                 Resource certifiedResource = ResponseParser.parseToObjectUsingMapper(certifyState.getResponse(), Resource.class);
131
132                 ServiceReqDetails serviceDetails = ElementFactory.getDefaultService("ciNewtestservice1", ServiceCategoriesEnum.MOBILITY, sdncModifierDetails.getUserId());
133
134                 // 2 create service
135                 RestResponse createServiceResponse = ServiceRestUtils.createService(serviceDetails, sdncModifierDetails);
136                 ResourceRestUtils.checkCreateResponse(createServiceResponse);
137                 Service service = ResponseParser.parseToObjectUsingMapper(createServiceResponse.getResponse(), Service.class);
138
139                 // 3 create vf instance in service
140                 ComponentInstanceReqDetails componentInstanceDetails = ElementFactory.getComponentInstance(certifiedResource);
141                 RestResponse createComponentInstance = ComponentInstanceRestUtils.createComponentInstance(componentInstanceDetails, sdncModifierDetails, service);
142                 ResourceRestUtils.checkCreateResponse(createComponentInstance);
143
144                 RestResponse getService = ServiceRestUtils.getService(service.getUniqueId());
145                 BaseRestUtils.checkSuccess(getService);
146                 service = ResponseParser.parseToObjectUsingMapper(getService.getResponse(), Service.class);
147
148                 List<ComponentInstance> componentInstances = service.getComponentInstances();
149                 assertNotNull(componentInstances);
150                 assertEquals(1, componentInstances.size());
151                 
152                 ComponentInstance vfi = componentInstances.get(0);
153                 Map<String, ArtifactDefinition> deploymentArtifacts = vfi.getDeploymentArtifacts();
154                 assertNotNull(deploymentArtifacts);
155                 assertEquals(4, deploymentArtifacts.size());
156                 ArtifactDefinition heatEnv = deploymentArtifacts.get("heat0env");
157                 assertNotNull(heatEnv);
158
159                 Map<String, Object> yaml = downloadComponentInstanceYamlFile(service.getUniqueId(), vfi.getUniqueId(), sdncModifierDetails, heatEnv.getUniqueId());
160                 assertNotNull(yaml);
161                 Map<String, Object> paramters = (Map<String, Object>)yaml.get("parameters");
162                 assertNotNull(paramters);
163                 assertEquals(8, paramters.size());
164                 assertEquals(null, paramters.get("param1"));
165                 assertEquals(null, paramters.get("param2"));
166                 assertEquals(null, paramters.get("param4"));
167                 assertEquals(null, paramters.get("param5"));
168                 assertEquals(null, paramters.get("param7"));
169                 assertEquals(null, paramters.get("param8"));
170                 List<HeatParameterDataDefinition> heatParameters = heatEnv.getHeatParameters();
171                 heatParameters.forEach(p -> {
172                         assertEquals(p.getCurrentValue(), paramters.get(p.getName()));
173                 });
174
175         }
176         //****************************************
177         private Map<String, Object> downloadComponentInstanceYamlFile(String serviceUniqueId, String resourceInstanceId, User user, String artifactUniqeId) throws Exception {
178                 RestResponse heatEnvDownloadResponse = ArtifactRestUtils.downloadResourceInstanceArtifact(serviceUniqueId, resourceInstanceId, user, artifactUniqeId);
179                 BaseRestUtils.checkSuccess(heatEnvDownloadResponse);
180
181                 ArtifactUiDownloadData artifactUiDownloadData = ResponseParser.parseToObject(heatEnvDownloadResponse.getResponse(), ArtifactUiDownloadData.class);
182                 byte[] fromUiDownload = artifactUiDownloadData.getBase64Contents().getBytes();
183                 byte[] decodeBase64 = Base64.decodeBase64(fromUiDownload);
184                 Yaml yaml = new Yaml();
185
186                 InputStream inputStream = new ByteArrayInputStream(decodeBase64);
187
188                 Map<String, Object> load = (Map<String, Object>) yaml.load(inputStream);
189
190                 return load;
191         }
192 }