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 com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.gson.Gson;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import fj.data.Either;
28 import org.apache.commons.io.IOUtils;
29 import org.apache.http.HttpEntity;
30 import org.apache.http.HttpResponse;
31 import org.apache.http.client.HttpResponseException;
32 import org.apache.http.client.methods.CloseableHttpResponse;
33 import org.apache.http.client.methods.HttpDelete;
34 import org.apache.http.client.methods.HttpGet;
35 import org.apache.http.client.methods.HttpPost;
36 import org.apache.http.entity.StringEntity;
37 import org.apache.http.impl.client.BasicResponseHandler;
38 import org.apache.http.impl.client.CloseableHttpClient;
39 import org.apache.http.impl.client.HttpClients;
40 import org.apache.http.util.EntityUtils;
41 import org.json.simple.JSONObject;
42 import org.json.simple.parser.JSONParser;
43 import org.json.simple.parser.ParseException;
44 import org.junit.Rule;
45 import org.junit.rules.TestName;
46 import org.openecomp.sdc.be.dao.api.ActionStatus;
47 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
48 import org.openecomp.sdc.be.model.*;
49 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
50 import org.openecomp.sdc.ci.tests.api.Urls;
51 import org.openecomp.sdc.ci.tests.config.Config;
52 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
53 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
54 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
55 import org.openecomp.sdc.ci.tests.datatypes.enums.*;
56 import org.openecomp.sdc.ci.tests.datatypes.expected.ExpectedResourceAuditJavaObject;
57 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
58 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
59 import org.openecomp.sdc.ci.tests.utils.Decoder;
60 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
61 import org.openecomp.sdc.ci.tests.utils.general.Convertor;
62 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
63 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
64 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
65 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
66 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
67 import org.openecomp.sdc.ci.tests.utils.validation.AuditValidationUtils;
68 import org.openecomp.sdc.ci.tests.utils.validation.ErrorValidationUtils;
69 import org.openecomp.sdc.ci.tests.utils.validation.ServiceValidationUtils;
70 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
71 import org.openecomp.sdc.common.api.Constants;
72 import org.openecomp.sdc.common.util.GeneralUtility;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
75 import org.testng.Assert;
76 import org.testng.AssertJUnit;
77 import org.testng.annotations.BeforeMethod;
78 import org.testng.annotations.Test;
80 import java.io.IOException;
81 import java.io.InputStream;
82 import java.io.UnsupportedEncodingException;
83 import java.util.HashMap;
86 public class DownloadComponentArt extends ComponentBaseTest {
88 private static Logger log = LoggerFactory.getLogger(DownloadComponentArt.class.getName());
89 protected static final String UPLOAD_ARTIFACT_PAYLOAD = "UHVUVFktVXNlci1LZXktRmlsZS0yOiBzc2gtcnNhDQpFbmNyeXB0aW9uOiBhZXMyNTYtY2JjDQpDb21tZW5wOA0K";
90 protected static final String UPLOAD_ARTIFACT_NAME = "TLV_prv.ppk";
92 protected Config config = Config.instance();
93 protected String contentTypeHeaderData = "application/json";
94 protected String acceptHeaderDate = "application/json";
98 protected Gson gson = new Gson();
99 protected JSONParser jsonParser = new JSONParser();
102 protected String serviceVersion;
103 protected ResourceReqDetails resourceDetails;
104 protected User sdncUserDetails;
105 protected ServiceReqDetails serviceDetails;
109 public void init() throws Exception{
110 sdncUserDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
111 Resource resourceObj = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VFC, UserRoleEnum.DESIGNER, true).left().value();
112 Service serviceObj = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true).left().value();
115 resourceDetails = new ResourceReqDetails(resourceObj);
116 serviceDetails = new ServiceReqDetails(serviceObj);
120 public static TestName name = new TestName();
122 // External API - Download artifact for resource
124 public void downloadArtifactFromResourceViaExternalAPI() throws Exception {
125 Resource resourceDetailsVF;
126 Either<Resource, RestResponse> createdResource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VF, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_INFRASTRUCTURE, UserRoleEnum.DESIGNER, true);
127 resourceDetailsVF = createdResource.left().value();
128 ArtifactDefinition heatArtifact = AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.HEAT, resourceDetailsVF, UserRoleEnum.DESIGNER, true, true).left().value();
129 resourceDetails = new ResourceReqDetails(resourceDetailsVF);
131 String resourceUUID = resourceDetailsVF.getUUID();
132 String artifactUUID = heatArtifact.getArtifactUUID();
134 System.out.println("Resource UUID: " + resourceUUID);
135 System.out.println("Artifact UUID: " + artifactUUID);
137 RestResponse restResponse = ArtifactRestUtils.getResourceDeploymentArtifactExternalAPI(resourceUUID, artifactUUID, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), "Resource");
139 Integer responseCode = restResponse.getErrorCode();
140 Integer expectedCode = 200;
141 Assert.assertEquals(responseCode,expectedCode, "Response code is not correct.");
143 String response = restResponse.getResponse();
145 String payloadData = "aGVhdF90ZW1wbGF0ZV92ZXJzaW9uOiAyMDEzLTA1LTIzDQoNCmRlc2NyaXB0aW9uOiBTaW1wbGUgdGVtcGxhdGUgdG8gZGVwbG95IGEgc3RhY2sgd2l0aCB0d28gdmlydHVhbCBtYWNoaW5lIGluc3RhbmNlcw0KDQpwYXJhbWV0ZXJzOg0KICBpbWFnZV9uYW1lXzE6DQogICAgdHlwZTogc3RyaW5nDQogICAgbGFiZWw6IEltYWdlIE5hbWUNCiAgICBkZXNjcmlwdGlvbjogU0NPSU1BR0UgU3BlY2lmeSBhbiBpbWFnZSBuYW1lIGZvciBpbnN0YW5jZTENCiAgICBkZWZhdWx0OiBjaXJyb3MtMC4zLjEteDg2XzY0DQogIGltYWdlX25hbWVfMjoNCiAgICB0eXBlOiBzdHJpbmcNCiAgICBsYWJlbDogSW1hZ2UgTmFtZQ0KICAgIGRlc2NyaXB0aW9uOiBTQ09JTUFHRSBTcGVjaWZ5IGFuIGltYWdlIG5hbWUgZm9yIGluc3RhbmNlMg0KICAgIGRlZmF1bHQ6IGNpcnJvcy0wLjMuMS14ODZfNjQNCiAgbmV0d29ya19pZDoNCiAgICB0eXBlOiBzdHJpbmcNCiAgICBsYWJlbDogTmV0d29yayBJRA0KICAgIGRlc2NyaXB0aW9uOiBTQ09ORVRXT1JLIE5ldHdvcmsgdG8gYmUgdXNlZCBmb3IgdGhlIGNvbXB1dGUgaW5zdGFuY2UNCiAgICBoaWRkZW46IHRydWUNCiAgICBjb25zdHJhaW50czoNCiAgICAgIC0gbGVuZ3RoOiB7IG1pbjogNiwgbWF4OiA4IH0NCiAgICAgICAgZGVzY3JpcHRpb246IFBhc3N3b3JkIGxlbmd0aCBtdXN0IGJlIGJldHdlZW4gNiBhbmQgOCBjaGFyYWN0ZXJzLg0KICAgICAgLSByYW5nZTogeyBtaW46IDYsIG1heDogOCB9DQogICAgICAgIGRlc2NyaXB0aW9uOiBSYW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QogICAgICAgIC0gbmV0d29yayA6IHsgZ2V0X3BhcmFtIDogbmV0d29ya19pZCB9";
146 String decodedPaypload = Decoder.decode(payloadData);
148 Assert.assertEquals(response, decodedPaypload, "Response deployment artifact not correct.");
150 String auditAction = "DownloadArtifact";
152 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = new ExpectedResourceAuditJavaObject();
153 expectedResourceAuditJavaObject.setAction(auditAction);
154 expectedResourceAuditJavaObject.setResourceName(resourceDetails.getName());
155 expectedResourceAuditJavaObject.setResourceType("Resource");
156 expectedResourceAuditJavaObject.setStatus("200");
157 expectedResourceAuditJavaObject.setDesc("OK");
159 expectedResourceAuditJavaObject.setCONSUMER_ID("ci");
160 String resource_url = String.format("/sdc/v1/catalog/resources/%s/artifacts/%s", resourceUUID, artifactUUID);
161 expectedResourceAuditJavaObject.setRESOURCE_URL(resource_url);
163 AuditValidationUtils.validateAuditDownloadExternalAPI(expectedResourceAuditJavaObject, auditAction, null, false);
167 // External API - Download artifact for resource - negative test
169 public void downloadArtifactFromResourceViaExternalAPINegativeTest() throws Exception {
170 Resource resourceDetailsVF;
171 Either<Resource, RestResponse> createdResource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VF, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_INFRASTRUCTURE, UserRoleEnum.DESIGNER, true);
172 resourceDetailsVF = createdResource.left().value();
173 ArtifactDefinition heatArtifact = AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.HEAT, resourceDetailsVF, UserRoleEnum.DESIGNER, true, true).left().value();
174 resourceDetails = new ResourceReqDetails(resourceDetailsVF);
176 String resourceUUID = resourceDetailsVF.getUUID();
177 String artifactUUID = heatArtifact.getArtifactUUID();
179 System.out.println("Resource UUID: " + resourceUUID);
180 System.out.println("Artifact UUID: " + artifactUUID);
182 RestResponse restResponse = ArtifactRestUtils.getResourceDeploymentArtifactExternalAPI(resourceUUID, "dfsgfdsg324", ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), "Resource");
184 Integer responseCode = restResponse.getErrorCode();
185 Integer expectedCode = 200;
186 Assert.assertEquals(responseCode,expectedCode, "Response code is not correct.");
193 // External API - Download artifact for service - negative test
195 public void downloadArtifactFromServiceViaExternalAPI() throws Exception {
197 Service resourceDetailsService;
198 Either<Service, RestResponse> createdResource = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true);
199 resourceDetailsService = createdResource.left().value();
201 ArtifactDefinition heatArtifact = AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.OTHER, resourceDetailsService, UserRoleEnum.DESIGNER, true, true).left().value();
203 String resourceUUID = resourceDetailsService.getUUID();
204 String artifactUUID = heatArtifact.getArtifactUUID();
206 System.out.println("Resource UUID: " + resourceUUID);
207 System.out.println("Artifact UUID: " + artifactUUID);
209 RestResponse restResponse = ArtifactRestUtils.getResourceDeploymentArtifactExternalAPI(resourceUUID, artifactUUID, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), "Service");
211 Integer responseCode = restResponse.getErrorCode();
212 Integer expectedCode = 200;
213 Assert.assertEquals(responseCode,expectedCode, "Response code is not correct.");
215 String response = restResponse.getResponse();
217 String payloadData = "aGVhdF90ZW1wbGF0ZV92ZXJzaW9uOiAyMDEzLTA1LTIzDQoNCmRlc2NyaXB0aW9uOiBTaW1wbGUgdGVtcGxhdGUgdG8gZGVwbG95IGEgc3RhY2sgd2l0aCB0d28gdmlydHVhbCBtYWNoaW5lIGluc3RhbmNlcw0KDQpwYXJhbWV0ZXJzOg0KICBpbWFnZV9uYW1lXzE6DQogICAgdHlwZTogc3RyaW5nDQogICAgbGFiZWw6IEltYWdlIE5hbWUNCiAgICBkZXNjcmlwdGlvbjogU0NPSU1BR0UgU3BlY2lmeSBhbiBpbWFnZSBuYW1lIGZvciBpbnN0YW5jZTENCiAgICBkZWZhdWx0OiBjaXJyb3MtMC4zLjEteDg2XzY0DQogIGltYWdlX25hbWVfMjoNCiAgICB0eXB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0YXJ0IHdpdGggYW4gdXBwZXJjYXNlIGNoYXJhY3Rlci4NCiAgICAgIC0gY3VzdG9tX2NvbnN0cmFpbnQ6IG5vdmEua2V5cGFpcg0KICAgICAgICBkZXNjcmlwdGlvbjogQ3VzdG9tIGRlc2NyaXB0aW9uDQoNCnJlc291cmNlczoNCiAgbXlfaW5zdGFuY2UxOg0KICAgIHR5cGU6IE9TOjpOb3ZhOjpTZXJ2ZXINCiAgICBwcm9wZXJ0aWVzOg0KICAgICAgaW1hZ2U6IHsgZ2V0X3BhcmFtOiBpbWFnZV9uYW1lXzEgfQ0KICAgICAgZmxhdm9yOiBtMS5zbWFsbA0KICAgICAgbmV0d29ya3M6DQogICAgICAgIC0gbmV0d29yayA6IHsgZ2V0X3BhcmFtIDogbmV0d29ya19pZCB9DQogIG15X2luc3RhbmNlMjoNCiAgICB0eXBlOiBPUzo6Tm92YTo6U2VydmVyDQogICAgcHJvcGVydGllczoNCiAgICAgIGltYWdlOiB7IGdldF9wYXJhbTogaW1hZ2VfbmFtZV8yIH0NCiAgICAgIGZsYXZvcjogbTEudGlueQ0KICAgICAgbmV0d29ya3M6DQogICAgICAgIC0gbmV0d29yayA6IHsgZ2V0X3BhcmFtIDogbmV0d29ya19pZCB9";
218 String decodedPaypload = Decoder.decode(payloadData);
220 Assert.assertEquals(response, decodedPaypload, "Response deployment artifact not correct.");
222 String auditAction = "DownloadArtifact";
224 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = new ExpectedResourceAuditJavaObject();
225 expectedResourceAuditJavaObject.setAction(auditAction);
226 expectedResourceAuditJavaObject.setResourceName(resourceDetailsService.getName());
227 expectedResourceAuditJavaObject.setResourceType("Service");
228 expectedResourceAuditJavaObject.setStatus("200");
229 expectedResourceAuditJavaObject.setDesc("OK");
231 expectedResourceAuditJavaObject.setCONSUMER_ID("ci");
232 String resource_url = String.format("/sdc/v1/catalog/services/%s/artifacts/%s", resourceUUID, artifactUUID);
233 expectedResourceAuditJavaObject.setRESOURCE_URL(resource_url);
235 AuditValidationUtils.validateAuditDownloadExternalAPI(expectedResourceAuditJavaObject, auditAction, null, false);
243 // External API - Download ComponentInstance artifact of service - negative test
245 public void downloadArtifactOfComponentInstanceFromServiceViaExternalAPI() throws Exception {
247 Either<Resource, RestResponse> resourceDetailsVF_01e = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VF, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_INFRASTRUCTURE, UserRoleEnum.DESIGNER, true);
248 Component resourceDetailsVF_01 = resourceDetailsVF_01e.left().value();
249 ArtifactDefinition heatArtifact = AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.HEAT, resourceDetailsVF_01, UserRoleEnum.DESIGNER, true, true).left().value();
251 resourceDetailsVF_01 = AtomicOperationUtils.changeComponentState(resourceDetailsVF_01, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CHECKIN, true).getLeft();
253 Service resourceDetailsService;
254 Either<Service, RestResponse> createdResource = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true);
255 resourceDetailsService = createdResource.left().value();
258 ComponentInstance resourceDetailsVF1ins_01 = AtomicOperationUtils.addComponentInstanceToComponentContainer(resourceDetailsVF_01, resourceDetailsService, UserRoleEnum.DESIGNER, true).left().value();
261 System.out.println("-----");
264 String resourceUUID = resourceDetailsService.getUUID();
265 String componentNormalizedName = resourceDetailsVF1ins_01.getNormalizedName();
266 String artifactUUID = heatArtifact.getArtifactUUID();
268 System.out.println("Resource UUID: " + resourceUUID);
269 System.out.println("Component NormalizedName: " + componentNormalizedName);
270 System.out.println("Artifact UUID: " + artifactUUID);
272 RestResponse restResponse = ArtifactRestUtils.getComponentInstanceDeploymentArtifactExternalAPI(resourceUUID, componentNormalizedName, artifactUUID, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), "Service");
274 Integer responseCode = restResponse.getErrorCode();
275 Integer expectedCode = 200;
276 Assert.assertEquals(responseCode,expectedCode, "Response code is not correct.");
278 String response = restResponse.getResponse();
280 String payloadData = "aGVhdF90ZW1wbGF0ZV92ZXJzaW9uOiAyMDEzLTA1LTIzDQoNCmRlc2NyaXB0aW9uOiBTaW1wbGUgdGVtcGxhdGUgdG8gZGVwbG95IGEgc3RhY2sgd2l0aCB0d28gdmlydHVhbCBtYWNoaW5lIGluc3RhbmNlcw0KDQpwYXJhbWV0ZXJzOg0KICBpbWFnZV9uYW1lXzE6DQogICAgdHlwZTogc3RyaW5nDQogICAgbGFiZWw6IEltYWdlIE5hbWUNCiAgICBkZXNjcmlwdGlvbjogU0NPSU1BR0UgU3BlY2lmeSBhbiBpbWFnZSBuYW1lIGZvciBpbnN0YW5jZTENCiAgICBkZWZhdWx0OiBjaXJyb3MtMC4zLjEteDg2XzY0DQogIGltYWdlX25hbWVfMjoNCiAgICB0eXBlOiBzdHJpbmcNCiAgICBsYWJlbDogSW1hZ2UgTmFtZQ0KICAgIGRlc2NyaXB0aW9uOiBTQ09JTUFHRSBTcGVjaWZ5IGFuIGltYWdlIG5hbWUgZm9yIGluc3RhbmNlMg0KICAgIGRlZmF1bHQ6IGNpcnJvcy0wLjMuMS14ODZfNjQNCiAgbmV0d29ya19pZDoNCiAgICB0eXBlOiBzdHJpbmcNCiAgICBsYWJlbDogTmV0d29yayBJRA0KICAgIGRlc2NyaXB0aW9uOiBTQ09ORVRXT1JLIE5ldHdvcmsgdG8gYmUgdXNlZCBmb3IgdGhlIGNvbXB1dGUgaW5zdGFuY2UNCiAgICBoaWRkZW46IHRydWUNCiAgICBjb25zdHJhaW50czoNCiAgICAgIC0gbGVuZ3RoOiB7IG1pbjogNiwgbWF4OiA4IH0NCiAgICAgICAgZGVzY3JpcHRpb246IFBhc3N3b3JkIGxlbmd0aCBtdXN0IGJlIGJldHdlZW4gNiBhbmQgOCBjaGFyYWN0ZXJzLg0KICAgICAgLSByYW5nZTogeyBtaW46IDYsIG1heDogOCB9DQogICAgICAgIGRlc2NyaXB0aW9uOiBSYW5nZSB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ICAgIC0gbmV0d29yayA6IHsgZ2V0X3BhcmFtIDogbmV0d29ya19pZCB9";
281 String decodedPaypload = Decoder.decode(payloadData);
283 Assert.assertEquals(response, decodedPaypload, "Response deployment artifact not correct.");
285 String auditAction = "DownloadArtifact";
287 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = new ExpectedResourceAuditJavaObject();
288 expectedResourceAuditJavaObject.setAction(auditAction);
289 expectedResourceAuditJavaObject.setResourceName(resourceDetailsVF1ins_01.getName());
290 expectedResourceAuditJavaObject.setResourceType("Service");
291 expectedResourceAuditJavaObject.setStatus("200");
292 expectedResourceAuditJavaObject.setDesc("OK");
294 expectedResourceAuditJavaObject.setCONSUMER_ID("ci");
295 String resource_url = String.format("/sdc/v1/catalog/services/%s/resourceInstances/%s/artifacts/%s", resourceUUID, componentNormalizedName, artifactUUID);
296 expectedResourceAuditJavaObject.setRESOURCE_URL(resource_url);
298 // AuditValidationUtils.validateAuditDownloadExternalAPI(expectedResourceAuditJavaObject, auditAction, null, false);
310 public void downloadArtifactFromResourceTest() throws Exception {
312 CloseableHttpClient httpclient = HttpClients.createDefault();
314 String jsonBody = createUploadArtifactBodyJson();
316 String resourceId = resourceDetails.getUniqueId();
317 String url = String.format(Urls.ADD_ARTIFACT_TO_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId);
318 HttpPost httppost = createPostAddArtifactRequeast(jsonBody, url, true);
319 HttpResponse response = httpclient.execute(httppost);
320 int status = response.getStatusLine().getStatusCode();
321 AssertJUnit.assertEquals("failed to add artifact", 200, status);
323 ArtifactDefinition origArtifact = getArtifactDataFromJson(jsonBody);
324 addArtifactDataFromResponse(response, origArtifact);
325 String artifactId = origArtifact.getUniqueId();
327 url = String.format(Urls.UI_DOWNLOAD_RESOURCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId, artifactId);
328 HttpGet httpGet = createGetRequest(url);
329 response = httpclient.execute(httpGet);
330 status = response.getStatusLine().getStatusCode();
331 AssertJUnit.assertEquals("failed to download artifact", 200, status);
333 InputStream inputStream = response.getEntity().getContent();
334 ArtifactUiDownloadData artifactUiDownloadData = getArtifactUiDownloadData(IOUtils.toString(inputStream));
335 AssertJUnit.assertEquals("Downloaded payload is different from uploaded one", UPLOAD_ARTIFACT_PAYLOAD, artifactUiDownloadData.getBase64Contents());
336 AssertJUnit.assertEquals("Downloaded artifact name is different from uploaded one", UPLOAD_ARTIFACT_NAME, artifactUiDownloadData.getArtifactName());
340 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = Convertor.constructFieldsForAuditValidation(resourceDetails, resourceDetails.getVersion(), sdncUserDetails);
341 String auditAction = "ArtifactDownload";
342 expectedResourceAuditJavaObject.setAction(auditAction);
343 expectedResourceAuditJavaObject.setPrevState("");
344 expectedResourceAuditJavaObject.setPrevVersion("");
345 expectedResourceAuditJavaObject.setCurrState((LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT).toString());
346 expectedResourceAuditJavaObject.setStatus("200");
347 expectedResourceAuditJavaObject.setDesc("OK");
348 expectedResourceAuditJavaObject.setArtifactData(AuditValidationUtils.buildArtifactDataAudit(origArtifact));
349 expectedResourceAuditJavaObject.setCurrArtifactUuid(origArtifact.getUniqueId());
350 expectedResourceAuditJavaObject.setPrevArtifactUuid("");
351 AuditValidationUtils.validateAudit(expectedResourceAuditJavaObject, auditAction, null, false);
360 public void downloadArtifactFromServiceTest() throws Exception {
362 CloseableHttpClient httpclient = HttpClients.createDefault();
366 String jsonStr = createUploadArtifactBodyJson();
368 String url = String.format(Urls.ADD_ARTIFACT_TO_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceDetails.getUniqueId());
369 HttpPost httpPost = createPostAddArtifactRequeast(jsonStr, url, true);
370 CloseableHttpResponse result = httpclient.execute(httpPost);
371 int status = result.getStatusLine().getStatusCode();
372 AssertJUnit.assertEquals("failed to add artifact", 200, status);
374 ArtifactDefinition origArtifact = getArtifactDataFromJson(jsonStr);
375 addArtifactDataFromResponse(result, origArtifact);
376 String artifactId = origArtifact.getUniqueId();
378 url = String.format(Urls.UI_DOWNLOAD_SERVICE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceDetails.getUniqueId(), artifactId);
379 HttpGet httpGet = createGetRequest(url);
380 CloseableHttpResponse response2 = httpclient.execute(httpGet);
381 status = response2.getStatusLine().getStatusCode();
382 AssertJUnit.assertEquals("failed to download artifact", 200, status);
383 InputStream inputStream = response2.getEntity().getContent();
384 ArtifactUiDownloadData artifactUiDownloadData = getArtifactUiDownloadData(IOUtils.toString(inputStream));
385 AssertJUnit.assertEquals("Downloaded payload is different from uploaded one", UPLOAD_ARTIFACT_PAYLOAD, artifactUiDownloadData.getBase64Contents());
386 AssertJUnit.assertEquals("Downloaded artifact name is different from uploaded one", UPLOAD_ARTIFACT_NAME, artifactUiDownloadData.getArtifactName());
389 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = AuditValidationUtils.constructFieldsForAuditValidation(serviceDetails, serviceDetails.getVersion(), sdncUserDetails);
390 String auditAction = "ArtifactDownload";
391 expectedResourceAuditJavaObject.setAction(auditAction);
392 expectedResourceAuditJavaObject.setPrevState("");
393 expectedResourceAuditJavaObject.setPrevVersion("");
394 expectedResourceAuditJavaObject.setCurrState((LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT).toString());
395 expectedResourceAuditJavaObject.setStatus("200");
396 expectedResourceAuditJavaObject.setDesc("OK");
397 expectedResourceAuditJavaObject.setArtifactData(AuditValidationUtils.buildArtifactDataAudit(origArtifact));
398 expectedResourceAuditJavaObject.setCurrArtifactUuid(origArtifact.getUniqueId());
399 expectedResourceAuditJavaObject.setPrevArtifactUuid("");
401 AuditValidationUtils.validateAudit(expectedResourceAuditJavaObject, auditAction, null, false);
404 // RestResponse response = ServiceRestUtils.deleteService(serviceDetails, serviceVersion, sdncUserDetails );
405 // checkDeleteResponse(response);
411 public void downloadArtifactFromResourceNotFound() throws Exception {
413 CloseableHttpClient httpclient = HttpClients.createDefault();
416 String resourceId = resourceDetails.getUniqueId();
417 String artifactIdNotFound = "11111";
419 ArtifactDefinition origArtifact = new ArtifactDefinition();
420 origArtifact.setUniqueId(artifactIdNotFound);
422 String url = String.format(Urls.UI_DOWNLOAD_RESOURCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId, artifactIdNotFound);
423 HttpGet httpGet = createGetRequest(url);
424 CloseableHttpResponse response = httpclient.execute(httpGet);
425 int status = response.getStatusLine().getStatusCode();
426 AssertJUnit.assertEquals("expected 404 not found", 404, status);
429 ErrorInfo errorInfo = ErrorValidationUtils.parseErrorConfigYaml(ActionStatus.ARTIFACT_NOT_FOUND.name());
430 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = Convertor.constructFieldsForAuditValidation(resourceDetails, resourceDetails.getVersion(), sdncUserDetails);
431 String auditAction = "ArtifactDownload";
432 expectedResourceAuditJavaObject.setAction(auditAction);
433 expectedResourceAuditJavaObject.setPrevState("");
434 expectedResourceAuditJavaObject.setPrevVersion("");
435 expectedResourceAuditJavaObject.setCurrState((LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT).toString());
436 expectedResourceAuditJavaObject.setStatus(errorInfo.getCode().toString());
437 expectedResourceAuditJavaObject.setDesc(errorInfo.getAuditDesc(""));
438 expectedResourceAuditJavaObject.setArtifactData("");
439 expectedResourceAuditJavaObject.setCurrArtifactUuid(origArtifact.getUniqueId());
440 expectedResourceAuditJavaObject.setPrevArtifactUuid("");
441 AuditValidationUtils.validateAudit(expectedResourceAuditJavaObject, auditAction, null, false);
442 expectedResourceAuditJavaObject.setPrevArtifactUuid(null);
450 public void downloadArtifactFromServiceNotFound() throws Exception {
452 CloseableHttpClient httpclient = HttpClients.createDefault();
455 String artifactIdNotFound = "11111";
456 ArtifactDefinition origArtifact = new ArtifactDefinition();
457 origArtifact.setUniqueId(artifactIdNotFound);
459 String url = String.format(Urls.UI_DOWNLOAD_SERVICE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceDetails.getUniqueId(), artifactIdNotFound);
460 HttpGet httpGet = createGetRequest(url);
461 CloseableHttpResponse response2 = httpclient.execute(httpGet);
462 int status = response2.getStatusLine().getStatusCode();
463 AssertJUnit.assertEquals("expected 404 not found", 404, status);
466 ErrorInfo errorInfo = ErrorValidationUtils.parseErrorConfigYaml(ActionStatus.ARTIFACT_NOT_FOUND.name());
467 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = ServiceValidationUtils.constructFieldsForAuditValidation(serviceDetails, serviceDetails.getVersion(), sdncUserDetails);
468 String auditAction = "ArtifactDownload";
469 expectedResourceAuditJavaObject.setAction(auditAction);
470 expectedResourceAuditJavaObject.setPrevState("");
471 expectedResourceAuditJavaObject.setPrevVersion("");
472 expectedResourceAuditJavaObject.setCurrState((LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT).toString());
473 expectedResourceAuditJavaObject.setStatus(errorInfo.getCode().toString());
474 expectedResourceAuditJavaObject.setDesc(errorInfo.getAuditDesc(""));
475 expectedResourceAuditJavaObject.setArtifactData("");
476 expectedResourceAuditJavaObject.setCurrArtifactUuid(origArtifact.getUniqueId());
477 expectedResourceAuditJavaObject.setPrevArtifactUuid("");
478 AuditValidationUtils.validateAudit(expectedResourceAuditJavaObject, auditAction, null, false);
487 public void addArtifactToResourceTest() throws Exception {
489 ArtifactReqDetails defaultArtifact = ElementFactory.getDefaultArtifact();
491 RestResponse response = ArtifactRestUtils.addInformationalArtifactToResource(defaultArtifact, sdncUserDetails, resourceDetails.getUniqueId());
492 int status = response.getErrorCode();
493 AssertJUnit.assertEquals("add informational artifact request returned status: " + response.getErrorCode(), 200, status);
495 RestResponse resourceResp = ResourceRestUtils.getResource(resourceDetails.getUniqueId());
496 Resource resource = ResponseParser.convertResourceResponseToJavaObject(resourceResp.getResponse());
497 AssertJUnit.assertNotNull(resource);
499 Map<String, ArtifactDefinition> artifacts = resource.getArtifacts();
500 boolean isExist = false;
501 for (Map.Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) {
502 if (entry.getKey().equals(defaultArtifact.getArtifactLabel())) {
507 AssertJUnit.assertTrue(isExist);
511 protected String createUploadArtifactBodyJson() {
512 Map<String, Object> jsonBody = new HashMap<String, Object>();
513 jsonBody.put("artifactName", UPLOAD_ARTIFACT_NAME);
514 jsonBody.put("artifactDisplayName", "configure");
515 jsonBody.put("artifactType", "SHELL");
516 jsonBody.put("mandatory", "false");
517 jsonBody.put("description", "ff");
518 jsonBody.put("payloadData", UPLOAD_ARTIFACT_PAYLOAD);
519 jsonBody.put("artifactLabel", "configure");
520 return gson.toJson(jsonBody);
523 protected ArtifactDefinition getArtifactDataFromJson(String json) {
524 Gson gson = new Gson();
525 JsonObject jsonElement = new JsonObject();
526 jsonElement = gson.fromJson(json, jsonElement.getClass());
527 ArtifactDefinition artifact = new ArtifactDefinition();
528 String payload = null;
529 JsonElement artifactPayload = jsonElement.get(Constants.ARTIFACT_PAYLOAD_DATA);
530 if (artifactPayload != null && !artifactPayload.isJsonNull()) {
531 payload = artifactPayload.getAsString();
533 jsonElement.remove(Constants.ARTIFACT_PAYLOAD_DATA);
534 artifact = gson.fromJson(jsonElement, ArtifactDefinition.class);
535 artifact.setPayloadData(payload);
537 /*atifact.setArtifactName(UPLOAD_ARTIFACT_NAME);
538 artifact.setArtifactDisplayName("configure");
539 artifact.setArtifactType("SHELL");
540 artifact.setMandatory(false);
541 artifact.setDescription("ff");
542 artifact.setPayloadData(UPLOAD_ARTIFACT_PAYLOAD);
543 artifact.setArtifactLabel("configure");*/
547 protected HttpGet createGetRequest(String url) {
548 HttpGet httpGet = new HttpGet(url);
549 httpGet.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
550 httpGet.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
551 httpGet.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncUserDetails.getUserId());
555 protected String getArtifactUid(HttpResponse response) throws HttpResponseException, IOException, ParseException {
556 String responseString = new BasicResponseHandler().handleResponse(response);
557 JSONObject responseMap = (JSONObject) jsonParser.parse(responseString);
558 String artifactId = (String) responseMap.get("uniqueId");
562 protected String getArtifactEsId(HttpResponse response) throws HttpResponseException, IOException, ParseException {
563 String responseString = new BasicResponseHandler().handleResponse(response);
564 JSONObject responseMap = (JSONObject) jsonParser.parse(responseString);
565 String esId = (String) responseMap.get("EsId");
569 protected ArtifactDefinition addArtifactDataFromResponse(HttpResponse response, ArtifactDefinition artifact) throws HttpResponseException, IOException, ParseException {
570 //String responseString = new BasicResponseHandler().handleResponse(response);
571 HttpEntity entity = response.getEntity();
572 String responseString = EntityUtils.toString(entity);
573 JSONObject responseMap = (JSONObject) jsonParser.parse(responseString);
574 artifact.setEsId((String)responseMap.get("esId"));
575 artifact.setUniqueId((String) responseMap.get("uniqueId"));
576 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.findType((String) responseMap.get("artifactGroupType")));
577 artifact.setTimeout(((Long) responseMap.get("timeout")).intValue());
581 protected String getLifecycleArtifactUid(CloseableHttpResponse response) throws HttpResponseException, IOException, ParseException {
582 String responseString = new BasicResponseHandler().handleResponse(response);
583 JSONObject responseMap = (JSONObject) jsonParser.parse(responseString);
584 responseMap = (JSONObject) responseMap.get("implementation");
585 String artifactId = (String) responseMap.get("uniqueId");
589 protected HttpDelete createDeleteArtifactRequest(String url) {
590 HttpDelete httpDelete = new HttpDelete(url);
591 httpDelete.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncUserDetails.getUserId());
592 httpDelete.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
596 protected HttpPost createPostAddArtifactRequeast(String jsonBody, String url, boolean addMd5Header) throws UnsupportedEncodingException {
597 HttpPost httppost = new HttpPost(url);
598 httppost.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
599 httppost.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
600 httppost.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncUserDetails.getUserId());
602 httppost.addHeader(HttpHeaderEnum.Content_MD5.getValue(), GeneralUtility.calculateMD5Base64EncodedByString(jsonBody));
604 StringEntity input = new StringEntity(jsonBody);
605 input.setContentType("application/json");
606 httppost.setEntity(input);
607 log.debug("Executing request {}" , httppost.getRequestLine());
611 protected String createLoadArtifactBody() {
612 Map<String, Object> json = new HashMap<String, Object>();
613 json.put("artifactName", "install_apache2.sh");
614 json.put("artifactType", "SHELL");
615 json.put("description", "ddd");
616 json.put("payloadData", "UEsDBAoAAAAIAAeLb0bDQz");
617 json.put("artifactLabel", "name123");
619 String jsonStr = gson.toJson(json);
623 protected void checkDeleteResponse(RestResponse response) {
624 BaseRestUtils.checkStatusCode(response, "delete request failed", false, 204, 404);
627 protected ArtifactUiDownloadData getArtifactUiDownloadData(String artifactUiDownloadDataStr) throws Exception {
629 ObjectMapper mapper = new ObjectMapper();
631 ArtifactUiDownloadData artifactUiDownloadData = mapper.readValue(artifactUiDownloadDataStr, ArtifactUiDownloadData.class);
632 return artifactUiDownloadData;
633 } catch (Exception e) {