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