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