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