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.ci.tests.execute.artifacts;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
26 import java.io.ByteArrayInputStream;
27 import java.io.InputStream;
28 import java.util.List;
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;
60 public class HeatEnvArtifact extends ComponentBaseTest {
63 public static TestName name = new TestName();
65 public HeatEnvArtifact() {
66 super(name, HeatEnvArtifact.class.getName());
70 public void heatEnvOnResourceFormatTest() throws Exception {
72 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
74 Resource createdResource = createVfFromCSAR(sdncModifierDetails, "csarHeatEnv.csar");
75 assertNotNull(createdResource);
77 RestResponse certifyState = LifecycleRestUtils.changeComponentState(createdResource, sdncModifierDetails, LifeCycleStatesEnum.CHECKIN);
78 BaseRestUtils.checkSuccess(certifyState);
80 Resource certifiedResource = ResponseParser.parseToObjectUsingMapper(certifyState.getResponse(), Resource.class);
82 ServiceReqDetails serviceDetails = ElementFactory.getDefaultService("ciNewtestservice1", ServiceCategoriesEnum.MOBILITY, sdncModifierDetails.getUserId());
85 RestResponse createServiceResponse = ServiceRestUtils.createService(serviceDetails, sdncModifierDetails);
86 ResourceRestUtils.checkCreateResponse(createServiceResponse);
87 Service service = ResponseParser.parseToObjectUsingMapper(createServiceResponse.getResponse(), Service.class);
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);
94 RestResponse getService = ServiceRestUtils.getService(service.getUniqueId());
95 BaseRestUtils.checkSuccess(getService);
96 service = ResponseParser.parseToObjectUsingMapper(getService.getResponse(), Service.class);
98 List<ComponentInstance> componentInstances = service.getComponentInstances();
99 assertNotNull(componentInstances);
100 assertEquals(1, componentInstances.size());
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);
109 Map<String, Object> yaml = downloadComponentInstanceYamlFile(service.getUniqueId(), vfi.getUniqueId(), sdncModifierDetails, heatEnv.getUniqueId());
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()));
120 @Test(enabled = true)
121 public void noHeatEnvOnResourceFormatTest() throws Exception {
122 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
124 Resource createdResource = createVfFromCSAR(sdncModifierDetails, "csarHeatNoEnv.csar");
125 assertNotNull(createdResource);
127 RestResponse certifyState = LifecycleRestUtils.changeComponentState(createdResource, sdncModifierDetails, LifeCycleStatesEnum.CHECKIN);
128 BaseRestUtils.checkSuccess(certifyState);
130 Resource certifiedResource = ResponseParser.parseToObjectUsingMapper(certifyState.getResponse(), Resource.class);
132 ServiceReqDetails serviceDetails = ElementFactory.getDefaultService("ciNewtestservice1", ServiceCategoriesEnum.MOBILITY, sdncModifierDetails.getUserId());
135 RestResponse createServiceResponse = ServiceRestUtils.createService(serviceDetails, sdncModifierDetails);
136 ResourceRestUtils.checkCreateResponse(createServiceResponse);
137 Service service = ResponseParser.parseToObjectUsingMapper(createServiceResponse.getResponse(), Service.class);
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);
144 RestResponse getService = ServiceRestUtils.getService(service.getUniqueId());
145 BaseRestUtils.checkSuccess(getService);
146 service = ResponseParser.parseToObjectUsingMapper(getService.getResponse(), Service.class);
148 List<ComponentInstance> componentInstances = service.getComponentInstances();
149 assertNotNull(componentInstances);
150 assertEquals(1, componentInstances.size());
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);
159 Map<String, Object> yaml = downloadComponentInstanceYamlFile(service.getUniqueId(), vfi.getUniqueId(), sdncModifierDetails, heatEnv.getUniqueId());
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()));
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);
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();
186 InputStream inputStream = new ByteArrayInputStream(decodeBase64);
188 Map<String, Object> load = (Map<String, Object>) yaml.load(inputStream);