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.*;
41 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
42 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
43 import org.openecomp.sdc.ci.tests.execute.devCI.ArtifactFromCsar;
44 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
45 import org.openecomp.sdc.ci.tests.pages.HomePage;
46 import org.openecomp.sdc.ci.tests.pages.ResourceGeneralPage;
47 import org.openecomp.sdc.ci.tests.pages.ToscaArtifactsPage;
48 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
49 import org.openecomp.sdc.ci.tests.utilities.OnboardingUiUtils;
50 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
51 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
52 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
53 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
54 import org.testng.Assert;
55 import org.testng.annotations.BeforeClass;
56 import org.testng.annotations.Test;
58 import com.clearspring.analytics.util.Pair;
59 import org.openecomp.sdc.ci.tests.utils.general.VendorLicenseModelRestUtils;
60 import org.openecomp.sdc.ci.tests.utils.general.VendorSoftwareProductRestUtils;
62 public class AddComponentInstancesArtifactsInCsar extends SetupCDTest {
64 private String filePath;
66 public void beforeClass(){
67 filePath = System.getProperty("filePath");
68 if (filePath == null && System.getProperty("os.name").contains("Windows")) {
69 filePath = FileHandling.getResourcesFilesPath() + "AddComponentInstancesArtifactsInCsar"+ File.separator;
71 else if(filePath.isEmpty() && !System.getProperty("os.name").contains("Windows")){
72 filePath = FileHandling.getBasePath() + File.separator + "Files" + File.separator + "AddComponentInstancesArtifactsInCsar"+ File.separator;
76 // US847439 - Story [BE] - Add Component Instance's artifacts in CSAR
77 // TC1521795 - VF CSAR - The Flow
79 public void vfAndServicerCsarTheFlow() throws Exception{
80 ResourceReqDetails resourceMetaData = ElementFactory.getDefaultResourceByType(ResourceTypeEnum.VF, getUser());
82 String vnfFile = "FDNT.zip";
83 String snmpFile = "Fault-alarms-ASDC-vprobes-vLB.zip";
85 AmdocsLicenseMembers amdocsLicenseMembers = VendorLicenseModelRestUtils.createVendorLicense(getUser());
86 ResourceReqDetails resourceReqDetails = ElementFactory.getDefaultResource();//getResourceReqDetails(ComponentConfigurationTypeEnum.DEFAULT);
87 Pair<String, VendorSoftwareProductObject> createVSP = VendorSoftwareProductRestUtils.createVSP(resourceReqDetails, vnfFile, filePath, getUser(), amdocsLicenseMembers);
88 String vspName = createVSP.left;
89 resourceMetaData.setName(vspName);
90 VendorSoftwareProductObject resourceMeta = createVSP.right;
91 VendorSoftwareProductRestUtils.addVFCArtifacts(filePath, snmpFile, null, resourceMeta, getUser());
92 VendorSoftwareProductRestUtils.prepareVspForUse(getUser(), resourceMeta, true);
94 HomePage.showVspRepository();
95 OnboardingUiUtils.importVSP(createVSP);
96 resourceMetaData.setVersion("0.1");
97 Resource vfResource = AtomicOperationUtils.getResourceObjectByNameAndVersion(UserRoleEnum.DESIGNER, resourceMetaData.getName(), resourceMetaData.getVersion());
100 Map<String, Object> artifacts = getArtifactsOfComponentAndComponentsInstance(vfResource);
102 List<ImmutablePair<ComponentInstance, ArtifactDefinition>> artifactsUploadedToComponentInstance = new LinkedList<>();
103 Random random = new Random();
104 for(int i=0; i<random.nextInt(10) + 10; i++) {
105 ImmutablePair<ComponentInstance, ArtifactDefinition> uploadArtifactOnRandomVfc = uploadArtifactOnRandomRI(vfResource);
107 if(uploadArtifactOnRandomVfc.getRight().getArtifactName() != null) {
108 artifactsUploadedToComponentInstance.add(uploadArtifactOnRandomVfc);
112 if(artifactsUploadedToComponentInstance.size() > 0) {
113 Map<String, Object> artifactsOfResourceInstance = getArtifactsOfResourceInstance(artifactsUploadedToComponentInstance);
114 artifacts.put("Resources", artifactsOfResourceInstance);
118 ResourceGeneralPage.getLeftMenu().moveToToscaArtifactsScreen();
119 ToscaArtifactsPage.downloadCsar();
120 File latestFilefromDir = FileHandling.getLastModifiedFileNameFromDir();
121 Map<String, Object> combineHeatArtifacstWithFolderArtifacsToMap = ArtifactFromCsar.getVFCArtifacts(latestFilefromDir.getAbsolutePath());
123 compareArtifactFromFileStructureToArtifactsFromJavaObject(artifacts, combineHeatArtifacstWithFolderArtifacsToMap);
126 // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
127 // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
128 // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
130 // // Submit for testing + certify
131 // DeploymentArtifactPage.clickSubmitForTestingButton(vspName);
133 // reloginWithNewRole(UserRoleEnum.TESTER);
134 // GeneralUIUtils.findComponentAndClick(vspName);
135 // TesterOperationPage.certifyComponent(vspName);
137 // reloginWithNewRole(UserRoleEnum.DESIGNER);
139 // ServiceReqDetails serviceMetadata = ElementFactory.getDefaultService();
140 // ServiceUIUtils.createService(serviceMetadata, getUser());
141 // serviceMetadata.setVersion("0.1");
144 // // Upload informationl artifact to service
145 // ServiceGeneralPage.getLeftMenu().moveToCompositionScreen();
147 // String HEAT_FILE_YAML_NAME = "Heat-File.yaml";
148 // String DESCRIPTION = "kuku";
149 // String ARTIFACT_LABEL = "artifact3";
151 // ArtifactInfo artifact = new ArtifactInfo(filePath, HEAT_FILE_YAML_NAME, DESCRIPTION, ARTIFACT_LABEL,"OTHER");
152 // CompositionPage.showDeploymentArtifactTab();
153 // CompositionPage.clickAddArtifactButton();
154 // ArtifactUIUtils.fillAndAddNewArtifactParameters(artifact, CompositionPage.artifactPopup());
156 // ArtifactInfo informationArtifact = new ArtifactInfo(filePath, "asc_heat 0 2.yaml", "kuku", "artifact1", "GUIDE");
157 // CompositionPage.showInformationArtifactTab();
158 // CompositionPage.clickAddArtifactButton();
159 // ArtifactUIUtils.fillAndAddNewArtifactParameters(informationArtifact, CompositionPage.artifactPopup());
163 // // Add component instance to canvas of the service
164 // CompositionPage.searchForElement(vspName);
165 // CanvasManager serviceCanvasManager = CanvasManager.getCanvasManager();
166 // CanvasElement vfElement = serviceCanvasManager.createElementOnCanvas(vspName);
168 // Service service = AtomicOperationUtils.getServiceObjectByNameAndVersion(UserRoleEnum.DESIGNER, serviceMetadata.getName(), serviceMetadata.getVersion());
170 //// ArtifactReqDetails artifactReqDetails = ElementFactory.getArtifactByType("ci", "OTHER", true, false);
171 //// RestResponse restResponse = ArtifactRestUtils.externalAPIUploadArtifactOfTheAsset(service, getUser(), artifactReqDetails);
172 //// Integer responseCode = restResponse.getErrorCode();
173 //// Assert.assertEquals(responseCode, (Integer)200, "Response code is not correct.");
175 //// service = AtomicOperationUtils.getServiceObjectByNameAndVersion(UserRoleEnum.DESIGNER, serviceMetadata.getName(), serviceMetadata.getVersion());
177 // Map<String, Object> artifactsService = getArtifactsOfComponentAndComponentsInstance(service);
179 // System.out.println("12354");
181 // artifactsService.put(vfResource.getToscaResourceName(), artifacts);
183 // System.out.println("1234");
187 public void compareArtifactFromFileStructureToArtifactsFromJavaObject(Map<String, Object> artifactFromJavaObject, Map<String, Object> artifactsFromFileStructure) {
188 for(String key: artifactFromJavaObject.keySet()) {
189 if((!key.equals("Deployment")) && (!key.equals("Informational"))) {
190 Map<String, Object> newArtifactFromJavaObject = (Map<String, Object>) artifactFromJavaObject.get(key);
191 Map<String, Object> newArtifactsFromFileStructure = (Map<String, Object>) artifactsFromFileStructure.get(key);
192 compareArtifactFromFileStructureToArtifactsFromJavaObject(newArtifactFromJavaObject, newArtifactsFromFileStructure);
194 compareArtifacts(artifactFromJavaObject.get(key), artifactsFromFileStructure.get(key));
200 private void compareArtifacts(Object artifactFromJavaObject, Object artifactsFromFileStructure) {
201 Map<String, List<String>> artifactsMap = (Map<String, List<String>>) artifactFromJavaObject;
202 List<HeatMetaFirstLevelDefinition> artifactsList = (List<HeatMetaFirstLevelDefinition>) artifactsFromFileStructure;
204 for(HeatMetaFirstLevelDefinition heatMetaFirstLevelDefinition: artifactsList) {
205 Assert.assertTrue(artifactsMap.get(heatMetaFirstLevelDefinition.getType()).contains(heatMetaFirstLevelDefinition.getFileName()),
206 "Expected that artifacts will be the same. Not exists: " + heatMetaFirstLevelDefinition.getFileName() + " of type: " + heatMetaFirstLevelDefinition.getType());
209 for(String key: artifactsMap.keySet()) {
210 List<String> artifacts = artifactsMap.get(key);
212 for(HeatMetaFirstLevelDefinition heatMetaFirstLevelDefinition: artifactsList) {
213 if(heatMetaFirstLevelDefinition.getType().equals(key)) {
214 if(artifacts.contains(heatMetaFirstLevelDefinition.getFileName())) {
215 artifacts.remove(heatMetaFirstLevelDefinition.getFileName());
220 Assert.assertEquals(artifacts.size(), 0, "Expected that all artifacts equal. There is artifacts which not equal: " + artifacts.toString());
225 public Map<String, Object> getArtifactsOfResourceInstance(List<ImmutablePair<ComponentInstance, ArtifactDefinition>> riList) {
226 Map<String, Object> artifacts = new HashMap<>();
228 for(ImmutablePair<ComponentInstance, ArtifactDefinition> ri: riList) {
229 ArtifactDefinition artifactDefinition = ri.getRight();
230 ComponentInstance componentInstance = ri.getLeft();
231 if(artifacts.containsKey(componentInstance.getNormalizedName())) {
232 if( ((Map<String, ArrayList<String>>)((Map<String, Object>)artifacts.get(componentInstance.getNormalizedName())).get("Deployment")).containsKey(artifactDefinition.getArtifactType()) ) {
234 ((Map<String, ArrayList<String>>)((Map<String, Object>) artifacts.get(componentInstance.getNormalizedName())).get("Deployment")).get(artifactDefinition.getArtifactType()).add(artifactDefinition.getArtifactName());
237 ArrayList<String> list = new ArrayList<String>();
238 list.add(artifactDefinition.getArtifactName());
239 ((Map<String, ArrayList<String>>)((Map<String, Object>) artifacts.get(componentInstance.getNormalizedName())).get("Deployment")).put(artifactDefinition.getArtifactType(), list);
246 ArrayList<String> list = new ArrayList<String>();
247 list.add(artifactDefinition.getArtifactName());
249 Map<String, ArrayList<String>> map = new HashMap<>();
250 map.put(artifactDefinition.getArtifactType(), list);
252 Map<String, Map<String, ArrayList<String>>> addMap = new HashMap<>();
253 addMap.put("Deployment", map);
255 artifacts.put(componentInstance.getNormalizedName(), addMap);
257 // if(artifacts.size() == 0) {
258 // artifacts.put("Deployment", addMap);
260 // ((Map<String, Map<String, ArrayList<String>>>) artifacts.get("Deployment")).putAll(addMap);
262 } catch (Exception e) {
263 Assert.fail("Artifact name is null for componentInstance: " + componentInstance.getNormalizedName());
270 public Map<String, Object> getArtifactsOfComponentAndComponentsInstance(Component component) {
271 Map<String, Object> artifacts = getArtifacstOfComponent(component);
273 for(ComponentInstance componentInstance: component.getComponentInstances()) {
274 Map<String, Object> artifacstOfComponentInstance = getArtifacstOfComponentInstance(componentInstance);
275 if(artifacstOfComponentInstance.size() > 0) {
276 artifacts.put(componentInstance.getToscaComponentName() + "." + componentInstance.getComponentVersion(), artifacstOfComponentInstance);
283 public Map<String, Object> getArtifacstOfComponentInstance(ComponentInstance componentInstance) {
284 Map<String, Object> map = new HashMap<>();
286 if(componentInstance.getArtifacts() != null) {
287 Map<String, Object> informationalArtifacts = getArtifacts(componentInstance.getArtifacts());
288 if(informationalArtifacts.size() > 0) {
289 map.put("Informational", informationalArtifacts);
293 if(componentInstance.getDeploymentArtifacts() != null) {
294 Map<String, Object> deploymentArtifacts = getArtifacts(componentInstance.getDeploymentArtifacts());
295 if(deploymentArtifacts.size() > 0) {
296 map.put("Deployment", deploymentArtifacts);
303 public Map<String, Object> getArtifacstOfComponent(Component component) {
304 Map<String, Object> map = new HashMap<>();
306 if(component.getArtifacts() != null) {
307 Map<String, Object> informationalArtifacts = getArtifacts(component.getArtifacts());
308 if(informationalArtifacts.size() > 0) {
309 map.put("Informational", informationalArtifacts);
313 if(component.getDeploymentArtifacts() != null) {
314 Map<String, Object> deploymentArtifacts = getArtifacts(component.getDeploymentArtifacts());
315 if(deploymentArtifacts.size() > 0) {
316 map.put("Deployment", deploymentArtifacts);
323 public Map<String, Object> getArtifacts(Map<String, ArtifactDefinition> artifacts) {
324 Map<String, Object> map = new HashMap<>();
326 for(String artifact: artifacts.keySet()) {
327 ArtifactDefinition artifactDefinition = artifacts.get(artifact);
328 if((artifactDefinition.getEsId() != null) && (!artifactDefinition.getEsId().equals("")) && (!artifactDefinition.getArtifactType().equals("HEAT_ENV"))) {
329 if(map.containsKey(artifactDefinition.getArtifactType())) {
330 ((List<String>) map.get(artifactDefinition.getArtifactType())).add(artifactDefinition.getArtifactName());
332 ArrayList<String> list = new ArrayList<String>();
333 list.add(artifactDefinition.getArtifactName());
334 map.put(artifactDefinition.getArtifactType(), list);
342 public ImmutablePair<ComponentInstance, ArtifactDefinition> uploadArtifactOnRandomRI(Component component) throws IOException, Exception {
343 ArtifactReqDetails artifactReqDetails = getRandomArtifact();
344 Random random = new Random();
345 int randInt = random.nextInt(component.getComponentInstances().size());
346 User defaultUser = ElementFactory.getDefaultUser(getRole());
347 ComponentInstance componentInstance = component.getComponentInstances().get(randInt);
349 RestResponse uploadArtifactRestResponse = ArtifactRestUtils.externalAPIUploadArtifactOfComponentInstanceOnAsset(component, defaultUser, artifactReqDetails, componentInstance);
351 // Check response of external API
352 Integer responseCode = uploadArtifactRestResponse.getErrorCode();
353 Assert.assertEquals(responseCode, (Integer)200, "Response code is not correct.");
355 ImmutablePair<ComponentInstance, ArtifactDefinition> pair = ImmutablePair.of(componentInstance, ResponseParser.convertArtifactDefinitionResponseToJavaObject(uploadArtifactRestResponse.getResponse()));
360 public ImmutablePair<ComponentInstance, ArtifactDefinition> uploadArtifactOnRandomRI(Resource resource) throws IOException, Exception {
361 ArtifactReqDetails artifactReqDetails = getRandomVfcArtifact();
362 Random random = new Random();
363 int randInt = random.nextInt(resource.getComponentInstances().size());
364 User defaultUser = ElementFactory.getDefaultUser(getRole());
365 ComponentInstance componentInstance = resource.getComponentInstances().get(randInt);
367 RestResponse uploadArtifactRestResponse = ArtifactRestUtils.externalAPIUploadArtifactOfComponentInstanceOnAsset(resource, defaultUser, artifactReqDetails, componentInstance);
371 // Check response of external API
372 Integer responseCode = uploadArtifactRestResponse.getErrorCode();
374 // if(responseCode.equals(404)) {
375 // getExtendTest().log(Status.SKIP, String.format("DE271521"));
376 // throw new SkipException("DE271521");
379 Assert.assertEquals(responseCode, (Integer)200, "Response code is not correct.");
381 ImmutablePair<ComponentInstance, ArtifactDefinition> pair = ImmutablePair.of(componentInstance, ResponseParser.convertArtifactDefinitionResponseToJavaObject(uploadArtifactRestResponse.getResponse()));
386 public ArtifactReqDetails getRandomArtifact() throws IOException, Exception {
387 List<String> artifactsTypeList = Arrays.asList("Other");
388 return getRandomArtifact(artifactsTypeList);
391 public ArtifactReqDetails getRandomVfcArtifact() throws IOException, Exception {
392 List<String> vfcArtifactsTypeList = Arrays.asList("DCAE_INVENTORY_TOSCA", "DCAE_INVENTORY_JSON", "DCAE_INVENTORY_POLICY", "DCAE_INVENTORY_DOC",
393 "DCAE_INVENTORY_BLUEPRINT", "DCAE_INVENTORY_EVENT", "SNMP_POLL", "SNMP_TRAP");
394 return getRandomArtifact(vfcArtifactsTypeList);
397 public ArtifactReqDetails getRandomArtifact(List<String> artifactType) throws IOException, Exception {
398 Random random = new Random();
400 ArtifactReqDetails artifactReqDetails = ElementFactory.getArtifactByType("ci", artifactType.get(random.nextInt(artifactType.size())), true, false);
401 return artifactReqDetails;
405 protected UserRoleEnum getRole() {
406 return UserRoleEnum.DESIGNER;