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