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