Sync Integ to Master
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / rest / AssetRestUtils.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.utils.rest;
22
23 import static org.testng.AssertJUnit.assertNotNull;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import java.io.File;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Map.Entry;
34
35 import org.apache.http.HttpEntity;
36 import org.apache.http.HttpResponse;
37 import org.apache.http.client.methods.CloseableHttpResponse;
38 import org.apache.http.client.methods.HttpGet;
39 import org.apache.http.impl.client.CloseableHttpClient;
40 import org.apache.http.impl.client.HttpClients;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42 import org.openecomp.sdc.be.datatypes.enums.AssetTypeEnum;
43 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
44 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
45 import org.openecomp.sdc.be.model.ArtifactDefinition;
46 import org.openecomp.sdc.be.model.Component;
47 import org.openecomp.sdc.be.model.ComponentInstance;
48 import org.openecomp.sdc.be.model.Resource;
49 import org.openecomp.sdc.be.model.Service;
50 import org.openecomp.sdc.ci.tests.api.Urls;
51 import org.openecomp.sdc.ci.tests.config.Config;
52 import org.openecomp.sdc.ci.tests.datatypes.ArtifactAssetStructure;
53 import org.openecomp.sdc.ci.tests.datatypes.AssetStructure;
54 import org.openecomp.sdc.ci.tests.datatypes.ResourceAssetStructure;
55 import org.openecomp.sdc.ci.tests.datatypes.ResourceDetailedAssetStructure;
56 import org.openecomp.sdc.ci.tests.datatypes.ResourceInstanceAssetStructure;
57 import org.openecomp.sdc.ci.tests.datatypes.ServiceAssetStructure;
58 import org.openecomp.sdc.ci.tests.datatypes.ServiceDetailedAssetStructure;
59 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
60 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
61 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
62 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
63 import org.openecomp.sdc.ci.tests.utils.Utils;
64 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
65 import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
68
69 import com.google.gson.Gson;
70 import com.google.gson.JsonArray;
71 import com.google.gson.JsonElement;
72 import com.google.gson.JsonObject;
73 import com.google.gson.JsonParser;
74
75 public class AssetRestUtils extends BaseRestUtils {
76         static Gson gson = new Gson();
77         static ObjectMapper objectMapper = new ObjectMapper();
78
79         static Logger logger = LoggerFactory.getLogger(AssetRestUtils.class.getName());
80         static final String basicAuthentication = "Basic Y2k6MTIzNDU2";
81         // /sdc/v1/catalog/{services/resources}/{componentUUID}/artifacts/{artifactUUID}
82         static final String COMPONENT_ARTIFACT_URL = "/sdc/v1/catalog/%s/%s/artifacts/%s";
83         // /sdc/v1/catalog/{services/resources}/{componentUUID}/resourceInstances/{resourceInstanceName}/artifacts/{artifactUUID}
84         static final String RESOURCE_INSTANCE_ARTIFACT_URL = "/sdc/v1/catalog/%s/%s/resourceInstances/%s/artifacts/%s";
85
86         public static HttpResponse getComponentToscaModel(AssetTypeEnum assetType, String uuid) throws IOException {
87                 Config config = Utils.getConfig();
88                 CloseableHttpClient httpclient = HttpClients.createDefault();
89                 String url = String.format(Urls.GET_TOSCA_MODEL, config.getCatalogBeHost(), config.getCatalogBePort(),
90                                 assetType.getValue(), uuid);
91                 HttpGet httpGet = new HttpGet(url);
92
93                 httpGet.addHeader(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
94                 httpGet.addHeader(HttpHeaderEnum.AUTHORIZATION.getValue(), basicAuthentication);
95
96                 logger.debug("Send GET request to get Tosca model: {}", url);
97
98                 return httpclient.execute(httpGet);
99         }
100         
101         public static File getToscaModelCsarFile(AssetTypeEnum assetType, String uuid) throws IOException {
102                 return getToscaModelCsarFile(assetType, uuid, "");
103         }
104         
105         public static File getToscaModelCsarFile(AssetTypeEnum assetType, String uuid, String fileName) throws IOException {
106                 File myFile;
107                 Config config = Utils.getConfig();
108                 CloseableHttpClient httpclient = HttpClients.createDefault();
109                 String url = String.format(Urls.GET_TOSCA_MODEL, config.getCatalogBeHost(), config.getCatalogBePort(),
110                                 assetType.getValue(), uuid);
111                 HttpGet httpGet = new HttpGet(url);
112                 
113                 if(fileName.isEmpty()) {
114                         myFile = new File("tmpCSAR");                   
115                 } else {
116                         String csarDir = FileHandling.getCreateDirByName("outputCsar");
117                         myFile = new File(csarDir+ File.separator + "tmpCSAR_" + fileName + ".csar");
118                 }
119
120                 
121                 httpGet.addHeader(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
122                 httpGet.addHeader(HttpHeaderEnum.AUTHORIZATION.getValue(), basicAuthentication);
123
124                 logger.debug("Send GET request to get Tosca model: {}", url);
125                 CloseableHttpResponse execute = httpclient.execute(httpGet);
126                 
127             HttpEntity entity = execute.getEntity();
128             if (entity != null) {
129                 try (FileOutputStream outstream = new FileOutputStream(myFile)) {
130                     entity.writeTo(outstream);
131                     outstream.close();
132                 }
133             }
134                 return myFile;
135         }
136         
137         public static RestResponse getRestResponseComponentToscaModel(AssetTypeEnum assetType, String uuid) throws IOException {
138                 Config config = Utils.getConfig();
139                 
140                 String url = String.format(Urls.GET_TOSCA_MODEL, config.getCatalogBeHost(), config.getCatalogBePort(),
141                                 assetType.getValue(), uuid);
142                 
143                 Map<String, String> headersMap = new HashMap<>();
144                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
145                 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
146                 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
147                 
148                 HttpRequest http = new HttpRequest();
149
150                 logger.debug("Send GET request to get Resource Assets: {}", url);
151                 System.out.println("Send GET request to get Resource Assets: " + url);
152                 
153                 logger.debug("Request headers: {}", headersMap);
154                 System.out.println("Request headers: " + headersMap);
155
156                 RestResponse sendGetResourceAssets = http.httpSendGet(url, headersMap);
157
158                 return sendGetResourceAssets;
159
160         }
161
162         public static RestResponse getComponentListByAssetType(boolean isBasicAuthentication, AssetTypeEnum assetType,
163                         String... filterArrayString) throws IOException {
164                 Config config = Utils.getConfig();
165                 Map<String, String> headersMap = new HashMap<>();
166                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), BaseRestUtils.contentTypeHeaderData);
167                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), BaseRestUtils.acceptHeaderData);
168                 if (isBasicAuthentication) {
169                         headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), basicAuthentication);
170                 }
171                 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
172
173                 HttpRequest http = new HttpRequest();
174                         String url = String.format(Urls.GET_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(),
175                                 assetType.getValue());
176                 if (filterArrayString != null && filterArrayString.length > 0) {
177                         url = buildUrlWithFilter(url, filterArrayString);
178                 }
179
180                 RestResponse sendGetResourceAssets = http.httpSendGet(url, headersMap);
181
182                 return sendGetResourceAssets;
183         }
184
185         public static RestResponse getFilteredComponentList(AssetTypeEnum assetType, String query) throws IOException {
186                 Config config = Utils.getConfig();
187                 Map<String, String> headersMap = new HashMap<>();
188                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), BaseRestUtils.contentTypeHeaderData);
189                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), BaseRestUtils.acceptHeaderData);
190                 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), basicAuthentication);
191                 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
192
193                 HttpRequest http = new HttpRequest();
194
195                 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(),
196                                 assetType.getValue(), query);
197
198                 logger.debug("Send GET request to get Resource Assets: {}", url);
199                 logger.debug("Request headers: {}", headersMap);
200
201                 RestResponse sendGetResourceAssets = http.httpSendGet(url, headersMap);
202
203                 return sendGetResourceAssets;
204         }
205
206         public static String buildUrlWithFilter(String url, String[] filterArrayString) {
207                 StringBuilder sb = new StringBuilder();
208                 int length = filterArrayString.length;
209                 int count = 0;
210                 for (String filterString : filterArrayString) {
211                         sb.append(filterString);
212                         count++;
213                         if (length != count) {
214                                 sb.append("&");
215                         }
216                 }
217                 return url + "?" + sb;
218         }
219
220         public static RestResponse getAssetMetadataByAssetTypeAndUuid(boolean isBasicAuthentication,
221                         AssetTypeEnum assetType, String uuid) throws IOException {
222
223                 Config config = Utils.getConfig();
224                 Map<String, String> headersMap = new HashMap<>();
225                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), BaseRestUtils.contentTypeHeaderData);
226                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), BaseRestUtils.acceptHeaderData);
227                 if (isBasicAuthentication) {
228                         headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), basicAuthentication);
229                 }
230                 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
231
232                 HttpRequest http = new HttpRequest();
233                 String url = String.format(Urls.GET_ASSET_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
234                                 assetType.getValue(), uuid);
235
236                 logger.debug("Send GET request to get Resource Assets: {}", url);
237                 logger.debug("Request headers: {}", headersMap);
238
239                 RestResponse sendGetResourceAssets = http.httpSendGet(url, headersMap);
240
241                 return sendGetResourceAssets;
242         }
243
244         public static List<ResourceAssetStructure> getResourceAssetList(RestResponse assetResponse) {
245                 List<ResourceAssetStructure> resourceAssetList = new ArrayList<>();
246
247                 JsonElement jelement = new JsonParser().parse(assetResponse.getResponse());
248                 JsonArray componenetArray = (JsonArray) jelement;
249                 for (JsonElement jElement : componenetArray) {
250                         ResourceAssetStructure resource = gson.fromJson(jElement, ResourceAssetStructure.class);
251                         resourceAssetList.add(resource);
252                 }
253                 return resourceAssetList;
254         }
255
256         public static ResourceDetailedAssetStructure getResourceAssetMetadata(RestResponse assetResponse) {
257
258                 List<ResourceInstanceAssetStructure> resourcesList = new ArrayList<>();
259                 List<ArtifactAssetStructure> artifactsList = new ArrayList<>();
260                 ResourceDetailedAssetStructure resourceAssetMetadata = new ResourceDetailedAssetStructure();
261                 String response = assetResponse.getResponse();
262
263                 JsonObject jObject = (JsonObject) new JsonParser().parse(response);
264                 resourceAssetMetadata = gson.fromJson(jObject, ResourceDetailedAssetStructure.class);
265
266                 setResourceInstanceAssetList(resourcesList, jObject);
267                 resourceAssetMetadata.setResources(resourcesList);
268
269                 setArtifactAssetList(artifactsList, jObject);
270                 resourceAssetMetadata.setArtifacts(artifactsList);
271
272                 return resourceAssetMetadata;
273         }
274
275         public static void generalMetadataFieldsValidatior(AssetStructure assetMetadata, Component component) {
276
277                 assertTrue("Expected resourceUuid is " + component.getUUID() + " actual: " + assetMetadata.getUuid(),
278                                 assetMetadata.getUuid().equals(component.getUUID()));
279                 assertTrue(
280                                 "Expected resourceInvariantUuid is " + component.getInvariantUUID() + " actual: "
281                                                 + assetMetadata.getInvariantUUID(),
282                                 assetMetadata.getInvariantUUID().equals(component.getInvariantUUID()));
283                 assertTrue("Expected asset name is " + component.getName() + " actual: " + assetMetadata.getName(),
284                                 assetMetadata.getName().equals(component.getName()));
285                 assertTrue("Expected asset version is " + component.getVersion() + " actual: " + assetMetadata.getVersion(),
286                                 assetMetadata.getVersion().equals(component.getVersion()));
287                 assertTrue(
288                                 "Expected asset lastUpdaterUserId is " + component.getLastUpdaterUserId() + " actual: "
289                                                 + assetMetadata.getLastUpdaterUserId(),
290                                 assetMetadata.getLastUpdaterUserId().equals(component.getLastUpdaterUserId()));
291                 assertNotNull("Expected asset toscaModel is null", assetMetadata.getToscaModelURL());
292                 assertTrue(
293                                 "Expected asset category is " + component.getCategories().get(0).getName() + " actual: "
294                                                 + assetMetadata.getCategory(),
295                                 assetMetadata.getCategory().equals(component.getCategories().get(0).getName()));
296                 assertTrue(
297                                 "Expected asset lifeCycleState is " + component.getLifecycleState() + " actual: "
298                                                 + assetMetadata.getLifecycleState(),
299                                 assetMetadata.getLifecycleState().equals(component.getLifecycleState().toString()));
300
301         }
302
303         public static void resourceMetadataValidatior(ResourceDetailedAssetStructure resourceAssetMetadata,
304                         Resource resource, AssetTypeEnum assetType) throws Exception {
305
306                 generalMetadataFieldsValidatior(resourceAssetMetadata, resource);
307                 assertTrue(
308                                 "Expected asset lastUpdaterFullName is " + resource.getLastUpdaterFullName() + " actual: "
309                                                 + resourceAssetMetadata.getLastUpdaterFullName(),
310                                 resourceAssetMetadata.getLastUpdaterFullName().equals(resource.getLastUpdaterFullName()));
311                 assertTrue(
312                                 "Expected asset subCategory is " + resource.getCategories().get(0).getSubcategories().get(0).getName()
313                                                 + " actual: " + resourceAssetMetadata.getSubCategory(),
314                                 resourceAssetMetadata.getSubCategory()
315                                                 .equals(resource.getCategories().get(0).getSubcategories().get(0).getName()));
316                 assertTrue(
317                                 "Expected asset toscaResourceName is " + resource.getToscaResourceName() + " actual: "
318                                                 + resourceAssetMetadata.getToscaResourceName(),
319                                 resourceAssetMetadata.getToscaResourceName().equals(resource.getToscaResourceName()));
320                 assertTrue(
321                                 "Expected asset resourceType is " + resource.getResourceType() + " actual: "
322                                                 + resourceAssetMetadata.getResourceType(),
323                                 resourceAssetMetadata.getResourceType().equals(resource.getResourceType().toString()));
324                 resourceInstanceAssetValidator(resourceAssetMetadata.getResources(), resource, assetType);
325                 // resourceInstanceAssetValidator(resourceAssetMetadata.getResources(),
326                 // resource);
327                 artifactAssetValidator(resourceAssetMetadata.getArtifacts(), resource, assetType);
328
329         }
330
331         public static void serviceMetadataValidatior(ServiceDetailedAssetStructure serviceAssetMetadata, Service service,
332                         AssetTypeEnum assetType) throws Exception {
333
334                 generalMetadataFieldsValidatior(serviceAssetMetadata, service);
335                 assertTrue(
336                                 "Expected asset lastUpdaterFullName is " + service.getLastUpdaterFullName() + " actual: "
337                                                 + serviceAssetMetadata.getLastUpdaterFullName(),
338                                 serviceAssetMetadata.getLastUpdaterFullName().equals(service.getLastUpdaterFullName()));
339                 assertTrue("Expected asset distributionStatus is " + service.getDistributionStatus() + " actual: "
340                                                 + serviceAssetMetadata.getDistributionStatus(),
341                                 serviceAssetMetadata.getDistributionStatus().equals(service.getDistributionStatus().toString()));
342                 resourceInstanceAssetValidator(serviceAssetMetadata.getResources(), service, assetType);
343                 // resourceInstanceAssetValidator(serviceAssetMetadata.getResources(),
344                 // service);
345                 artifactAssetValidator(serviceAssetMetadata.getArtifacts(), service, assetType);
346
347         }
348
349         private static void artifactAssetValidator(List<ArtifactAssetStructure> artifactAssetStructureList,
350                         Component component, AssetTypeEnum assetType) {
351                 Map<String, ArtifactDefinition> componentDeploymentArtifacts = component.getDeploymentArtifacts();
352                 validateArtifactMetadata(componentDeploymentArtifacts, artifactAssetStructureList, component.getUUID(),
353                                 assetType, null);
354         }
355
356         private static void validateArtifactMetadata(Map<String, ArtifactDefinition> componentDeploymentArtifacts,
357                         List<ArtifactAssetStructure> artifactAssetStructureList, String componentUuid, AssetTypeEnum assetType,
358                         String resourceInstanceName) {
359                 if(componentDeploymentArtifacts != null){
360                         for (Entry<String, ArtifactDefinition> componentDeploymentArtifact : componentDeploymentArtifacts.entrySet()) {
361                                 ArtifactAssetStructure artifactAssetStructure = getArtifactMetadata(artifactAssetStructureList,
362                                                 componentDeploymentArtifact.getValue().getArtifactUUID());
363                                 ArtifactDefinition componentDeploymentArtifactValue = componentDeploymentArtifact.getValue();
364                                 if (artifactAssetStructure != null) {
365                                         assertTrue(
366                                                         "Expected artifact asset artifactName is " + componentDeploymentArtifactValue.getArtifactName()
367                                                                         + " actual: " + artifactAssetStructure.getArtifactName(),
368                                                         componentDeploymentArtifactValue.getArtifactName()
369                                                                         .equals(artifactAssetStructure.getArtifactName()));
370                                         assertTrue(
371                                                         "Expected artifact asset Type is " + componentDeploymentArtifactValue.getArtifactType()
372                                                                         + " actual: " + artifactAssetStructure.getArtifactType(),
373                                                         componentDeploymentArtifactValue.getArtifactType()
374                                                                         .equals(artifactAssetStructure.getArtifactType()));
375                                         // assertNotNull("Expected artifact asset resourceInvariantUUID
376                                         // is null",
377                                         // resourceInstanceAssetStructure.getResourceInvariantUUID());
378                                         // String expectedArtifactUrl = "/sdc/v1/catalog/" +
379                                         // assetType.getValue() + "/" + componentUuid + "/artifacts/" +
380                                         // componentDeploymentArtifactValue.getArtifactUUID();
381                                         String expectedArtifactUrl = "";
382                                         if (resourceInstanceName == null) {
383                                                 expectedArtifactUrl = String.format(COMPONENT_ARTIFACT_URL, assetType.getValue(), componentUuid,
384                                                                 componentDeploymentArtifactValue.getArtifactUUID());
385                                         } else {
386                                                 expectedArtifactUrl = String.format(RESOURCE_INSTANCE_ARTIFACT_URL, assetType.getValue(),
387                                                                 componentUuid, resourceInstanceName, componentDeploymentArtifactValue.getArtifactUUID());
388                                         }
389         
390                                         assertTrue(
391                                                         "Expected artifact asset URL is " + expectedArtifactUrl + " actual: "
392                                                                         + artifactAssetStructure.getArtifactURL(),
393                                                         artifactAssetStructure.getArtifactURL().equals(expectedArtifactUrl));
394                                         assertTrue(
395                                                         "Expected artifact asset description is " + componentDeploymentArtifactValue.getDescription()
396                                                                         + " actual: " + artifactAssetStructure.getArtifactDescription(),
397                                                         componentDeploymentArtifactValue.getDescription().toString()
398                                                                         .equals(artifactAssetStructure.getArtifactDescription()));
399                                         assertTrue(
400                                                         "Expected artifact asset checkSum is " + componentDeploymentArtifactValue.getArtifactChecksum()
401                                                                         + " actual: " + artifactAssetStructure.getArtifactChecksum(),
402                                                         componentDeploymentArtifactValue.getArtifactChecksum()
403                                                                         .equals(artifactAssetStructure.getArtifactChecksum()));
404                                         assertTrue(
405                                                         "Expected artifact asset version is " + componentDeploymentArtifactValue.getArtifactVersion()
406                                                                         + " actual: " + artifactAssetStructure.getArtifactVersion(),
407                                                         componentDeploymentArtifactValue.getArtifactVersion()
408                                                                         .equals(artifactAssetStructure.getArtifactVersion()));
409                                         if (componentDeploymentArtifactValue.getTimeout() > 0) {
410                                                 assertTrue(
411                                                                 "Expected artifact asset timeout is " + componentDeploymentArtifactValue.getTimeout()
412                                                                                 + " actual: " + artifactAssetStructure.getArtifactTimeout(),
413                                                                 componentDeploymentArtifactValue.getTimeout()
414                                                                                 .equals(artifactAssetStructure.getArtifactTimeout()));
415                                         }
416         
417                                 } else {
418                                         assertTrue("artifact asset with UUID" + componentDeploymentArtifact.getValue().getArtifactUUID()
419                                                         + " not found in get Metadata response", false);
420                                 }
421                         }
422                 }else{
423                         System.out.println("componentDeploymentArtifacts is null");
424                         logger.debug("componentDeploymentArtifacts is null");
425                 }
426
427         }
428
429         private static ArtifactAssetStructure getArtifactMetadata(List<ArtifactAssetStructure> artifactAssetStructureList,
430                         String artifactUUID) {
431                 for (ArtifactAssetStructure artifactAssetStructure : artifactAssetStructureList) {
432                         if (artifactAssetStructure.getArtifactUUID().equals(artifactUUID)) {
433                                 return artifactAssetStructure;
434                         }
435                 }
436                 return null;
437         }
438
439         private static void resourceInstanceAssetValidator(
440                         List<ResourceInstanceAssetStructure> resourceInstanceAssetStructures, Component component,
441                         AssetTypeEnum assetType) throws Exception {
442
443                 List<ComponentInstance> componentInstances = component.getComponentInstances();
444                 if (componentInstances != null) {
445                         for (ComponentInstance componentInstance : componentInstances) {
446                                 ResourceInstanceAssetStructure resourceInstanceAssetStructure = getResourceInstanceMetadata(
447                                                 resourceInstanceAssetStructures, componentInstance.getName());
448                                 if (resourceInstanceAssetStructure != null) {
449                                         assertTrue(
450                                                         "Expected RI asset resourceName is " + componentInstance.getComponentName() + " actual: "
451                                                                         + resourceInstanceAssetStructure.getResourceName(),
452                                                         componentInstance.getComponentName()
453                                                                         .equals(resourceInstanceAssetStructure.getResourceName()));
454                                         assertTrue(
455                                                         "Expected RI asset Name is " + componentInstance.getName() + " actual: "
456                                                                         + resourceInstanceAssetStructure.getResourceInstanceName(),
457                                                         componentInstance.getName()
458                                                                         .equals(resourceInstanceAssetStructure.getResourceInstanceName()));
459                                         assertNotNull("Expected RI asset resourceInvariantUUID is null",
460                                                         resourceInstanceAssetStructure.getResourceInvariantUUID());
461                                         assertTrue(
462                                                         "Expected RI asset resourceVersion is " + componentInstance.getComponentVersion()
463                                                                         + " actual: " + resourceInstanceAssetStructure.getResourceVersion(),
464                                                         componentInstance.getComponentVersion()
465                                                                         .equals(resourceInstanceAssetStructure.getResourceVersion()));
466                                         assertTrue(
467                                                         "Expected RI asset resourceType is " + componentInstance.getOriginType() + " actual: "
468                                                                         + resourceInstanceAssetStructure.getResoucreType(),
469                                                         componentInstance.getOriginType().toString()
470                                                                         .equals(resourceInstanceAssetStructure.getResoucreType()));
471                                         Resource resource = AtomicOperationUtils.getResourceObject(componentInstance.getComponentUid());
472                                         assertTrue("Expected RI asset resourceUUID is " + resource.getUUID() + " actual: " + resourceInstanceAssetStructure.getResourceUUID(),
473                                                         resource.getUUID().equals(resourceInstanceAssetStructure.getResourceUUID()));
474                                         validateArtifactMetadata(componentInstance.getDeploymentArtifacts(),
475                                                         resourceInstanceAssetStructure.getArtifacts(), component.getUUID(), assetType,
476                                                         componentInstance.getNormalizedName());
477                                         // validateArtifactMetadata(componentInstance.getDeploymentArtifacts(),
478                                         // resourceInstanceAssetStructure.getArtifacts(),
479                                         // component.getUUID(), AssetTypeEnum.RESOURCES);
480                                 } else {
481                                         assertTrue("resourceInstance asset with UUID" + componentInstance.getComponentUid()
482                                                         + " not found in get Metadata response", false);
483                                 }
484                         }
485                 }
486
487         }
488
489         // private static ResourceInstanceAssetStructure
490         // getResourceInstanceMetadata(List<ResourceInstanceAssetStructure>
491         // resourceInstanceAssetStructures, String componentUid) {
492         private static ResourceInstanceAssetStructure getResourceInstanceMetadata(
493                         List<ResourceInstanceAssetStructure> resourceInstanceAssetStructures, String name) {
494                 for (ResourceInstanceAssetStructure resourceInstanceAssetStructure : resourceInstanceAssetStructures) {
495                         if (resourceInstanceAssetStructure.getResourceInstanceName().equals(name)) {
496                                 return resourceInstanceAssetStructure;
497                         }
498                 }
499                 return null;
500         }
501
502         public static ServiceDetailedAssetStructure getServiceAssetMetadata(RestResponse assetResponse) {
503
504                 List<ResourceInstanceAssetStructure> resourcesList = new ArrayList<>();
505                 List<ArtifactAssetStructure> artifactsList = new ArrayList<>();
506                 ServiceDetailedAssetStructure serviceAssetMetadata;
507
508                 JsonObject jObject = (JsonObject) new JsonParser().parse(assetResponse.getResponse());
509                 serviceAssetMetadata = gson.fromJson(jObject, ServiceDetailedAssetStructure.class);
510
511                 setResourceInstanceAssetList(resourcesList, jObject);
512                 serviceAssetMetadata.setResources(resourcesList);
513
514                 setArtifactAssetList(artifactsList, jObject);
515                 serviceAssetMetadata.setArtifacts(artifactsList);
516
517                 return serviceAssetMetadata;
518         }
519
520         public static void setArtifactAssetList(List<ArtifactAssetStructure> artifactsList, JsonObject jObject) {
521                 JsonArray artifactsArray = jObject.getAsJsonArray("artifacts");
522                 if (artifactsArray != null) {
523                         for (JsonElement jElement : artifactsArray) {
524                                 ArtifactAssetStructure artifact = gson.fromJson(jElement, ArtifactAssetStructure.class);
525                                 artifactsList.add(artifact);
526                         }
527                 }
528         }
529
530         public static void setResourceInstanceAssetList(List<ResourceInstanceAssetStructure> resourcesList,
531                         JsonObject jObject) {
532                 JsonArray resourcesArray = jObject.getAsJsonArray("resources");
533                 if (resourcesArray != null) {
534                         for (JsonElement jElement : resourcesArray) {
535                                 ResourceInstanceAssetStructure resource = gson.fromJson(jElement, ResourceInstanceAssetStructure.class);
536                                 resourcesList.add(resource);
537                         }
538                 }
539         }
540
541         public static List<ServiceAssetStructure> getServiceAssetList(RestResponse assetResponse) {
542                 List<ServiceAssetStructure> serviceAssetList = new ArrayList<>();
543
544                 JsonElement jelement = new JsonParser().parse(assetResponse.getResponse());
545                 JsonArray componenetArray = (JsonArray) jelement;
546                 for (JsonElement jElement : componenetArray) {
547                         ServiceAssetStructure service = gson.fromJson(jElement, ServiceAssetStructure.class);
548                         serviceAssetList.add(service);
549                 }
550                 return serviceAssetList;
551         }
552
553         public static List<String> getResourceNamesList(List<ResourceAssetStructure> resourceAssetList) {
554                 List<String> assetNamesList = new ArrayList<>();
555                 for (ResourceAssetStructure resourceAsset : resourceAssetList) {
556                         assetNamesList.add(resourceAsset.getName());
557                 }
558                 return assetNamesList;
559         }
560         
561         public static Map<String,String> getResourceAssetMap(RestResponse assetResponse) {
562                 Map<String,String> resourceAssetMap =  new HashMap<>();
563
564                 JsonElement jelement = new JsonParser().parse(assetResponse.getResponse());
565                 JsonArray componenetArray = (JsonArray) jelement;
566                 for (JsonElement jElement : componenetArray) {
567                         ResourceAssetStructure resource = gson.fromJson(jElement, ResourceAssetStructure.class);
568                         resourceAssetMap.put(resource.getName(), resource.getVersion());
569                 }
570                 return resourceAssetMap;
571         }
572         
573         public static Map<String,String> getResourceListFiltteredByWholeVersion(Map<String,String> resourceAssetList) {
574                 Map<String,String> assetNamesMap = new HashMap<>();
575                         for (Entry<String, String> entry : resourceAssetList.entrySet()) {
576                             String key = entry.getKey();
577                             String[] parts = entry.getValue().split("\\.");
578                             String lastOne = parts[parts.length-1];
579                             
580                             if (key.contains("CinderVolume") ){
581                                 assetNamesMap.put(key,entry.getValue());
582                             }
583                             
584                             if (lastOne.equals("0") && !key.contains("Ci") ){
585                                 assetNamesMap.put(key,entry.getValue());
586                             }
587                         }
588                         
589                 return assetNamesMap;
590         }
591
592         public static List<String> getResourceObjectByNameAndVersionToscaNamesList(Map<String,String> resourceAssetList) throws Exception {
593                 List<String> assetNamesList = new ArrayList<>();
594                 for (Entry<String, String> entry : resourceAssetList.entrySet()) {
595                         System.out.println("fetch data---->"+entry.getKey()+entry.getValue());
596                         Resource resourceObjectByNameAndVersion = AtomicOperationUtils.getResourceObjectByNameAndVersion(UserRoleEnum.DESIGNER, entry.getKey(), entry.getValue());
597                         assetNamesList.add(resourceObjectByNameAndVersion.getToscaResourceName());
598                     
599                 }
600                 
601                 return assetNamesList;
602         }
603         
604
605         public static List<String> getServiceNamesList(List<ServiceAssetStructure> serviceAssetList) {
606                 List<String> assetNamesList = new ArrayList<>();
607                 for (ServiceAssetStructure serviceAsset : serviceAssetList) {
608                         assetNamesList.add(serviceAsset.getName());
609                 }
610                 return assetNamesList;
611         }
612
613         public static void checkResourceTypeInObjectList(List<ResourceAssetStructure> resourceAssetList, ResourceTypeEnum resourceType) {
614                 for (ResourceAssetStructure resourceAsset : resourceAssetList) {
615                         assertTrue("Expected resourceType is " + resourceType.toString() + " actual: " + resourceAsset.getResourceType(),
616                                         resourceAsset.getResourceType().equals(resourceType.toString()));
617                 }
618         }
619
620         public static void checkComponentTypeInObjectList(List<ResourceAssetStructure> resourceAssetList, ComponentTypeEnum componentType) {
621                 ComponentTypeEnum actualComponentType;
622                 for (ResourceAssetStructure resourceAsset : resourceAssetList) {
623                         actualComponentType = detectComponentType(resourceAsset);
624                         assertTrue("Expected componentType is " + componentType.getValue() + " actual: " + actualComponentType.getValue(), actualComponentType.equals(componentType));
625                 }
626         }
627
628         private static ComponentTypeEnum detectComponentType(ResourceAssetStructure resourceAsset) {
629                 String resourceType = resourceAsset.getResourceType();
630                 if(ResourceTypeEnum.getType(resourceType) !=null){
631                         return ComponentTypeEnum.RESOURCE;
632                 }
633                 return null;
634         }
635         
636 }