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