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