re base code
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / artifacts / ArtifactServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.execute.artifacts;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.gson.Gson;
25 import fj.data.Either;
26 import org.apache.http.HttpEntity;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.client.HttpResponseException;
29 import org.apache.http.client.methods.CloseableHttpResponse;
30 import org.apache.http.client.methods.HttpDelete;
31 import org.apache.http.client.methods.HttpGet;
32 import org.apache.http.client.methods.HttpPost;
33 import org.apache.http.entity.StringEntity;
34 import org.apache.http.impl.client.BasicResponseHandler;
35 import org.apache.http.impl.client.CloseableHttpClient;
36 import org.apache.http.impl.client.HttpClients;
37 import org.apache.http.util.EntityUtils;
38 import org.json.simple.JSONArray;
39 import org.json.simple.JSONObject;
40 import org.json.simple.parser.JSONParser;
41 import org.json.simple.parser.ParseException;
42 import org.junit.Rule;
43 import org.junit.rules.TestName;
44 import org.openecomp.sdc.be.dao.api.ActionStatus;
45 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
46 import org.openecomp.sdc.be.model.*;
47 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
48 import org.openecomp.sdc.ci.tests.api.Urls;
49 import org.openecomp.sdc.ci.tests.config.Config;
50 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
51 import org.openecomp.sdc.ci.tests.datatypes.enums.ErrorInfo;
52 import org.openecomp.sdc.ci.tests.datatypes.enums.NormativeTypesEnum;
53 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
54 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
55 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
56 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
57 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
58 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
59 import org.openecomp.sdc.ci.tests.utils.rest.*;
60 import org.openecomp.sdc.ci.tests.utils.validation.ErrorValidationUtils;
61 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
62 import org.openecomp.sdc.common.util.GeneralUtility;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65 import org.testng.AssertJUnit;
66 import org.testng.annotations.BeforeMethod;
67 import org.testng.annotations.Test;
68
69 import java.io.IOException;
70 import java.io.UnsupportedEncodingException;
71 import java.util.HashMap;
72 import java.util.Map;
73
74 public class ArtifactServletTest extends ComponentBaseTest {
75
76         private static Logger log = LoggerFactory.getLogger(ArtifactServletTest.class.getName());
77         protected static final String UPLOAD_ARTIFACT_PAYLOAD = "UHVUVFktVXNlci1LZXktRmlsZS0yOiBzc2gtcnNhDQpFbmNyeXB0aW9uOiBhZXMyNTYtY2JjDQpDb21tZW5wOA0K";
78         protected static final String UPLOAD_ARTIFACT_NAME = "TLV_prv.ppk";
79         protected Config config = Config.instance();
80         protected String contentTypeHeaderData = "application/json";
81         protected String acceptHeaderDate = "application/json";
82         protected Gson gson = new Gson();
83         protected JSONParser jsonParser = new JSONParser();
84         protected String serviceVersion;
85         protected Resource resourceDetailsVFCcomp;
86         protected Service defaultService1;
87
88         protected User sdncUserDetails;
89
90         @Rule
91         public static TestName name = new TestName();
92
93         public ArtifactServletTest() {
94                 super(name, ArtifactServletTest.class.getName());
95
96         }
97
98         @BeforeMethod
99         public void create() throws Exception {
100
101                 sdncUserDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
102                 Either<Resource, RestResponse> resourceDetailsVFCcompE = AtomicOperationUtils
103                                 .createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VFC, NormativeTypesEnum.COMPUTE,
104                                                 ResourceCategoryEnum.APPLICATION_L4_APP_SERVER, UserRoleEnum.DESIGNER, true);
105                 resourceDetailsVFCcomp = resourceDetailsVFCcompE.left().value();
106                 Either<Service, RestResponse> defaultService1e = AtomicOperationUtils
107                                 .createDefaultService(UserRoleEnum.DESIGNER, true);
108                 defaultService1 = defaultService1e.left().value();
109         }
110
111         @Test
112         public void upadteArtifactWithPayLoadToResourcseTest() throws Exception {
113
114                 ArtifactReqDetails defaultArtifact = ElementFactory.getDefaultArtifact();
115
116                 RestResponse response = ArtifactRestUtils.addInformationalArtifactToResource(defaultArtifact, sdncUserDetails,
117                                 resourceDetailsVFCcomp.getUniqueId());
118                 int status = response.getErrorCode();
119                 AssertJUnit.assertEquals("add informational artifact request returned status: " + response.getErrorCode(), 200,
120                                 status);
121
122                 defaultArtifact.setDescription("kjglkh");
123                 defaultArtifact.setArtifactName("install_apache.sh");
124                 defaultArtifact.setArtifactType("SHELL");
125                 defaultArtifact.setPayload("new payload");
126
127                 response = ArtifactRestUtils.updateInformationalArtifactToResource(defaultArtifact, sdncUserDetails,
128                                 resourceDetailsVFCcomp.getUniqueId());
129                 status = response.getErrorCode();
130                 AssertJUnit.assertEquals("failed to update artifact metatdata: " + response.getErrorCode(), 200, status);
131
132                 response = ArtifactRestUtils.deleteInformationalArtifactFromResource(resourceDetailsVFCcomp.getUniqueId(),
133                                 defaultArtifact, sdncUserDetails);
134                 status = response.getErrorCode();
135                 AssertJUnit.assertEquals("failed to remove artifact: " + response.getErrorCode(), 200, status);
136
137         }
138
139         @Test
140         public void createAndUpdateArtifactToInterface() throws Exception {
141
142                 CloseableHttpResponse response;
143                 int status;
144                 CloseableHttpClient httpclient = HttpClients.createDefault();
145
146                 try {
147                         // upload artifact to interface
148                         String interfaceName = "Standard";
149                         String operationName = "configure";
150
151                         String userBodyJson = createUploadArtifactBodyJson();
152                         String url = String.format(Urls.UPLOAD_ARTIFACT_BY_INTERFACE_TO_RESOURCE, config.getCatalogBeHost(),
153                                         config.getCatalogBePort(), resourceDetailsVFCcomp.getUniqueId(), interfaceName, operationName);
154
155                         HttpPost httpPost = createPostAddArtifactRequeast(userBodyJson, url, true);
156                         response = httpclient.execute(httpPost);
157                         status = response.getStatusLine().getStatusCode();
158                         AssertJUnit.assertEquals("response code is not 200, returned :" + status, status, 200);
159
160                         // get artifact uniqueId
161                         String artifactId = getLifecycleArtifactUid(response);
162
163                         Map<String, Object> jsonBody = new HashMap<String, Object>();
164                         jsonBody.put("artifactName", "TLV_prv.ppk");
165                         jsonBody.put("artifactDisplayName", "configure");
166                         jsonBody.put("artifactType", "SHELL");
167                         jsonBody.put("mandatory", "false");
168                         String newDescription = "new something";
169                         jsonBody.put("description", newDescription);
170                         jsonBody.put("artifactLabel", "configure");
171                         userBodyJson = gson.toJson(jsonBody);
172
173                         url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_BY_INTERFACE_TO_RESOURCE, config.getCatalogBeHost(),
174                                         config.getCatalogBePort(), resourceDetailsVFCcomp.getUniqueId(), interfaceName, operationName,
175                                         artifactId);
176
177                         httpPost = createPostAddArtifactRequeast(userBodyJson, url, false);
178
179                         response = httpclient.execute(httpPost);
180                         status = response.getStatusLine().getStatusCode();
181                         AssertJUnit.assertEquals("response code is not 200, returned :" + status, 200, status);
182
183                         url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
184                                         resourceDetailsVFCcomp.getUniqueId());
185                         HttpGet httpGet = createGetRequest(url);
186                         response = httpclient.execute(httpGet);
187                         AssertJUnit.assertTrue(response.getStatusLine().getStatusCode() == 200);
188                         String responseString = new BasicResponseHandler().handleResponse(response);
189
190                         JSONObject responseMap = (JSONObject) jsonParser.parse(responseString);
191                         responseMap = (JSONObject) responseMap.get("interfaces");
192                         responseMap = (JSONObject) responseMap.get(interfaceName.toLowerCase());
193                         responseMap = (JSONObject) responseMap.get("operations");
194                         responseMap = (JSONObject) responseMap.get(operationName.toLowerCase());
195                         responseMap = (JSONObject) responseMap.get("implementation");
196                         String description = (String) responseMap.get("description");
197
198                         AssertJUnit.assertEquals("the new description value was not set", newDescription, description);
199
200                         // delete artifact
201                         url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_BY_INTERFACE_TO_RESOURCE, config.getCatalogBeHost(),
202                                         config.getCatalogBePort(), resourceDetailsVFCcomp.getUniqueId(), interfaceName, operationName,
203                                         artifactId);
204                         HttpDelete httpDelete = createDeleteArtifactRequest(url);
205
206                         response = httpclient.execute(httpDelete);
207                         status = response.getStatusLine().getStatusCode();
208                         AssertJUnit.assertEquals("response code is not 200, returned :" + status, status, 200);
209                 } finally {
210                         httpclient.close();
211                 }
212
213         }
214
215         protected String createUploadArtifactBodyJson() {
216                 Map<String, Object> jsonBody = new HashMap<String, Object>();
217                 jsonBody.put("artifactName", UPLOAD_ARTIFACT_NAME);
218                 jsonBody.put("artifactDisplayName", "configure");
219                 jsonBody.put("artifactType", "SHELL");
220                 jsonBody.put("mandatory", "false");
221                 jsonBody.put("description", "ff");
222                 jsonBody.put("payloadData", UPLOAD_ARTIFACT_PAYLOAD);
223                 jsonBody.put("artifactLabel", "configure");
224                 return gson.toJson(jsonBody);
225         }
226
227         protected ArtifactDefinition getArtifactDataFromJson(String json) {
228                 Gson gson = new Gson();
229                 ArtifactDefinition artifact = new ArtifactDefinition();
230                 artifact = gson.fromJson(json, ArtifactDefinition.class);
231
232                 /*
233                  * atifact.setArtifactName(UPLOAD_ARTIFACT_NAME);
234                  * artifact.setArtifactDisplayName("configure");
235                  * artifact.setArtifactType("SHELL"); artifact.setMandatory(false);
236                  * artifact.setDescription("ff");
237                  * artifact.setPayloadData(UPLOAD_ARTIFACT_PAYLOAD);
238                  * artifact.setArtifactLabel("configure");
239                  */
240                 return artifact;
241         }
242
243         protected HttpGet createGetRequest(String url) {
244                 HttpGet httpGet = new HttpGet(url);
245                 httpGet.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
246                 httpGet.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
247                 httpGet.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncUserDetails.getUserId());
248                 return httpGet;
249         }
250
251         protected String getArtifactUid(HttpResponse response) throws HttpResponseException, IOException, ParseException {
252                 String responseString = new BasicResponseHandler().handleResponse(response);
253                 JSONObject responseMap = (JSONObject) jsonParser.parse(responseString);
254                 String artifactId = (String) responseMap.get("uniqueId");
255                 return artifactId;
256         }
257
258         protected String getArtifactEsId(HttpResponse response) throws HttpResponseException, IOException, ParseException {
259                 String responseString = new BasicResponseHandler().handleResponse(response);
260                 JSONObject responseMap = (JSONObject) jsonParser.parse(responseString);
261                 String esId = (String) responseMap.get("EsId");
262                 return esId;
263         }
264
265         protected ArtifactDefinition addArtifactDataFromResponse(HttpResponse response, ArtifactDefinition artifact)
266                         throws HttpResponseException, IOException, ParseException {
267                 // String responseString = new
268                 // BasicResponseHandler().handleResponse(response);
269                 HttpEntity entity = response.getEntity();
270                 String responseString = EntityUtils.toString(entity);
271                 JSONObject responseMap = (JSONObject) jsonParser.parse(responseString);
272                 artifact.setEsId((String) responseMap.get("esId"));
273                 artifact.setUniqueId((String) responseMap.get("uniqueId"));
274                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.findType((String) responseMap.get("artifactGroupType")));
275                 artifact.setTimeout(((Long) responseMap.get("timeout")).intValue());
276                 return artifact;
277         }
278
279         protected String getLifecycleArtifactUid(CloseableHttpResponse response)
280                         throws HttpResponseException, IOException, ParseException {
281                 String responseString = new BasicResponseHandler().handleResponse(response);
282                 JSONObject responseMap = (JSONObject) jsonParser.parse(responseString);
283                 responseMap = (JSONObject) responseMap.get("implementation");
284                 String artifactId = (String) responseMap.get("uniqueId");
285                 return artifactId;
286         }
287
288         protected HttpDelete createDeleteArtifactRequest(String url) {
289                 HttpDelete httpDelete = new HttpDelete(url);
290                 httpDelete.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncUserDetails.getUserId());
291                 httpDelete.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
292                 return httpDelete;
293         }
294
295         protected HttpPost createPostAddArtifactRequeast(String jsonBody, String url, boolean addMd5Header)
296                         throws UnsupportedEncodingException {
297                 HttpPost httppost = new HttpPost(url);
298                 httppost.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
299                 httppost.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
300                 httppost.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncUserDetails.getUserId());
301                 if (addMd5Header) {
302                         httppost.addHeader(HttpHeaderEnum.Content_MD5.getValue(), GeneralUtility.calculateMD5Base64EncodedByString(jsonBody));
303                 }
304                 StringEntity input = new StringEntity(jsonBody);
305                 input.setContentType("application/json");
306                 httppost.setEntity(input);
307                 log.debug("Executing request {}", httppost.getRequestLine());
308                 return httppost;
309         }
310
311         protected String createLoadArtifactBody() {
312                 Map<String, Object> json = new HashMap<String, Object>();
313                 json.put("artifactName", "install_apache2.sh");
314                 json.put("artifactType", "SHELL");
315                 json.put("description", "ddd");
316                 json.put("payloadData", "UEsDBAoAAAAIAAeLb0bDQz");
317                 json.put("artifactLabel", "name123");
318
319                 String jsonStr = gson.toJson(json);
320                 return jsonStr;
321         }
322
323         protected void checkDeleteResponse(RestResponse response) {
324                 BaseRestUtils.checkStatusCode(response, "delete request failed", false, 204, 404);
325         }
326
327         protected ArtifactUiDownloadData getArtifactUiDownloadData(String artifactUiDownloadDataStr) throws Exception {
328
329                 ObjectMapper mapper = new ObjectMapper();
330                 try {
331                         ArtifactUiDownloadData artifactUiDownloadData = mapper.readValue(artifactUiDownloadDataStr,
332                                         ArtifactUiDownloadData.class);
333                         return artifactUiDownloadData;
334                 } catch (Exception e) {
335                         e.printStackTrace();
336                 }
337                 return null;
338         }
339
340         // TODO
341         // @Ignore("")
342         @Test
343         public void addArtifactNoPayLoadToResourcseTest() throws Exception {
344                 ArtifactReqDetails defaultArtifact = ElementFactory.getDefaultArtifact();
345                 defaultArtifact.setPayload(null);
346
347                 RestResponse response = ArtifactRestUtils.addInformationalArtifactToResource(defaultArtifact, sdncUserDetails,
348                                 resourceDetailsVFCcomp.getUniqueId());
349                 int status = response.getErrorCode();
350                 AssertJUnit.assertTrue(status == 400);
351
352         }
353
354         @Test
355         public void upadteArtifactNoPayLoadToResourcseTest() throws Exception {
356
357                 ArtifactReqDetails defaultArtifact = ElementFactory.getDefaultArtifact();
358
359                 RestResponse response = ArtifactRestUtils.addInformationalArtifactToResource(defaultArtifact, sdncUserDetails,
360                                 resourceDetailsVFCcomp.getUniqueId());
361                 int status = response.getErrorCode();
362                 AssertJUnit.assertEquals("add informational artifact request returned status: " + response.getErrorCode(), 200,
363                                 status);
364
365                 defaultArtifact.setDescription("kjglkh");
366                 defaultArtifact.setArtifactName("install_apache.sh");
367                 defaultArtifact.setArtifactType("SHELL");
368                 defaultArtifact.setPayload(null);
369
370                 response = ArtifactRestUtils.updateInformationalArtifactToResource(defaultArtifact, sdncUserDetails,
371                                 resourceDetailsVFCcomp.getUniqueId());
372                 status = response.getErrorCode();
373                 AssertJUnit.assertEquals("failed to update artifact metatdata: " + response.getErrorCode(), 200, status);
374
375                 response = ArtifactRestUtils.deleteInformationalArtifactFromResource(resourceDetailsVFCcomp.getUniqueId(),
376                                 defaultArtifact, sdncUserDetails);
377                 status = response.getErrorCode();
378                 AssertJUnit.assertEquals("failed to remove artifact: " + response.getErrorCode(), 200, status);
379
380         }
381
382         // TODO
383         @Test(enabled = false)
384         public void updateDeploymentArtifactToResourcseTest() throws Exception {
385
386                 ArtifactReqDetails defaultArtifact = ElementFactory.getDefaultDeploymentArtifactForType("HEAT");
387
388                 RestResponse response = ArtifactRestUtils.addInformationalArtifactToResource(defaultArtifact, sdncUserDetails,
389                                 resourceDetailsVFCcomp.getUniqueId());
390                 int status = response.getErrorCode();
391                 AssertJUnit.assertEquals("add informational artifact request returned status: " + response.getErrorCode(), 200,
392                                 status);
393
394                 response = ArtifactRestUtils.updateInformationalArtifactToResource(defaultArtifact, sdncUserDetails,
395                                 resourceDetailsVFCcomp.getUniqueId());
396                 status = response.getErrorCode();
397                 AssertJUnit.assertEquals("failed to update artifact metatdata: " + response.getErrorCode(), 200, status);
398
399                 response = ArtifactRestUtils.deleteInformationalArtifactFromResource(resourceDetailsVFCcomp.getUniqueId(),
400                                 defaultArtifact, sdncUserDetails);
401                 status = response.getErrorCode();
402                 AssertJUnit.assertEquals("failed to remove artifact: " + response.getErrorCode(), 200, status);
403
404         }
405
406         // --------------------
407         @Test
408         public void addArtifactToResourcse_AlreadyExistsTest() throws Exception {
409                 CloseableHttpClient httpclient = HttpClients.createDefault();
410                 try {
411                         String jsonBody = createLoadArtifactBody();
412
413                         String url = String.format(Urls.ADD_ARTIFACT_TO_RESOURCE, config.getCatalogBeHost(),
414                                         config.getCatalogBePort(), resourceDetailsVFCcomp.getUniqueId());
415                         HttpPost httppost = createPostAddArtifactRequeast(jsonBody, url, true);
416                         CloseableHttpResponse response = httpclient.execute(httppost);
417                         int status = response.getStatusLine().getStatusCode();
418                         AssertJUnit.assertTrue("failed to add artifact", status == 200);
419
420                         String artifactId = getArtifactUid(response);
421
422                         httppost = createPostAddArtifactRequeast(jsonBody, url, true);
423                         response = httpclient.execute(httppost);
424                         status = response.getStatusLine().getStatusCode();
425                         AssertJUnit.assertEquals("the returned status code is in correct", status, 400);
426
427                         url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(),
428                                         config.getCatalogBePort(), resourceDetailsVFCcomp.getUniqueId(), artifactId);
429                         HttpDelete httpDelete = createDeleteArtifactRequest(url);
430                         response = httpclient.execute(httpDelete);
431                         status = response.getStatusLine().getStatusCode();
432                         AssertJUnit.assertTrue("failed to remove artifact", status == 200);
433                 } finally {
434                         httpclient.close();
435                 }
436
437         }
438
439         @Test
440         public void addArtifactToResourcse_MissingContentTest() throws Exception {
441
442                 CloseableHttpClient httpclient = HttpClients.createDefault();
443                 try {
444                         Map<String, Object> json = new HashMap<String, Object>();
445                         json.put("description", "desc");
446                         json.put("payloadData", "UEsDBAoAAAAIAAeLb0bDQz");
447                         json.put("Content-MD5", "YTg2Mjg4MWJhNmI5NzBiNzdDFkMWI=");
448
449                         String jsonBody = gson.toJson(json);
450
451                         String url = String.format(Urls.ADD_ARTIFACT_TO_RESOURCE, config.getCatalogBeHost(),
452                                         config.getCatalogBePort(), resourceDetailsVFCcomp.getUniqueId());
453                         HttpPost httppost = createPostAddArtifactRequeast(jsonBody, url, true);
454                         CloseableHttpResponse response = httpclient.execute(httppost);
455                         int status = response.getStatusLine().getStatusCode();
456                         AssertJUnit.assertEquals("the returned status code is in correct", status, 400);
457                 } finally {
458                         httpclient.close();
459                 }
460
461         }
462
463         @Test
464         public void addArtifactToResourcse_MissingMd5Test() throws Exception {
465
466                 CloseableHttpClient httpclient = HttpClients.createDefault();
467                 try {
468                         HashMap<String, Object> json = new HashMap<String, Object>();
469                         json.put("artifactName", "install_apache.sh");
470                         json.put("artifactType", "SHELL");
471                         json.put("description", "kjglkh");
472                         json.put("payloadData", "UEsDBYTEIWUYIFHWFMABCNAoAAAAIAAeLb0bDQz");
473                         json.put("artifactLabel", "name123");
474                         String url = String.format(Urls.ADD_ARTIFACT_TO_RESOURCE, config.getCatalogBeHost(),
475                                         config.getCatalogBePort(), resourceDetailsVFCcomp.getUniqueId());
476                         String jsonBody = gson.toJson(json);
477                         HttpPost httppost = createPostAddArtifactRequeast(jsonBody, url, false);
478                         CloseableHttpResponse response = httpclient.execute(httppost);
479                         int status = response.getStatusLine().getStatusCode();
480                         AssertJUnit.assertTrue("failed to update artifact metatdata", status == 400);
481                 } finally {
482                         httpclient.close();
483                 }
484
485         }
486
487         @Test
488         public void deleteArtifact_NotExistsTest() throws Exception {
489                 CloseableHttpClient httpclient = HttpClients.createDefault();
490                 try {
491                         String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(),
492                                         config.getCatalogBePort(), resourceDetailsVFCcomp.getUniqueId(), "someFakeId");
493                         HttpDelete httpDelete = createDeleteArtifactRequest(url);
494                         CloseableHttpResponse response = httpclient.execute(httpDelete);
495                         int status = response.getStatusLine().getStatusCode();
496                         AssertJUnit.assertEquals("the returned status code is in correct", status, 404);
497                 } finally {
498                         httpclient.close();
499                 }
500
501         }
502
503         @Test
504         public void createAndRemoveArtifactToInterface() throws Exception {
505                 CloseableHttpResponse response;
506                 int status;
507                 CloseableHttpClient httpclient = HttpClients.createDefault();
508
509                 try {
510                         // upload artifact to interface
511                         String interfaceName = "Standard";
512                         String operationName = "configure";
513
514                         String userBodyJson = createUploadArtifactBodyJson();
515                         String url = String.format(Urls.UPLOAD_ARTIFACT_BY_INTERFACE_TO_RESOURCE, config.getCatalogBeHost(),
516                                         config.getCatalogBePort(), resourceDetailsVFCcomp.getUniqueId(), interfaceName, operationName);
517
518                         HttpPost httpPost = createPostAddArtifactRequeast(userBodyJson, url, true);
519                         response = httpclient.execute(httpPost);
520                         status = response.getStatusLine().getStatusCode();
521                         AssertJUnit.assertEquals("response code is not 200, returned :" + status, status, 200);
522
523                         // get artifact uniqueId
524                         String artifactId = getLifecycleArtifactUid(response);
525
526                         // delete artifact
527                         url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_BY_INTERFACE_TO_RESOURCE, config.getCatalogBeHost(),
528                                         config.getCatalogBePort(), resourceDetailsVFCcomp.getUniqueId(), interfaceName, operationName,
529                                         artifactId);
530                         HttpDelete httpDelete = createDeleteArtifactRequest(url);
531
532                         response = httpclient.execute(httpDelete);
533                         status = response.getStatusLine().getStatusCode();
534                         AssertJUnit.assertEquals("response code is not 200, returned :" + status, status, 200);
535                 } finally {
536                         httpclient.close();
537                 }
538
539         }
540
541         @Test
542         public void addArtifactToServiceTest() throws Exception {
543
544                 CloseableHttpClient httpclient = HttpClients.createDefault();
545
546                 try {
547                         String jsonStr = createLoadArtifactBody();
548
549                         String url = String.format(Urls.ADD_ARTIFACT_TO_SERVICE, config.getCatalogBeHost(),
550                                         config.getCatalogBePort(), defaultService1.getUniqueId());
551                         HttpPost httpPost = createPostAddArtifactRequeast(jsonStr, url, true);
552                         CloseableHttpResponse result = httpclient.execute(httpPost);
553                         int status = result.getStatusLine().getStatusCode();
554                         AssertJUnit.assertEquals("response code is not 200, returned :" + status, 200, status);
555
556                         String artifactId = getArtifactUid(result);
557
558                         url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),
559                                         config.getCatalogBePort(), defaultService1.getUniqueId(), artifactId);
560                         HttpDelete httpDelete = createDeleteArtifactRequest(url);
561
562                         result = httpclient.execute(httpDelete);
563                         status = result.getStatusLine().getStatusCode();
564                         AssertJUnit.assertEquals("response code is not 200, returned :" + status, 200, status);
565                 } finally {
566                         RestResponse response = ServiceRestUtils.deleteService(defaultService1.getName(), serviceVersion,
567                                         sdncUserDetails);
568                         checkDeleteResponse(response);
569                         httpclient.close();
570                 }
571         }
572
573         @Test
574         public void addArtifactNotSupportedTypeToServiceTest() throws Exception {
575                 CloseableHttpClient httpclient = HttpClients.createDefault();
576                 try {
577                         Map<String, Object> json = new HashMap<String, Object>();
578                         json.put("artifactName", "install_apache.sh");
579                         json.put("artifactType", "SHELL11");
580                         json.put("description", "fff");
581                         json.put("payloadData", "UEsDBAoAAAAIAAeLb0bDQz");
582                         json.put("artifactLabel", "name123");
583
584                         String jsonStr = gson.toJson(json);
585
586                         String url = String.format(Urls.ADD_ARTIFACT_TO_SERVICE, config.getCatalogBeHost(),
587                                         config.getCatalogBePort(), defaultService1.getUniqueId());
588
589                         HttpPost httpPost = createPostAddArtifactRequeast(jsonStr, url, true);
590                         CloseableHttpResponse result = httpclient.execute(httpPost);
591                         int status = result.getStatusLine().getStatusCode();
592                         AssertJUnit.assertEquals("response code is not 400, returned :" + status, 400, status);
593
594                         ErrorInfo errorInfo = ErrorValidationUtils
595                                         .parseErrorConfigYaml(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED.name());
596
597                         String responseString = EntityUtils.toString(result.getEntity());
598
599                         JSONObject map = (JSONObject) jsonParser.parse(responseString);
600                         JSONObject requestError = (JSONObject) map.get("requestError");
601                         JSONObject serviceException = (JSONObject) requestError.get("serviceException");
602
603                         String msgId = (String) serviceException.get("messageId");
604                         AssertJUnit.assertEquals("message id did not match expacted", errorInfo.getMessageId(), msgId);
605
606                         String text = (String) serviceException.get("text");
607                         AssertJUnit.assertEquals("text did not match expacted", errorInfo.getMessage(), text);
608
609                         JSONArray variables = (JSONArray) serviceException.get("variables");
610                         String type = (String) variables.get(0);
611                         AssertJUnit.assertEquals("variable did not match expacted", "SHELL11", type);
612                 } finally {
613                         RestResponse response = ServiceRestUtils.deleteService(defaultService1.getName(), serviceVersion,
614                                         sdncUserDetails);
615                         checkDeleteResponse(response);
616                         httpclient.close();
617                 }
618
619         }
620
621         @Test
622         public void addArtifactToResourceTest() throws Exception {
623
624                 ArtifactReqDetails defaultArtifact = ElementFactory.getDefaultArtifact();
625
626                 RestResponse response = ArtifactRestUtils.addInformationalArtifactToResource(defaultArtifact, sdncUserDetails,
627                                 resourceDetailsVFCcomp.getUniqueId());
628                 int status = response.getErrorCode();
629                 AssertJUnit.assertEquals("add informational artifact request returned status: " + response.getErrorCode(), 200,
630                                 status);
631
632                 RestResponse resourceResp = ResourceRestUtils.getResource(resourceDetailsVFCcomp.getUniqueId());
633                 Resource resource = ResponseParser.convertResourceResponseToJavaObject(resourceResp.getResponse());
634                 AssertJUnit.assertNotNull(resource);
635
636                 Map<String, ArtifactDefinition> artifacts = resource.getArtifacts();
637                 boolean isExist = false;
638                 for (Map.Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) {
639                         if (entry.getKey().equals(defaultArtifact.getArtifactLabel())) {
640                                 isExist = true;
641
642                         }
643                 }
644                 AssertJUnit.assertTrue(isExist);
645         }
646 }