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