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 public DownloadComponentArt() {
123 super(name, DownloadComponentArt.class.getName());
129 // External API - Download artifact for resource
131 public void downloadArtifactFromResourceViaExternalAPI() throws Exception {
132 Resource resourceDetailsVF;
133 Either<Resource, RestResponse> createdResource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VF, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_INFRASTRUCTURE, UserRoleEnum.DESIGNER, true);
134 resourceDetailsVF = createdResource.left().value();
135 ArtifactDefinition heatArtifact = AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.HEAT, resourceDetailsVF, UserRoleEnum.DESIGNER, true, true).left().value();
136 resourceDetails = new ResourceReqDetails(resourceDetailsVF);
138 String resourceUUID = resourceDetailsVF.getUUID();
139 String artifactUUID = heatArtifact.getArtifactUUID();
141 System.out.println("Resource UUID: " + resourceUUID);
142 System.out.println("Artifact UUID: " + artifactUUID);
144 RestResponse restResponse = ArtifactRestUtils.getResourceDeploymentArtifactExternalAPI(resourceUUID, artifactUUID, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), "Resource");
146 Integer responseCode = restResponse.getErrorCode();
147 Integer expectedCode = 200;
148 Assert.assertEquals(responseCode,expectedCode, "Response code is not correct.");
150 String response = restResponse.getResponse();
152 String payloadData = "aGVhdF90ZW1wbGF0ZV92ZXJzaW9uOiAyMDEzLTA1LTIzDQoNCmRlc2NyaXB0aW9uOiBTaW1wbGUgdGVtcGxhdGUgdG8gZGVwbG95IGEgc3RhY2sgd2l0aCB0d28gdmlydHVhbCBtYWNoaW5lIGluc3RhbmNlcw0KDQpwYXJhbWV0ZXJzOg0KICBpbWFnZV9uYW1lXzE6DQogICAgdHlwZTogc3RyaW5nDQogICAgbGFiZWw6IEltYWdlIE5hbWUNCiAgICBkZXNjcmlwdGlvbjogU0NPSU1BR0UgU3BlY2lmeSBhbiBpbWFnZSBuYW1lIGZvciBpbnN0YW5jZTENCiAgICBkZWZhdWx0OiBjaXJyb3MtMC4zLjEteDg2XzY0DQogIGltYWdlX25hbWVfMjoNCiAgICB0eXBlOiBzdHJpbmcNCiAgICBsYWJlbDogSW1hZ2UgTmFtZQ0KICAgIGRlc2NyaXB0aW9uOiBTQ09JTUFHRSBTcGVjaWZ5IGFuIGltYWdlIG5hbWUgZm9yIGluc3RhbmNlMg0KICAgIGRlZmF1bHQ6IGNpcnJvcy0wLjMuMS14ODZfNjQNCiAgbmV0d29ya19pZDoNCiAgICB0eXBlOiBzdHJpbmcNCiAgICBsYWJlbDogTmV0d29yayBJRA0KICAgIGRlc2NyaXB0aW9uOiBTQ09ORVRXT1JLIE5ldHdvcmsgdG8gYmUgdXNlZCBmb3IgdGhlIGNvbXB1dGUgaW5zdGFuY2UNCiAgICBoaWRkZW46IHRydWUNCiAgICBjb25zdHJhaW50czoNCiAgICAgIC0gbGVuZ3RoOiB7IG1pbjogNiwgbWF4OiA4IH0NCiAgICAgICAgZGVzY3JpcHRpb246IFBhc3N3b3JkIGxlbmd0aCBtdXN0IGJlIGJldHdlZW4gNiBhbmQgOCBjaGFyYWN0ZXJzLg0KICAgICAgLSByYW5nZTogeyBtaW46IDYsIG1he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sYXZvcjogbTEudGlueQ0KICAgICAgbmV0d29ya3M6DQogICAgICAgIC0gbmV0d29yayA6IHsgZ2V0X3BhcmFtIDogbmV0d29ya19pZCB9";
153 String decodedPaypload = Decoder.decode(payloadData);
155 Assert.assertEquals(response, decodedPaypload, "Response deployment artifact not correct.");
157 String auditAction = "DownloadArtifact";
159 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = new ExpectedResourceAuditJavaObject();
160 expectedResourceAuditJavaObject.setAction(auditAction);
161 expectedResourceAuditJavaObject.setResourceName(resourceDetails.getName());
162 expectedResourceAuditJavaObject.setResourceType("Resource");
163 expectedResourceAuditJavaObject.setStatus("200");
164 expectedResourceAuditJavaObject.setDesc("OK");
166 expectedResourceAuditJavaObject.setCONSUMER_ID("ci");
167 String resource_url = String.format("/sdc/v1/catalog/resources/%s/artifacts/%s", resourceUUID, artifactUUID);
168 expectedResourceAuditJavaObject.setRESOURCE_URL(resource_url);
170 AuditValidationUtils.validateAuditDownloadExternalAPI(expectedResourceAuditJavaObject, auditAction, null, false);
174 // External API - Download artifact for resource - negative test
176 public void downloadArtifactFromResourceViaExternalAPINegativeTest() throws Exception {
177 Resource resourceDetailsVF;
178 Either<Resource, RestResponse> createdResource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VF, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_INFRASTRUCTURE, UserRoleEnum.DESIGNER, true);
179 resourceDetailsVF = createdResource.left().value();
180 ArtifactDefinition heatArtifact = AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.HEAT, resourceDetailsVF, UserRoleEnum.DESIGNER, true, true).left().value();
181 resourceDetails = new ResourceReqDetails(resourceDetailsVF);
183 String resourceUUID = resourceDetailsVF.getUUID();
184 String artifactUUID = heatArtifact.getArtifactUUID();
186 System.out.println("Resource UUID: " + resourceUUID);
187 System.out.println("Artifact UUID: " + artifactUUID);
189 RestResponse restResponse = ArtifactRestUtils.getResourceDeploymentArtifactExternalAPI(resourceUUID, "dfsgfdsg324", ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), "Resource");
191 Integer responseCode = restResponse.getErrorCode();
192 Integer expectedCode = 200;
193 Assert.assertEquals(responseCode,expectedCode, "Response code is not correct.");
200 // External API - Download artifact for service - negative test
202 public void downloadArtifactFromServiceViaExternalAPI() throws Exception {
204 Service resourceDetailsService;
205 Either<Service, RestResponse> createdResource = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true);
206 resourceDetailsService = createdResource.left().value();
208 ArtifactDefinition heatArtifact = AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.OTHER, resourceDetailsService, UserRoleEnum.DESIGNER, true, true).left().value();
210 String resourceUUID = resourceDetailsService.getUUID();
211 String artifactUUID = heatArtifact.getArtifactUUID();
213 System.out.println("Resource UUID: " + resourceUUID);
214 System.out.println("Artifact UUID: " + artifactUUID);
216 RestResponse restResponse = ArtifactRestUtils.getResourceDeploymentArtifactExternalAPI(resourceUUID, artifactUUID, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), "Service");
218 Integer responseCode = restResponse.getErrorCode();
219 Integer expectedCode = 200;
220 Assert.assertEquals(responseCode,expectedCode, "Response code is not correct.");
222 String response = restResponse.getResponse();
224 String payloadData = "aGVhdF90ZW1wbGF0ZV92ZXJzaW9uOiAyMDEzLTA1LTIzDQoNCmRlc2NyaXB0aW9uOiBTaW1wbGUgdGVtcGxhdGUgdG8gZGVwbG95IGEgc3RhY2sgd2l0aCB0d28gdmlydHVhbCBtYWNoaW5lIGluc3RhbmNlcw0KDQpwYXJhbWV0ZXJzOg0KICBpbWFnZV9uYW1lXzE6DQogICAgdHlwZTogc3RyaW5nDQogICAgbGFiZWw6IEltYWdlIE5hbWUNCiAgICBkZXNjcmlwdGlvbjogU0NPSU1BR0UgU3BlY2lmeSBhbiBpbWFnZSBuYW1lIGZvciBpbnN0YW5jZTENCiAgICBkZWZhdWx0OiBjaXJyb3MtMC4zLjEteD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GRlc2NyaXB0aW9uOiBQYXNzd29yZCBtdXN0IHN0YXJ0IHdpdGggYW4gdXBwZXJjYXNlIGNoYXJhY3Rlci4NCiAgICAgIC0gY3VzdG9tX2NvbnN0cmFpbnQ6IG5vdmEua2V5cGFpcg0KICAgICAgICBkZXNjcmlwdGlvbjogQ3VzdG9tIGRlc2NyaXB0aW9uDQoNCnJlc291cmNlczoNCiAgbXlfaW5zdGFuY2UxOg0KICAgIHR5cGU6IE9TOjpOb3ZhOjpTZXJ2ZXINCiAgICBwcm9wZXJ0aWVzOg0KICAgICAgaW1hZ2U6IHsgZ2V0X3BhcmFtOiBpbWFnZV9uYW1lXzEgfQ0KICAgICAgZmxhdm9yOiBtMS5zbWFsbA0KICAgICAgbmV0d29ya3M6DQogICAgICAgIC0gbmV0d29yayA6IHsgZ2V0X3BhcmFtIDogbmV0d29ya19pZCB9DQogIG15X2luc3RhbmNlMjoNCiAgICB0eXBlOiBPUzo6Tm92YTo6U2VydmVyDQogICAgcHJvcGVydGllczoNCiAgICAgIGltYWdlOiB7IGdldF9wYXJhbTogaW1hZ2VfbmFtZV8yIH0NCiAgICAgIGZsYXZvcjogbTEudGlueQ0KICAgICAgbmV0d29ya3M6DQogICAgICAgIC0gbmV0d29yayA6IHsgZ2V0X3BhcmFtIDogbmV0d29ya19pZCB9";
225 String decodedPaypload = Decoder.decode(payloadData);
227 Assert.assertEquals(response, decodedPaypload, "Response deployment artifact not correct.");
229 String auditAction = "DownloadArtifact";
231 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = new ExpectedResourceAuditJavaObject();
232 expectedResourceAuditJavaObject.setAction(auditAction);
233 expectedResourceAuditJavaObject.setResourceName(resourceDetailsService.getName());
234 expectedResourceAuditJavaObject.setResourceType("Service");
235 expectedResourceAuditJavaObject.setStatus("200");
236 expectedResourceAuditJavaObject.setDesc("OK");
238 expectedResourceAuditJavaObject.setCONSUMER_ID("ci");
239 String resource_url = String.format("/sdc/v1/catalog/services/%s/artifacts/%s", resourceUUID, artifactUUID);
240 expectedResourceAuditJavaObject.setRESOURCE_URL(resource_url);
242 AuditValidationUtils.validateAuditDownloadExternalAPI(expectedResourceAuditJavaObject, auditAction, null, false);
250 // External API - Download ComponentInstance artifact of service - negative test
252 public void downloadArtifactOfComponentInstanceFromServiceViaExternalAPI() throws Exception {
254 Either<Resource, RestResponse> resourceDetailsVF_01e = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VF, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_INFRASTRUCTURE, UserRoleEnum.DESIGNER, true);
255 Component resourceDetailsVF_01 = resourceDetailsVF_01e.left().value();
256 ArtifactDefinition heatArtifact = AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.HEAT, resourceDetailsVF_01, UserRoleEnum.DESIGNER, true, true).left().value();
258 resourceDetailsVF_01 = AtomicOperationUtils.changeComponentState(resourceDetailsVF_01, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CHECKIN, true).getLeft();
260 Service resourceDetailsService;
261 Either<Service, RestResponse> createdResource = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true);
262 resourceDetailsService = createdResource.left().value();
265 ComponentInstance resourceDetailsVF1ins_01 = AtomicOperationUtils.addComponentInstanceToComponentContainer(resourceDetailsVF_01, resourceDetailsService, UserRoleEnum.DESIGNER, true).left().value();
268 System.out.println("-----");
271 String resourceUUID = resourceDetailsService.getUUID();
272 String componentNormalizedName = resourceDetailsVF1ins_01.getNormalizedName();
273 String artifactUUID = heatArtifact.getArtifactUUID();
275 System.out.println("Resource UUID: " + resourceUUID);
276 System.out.println("Component NormalizedName: " + componentNormalizedName);
277 System.out.println("Artifact UUID: " + artifactUUID);
279 RestResponse restResponse = ArtifactRestUtils.getComponentInstanceDeploymentArtifactExternalAPI(resourceUUID, componentNormalizedName, artifactUUID, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), "Service");
281 Integer responseCode = restResponse.getErrorCode();
282 Integer expectedCode = 200;
283 Assert.assertEquals(responseCode,expectedCode, "Response code is not correct.");
285 String response = restResponse.getResponse();
287 String payloadData = "aGVhdF90ZW1wbGF0ZV92ZXJzaW9uOiAyMDEzLTA1LTIzDQoNCmRlc2NyaXB0aW9uOiBTaW1wbGUgdGVtcGxhdGUgdG8gZGVwbG95IGEgc3RhY2sgd2l0aCB0d28gdmlydHVhbCBtYWNoaW5lIGluc3RhbmNlcw0KDQpwYXJhbWV0ZXJzOg0KICBpbWFnZV9uYW1lXzE6DQogICAgdHlwZTogc3RyaW5nDQogICAgbGFiZWw6IEltYWdlIE5hbWUNCiAgICBkZXNjcmlwdGlvbjogU0NPSU1BR0UgU3BlY2lmeSBhbiBpbWFnZSBuYW1lIGZvciBpbnN0YW5jZTENCiAgICBkZWZhdWx0OiBjaXJyb3MtMC4zLjEteDg2XzY0DQogIGltYWdlX25hbWVfMjoNCiAgICB0eXBlOiBzdHJpbmcNCiAgICBsYWJlbDogSW1hZ2UgTmFtZQ0KICAgIGRlc2NyaXB0aW9uOiBTQ09JTUFHRSBTcGVjaWZ5IGFuIGltYWdlIG5hbWUgZm9yIGluc3RhbmNlMg0KICAgIGRlZmF1bHQ6IGNpcnJvcy0wLjMuMS14ODZfNjQNCiAgbmV0d29ya19pZDoNCiAgICB0eXBlOiBzdHJpbmcNCiAgICBsYWJlbDogTmV0d29yayBJRA0KICAgIGRlc2NyaXB0aW9uOiBTQ09ORVRXT1JLIE5ldHdvcmsgdG8gYmUgdXNlZCBmb3IgdGhlIGNvbXB1dGUgaW5zdGFuY2UNCiAgICBoaWRkZW46IHRydWUNCiAgICBjb25zdHJhaW50czoNCiAgICAgIC0gbGVuZ3RoOiB7IG1pbjogNiwgbWF4OiA4IH0NCiAgICAgICAgZGVzY3JpcHRpb246IFBhc3N3b3JkIGxlbmd0aCBtdXN0IGJlIGJldHdlZW4gNiBhbmQgOCBjaGFyYWN0ZXJzLg0KICAgICAgLSByYW5nZTogeyBtaW46IDYsIG1heDogOCB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cjogbTEudGlueQ0KICAgICAgbmV0d29ya3M6DQogICAgICAgIC0gbmV0d29yayA6IHsgZ2V0X3BhcmFtIDogbmV0d29ya19pZCB9";
288 String decodedPaypload = Decoder.decode(payloadData);
290 Assert.assertEquals(response, decodedPaypload, "Response deployment artifact not correct.");
292 String auditAction = "DownloadArtifact";
294 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = new ExpectedResourceAuditJavaObject();
295 expectedResourceAuditJavaObject.setAction(auditAction);
296 expectedResourceAuditJavaObject.setResourceName(resourceDetailsVF1ins_01.getName());
297 expectedResourceAuditJavaObject.setResourceType("Service");
298 expectedResourceAuditJavaObject.setStatus("200");
299 expectedResourceAuditJavaObject.setDesc("OK");
301 expectedResourceAuditJavaObject.setCONSUMER_ID("ci");
302 String resource_url = String.format("/sdc/v1/catalog/services/%s/resourceInstances/%s/artifacts/%s", resourceUUID, componentNormalizedName, artifactUUID);
303 expectedResourceAuditJavaObject.setRESOURCE_URL(resource_url);
305 // AuditValidationUtils.validateAuditDownloadExternalAPI(expectedResourceAuditJavaObject, auditAction, null, false);
317 public void downloadArtifactFromResourceTest() throws Exception {
319 CloseableHttpClient httpclient = HttpClients.createDefault();
321 String jsonBody = createUploadArtifactBodyJson();
323 String resourceId = resourceDetails.getUniqueId();
324 String url = String.format(Urls.ADD_ARTIFACT_TO_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId);
325 HttpPost httppost = createPostAddArtifactRequeast(jsonBody, url, true);
326 HttpResponse response = httpclient.execute(httppost);
327 int status = response.getStatusLine().getStatusCode();
328 AssertJUnit.assertEquals("failed to add artifact", 200, status);
330 ArtifactDefinition origArtifact = getArtifactDataFromJson(jsonBody);
331 addArtifactDataFromResponse(response, origArtifact);
332 String artifactId = origArtifact.getUniqueId();
334 url = String.format(Urls.UI_DOWNLOAD_RESOURCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId, artifactId);
335 HttpGet httpGet = createGetRequest(url);
336 response = httpclient.execute(httpGet);
337 status = response.getStatusLine().getStatusCode();
338 AssertJUnit.assertEquals("failed to download artifact", 200, status);
340 InputStream inputStream = response.getEntity().getContent();
341 ArtifactUiDownloadData artifactUiDownloadData = getArtifactUiDownloadData(IOUtils.toString(inputStream));
342 AssertJUnit.assertEquals("Downloaded payload is different from uploaded one", UPLOAD_ARTIFACT_PAYLOAD, artifactUiDownloadData.getBase64Contents());
343 AssertJUnit.assertEquals("Downloaded artifact name is different from uploaded one", UPLOAD_ARTIFACT_NAME, artifactUiDownloadData.getArtifactName());
347 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = Convertor.constructFieldsForAuditValidation(resourceDetails, resourceDetails.getVersion(), sdncUserDetails);
348 String auditAction = "ArtifactDownload";
349 expectedResourceAuditJavaObject.setAction(auditAction);
350 expectedResourceAuditJavaObject.setPrevState("");
351 expectedResourceAuditJavaObject.setPrevVersion("");
352 expectedResourceAuditJavaObject.setCurrState((LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT).toString());
353 expectedResourceAuditJavaObject.setStatus("200");
354 expectedResourceAuditJavaObject.setDesc("OK");
355 expectedResourceAuditJavaObject.setArtifactData(AuditValidationUtils.buildArtifactDataAudit(origArtifact));
356 expectedResourceAuditJavaObject.setCurrArtifactUuid(origArtifact.getUniqueId());
357 expectedResourceAuditJavaObject.setPrevArtifactUuid("");
358 AuditValidationUtils.validateAudit(expectedResourceAuditJavaObject, auditAction, null, false);
367 public void downloadArtifactFromServiceTest() throws Exception {
369 CloseableHttpClient httpclient = HttpClients.createDefault();
373 String jsonStr = createUploadArtifactBodyJson();
375 String url = String.format(Urls.ADD_ARTIFACT_TO_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceDetails.getUniqueId());
376 HttpPost httpPost = createPostAddArtifactRequeast(jsonStr, url, true);
377 CloseableHttpResponse result = httpclient.execute(httpPost);
378 int status = result.getStatusLine().getStatusCode();
379 AssertJUnit.assertEquals("failed to add artifact", 200, status);
381 ArtifactDefinition origArtifact = getArtifactDataFromJson(jsonStr);
382 addArtifactDataFromResponse(result, origArtifact);
383 String artifactId = origArtifact.getUniqueId();
385 url = String.format(Urls.UI_DOWNLOAD_SERVICE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceDetails.getUniqueId(), artifactId);
386 HttpGet httpGet = createGetRequest(url);
387 CloseableHttpResponse response2 = httpclient.execute(httpGet);
388 status = response2.getStatusLine().getStatusCode();
389 AssertJUnit.assertEquals("failed to download artifact", 200, status);
390 InputStream inputStream = response2.getEntity().getContent();
391 ArtifactUiDownloadData artifactUiDownloadData = getArtifactUiDownloadData(IOUtils.toString(inputStream));
392 AssertJUnit.assertEquals("Downloaded payload is different from uploaded one", UPLOAD_ARTIFACT_PAYLOAD, artifactUiDownloadData.getBase64Contents());
393 AssertJUnit.assertEquals("Downloaded artifact name is different from uploaded one", UPLOAD_ARTIFACT_NAME, artifactUiDownloadData.getArtifactName());
396 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = AuditValidationUtils.constructFieldsForAuditValidation(serviceDetails, serviceDetails.getVersion(), sdncUserDetails);
397 String auditAction = "ArtifactDownload";
398 expectedResourceAuditJavaObject.setAction(auditAction);
399 expectedResourceAuditJavaObject.setPrevState("");
400 expectedResourceAuditJavaObject.setPrevVersion("");
401 expectedResourceAuditJavaObject.setCurrState((LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT).toString());
402 expectedResourceAuditJavaObject.setStatus("200");
403 expectedResourceAuditJavaObject.setDesc("OK");
404 expectedResourceAuditJavaObject.setArtifactData(AuditValidationUtils.buildArtifactDataAudit(origArtifact));
405 expectedResourceAuditJavaObject.setCurrArtifactUuid(origArtifact.getUniqueId());
406 expectedResourceAuditJavaObject.setPrevArtifactUuid("");
408 AuditValidationUtils.validateAudit(expectedResourceAuditJavaObject, auditAction, null, false);
411 // RestResponse response = ServiceRestUtils.deleteService(serviceDetails, serviceVersion, sdncUserDetails );
412 // checkDeleteResponse(response);
418 public void downloadArtifactFromResourceNotFound() throws Exception {
420 CloseableHttpClient httpclient = HttpClients.createDefault();
423 String resourceId = resourceDetails.getUniqueId();
424 String artifactIdNotFound = "11111";
426 ArtifactDefinition origArtifact = new ArtifactDefinition();
427 origArtifact.setUniqueId(artifactIdNotFound);
429 String url = String.format(Urls.UI_DOWNLOAD_RESOURCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId, artifactIdNotFound);
430 HttpGet httpGet = createGetRequest(url);
431 CloseableHttpResponse response = httpclient.execute(httpGet);
432 int status = response.getStatusLine().getStatusCode();
433 AssertJUnit.assertEquals("expected 404 not found", 404, status);
436 ErrorInfo errorInfo = ErrorValidationUtils.parseErrorConfigYaml(ActionStatus.ARTIFACT_NOT_FOUND.name());
437 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = Convertor.constructFieldsForAuditValidation(resourceDetails, resourceDetails.getVersion(), sdncUserDetails);
438 String auditAction = "ArtifactDownload";
439 expectedResourceAuditJavaObject.setAction(auditAction);
440 expectedResourceAuditJavaObject.setPrevState("");
441 expectedResourceAuditJavaObject.setPrevVersion("");
442 expectedResourceAuditJavaObject.setCurrState((LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT).toString());
443 expectedResourceAuditJavaObject.setStatus(errorInfo.getCode().toString());
444 expectedResourceAuditJavaObject.setDesc(errorInfo.getAuditDesc(""));
445 expectedResourceAuditJavaObject.setArtifactData("");
446 expectedResourceAuditJavaObject.setCurrArtifactUuid(origArtifact.getUniqueId());
447 expectedResourceAuditJavaObject.setPrevArtifactUuid("");
448 AuditValidationUtils.validateAudit(expectedResourceAuditJavaObject, auditAction, null, false);
449 expectedResourceAuditJavaObject.setPrevArtifactUuid(null);
457 public void downloadArtifactFromServiceNotFound() throws Exception {
459 CloseableHttpClient httpclient = HttpClients.createDefault();
462 String artifactIdNotFound = "11111";
463 ArtifactDefinition origArtifact = new ArtifactDefinition();
464 origArtifact.setUniqueId(artifactIdNotFound);
466 String url = String.format(Urls.UI_DOWNLOAD_SERVICE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceDetails.getUniqueId(), artifactIdNotFound);
467 HttpGet httpGet = createGetRequest(url);
468 CloseableHttpResponse response2 = httpclient.execute(httpGet);
469 int status = response2.getStatusLine().getStatusCode();
470 AssertJUnit.assertEquals("expected 404 not found", 404, status);
473 ErrorInfo errorInfo = ErrorValidationUtils.parseErrorConfigYaml(ActionStatus.ARTIFACT_NOT_FOUND.name());
474 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = ServiceValidationUtils.constructFieldsForAuditValidation(serviceDetails, serviceDetails.getVersion(), sdncUserDetails);
475 String auditAction = "ArtifactDownload";
476 expectedResourceAuditJavaObject.setAction(auditAction);
477 expectedResourceAuditJavaObject.setPrevState("");
478 expectedResourceAuditJavaObject.setPrevVersion("");
479 expectedResourceAuditJavaObject.setCurrState((LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT).toString());
480 expectedResourceAuditJavaObject.setStatus(errorInfo.getCode().toString());
481 expectedResourceAuditJavaObject.setDesc(errorInfo.getAuditDesc(""));
482 expectedResourceAuditJavaObject.setArtifactData("");
483 expectedResourceAuditJavaObject.setCurrArtifactUuid(origArtifact.getUniqueId());
484 expectedResourceAuditJavaObject.setPrevArtifactUuid("");
485 AuditValidationUtils.validateAudit(expectedResourceAuditJavaObject, auditAction, null, false);
494 public void addArtifactToResourceTest() throws Exception {
496 ArtifactReqDetails defaultArtifact = ElementFactory.getDefaultArtifact();
498 RestResponse response = ArtifactRestUtils.addInformationalArtifactToResource(defaultArtifact, sdncUserDetails, resourceDetails.getUniqueId());
499 int status = response.getErrorCode();
500 AssertJUnit.assertEquals("add informational artifact request returned status: " + response.getErrorCode(), 200, status);
502 RestResponse resourceResp = ResourceRestUtils.getResource(resourceDetails.getUniqueId());
503 Resource resource = ResponseParser.convertResourceResponseToJavaObject(resourceResp.getResponse());
504 AssertJUnit.assertNotNull(resource);
506 Map<String, ArtifactDefinition> artifacts = resource.getArtifacts();
507 boolean isExist = false;
508 for (Map.Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) {
509 if (entry.getKey().equals(defaultArtifact.getArtifactLabel())) {
514 AssertJUnit.assertTrue(isExist);
518 protected String createUploadArtifactBodyJson() {
519 Map<String, Object> jsonBody = new HashMap<String, Object>();
520 jsonBody.put("artifactName", UPLOAD_ARTIFACT_NAME);
521 jsonBody.put("artifactDisplayName", "configure");
522 jsonBody.put("artifactType", "SHELL");
523 jsonBody.put("mandatory", "false");
524 jsonBody.put("description", "ff");
525 jsonBody.put("payloadData", UPLOAD_ARTIFACT_PAYLOAD);
526 jsonBody.put("artifactLabel", "configure");
527 return gson.toJson(jsonBody);
530 protected ArtifactDefinition getArtifactDataFromJson(String json) {
531 Gson gson = new Gson();
532 JsonObject jsonElement = new JsonObject();
533 jsonElement = gson.fromJson(json, jsonElement.getClass());
534 ArtifactDefinition artifact = new ArtifactDefinition();
535 String payload = null;
536 JsonElement artifactPayload = jsonElement.get(Constants.ARTIFACT_PAYLOAD_DATA);
537 if (artifactPayload != null && !artifactPayload.isJsonNull()) {
538 payload = artifactPayload.getAsString();
540 jsonElement.remove(Constants.ARTIFACT_PAYLOAD_DATA);
541 artifact = gson.fromJson(jsonElement, ArtifactDefinition.class);
542 artifact.setPayloadData(payload);
544 /*atifact.setArtifactName(UPLOAD_ARTIFACT_NAME);
545 artifact.setArtifactDisplayName("configure");
546 artifact.setArtifactType("SHELL");
547 artifact.setMandatory(false);
548 artifact.setDescription("ff");
549 artifact.setPayloadData(UPLOAD_ARTIFACT_PAYLOAD);
550 artifact.setArtifactLabel("configure");*/
554 protected HttpGet createGetRequest(String url) {
555 HttpGet httpGet = new HttpGet(url);
556 httpGet.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
557 httpGet.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
558 httpGet.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncUserDetails.getUserId());
562 protected String getArtifactUid(HttpResponse response) throws HttpResponseException, IOException, ParseException {
563 String responseString = new BasicResponseHandler().handleResponse(response);
564 JSONObject responseMap = (JSONObject) jsonParser.parse(responseString);
565 String artifactId = (String) responseMap.get("uniqueId");
569 protected String getArtifactEsId(HttpResponse response) throws HttpResponseException, IOException, ParseException {
570 String responseString = new BasicResponseHandler().handleResponse(response);
571 JSONObject responseMap = (JSONObject) jsonParser.parse(responseString);
572 String esId = (String) responseMap.get("EsId");
576 protected ArtifactDefinition addArtifactDataFromResponse(HttpResponse response, ArtifactDefinition artifact) throws HttpResponseException, IOException, ParseException {
577 //String responseString = new BasicResponseHandler().handleResponse(response);
578 HttpEntity entity = response.getEntity();
579 String responseString = EntityUtils.toString(entity);
580 JSONObject responseMap = (JSONObject) jsonParser.parse(responseString);
581 artifact.setEsId((String)responseMap.get("esId"));
582 artifact.setUniqueId((String) responseMap.get("uniqueId"));
583 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.findType((String) responseMap.get("artifactGroupType")));
584 artifact.setTimeout(((Long) responseMap.get("timeout")).intValue());
588 protected String getLifecycleArtifactUid(CloseableHttpResponse response) throws HttpResponseException, IOException, ParseException {
589 String responseString = new BasicResponseHandler().handleResponse(response);
590 JSONObject responseMap = (JSONObject) jsonParser.parse(responseString);
591 responseMap = (JSONObject) responseMap.get("implementation");
592 String artifactId = (String) responseMap.get("uniqueId");
596 protected HttpDelete createDeleteArtifactRequest(String url) {
597 HttpDelete httpDelete = new HttpDelete(url);
598 httpDelete.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncUserDetails.getUserId());
599 httpDelete.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
603 protected HttpPost createPostAddArtifactRequeast(String jsonBody, String url, boolean addMd5Header) throws UnsupportedEncodingException {
604 HttpPost httppost = new HttpPost(url);
605 httppost.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
606 httppost.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
607 httppost.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncUserDetails.getUserId());
609 httppost.addHeader(HttpHeaderEnum.Content_MD5.getValue(), GeneralUtility.calculateMD5Base64EncodedByString(jsonBody));
611 StringEntity input = new StringEntity(jsonBody);
612 input.setContentType("application/json");
613 httppost.setEntity(input);
614 log.debug("Executing request {}" , httppost.getRequestLine());
618 protected String createLoadArtifactBody() {
619 Map<String, Object> json = new HashMap<String, Object>();
620 json.put("artifactName", "install_apache2.sh");
621 json.put("artifactType", "SHELL");
622 json.put("description", "ddd");
623 json.put("payloadData", "UEsDBAoAAAAIAAeLb0bDQz");
624 json.put("artifactLabel", "name123");
626 String jsonStr = gson.toJson(json);
630 protected void checkDeleteResponse(RestResponse response) {
631 BaseRestUtils.checkStatusCode(response, "delete request failed", false, 204, 404);
634 protected ArtifactUiDownloadData getArtifactUiDownloadData(String artifactUiDownloadDataStr) throws Exception {
636 ObjectMapper mapper = new ObjectMapper();
638 ArtifactUiDownloadData artifactUiDownloadData = mapper.readValue(artifactUiDownloadDataStr, ArtifactUiDownloadData.class);
639 return artifactUiDownloadData;
640 } catch (Exception e) {