Fix for radio buttons
[sdc.git] / asdc-tests / src / main / java / org / openecomp / sdc / ci / tests / execute / distribution / DistributionDownloadArtifactTest.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.distribution;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.InputStream;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.zip.ZipInputStream;
32
33 import org.apache.commons.codec.binary.Base64;
34 import org.junit.Rule;
35 import org.junit.rules.TestName;
36 import org.openecomp.sdc.be.datatypes.elements.ConsumerDataDefinition;
37 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
38 import org.openecomp.sdc.be.model.ArtifactDefinition;
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.api.Urls;
44 import org.openecomp.sdc.ci.tests.config.Config;
45 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
46 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
47 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
48 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
49 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
50 import org.openecomp.sdc.ci.tests.datatypes.enums.NormativeTypesEnum;
51 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
52 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
53 import org.openecomp.sdc.ci.tests.datatypes.expected.ExpectedDistDownloadAudit;
54 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
55 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
56 import org.openecomp.sdc.ci.tests.utils.ArtifactUtils;
57 import org.openecomp.sdc.ci.tests.utils.Utils;
58 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
59 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
60 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
61 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
62 import org.openecomp.sdc.ci.tests.utils.rest.ConsumerRestUtils;
63 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
64 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
65 import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
66 import org.openecomp.sdc.ci.tests.utils.validation.AuditValidationUtils;
67 import org.openecomp.sdc.common.api.Constants;
68 import org.openecomp.sdc.common.util.GeneralUtility;
69 import org.openecomp.sdc.common.util.ValidationUtils;
70 import org.testng.AssertJUnit;
71 import org.testng.annotations.BeforeMethod;
72 import org.testng.annotations.Test;
73
74 public class DistributionDownloadArtifactTest extends ComponentBaseTest {
75
76         protected static ResourceReqDetails resourceDetails;
77         protected static User designerUser;
78         protected static User adminUser;
79         protected static String resourceBaseVersion;
80         // user ci password 123456
81         protected final String authorizationHeader = "Basic Y2k6MTIzNDU2";
82         protected ConsumerDataDefinition consumerDataDefinition;
83
84         @Rule
85         public static TestName name = new TestName();
86         protected static String artifactInterfaceType;
87         protected static String artifactOperationName;
88
89         protected static ServiceReqDetails serviceDetails;
90         protected static String serviceBaseVersion;
91         protected static String serviceUniqueId;
92         protected final String USER = "ci";
93         protected final String PASSWORD = "123456";
94         protected final String SALT = "2a1f887d607d4515d4066fe0f5452a50";
95         protected final String HASHED_PASSWORD = "0a0dc557c3bf594b1a48030e3e99227580168b21f44e285c69740b8d5b13e33b";
96
97         public DistributionDownloadArtifactTest() {
98                 super(name, DistributionDownloadArtifactTest.class.getName());
99         }
100
101         // @BeforeClass
102         // public static void InitBeforeTest() throws Exception
103         // {
104         //
105         //
106         // resourceBaseVersion = "0.1";
107         // serviceBaseVersion = "0.1";
108         // designerUser = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
109         // adminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
110         // resourceDetails =
111         // ElementFactory.getDefaultResourceByTypeNormTypeAndCatregory(ResourceTypeEnum.VFC,
112         // NormativeTypesEnum.ROOT, ResourceCategoryEnum.NETWORK_L2_3_ROUTERS,
113         // adminUser);
114         // serviceDetails = ElementFactory.getDefaultService();
115         // serviceUniqueId = "svc_" + serviceDetails.getName().toLowerCase() + "." +
116         // serviceBaseVersion;
117         // artifactInterfaceType = "standard";
118         // artifactOperationName = "start";
119         // }
120
121         @BeforeMethod
122         public void setup() throws Exception {
123
124                 resourceBaseVersion = "0.1";
125                 serviceBaseVersion = "0.1";
126                 designerUser = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
127                 adminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
128                 resourceDetails = ElementFactory.getDefaultResourceByTypeNormTypeAndCatregory(ResourceTypeEnum.VFC,
129                                 NormativeTypesEnum.ROOT, ResourceCategoryEnum.NETWORK_L2_3_ROUTERS, adminUser);
130                 serviceDetails = ElementFactory.getDefaultService();
131                 serviceUniqueId = "svc_" + serviceDetails.getName().toLowerCase() + "." + serviceBaseVersion;
132                 artifactInterfaceType = "standard";
133                 artifactOperationName = "start";
134                 consumerDataDefinition = createConsumer();
135                 RestResponse deleteResponse = ConsumerRestUtils.deleteConsumer(consumerDataDefinition, adminUser);
136                 BaseRestUtils.checkStatusCode(deleteResponse, "delete operation filed", false, 404, 200);
137
138                 RestResponse createResponse = ConsumerRestUtils.createConsumer(consumerDataDefinition, adminUser);
139                 BaseRestUtils.checkCreateResponse(createResponse);
140         }
141
142         @Test
143         public void downloadResourceArtifactSuccess() throws Exception {
144                 // Create service
145                 RestResponse serviceResponse = ServiceRestUtils.createService(serviceDetails, designerUser);
146                 AssertJUnit.assertEquals("Check response code after creating resource", 201,
147                                 serviceResponse.getErrorCode().intValue());
148
149                 // Create resource
150                 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, designerUser);
151                 AssertJUnit.assertEquals("Check response code after creating resource", 201,
152                                 createResource.getErrorCode().intValue());
153                 Resource resource = ResponseParser.convertResourceResponseToJavaObject(createResource.getResponse());
154
155                 ArtifactReqDetails artifactDetails = ElementFactory
156                                 .getDefaultDeploymentArtifactForType(ArtifactTypeEnum.HEAT.getType());
157                 // Setting the name to be with space
158                 artifactDetails.setArtifactName("test artifact file.yaml");
159                 // artifactDetails.set(ArtifactRestUtils.calculateChecksum(artifactDetails));
160
161                 RestResponse addArtifactResponse = ArtifactRestUtils.addInformationalArtifactToResource(artifactDetails,
162                                 designerUser, resource.getUniqueId(), ArtifactRestUtils.calculateChecksum(artifactDetails));
163                 AssertJUnit.assertEquals("Check response code after adding interface artifact", 200,
164                                 addArtifactResponse.getErrorCode().intValue());
165
166                 // Getting expected artifact checksum
167                 ArtifactDefinition artifactResp = ResponseParser
168                                 .convertArtifactDefinitionResponseToJavaObject(addArtifactResponse.getResponse());
169                 String expectedPayloadChecksum = artifactResp.getArtifactChecksum();
170
171                 Config config = Utils.getConfig();
172                 String relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_RESOURCE_ARTIFACT_RELATIVE_URL,
173                                 ValidationUtils.convertToSystemName(serviceDetails.getName()), serviceBaseVersion,
174                                 ValidationUtils.convertToSystemName(resource.getName()), resource.getVersion(),
175                                 artifactResp.getArtifactName()));
176                 // String fullUrlFormatted =
177                 // String.format(Urls.DOWNLOAD_RESOURCE_ARTIFACT_FULL_URL,
178                 // config.getCatalogBeHost(),config.getCatalogBePort(), relativeUrl);
179                 // String consumerId = "dummy.ecomp";
180
181                 ResourceReqDetails resourceInfo = new ResourceReqDetails();
182                 resourceInfo.setName(resource.getName());
183                 resourceInfo.setVersion(resource.getVersion());
184
185                 Map<String, String> authorizationHeaders = new HashMap<String, String>();
186                 authorizationHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
187                 RestResponse restResponse = ArtifactRestUtils.downloadResourceArtifact(serviceDetails, resourceInfo,
188                                 artifactDetails, designerUser, authorizationHeaders);
189                 // RestResponse restResponse =
190                 // artifactUtils.downloadResourceArtifact(designerUser,fullUrlFormatted,
191                 // consumerId,true);
192                 AssertJUnit.assertEquals("Check response code after download resource", 200,
193                                 restResponse.getErrorCode().intValue());
194
195                 // Validating headers
196                 // content disposition
197                 List<String> contDispHeaderList = restResponse.getHeaderFields().get(Constants.CONTENT_DISPOSITION_HEADER);
198                 AssertJUnit.assertNotNull(contDispHeaderList);
199                 AssertJUnit
200                                 .assertEquals(
201                                                 "Check content disposition header", new StringBuilder().append("attachment; filename=\"")
202                                                                 .append(artifactResp.getArtifactName()).append("\"").toString(),
203                                                 contDispHeaderList.get(0));
204
205                 // content type
206                 List<String> contTypeHeaderList = restResponse.getHeaderFields().get(Constants.CONTENT_TYPE_HEADER);
207                 AssertJUnit.assertNotNull(contTypeHeaderList);
208                 AssertJUnit.assertEquals("Check content type", "application/octet-stream", contTypeHeaderList.get(0));
209
210                 String actualContents = restResponse.getResponse();
211
212                 // Contents - comparing decoded content
213                 AssertJUnit.assertEquals(artifactDetails.getPayload(), Base64.encodeBase64String(actualContents.getBytes()));
214
215                 // validating checksum
216                 String actualPayloadChecksum = GeneralUtility.calculateMD5Base64EncodedByByteArray(actualContents.getBytes());
217                 AssertJUnit.assertEquals(expectedPayloadChecksum, actualPayloadChecksum);
218
219                 // validate audit
220                 String auditAction = "DArtifactDownload";
221
222                 ExpectedDistDownloadAudit expectedDistDownloadAudit = new ExpectedDistDownloadAudit(auditAction,
223                                 BaseRestUtils.ecomp, relativeUrl, "200", "OK");
224                 AuditValidationUtils.validateAudit(expectedDistDownloadAudit, auditAction);
225         }
226
227         protected void download_serviceNameNotFound_inner(String serviceName, String serviceVersion, String resourceName,
228                         String resourceVersion) throws Exception {
229                 Config config = Utils.getConfig();
230                 String artifactName = "kuku";
231                 ArtifactReqDetails artifact = new ArtifactReqDetails();
232                 artifact.setArtifactName(artifactName);
233                 String relativeUrl;
234                 Map<String, String> authorizationHeaders = new HashMap<String, String>();
235                 authorizationHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
236                 ServiceReqDetails serviceInfo = new ServiceReqDetails();
237                 serviceInfo.setName(serviceName);
238                 serviceInfo.setVersion(serviceVersion);
239                 RestResponse restResponse = null;
240                 if (resourceName != null) {
241                         ResourceReqDetails resourceDetailes = new ResourceReqDetails();
242                         resourceDetailes.setName(resourceName);
243                         resourceDetailes.setVersion(resourceVersion);
244                         relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_RESOURCE_ARTIFACT_RELATIVE_URL,
245                                         ValidationUtils.convertToSystemName(serviceName), serviceVersion,
246                                         ValidationUtils.convertToSystemName(resourceName), resourceVersion, artifactName));
247                         restResponse = ArtifactRestUtils.downloadResourceArtifact(serviceInfo, resourceDetailes, artifact,
248                                         designerUser, authorizationHeaders);
249                 } else {
250                         relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_SERVICE_ARTIFACT_RELATIVE_URL,
251                                         ValidationUtils.convertToSystemName(serviceName), serviceVersion, artifactName));
252                         restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceInfo, artifact, designerUser,
253                                         authorizationHeaders);
254                 }
255
256                 // RestResponse restResponse =
257                 // artifactUtils.downloadResourceArtifact(designerUser,fullUrlFormatted,
258                 // consumerId,true);
259                 AssertJUnit.assertEquals("Check response code after download resource", 404,
260                                 restResponse.getErrorCode().intValue());
261
262                 // validate audit
263                 String auditAction = "DArtifactDownload";
264
265                 ExpectedDistDownloadAudit expectedDistDownloadAudit = new ExpectedDistDownloadAudit(auditAction,
266                                 BaseRestUtils.ecomp, relativeUrl, "404", "SVC4503: Error: Requested '"
267                                                 + ValidationUtils.convertToSystemName(serviceName) + "' service was not found.");
268                 AuditValidationUtils.validateAudit(expectedDistDownloadAudit, auditAction);
269         }
270
271         protected void download_serviceVersionNotFound_inner(String serviceName, String serviceVersion, String resourceName,
272                         String resourceVersion) throws Exception {
273                 Config config = Utils.getConfig();
274                 String artifactName = "kuku";
275                 String relativeUrl;
276                 ArtifactReqDetails artifact = new ArtifactReqDetails();
277                 artifact.setArtifactName(artifactName);
278                 Map<String, String> authorizationHeaders = new HashMap<String, String>();
279                 authorizationHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
280                 ServiceReqDetails serviceInfo = new ServiceReqDetails();
281                 serviceInfo.setName(serviceName);
282                 serviceInfo.setVersion(serviceVersion);
283                 RestResponse restResponse = null;
284                 if (resourceName != null) {
285                         ResourceReqDetails resourceDetailes = new ResourceReqDetails();
286                         resourceDetailes.setName(resourceName);
287                         resourceDetailes.setVersion(resourceVersion);
288                         relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_RESOURCE_ARTIFACT_RELATIVE_URL,
289                                         ValidationUtils.convertToSystemName(serviceName), serviceVersion,
290                                         ValidationUtils.convertToSystemName(resourceName), resourceVersion, artifactName));
291                         restResponse = ArtifactRestUtils.downloadResourceArtifact(serviceInfo, resourceDetailes, artifact,
292                                         designerUser, authorizationHeaders);
293                 } else {
294                         relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_SERVICE_ARTIFACT_RELATIVE_URL,
295                                         ValidationUtils.convertToSystemName(serviceName), serviceVersion, artifactName));
296                         restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceInfo, artifact, designerUser,
297                                         authorizationHeaders);
298                 }
299                 // String fullUrlFormatted =
300                 // String.format(Urls.DOWNLOAD_RESOURCE_ARTIFACT_FULL_URL,
301                 // config.getCatalogBeHost(),config.getCatalogBePort(), relativeUrl);
302                 // String consumerId = "dummy.ecomp";
303
304                 // RestResponse restResponse =
305                 // artifactUtils.downloadResourceArtifact(designerUser,fullUrlFormatted,
306                 // consumerId,true);
307                 AssertJUnit.assertEquals("Check response code after download resource", 404,
308                                 restResponse.getErrorCode().intValue());
309
310                 // validate audit
311                 String auditAction = "DArtifactDownload";
312
313                 ExpectedDistDownloadAudit expectedDistDownloadAudit = new ExpectedDistDownloadAudit(auditAction,
314                                 BaseRestUtils.ecomp, relativeUrl, "404",
315                                 "SVC4504: Error: Service version " + serviceVersion + " was not found.");
316                 AuditValidationUtils.validateAudit(expectedDistDownloadAudit, auditAction);
317         }
318
319         protected String encodeUrlForDownload(String url) {
320                 return url.replaceAll(" ", "%20");
321         }
322
323         protected ConsumerDataDefinition createConsumer() {
324                 ConsumerDataDefinition consumer = new ConsumerDataDefinition();
325                 consumer.setConsumerName(USER);
326                 consumer.setConsumerSalt(SALT);
327                 consumer.setConsumerPassword(HASHED_PASSWORD);
328                 return consumer;
329
330         }
331
332         @Test(enabled = false)
333         public void downloadServiceArtifactSuccess() throws Exception {
334                 // Create service
335                 RestResponse serviceResponse = ServiceRestUtils.createService(serviceDetails, designerUser);
336                 assertEquals("Check response code after creating resource", 201, serviceResponse.getErrorCode().intValue());
337                 serviceUniqueId = ResponseParser.convertServiceResponseToJavaObject(serviceResponse.getResponse())
338                                 .getUniqueId();
339
340                 ArtifactReqDetails artifactDetails = ElementFactory.getDefaultDeploymentArtifactForType("MURANO_PKG");
341
342                 RestResponse addArtifactResponse = ArtifactRestUtils.addInformationalArtifactToService(artifactDetails,
343                                 designerUser, serviceUniqueId, ArtifactRestUtils.calculateMD5Header(artifactDetails));
344                 assertEquals("Check response code after adding interface artifact", 200,
345                                 addArtifactResponse.getErrorCode().intValue());
346
347                 // Getting expected artifact checksum
348
349                 // ArtifactResJavaObject artifactResp =
350                 // artifactUtils.parseInformationalArtifactResp(addArtifactResponse);
351                 String expectedPayloadChecksum = ResponseParser
352                                 .convertArtifactDefinitionResponseToJavaObject(addArtifactResponse.getResponse()).getArtifactChecksum();
353
354                 String artifactName = ValidationUtils.normalizeFileName(artifactDetails.getArtifactName());
355
356                 String relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_SERVICE_ARTIFACT_RELATIVE_URL,
357                                 ValidationUtils.convertToSystemName(serviceDetails.getName()), serviceBaseVersion, artifactName));
358
359                 Map<String, String> authorizationHeaders = new HashMap<String, String>();
360                 authorizationHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
361                 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, artifactDetails,
362                                 designerUser, authorizationHeaders);
363                 assertEquals("Check response code after download resource", 200, restResponse.getErrorCode().intValue());
364
365                 // Validating headers
366                 // content disposition
367                 List<String> contDispHeaderList = restResponse.getHeaderFields().get(Constants.CONTENT_DISPOSITION_HEADER);
368                 assertNotNull(contDispHeaderList);
369                 assertEquals("Check content disposition header",
370                                 new StringBuilder().append("attachment; filename=\"").append(artifactName).append("\"").toString(),
371                                 contDispHeaderList.get(0));
372
373                 // content type
374                 List<String> contTypeHeaderList = restResponse.getHeaderFields().get(Constants.CONTENT_TYPE_HEADER);
375                 assertNotNull(contTypeHeaderList);
376                 assertEquals("Check content type", "application/octet-stream", contTypeHeaderList.get(0));
377
378                 String actualContents = restResponse.getResponse();
379
380                 assertEquals(artifactDetails.getPayload(), Base64.encodeBase64String(actualContents.getBytes()));
381
382                 // validating checksum
383                 byte[] bytes = actualContents.getBytes();
384                 String actualPayloadChecksum = GeneralUtility.calculateMD5Base64EncodedByByteArray(bytes);
385                 assertEquals(expectedPayloadChecksum, actualPayloadChecksum);
386
387                 // validating valid zip
388                 InputStream is = new ByteArrayInputStream(bytes);
389                 InputStream zis = new ZipInputStream(is);
390                 zis.close();
391
392                 // validate audit
393                 String auditAction = "DArtifactDownload";
394
395                 ExpectedDistDownloadAudit expectedDistDownloadAudit = new ExpectedDistDownloadAudit(auditAction,
396                                 ResourceRestUtils.ecomp, encodeUrlForDownload(relativeUrl), "200", "OK");
397                 AuditValidationUtils.validateAudit(expectedDistDownloadAudit, auditAction);
398         }
399
400         @Test
401         public void downloadResourceArtifact_NoConsumerId() throws Exception {
402
403                 String artifactName = "kuku";
404                 ArtifactReqDetails artifact = new ArtifactReqDetails();
405                 artifact.setArtifactName(artifactName);
406                 ResourceReqDetails resource = new ResourceReqDetails();
407                 resource.setName("notExisting");
408                 resource.setVersion("0.1");
409                 String relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_RESOURCE_ARTIFACT_RELATIVE_URL,
410                                 ValidationUtils.convertToSystemName(serviceDetails.getName()), serviceBaseVersion,
411                                 ValidationUtils.convertToSystemName(resource.getName()), resource.getVersion(), artifactName));
412                 serviceDetails.setVersion("0.1");
413                 Map<String, String> authorizationHeaders = new HashMap<String, String>();
414                 authorizationHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
415                 RestResponse restResponse = ArtifactRestUtils.downloadResourceArtifact(serviceDetails, resource, artifact,
416                                 designerUser, authorizationHeaders, false);
417                 assertEquals("Check response code after download resource", 400, restResponse.getErrorCode().intValue());
418
419                 // validate audit
420                 String auditAction = "DArtifactDownload";
421
422                 ExpectedDistDownloadAudit expectedDistDownloadAudit = new ExpectedDistDownloadAudit(auditAction, "",
423                                 relativeUrl, "400", "POL5001: Error: Missing 'X-ECOMP-InstanceID' HTTP header.");
424                 AuditValidationUtils.validateAudit(expectedDistDownloadAudit, auditAction);
425         }
426
427         @Test
428         public void downloadResourceArtifact_ResourceNameNotFound() throws Exception {
429
430                 String artifactName = "kuku";
431                 ArtifactReqDetails artifact = new ArtifactReqDetails();
432                 artifact.setArtifactName(artifactName);
433                 ResourceReqDetails resource = new ResourceReqDetails();
434                 resource.setName("notExisting");
435                 resource.setVersion("0.1");
436                 serviceDetails.setVersion("0.1");
437                 String relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_RESOURCE_ARTIFACT_RELATIVE_URL,
438                                 ValidationUtils.convertToSystemName(serviceDetails.getName()), serviceDetails.getVersion(),
439                                 ValidationUtils.convertToSystemName(resource.getName()), resource.getVersion(), artifactName));
440
441                 Map<String, String> authorizationHeaders = new HashMap<String, String>();
442                 authorizationHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
443                 RestResponse restResponse = ArtifactRestUtils.downloadResourceArtifact(serviceDetails, resource, artifact,
444                                 designerUser, authorizationHeaders);
445
446                 assertEquals("Check response code after download resource", 404, restResponse.getErrorCode().intValue());
447
448                 // validate audit
449                 String auditAction = "DArtifactDownload";
450
451                 ExpectedDistDownloadAudit expectedDistDownloadAudit = new ExpectedDistDownloadAudit(auditAction,
452                                 BaseRestUtils.ecomp, relativeUrl, "404",
453                                 "SVC4063: Error: Requested 'Notexisting' resource was not found.");
454                 AuditValidationUtils.validateAudit(expectedDistDownloadAudit, auditAction);
455         }
456
457         @Test
458         public void downloadResourceArtifact_ResourceVersionNotFound() throws Exception {
459                 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, designerUser);
460                 assertEquals("Check response code after creating resource", 201, createResource.getErrorCode().intValue());
461
462                 Resource resource = ResponseParser.convertResourceResponseToJavaObject(createResource.getResponse());
463                 ResourceReqDetails resourceDetailes = new ResourceReqDetails();
464                 resourceDetailes.setName(resource.getName());
465                 resourceDetailes.setVersion("0.2");
466
467                 serviceDetails.setVersion("0.1");
468
469                 String artifactName = "kuku";
470                 ArtifactReqDetails artifact = new ArtifactReqDetails();
471                 artifact.setArtifactName(artifactName);
472
473                 String relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_RESOURCE_ARTIFACT_RELATIVE_URL,
474                                 ValidationUtils.convertToSystemName(serviceDetails.getName()), serviceBaseVersion,
475                                 ValidationUtils.convertToSystemName(resourceDetailes.getName()), resourceDetailes.getVersion(),
476                                 artifactName));
477
478                 Map<String, String> authorizationHeaders = new HashMap<String, String>();
479                 authorizationHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
480                 RestResponse restResponse = ArtifactRestUtils.downloadResourceArtifact(serviceDetails, resourceDetailes,
481                                 artifact, designerUser, authorizationHeaders);
482                 assertEquals("Check response code after download resource", 404, restResponse.getErrorCode().intValue());
483
484                 // validate audit
485                 String auditAction = "DArtifactDownload";
486
487                 ExpectedDistDownloadAudit expectedDistDownloadAudit = new ExpectedDistDownloadAudit(auditAction,
488                                 BaseRestUtils.ecomp, relativeUrl, "404", "SVC4504: Error: Resource version 0.2 was not found.");
489                 AuditValidationUtils.validateAudit(expectedDistDownloadAudit, auditAction);
490         }
491
492         @Test
493         public void downloadResourceArtifact_ServiceNameNotFound() throws Exception {
494                 // Create resource
495                 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, designerUser);
496                 assertEquals("Check response code after creating resource", 201, createResource.getErrorCode().intValue());
497                 Resource resource = ResponseParser.convertResourceResponseToJavaObject(createResource.getResponse());
498                 download_serviceNameNotFound_inner("notExistingServiceName", serviceBaseVersion, resource.getName(),
499                                 resource.getVersion());
500
501         }
502
503         @Test
504         public void downloadResourceArtifact_ServiceVersionNotFound() throws Exception {
505                 // Create resource
506                 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, designerUser);
507                 assertEquals("Check response code after creating resource", 201, createResource.getErrorCode().intValue());
508                 Resource resource = ResponseParser.convertResourceResponseToJavaObject(createResource.getResponse());
509
510                 // Create service
511                 RestResponse serviceResponse = ServiceRestUtils.createService(serviceDetails, designerUser);
512                 assertEquals("Check response code after creating resource", 201, serviceResponse.getErrorCode().intValue());
513                 serviceUniqueId = ResponseParser.convertServiceResponseToJavaObject(serviceResponse.getResponse())
514                                 .getUniqueId();
515
516                 download_serviceVersionNotFound_inner(serviceDetails.getName(), "0.3", resource.getName(),
517                                 resource.getVersion());
518         }
519
520         @Test
521         public void downloadServiceArtifact_ServiceNameNotFound() throws Exception {
522                 download_serviceNameNotFound_inner("notExistingServiceName", serviceBaseVersion, null, null);
523
524         }
525
526         @Test
527         public void downloadServiceArtifact_ServiceVersionNotFound() throws Exception {
528
529                 // Create service
530                 RestResponse serviceResponse = ServiceRestUtils.createService(serviceDetails, designerUser);
531                 assertEquals("Check response code after creating resource", 201, serviceResponse.getErrorCode().intValue());
532                 serviceUniqueId = ResponseParser.convertServiceResponseToJavaObject(serviceResponse.getResponse())
533                                 .getUniqueId();
534
535                 download_serviceVersionNotFound_inner(serviceDetails.getName(), "0.2", null, null);
536         }
537         
538         @Test
539         public void downloadServiceToscaArtifactSuccess() throws Exception {
540                 // Creates service
541                 RestResponse serviceResponse = ServiceRestUtils.createService(serviceDetails, designerUser);
542                 assertEquals("Check response code after creating resource", 201, serviceResponse.getErrorCode().intValue());
543                 Service service = ResponseParser.convertServiceResponseToJavaObject(serviceResponse.getResponse());
544                 service = (Service) AtomicOperationUtils.changeComponentState(service, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
545                 // Approves service distribution
546                 RestResponse serviceDistributionApproveRes = ServiceRestUtils.approveServiceDistribution(service.getUniqueId(), UserRoleEnum.GOVERNOR.getUserId());
547                 service = ResponseParser.convertServiceResponseToJavaObject(serviceDistributionApproveRes.getResponse());
548                 // Distributes service
549                 RestResponse serviceDistributionRes = AtomicOperationUtils.distributeService(service, true);
550                 service = ResponseParser.convertServiceResponseToJavaObject(serviceDistributionRes.getResponse());
551                 // Gets tosca template artifact from service
552                 ArtifactDefinition toscaTemplateArtifact = service.getToscaArtifacts().get("assettoscatemplate");
553                 String expectedPayloadChecksum = toscaTemplateArtifact.getArtifactChecksum();
554                 String artifactName = toscaTemplateArtifact.getArtifactName();
555                 ArtifactReqDetails artifactDetails = ArtifactUtils.convertArtifactDefinitionToArtifactReqDetails(toscaTemplateArtifact);
556                 // Downloads tosca template artifact
557                 Map<String, String> authorizationHeaders = new HashMap<String, String>();
558                 authorizationHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
559                 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, artifactDetails,
560                                 designerUser, authorizationHeaders);
561                 assertEquals("Check response code after download resource", 200, restResponse.getErrorCode().intValue());
562
563                 // Validating headers
564                 // content disposition
565                 List<String> contDispHeaderList = restResponse.getHeaderFields().get(Constants.CONTENT_DISPOSITION_HEADER);
566                 assertNotNull(contDispHeaderList);
567                 assertEquals("Check content disposition header",
568                                 new StringBuilder().append("attachment; filename=\"").append(artifactName).append("\"").toString(),
569                                 contDispHeaderList.get(0));
570
571                 // content type
572                 List<String> contTypeHeaderList = restResponse.getHeaderFields().get(Constants.CONTENT_TYPE_HEADER);
573                 assertNotNull(contTypeHeaderList);
574                 assertEquals("Check content type", "application/octet-stream", contTypeHeaderList.get(0));
575                 String actualContents = restResponse.getResponse();
576
577                 // validating checksum
578                 byte[] bytes = actualContents.getBytes();
579                 String actualPayloadChecksum = GeneralUtility.calculateMD5Base64EncodedByByteArray(bytes);
580                 AssertJUnit.assertEquals(expectedPayloadChecksum, actualPayloadChecksum);
581
582                 // validating valid zip
583                 InputStream is = new ByteArrayInputStream(bytes);
584                 InputStream zis = new ZipInputStream(is);
585                 zis.close();
586
587                 // validate audit
588                 String relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_SERVICE_ARTIFACT_RELATIVE_URL,
589                                 ValidationUtils.convertToSystemName(serviceDetails.getName()), serviceBaseVersion, artifactName));
590                 String auditAction = "DArtifactDownload";
591                 ExpectedDistDownloadAudit expectedDistDownloadAudit = new ExpectedDistDownloadAudit(auditAction,
592                                 ResourceRestUtils.ecomp, encodeUrlForDownload(relativeUrl), "200", "OK");
593                 AuditValidationUtils.validateAudit(expectedDistDownloadAudit, auditAction);
594         }
595
596 }