Sync Integ to Master
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / utilities / OnboardingUiUtils.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.utilities;
22
23 import com.aventstack.extentreports.Status;
24 import com.clearspring.analytics.util.Pair;
25 import org.openecomp.sdc.be.model.User;
26 import org.openecomp.sdc.ci.tests.datatypes.*;
27 import org.openecomp.sdc.ci.tests.execute.devCI.ArtifactFromCsar;
28 import org.openecomp.sdc.ci.tests.execute.setup.ArtifactsCorrelationManager;
29 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
30 import org.openecomp.sdc.ci.tests.pages.DeploymentArtifactPage;
31 import org.openecomp.sdc.ci.tests.pages.HomePage;
32 import org.openecomp.sdc.ci.tests.pages.ResourceGeneralPage;
33 import org.openecomp.sdc.ci.tests.utils.general.VendorLicenseModelRestUtils;
34 import org.openecomp.sdc.ci.tests.utils.general.VendorSoftwareProductRestUtils;
35 import org.openecomp.sdc.ci.tests.verificator.VfVerificator;
36 import org.openqa.selenium.WebElement;
37 import org.testng.Assert;
38
39 import java.io.File;
40 import java.util.LinkedList;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.stream.Collectors;
44
45
46 public class OnboardingUiUtils {
47
48         private static void importUpdateVSP(Pair<String, VendorSoftwareProductObject> vsp, boolean isUpdate) throws Exception{
49                 String vspName = vsp.left;
50                 VendorSoftwareProductObject vspMetadata = vsp.right;
51                 boolean vspFound = HomePage.searchForVSP(vspName);
52
53                 if (vspFound){
54
55                         List<WebElement> elemenetsFromTable = HomePage.getElemenetsFromTable();
56 //                      WebDriverWait wait = new WebDriverWait(GeneralUIUtils.getDriver(), 30);
57 //                      WebElement findElement = wait.until(ExpectedConditions.visibilityOf(elemenetsFromTable.get(1)));
58 //                      findElement.click();
59                         elemenetsFromTable.get(1).click();
60                         GeneralUIUtils.waitForLoader();
61
62                         if (isUpdate){
63                                 GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ImportVfRepository.UPDATE_VSP.getValue());
64                         }
65                         else{
66                                 GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ImportVfRepository.IMPORT_VSP.getValue());
67                         }
68
69                         String lifeCycleState = ResourceGeneralPage.getLifeCycleState();
70                         boolean needCheckout = lifeCycleState.equals(LifeCycleStateEnum.CHECKIN.getValue()) || lifeCycleState.equals(LifeCycleStateEnum.CERTIFIED.getValue());
71                         if (needCheckout)
72                         {
73                                 try {
74                                         ResourceGeneralPage.clickCheckoutButton();
75                                         Assert.assertTrue(ResourceGeneralPage.getLifeCycleState().equals(LifeCycleStateEnum.CHECKOUT.getValue()), "Did not succeed to checkout");
76                                 } catch (Exception e) {
77                                         ExtentTestActions.log(Status.ERROR, "Did not succeed to checkout");
78                                         e.printStackTrace();
79                                 }
80                                 GeneralUIUtils.waitForLoader();
81                         }
82
83                         //Metadata verification
84                         VfVerificator.verifyOnboardedVnfMetadata(vspName, vspMetadata);
85
86                         ExtentTestActions.log(Status.INFO, "Clicking create/update VNF");
87                         String duration = GeneralUIUtils.getActionDuration(() -> waitUntilVnfCreated());
88                     ExtentTestActions.log(Status.INFO, "Succeeded in importing/updating " + vspName, duration);
89                 }
90                 else{
91                         Assert.fail("Did not find VSP named " + vspName);
92                 }
93         }
94
95         private static void waitUntilVnfCreated() {
96                 GeneralUIUtils.clickOnElementByTestIdWithoutWait(DataTestIdEnum.GeneralElementsEnum.CREATE_BUTTON.getValue());
97                 GeneralUIUtils.waitForLoader(60*10);
98                 GeneralUIUtils.waitForAngular();
99                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralElementsEnum.CHECKIN_BUTTON.getValue());
100         }
101         
102         public static void updateVSP(Pair<String, VendorSoftwareProductObject> vsp) throws Exception{
103                 ExtentTestActions.log(Status.INFO, "Updating VSP " + vsp.left);
104                 importUpdateVSP(vsp, true);
105         }
106         
107         public static void importVSP(Pair<String, VendorSoftwareProductObject> vsp) throws Exception{
108                 ExtentTestActions.log(Status.INFO, "Importing VSP " + vsp.left);
109                 importUpdateVSP(vsp, false);
110         }
111
112         public static void updateVnfAndValidate(String filePath, Pair<String, VendorSoftwareProductObject> vsp, String updatedVnfFile, User user) throws Exception, Throwable {
113                 ExtentTestActions.log(Status.INFO, String.format("Going to update the VNF with %s......", updatedVnfFile));
114                 System.out.println(String.format("Going to update the VNF with %s......", updatedVnfFile));
115
116                 VendorSoftwareProductRestUtils.updateVendorSoftwareProductToNextVersion(vsp.right, user, filePath, updatedVnfFile);
117                 HomePage.showVspRepository();
118                 updateVSP(vsp);
119                 ResourceGeneralPage.getLeftMenu().moveToDeploymentArtifactScreen();
120                 DeploymentArtifactPage.verifyArtifactsExistInTable(filePath, updatedVnfFile);
121         }
122
123         public static Pair<String, VendorSoftwareProductObject> onboardAndValidate(ResourceReqDetails resourceReqDetails, String filepath, String vnfFile, User user) throws Exception {
124                 ExtentTestActions.log(Status.INFO, String.format("Going to onboard the VNF %s", vnfFile));
125                 System.out.println(String.format("Going to onboard the VNF %s", vnfFile));
126         
127                 AmdocsLicenseMembers amdocsLicenseMembers = VendorLicenseModelRestUtils.createVendorLicense(user);
128                 Pair<String, VendorSoftwareProductObject> createVendorSoftwareProduct = VendorSoftwareProductRestUtils.createVendorSoftwareProduct(resourceReqDetails, vnfFile, filepath, user, amdocsLicenseMembers);
129                 String vspName = createVendorSoftwareProduct.left;
130                 
131                 DownloadManager.downloadCsarByNameFromVSPRepository(vspName, createVendorSoftwareProduct.right.getVspId());
132                 File latestFilefromDir = FileHandling.getLastModifiedFileNameFromDir();
133                 
134                 ExtentTestActions.log(Status.INFO, String.format("Searching for onboarded %s", vnfFile));
135                 HomePage.showVspRepository();
136                 ExtentTestActions.log(Status.INFO,String.format("Going to import %s", vnfFile.substring(0, vnfFile.indexOf("."))));
137                 importVSP(createVendorSoftwareProduct);
138                 
139                 ResourceGeneralPage.getLeftMenu().moveToDeploymentArtifactScreen();
140                 
141                 // Verify deployment artifacts
142                 Map<String, Object> combinedMap = ArtifactFromCsar.combineHeatArtifacstWithFolderArtifacsToMap(latestFilefromDir.getAbsolutePath());
143                 
144                 LinkedList<HeatMetaFirstLevelDefinition> deploymentArtifacts = ((LinkedList<HeatMetaFirstLevelDefinition>) combinedMap.get("Deployment"));
145                 ArtifactsCorrelationManager.addVNFartifactDetails(vspName, deploymentArtifacts);
146                 
147                 List<String> heatEnvFilesFromCSAR = deploymentArtifacts.stream().filter(e -> e.getType().equals("HEAT_ENV")).
148                                                                                                                                                  map(e -> e.getFileName()).
149                                                                                                                                                  collect(Collectors.toList());
150
151                 validateDeploymentArtifactsVersion(deploymentArtifacts, heatEnvFilesFromCSAR);
152
153                 DeploymentArtifactPage.verifyArtifactsExistInTable(filepath, vnfFile);
154                 return createVendorSoftwareProduct;
155         }
156
157         public static void validateDeploymentArtifactsVersion(LinkedList<HeatMetaFirstLevelDefinition> deploymentArtifacts,
158                         List<String> heatEnvFilesFromCSAR) {
159                 String artifactVersion;
160                 String artifactName;
161
162                 for(HeatMetaFirstLevelDefinition deploymentArtifact: deploymentArtifacts) {
163                         artifactVersion = "1";
164
165                         if(deploymentArtifact.getType().equals("HEAT_ENV")) {
166                                 continue;
167                         } else if(deploymentArtifact.getFileName().contains(".")) {
168                                 artifactName = deploymentArtifact.getFileName().trim().substring(0, deploymentArtifact.getFileName().lastIndexOf("."));
169                         } else {
170                                 artifactName = deploymentArtifact.getFileName().trim();
171                         }
172
173                         if (heatEnvFilesFromCSAR.contains(artifactName + ".env")){
174                                 artifactVersion = "2";
175                         }
176                         ArtifactUIUtils.validateArtifactNameVersionType(artifactName, artifactVersion, deploymentArtifact.getType());
177                 }
178         }
179
180 }