2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.US;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.LinkedList;
29 import java.util.List;
31 import java.util.Random;
33 import org.apache.commons.lang3.tuple.ImmutablePair;
34 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
35 import org.openecomp.sdc.be.model.ArtifactDefinition;
36 import org.openecomp.sdc.be.model.Component;
37 import org.openecomp.sdc.be.model.ComponentInstance;
38 import org.openecomp.sdc.be.model.Resource;
39 import org.openecomp.sdc.be.model.User;
40 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
41 import org.openecomp.sdc.ci.tests.datatypes.HeatMetaFirstLevelDefinition;
42 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
43 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
44 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
45 import org.openecomp.sdc.ci.tests.execute.devCI.ArtifactFromCsar;
46 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
47 import org.openecomp.sdc.ci.tests.pages.HomePage;
48 import org.openecomp.sdc.ci.tests.pages.ResourceGeneralPage;
49 import org.openecomp.sdc.ci.tests.pages.ToscaArtifactsPage;
50 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
51 import org.openecomp.sdc.ci.tests.utilities.OnboardingUtils;
52 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
53 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
54 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
55 import org.testng.Assert;
56 import org.testng.SkipException;
57 import org.testng.annotations.BeforeClass;
58 import org.testng.annotations.Test;
60 import com.aventstack.extentreports.Status;
61 import com.clearspring.analytics.util.Pair;
64 public class AddComponentInstancesArtifactsInCsar extends SetupCDTest {
66 private String filePath;
68 public void beforeClass(){
69 filePath = System.getProperty("filepath");
70 if (filePath == null && System.getProperty("os.name").contains("Windows")) {
71 filePath = FileHandling.getResourcesFilesPath() + "AddComponentInstancesArtifactsInCsar"+ File.separator;
73 else if(filePath.isEmpty() && !System.getProperty("os.name").contains("Windows")){
74 filePath = FileHandling.getBasePath() + File.separator + "Files" + File.separator + "AddComponentInstancesArtifactsInCsar"+ File.separator;
78 // US847439 - Story [BE] - Add Component Instance's artifacts in CSAR
79 // TC1521795 - VF CSAR - The Flow
81 public void vfAndServicerCsarTheFlow() throws Exception{
82 ResourceReqDetails resourceMetaData = ElementFactory.getDefaultResourceByType(ResourceTypeEnum.VF, getUser());
84 String vnfFile = "FDNT.zip";
85 String snmpFile = "Fault-alarms-ASDC-vprobes-vLB.zip";
87 OnboardingUtils.createVendorLicense(getUser());
88 Pair<String, Map<String, String>> createVSP = OnboardingUtils.createVSP(vnfFile, filePath, getUser());
89 String vspName = createVSP.left;
90 resourceMetaData.setName(vspName);
91 Map<String, String> resourceMeta = createVSP.right;
92 String vspid = resourceMeta.get("vspId");
93 OnboardingUtils.addVFCArtifacts(filePath, snmpFile, null, vspid, getUser());
94 OnboardingUtils.prepareVspForUse(getUser(), vspid);
96 HomePage.showVspRepository();
97 OnboardingUtils.importVSP(createVSP);
98 resourceMetaData.setVersion("0.1");
99 Resource vfResource = AtomicOperationUtils.getResourceObjectByNameAndVersion(UserRoleEnum.DESIGNER, resourceMetaData.getName(), resourceMetaData.getVersion());
102 Map<String, Object> artifacts = getArtifactsOfComponentAndComponentsInstance(vfResource);
104 List<ImmutablePair<ComponentInstance, ArtifactDefinition>> artifactsUploadedToComponentInstance = new LinkedList<>();
105 Random random = new Random();
106 for(int i=0; i<random.nextInt(10) + 10; i++) {
107 ImmutablePair<ComponentInstance, ArtifactDefinition> uploadArtifactOnRandomVfc = uploadArtifactOnRandomRI(vfResource);
109 if(uploadArtifactOnRandomVfc.getRight().getArtifactName() != null) {
110 artifactsUploadedToComponentInstance.add(uploadArtifactOnRandomVfc);
114 if(artifactsUploadedToComponentInstance.size() > 0) {
115 Map<String, Object> artifactsOfResourceInstance = getArtifactsOfResourceInstance(artifactsUploadedToComponentInstance);
116 artifacts.put("Resources", artifactsOfResourceInstance);
120 ResourceGeneralPage.getLeftMenu().moveToToscaArtifactsScreen();
121 ToscaArtifactsPage.downloadCsar();
122 File latestFilefromDir = FileHandling.getLastModifiedFileFromDir();
123 Map<String, Object> combineHeatArtifacstWithFolderArtifacsToMap = ArtifactFromCsar.getVFCArtifacts(latestFilefromDir.getAbsolutePath());
125 compareArtifactFromFileStructureToArtifactsFromJavaObject(artifacts, combineHeatArtifacstWithFolderArtifacsToMap);
128 // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
129 // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
130 // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
132 // // Submit for testing + certify
133 // DeploymentArtifactPage.clickSubmitForTestingButton(vspName);
135 // reloginWithNewRole(UserRoleEnum.TESTER);
136 // GeneralUIUtils.findComponentAndClick(vspName);
137 // TesterOperationPage.certifyComponent(vspName);
139 // reloginWithNewRole(UserRoleEnum.DESIGNER);
141 // ServiceReqDetails serviceMetadata = ElementFactory.getDefaultService();
142 // ServiceUIUtils.createService(serviceMetadata, getUser());
143 // serviceMetadata.setVersion("0.1");
146 // // Upload informationl artifact to service
147 // ServiceGeneralPage.getLeftMenu().moveToCompositionScreen();
149 // String HEAT_FILE_YAML_NAME = "Heat-File.yaml";
150 // String DESCRIPTION = "kuku";
151 // String ARTIFACT_LABEL = "artifact3";
153 // ArtifactInfo artifact = new ArtifactInfo(filePath, HEAT_FILE_YAML_NAME, DESCRIPTION, ARTIFACT_LABEL,"OTHER");
154 // CompositionPage.showDeploymentArtifactTab();
155 // CompositionPage.clickAddArtifactButton();
156 // ArtifactUIUtils.fillAndAddNewArtifactParameters(artifact, CompositionPage.artifactPopup());
158 // ArtifactInfo informationArtifact = new ArtifactInfo(filePath, "asc_heat 0 2.yaml", "kuku", "artifact1", "GUIDE");
159 // CompositionPage.showInformationArtifactTab();
160 // CompositionPage.clickAddArtifactButton();
161 // ArtifactUIUtils.fillAndAddNewArtifactParameters(informationArtifact, CompositionPage.artifactPopup());
165 // // Add component instance to canvas of the service
166 // CompositionPage.searchForElement(vspName);
167 // CanvasManager serviceCanvasManager = CanvasManager.getCanvasManager();
168 // CanvasElement vfElement = serviceCanvasManager.createElementOnCanvas(vspName);
170 // Service service = AtomicOperationUtils.getServiceObjectByNameAndVersion(UserRoleEnum.DESIGNER, serviceMetadata.getName(), serviceMetadata.getVersion());
172 //// ArtifactReqDetails artifactReqDetails = ElementFactory.getArtifactByType("ci", "OTHER", true, false);
173 //// RestResponse restResponse = ArtifactRestUtils.externalAPIUploadArtifactOfTheAsset(service, getUser(), artifactReqDetails);
174 //// Integer responseCode = restResponse.getErrorCode();
175 //// Assert.assertEquals(responseCode, (Integer)200, "Response code is not correct.");
177 //// service = AtomicOperationUtils.getServiceObjectByNameAndVersion(UserRoleEnum.DESIGNER, serviceMetadata.getName(), serviceMetadata.getVersion());
179 // Map<String, Object> artifactsService = getArtifactsOfComponentAndComponentsInstance(service);
181 // System.out.println("12354");
183 // artifactsService.put(vfResource.getToscaResourceName(), artifacts);
185 // System.out.println("1234");
189 public void compareArtifactFromFileStructureToArtifactsFromJavaObject(Map<String, Object> artifactFromJavaObject, Map<String, Object> artifactsFromFileStructure) {
190 for(String key: artifactFromJavaObject.keySet()) {
191 if((!key.equals("Deployment")) && (!key.equals("Informational"))) {
192 Map<String, Object> newArtifactFromJavaObject = (Map<String, Object>) artifactFromJavaObject.get(key);
193 Map<String, Object> newArtifactsFromFileStructure = (Map<String, Object>) artifactsFromFileStructure.get(key);
194 compareArtifactFromFileStructureToArtifactsFromJavaObject(newArtifactFromJavaObject, newArtifactsFromFileStructure);
196 compareArtifacts(artifactFromJavaObject.get(key), artifactsFromFileStructure.get(key));
206 private void compareArtifacts(Object artifactFromJavaObject, Object artifactsFromFileStructure) {
207 Map<String, List<String>> artifactsMap = (Map<String, List<String>>) artifactFromJavaObject;
208 List<HeatMetaFirstLevelDefinition> artifactsList = (List<HeatMetaFirstLevelDefinition>) artifactsFromFileStructure;
210 for(HeatMetaFirstLevelDefinition heatMetaFirstLevelDefinition: artifactsList) {
211 Assert.assertTrue(artifactsMap.get(heatMetaFirstLevelDefinition.getType()).contains(heatMetaFirstLevelDefinition.getFileName()),
212 "Expected that artifacts will be the same. Not exists: " + heatMetaFirstLevelDefinition.getFileName() + " of type: " + heatMetaFirstLevelDefinition.getType());
215 for(String key: artifactsMap.keySet()) {
216 List<String> artifacts = artifactsMap.get(key);
218 for(HeatMetaFirstLevelDefinition heatMetaFirstLevelDefinition: artifactsList) {
219 if(heatMetaFirstLevelDefinition.getType().equals(key)) {
220 if(artifacts.contains(heatMetaFirstLevelDefinition.getFileName())) {
221 artifacts.remove(heatMetaFirstLevelDefinition.getFileName());
226 Assert.assertEquals(artifacts.size(), 0, "Expected that all artifacts equal. There is artifacts which not equal: " + artifacts.toString());
231 public Map<String, Object> getArtifactsOfResourceInstance(List<ImmutablePair<ComponentInstance, ArtifactDefinition>> riList) {
232 Map<String, Object> artifacts = new HashMap<>();
234 for(ImmutablePair<ComponentInstance, ArtifactDefinition> ri: riList) {
235 ArtifactDefinition artifactDefinition = ri.getRight();
236 ComponentInstance componentInstance = ri.getLeft();
237 if(artifacts.containsKey(componentInstance.getNormalizedName())) {
238 if( ((Map<String, ArrayList<String>>)((Map<String, Object>)artifacts.get(componentInstance.getNormalizedName())).get("Deployment")).containsKey(artifactDefinition.getArtifactType()) ) {
240 ((Map<String, ArrayList<String>>)((Map<String, Object>) artifacts.get(componentInstance.getNormalizedName())).get("Deployment")).get(artifactDefinition.getArtifactType()).add(artifactDefinition.getArtifactName());
243 ArrayList<String> list = new ArrayList<String>();
244 list.add(artifactDefinition.getArtifactName());
245 ((Map<String, ArrayList<String>>)((Map<String, Object>) artifacts.get(componentInstance.getNormalizedName())).get("Deployment")).put(artifactDefinition.getArtifactType(), list);
252 ArrayList<String> list = new ArrayList<String>();
253 list.add(artifactDefinition.getArtifactName());
255 Map<String, ArrayList<String>> map = new HashMap<>();
256 map.put(artifactDefinition.getArtifactType(), list);
258 Map<String, Map<String, ArrayList<String>>> addMap = new HashMap<>();
259 addMap.put("Deployment", map);
261 artifacts.put(componentInstance.getNormalizedName(), addMap);
263 // if(artifacts.size() == 0) {
264 // artifacts.put("Deployment", addMap);
266 // ((Map<String, Map<String, ArrayList<String>>>) artifacts.get("Deployment")).putAll(addMap);
268 } catch (Exception e) {
269 Assert.fail("Artifact name is null for componentInstance: " + componentInstance.getNormalizedName());
276 public Map<String, Object> getArtifactsOfComponentAndComponentsInstance(Component component) {
277 Map<String, Object> artifacts = getArtifacstOfComponent(component);
279 for(ComponentInstance componentInstance: component.getComponentInstances()) {
280 Map<String, Object> artifacstOfComponentInstance = getArtifacstOfComponentInstance(componentInstance);
281 if(artifacstOfComponentInstance.size() > 0) {
282 artifacts.put(componentInstance.getToscaComponentName() + "." + componentInstance.getComponentVersion(), artifacstOfComponentInstance);
289 public Map<String, Object> getArtifacstOfComponentInstance(ComponentInstance componentInstance) {
290 Map<String, Object> map = new HashMap<>();
292 if(componentInstance.getArtifacts() != null) {
293 Map<String, Object> informationalArtifacts = getArtifacts(componentInstance.getArtifacts());
294 if(informationalArtifacts.size() > 0) {
295 map.put("Informational", informationalArtifacts);
299 if(componentInstance.getDeploymentArtifacts() != null) {
300 Map<String, Object> deploymentArtifacts = getArtifacts(componentInstance.getDeploymentArtifacts());
301 if(deploymentArtifacts.size() > 0) {
302 map.put("Deployment", deploymentArtifacts);
309 public Map<String, Object> getArtifacstOfComponent(Component component) {
310 Map<String, Object> map = new HashMap<>();
312 if(component.getArtifacts() != null) {
313 Map<String, Object> informationalArtifacts = getArtifacts(component.getArtifacts());
314 if(informationalArtifacts.size() > 0) {
315 map.put("Informational", informationalArtifacts);
319 if(component.getDeploymentArtifacts() != null) {
320 Map<String, Object> deploymentArtifacts = getArtifacts(component.getDeploymentArtifacts());
321 if(deploymentArtifacts.size() > 0) {
322 map.put("Deployment", deploymentArtifacts);
329 public Map<String, Object> getArtifacts(Map<String, ArtifactDefinition> artifacts) {
330 Map<String, Object> map = new HashMap<>();
332 for(String artifact: artifacts.keySet()) {
333 ArtifactDefinition artifactDefinition = artifacts.get(artifact);
334 if((artifactDefinition.getEsId() != null) && (!artifactDefinition.getEsId().equals("")) && (!artifactDefinition.getArtifactType().equals("HEAT_ENV"))) {
335 if(map.containsKey(artifactDefinition.getArtifactType())) {
336 ((List<String>) map.get(artifactDefinition.getArtifactType())).add(artifactDefinition.getArtifactName());
338 ArrayList<String> list = new ArrayList<String>();
339 list.add(artifactDefinition.getArtifactName());
340 map.put(artifactDefinition.getArtifactType(), list);
348 public ImmutablePair<ComponentInstance, ArtifactDefinition> uploadArtifactOnRandomRI(Component component) throws IOException, Exception {
349 ArtifactReqDetails artifactReqDetails = getRandomArtifact();
350 Random random = new Random();
351 int randInt = random.nextInt(component.getComponentInstances().size());
352 User defaultUser = ElementFactory.getDefaultUser(getRole());
353 ComponentInstance componentInstance = component.getComponentInstances().get(randInt);
355 RestResponse uploadArtifactRestResponse = ArtifactRestUtils.externalAPIUploadArtifactOfComponentInstanceOnAsset(component, defaultUser, artifactReqDetails, componentInstance);
357 // Check response of external API
358 Integer responseCode = uploadArtifactRestResponse.getErrorCode();
359 Assert.assertEquals(responseCode, (Integer)200, "Response code is not correct.");
361 ImmutablePair<ComponentInstance, ArtifactDefinition> pair = ImmutablePair.of(componentInstance, ArtifactRestUtils.getArtifactDataFromJson(uploadArtifactRestResponse.getResponse()));
366 public ImmutablePair<ComponentInstance, ArtifactDefinition> uploadArtifactOnRandomRI(Resource resource) throws IOException, Exception {
367 ArtifactReqDetails artifactReqDetails = getRandomVfcArtifact();
368 Random random = new Random();
369 int randInt = random.nextInt(resource.getComponentInstances().size());
370 User defaultUser = ElementFactory.getDefaultUser(getRole());
371 ComponentInstance componentInstance = resource.getComponentInstances().get(randInt);
373 RestResponse uploadArtifactRestResponse = ArtifactRestUtils.externalAPIUploadArtifactOfComponentInstanceOnAsset(resource, defaultUser, artifactReqDetails, componentInstance);
377 // Check response of external API
378 Integer responseCode = uploadArtifactRestResponse.getErrorCode();
380 if(responseCode.equals(404)) {
381 getExtendTest().log(Status.SKIP, String.format("DE271521"));
382 throw new SkipException("DE271521");
385 Assert.assertEquals(responseCode, (Integer)200, "Response code is not correct.");
387 ImmutablePair<ComponentInstance, ArtifactDefinition> pair = ImmutablePair.of(componentInstance, ArtifactRestUtils.getArtifactDataFromJson(uploadArtifactRestResponse.getResponse()));
392 public ArtifactReqDetails getRandomArtifact() throws IOException, Exception {
393 List<String> artifactsTypeList = Arrays.asList("Other");
394 return getRandomArtifact(artifactsTypeList);
397 public ArtifactReqDetails getRandomVfcArtifact() throws IOException, Exception {
398 List<String> vfcArtifactsTypeList = Arrays.asList("DCAE_INVENTORY_TOSCA", "DCAE_INVENTORY_JSON", "DCAE_INVENTORY_POLICY", "DCAE_INVENTORY_DOC",
399 "DCAE_INVENTORY_BLUEPRINT", "DCAE_INVENTORY_EVENT", "SNMP_POLL", "SNMP_TRAP");
400 return getRandomArtifact(vfcArtifactsTypeList);
403 public ArtifactReqDetails getRandomArtifact(List<String> artifactType) throws IOException, Exception {
404 Random random = new Random();
406 ArtifactReqDetails artifactReqDetails = ElementFactory.getArtifactByType("ci", artifactType.get(random.nextInt(artifactType.size())), true, false);
407 return artifactReqDetails;
411 protected UserRoleEnum getRole() {
412 return UserRoleEnum.DESIGNER;