[SDC-29] rebase continue work to align source
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / utilities / CatalogUIUtilitis.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 java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.codehaus.jettison.json.JSONArray;
29 import org.codehaus.jettison.json.JSONException;
30 import org.openecomp.sdc.ci.tests.datatypes.TopMenuButtonsEnum;
31 import org.openecomp.sdc.ci.tests.datatypes.CheckBoxStatusEnum;
32 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum;
33 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum.CatalogPageLeftPanelFilterTitle;
34 import org.openecomp.sdc.ci.tests.datatypes.TypesEnum;
35 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
36 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
37 import org.openecomp.sdc.ci.tests.utils.rest.CatalogRestUtils;
38 import org.openqa.selenium.WebElement;
39 import org.testng.annotations.Test;
40
41 import com.aventstack.extentreports.Status;
42
43 public class CatalogUIUtilitis {
44         
45         
46         
47         
48
49         // Get all Categories , Subcategories and Icons.
50         public void getAllCategoriesAndSubcategories() throws IOException, JSONException {
51                 RestResponse allcategoriesJson = CatalogRestUtils.getAllCategoriesTowardsCatalogBe();
52                 JSONArray categories = new JSONArray(allcategoriesJson.getResponse());
53                 for (int i = 0; i < categories.length(); i++) {
54                         String categoryname = (String) categories.getJSONObject(i).get("name");
55                         JSONArray subcategories = (JSONArray) categories.getJSONObject(i).get("subcategories");
56                         for (int j = 0; j < subcategories.length(); j++) {
57                                 String subcategoryname = (String) subcategories.getJSONObject(j).get("name");
58                                 System.out.println(subcategoryname);
59                         }
60                         for (int j = 0; j < subcategories.length(); j++) {
61                                 JSONArray icons = (JSONArray) subcategories.getJSONObject(j).get("icons");
62                                 for (int k = 0; k < icons.length(); k++) {
63                                         System.out.println(icons.get(k));
64                                 }
65                         }
66                         System.out.println("-------------------------------");
67                 }
68         }
69
70         @Test
71         // FOr testing---delete.
72         public static List<String> abcd() throws IOException, JSONException {
73                 RestResponse allcategoriesJson = CatalogRestUtils.getAllCategoriesTowardsCatalogBe();
74                 JSONArray categories = new JSONArray(allcategoriesJson.getResponse());
75                 List<String> allcat = new ArrayList<>();
76                 String uniqueId = null;
77                 for (int i = 0; i < categories.length(); i++) {
78                         String categoryname = (String) categories.getJSONObject(i).get("name");
79                         uniqueId = (String) categories.getJSONObject(i).get("uniqueId");
80                         allcat.add(uniqueId);
81                         JSONArray subcategories = (JSONArray) categories.getJSONObject(i).get("subcategories");
82                         for (int j = 0; j < subcategories.length(); j++) {
83                                 String subcategoryname = (String) subcategories.getJSONObject(j).get("name");
84                                 uniqueId = (String) subcategories.getJSONObject(j).get("uniqueId");
85                                 allcat.add(uniqueId);
86                         }
87                 }
88                 return allcat;
89
90         }
91         
92         public static void clickTopMenuButton(TopMenuButtonsEnum button) {
93                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Clicking on %s button ...", button.name()));
94                 switch (button) {
95                 case CATALOG:
96                         GeneralUIUtils.getWebElementByTestID(button.getButton()).click();
97                         break;
98                 case HOME:
99                         GeneralUIUtils.getWebElementByTestID(button.getButton()).click();
100                         break;
101                 case ON_BOARDING:
102                         GeneralUIUtils.getWebElementByTestID(button.getButton()).click();
103                         break;
104                 default:
105                         break;
106                 }
107                 GeneralUIUtils.ultimateWait();
108         }
109         
110         public static String catalogFilterTypeChecBox(TypesEnum enumtype) throws Exception {
111                 String Type = enumtype.toString().toLowerCase();
112                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Clicking on %s ...", Type));
113                 GeneralUIUtils.getWebElementByTestID(enumtype.getValue()).click();
114                 return Type;
115         }
116         
117         public static List<String> catalogFilterStatusChecBox(CheckBoxStatusEnum statusEnum) throws Exception {
118                 List<String> status = null;
119                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Clicking on %s status", statusEnum.name()));
120                 switch (statusEnum) {
121                 case IN_DESIGN:
122                         status = Arrays.asList("NOT_CERTIFIED_CHECKIN", "NOT_CERTIFIED_CHECKOUT");
123                         GeneralUIUtils.getWebElementByTestID(statusEnum.getCatalogValue()).click();
124                         break;
125                 case READY_FOR_TESTING:
126                         status = Arrays.asList("READY_FOR_CERTIFICATION");
127                         GeneralUIUtils.getWebElementByTestID(statusEnum.getCatalogValue()).click();
128                         break;
129                 case IN_TESTING:
130                         status = Arrays.asList("CERTIFICATION_IN_PROGRESS");
131                         GeneralUIUtils.getWebElementByTestID(statusEnum.getCatalogValue()).click();
132                         break;
133                 case CERTIFIED:
134                         status = Arrays.asList("CERTIFIED");
135                         GeneralUIUtils.getWebElementByTestID(statusEnum.getCatalogValue()).click();
136                         break;
137                 case DISTRIBUTED:
138                         status = Arrays.asList("CERTIFIED");
139                         GeneralUIUtils.getWebElementByTestID(statusEnum.getCatalogValue()).click();
140                         break;
141                 }
142                 return status;
143         }
144
145         // Get all Categories uniqueID .//The parent categories.
146         public static List<String> getCategories() throws IOException, JSONException {
147                 List<String> allCategoriesList = new ArrayList<>();
148                 RestResponse allcategoriesJson = CatalogRestUtils.getAllCategoriesTowardsCatalogBe();
149                 JSONArray categories = new JSONArray(allcategoriesJson.getResponse());
150                 for (int i = 0; i < categories.length(); i++) {
151                         String categoryname = (String) categories.getJSONObject(i).get("name");
152                         System.out.println(categoryname);
153                         allCategoriesList.add(categoryname);
154                 }
155                 return allCategoriesList;
156         }
157
158         @Test
159         // Get Subcategories by Category name
160         public static List<String> getAllSubcategoriesByUniqueId(String uniqueId) throws IOException, JSONException {
161
162                 RestResponse allcategoriesJson = CatalogRestUtils.getAllCategoriesTowardsCatalogBe();
163                 JSONArray categories = new JSONArray(allcategoriesJson.getResponse());
164                 List<String> subCategories = new ArrayList<>();// subCategories to
165                                                                                                                 // return.
166                 JSONArray subcategories = null;
167
168                 for (int i = 0; i < categories.length(); i++) {
169
170                         String categoryuniqueId = (String) categories.getJSONObject(i).get("uniqueId");
171
172                         if (categoryuniqueId.contentEquals(uniqueId)) {
173                                 subcategories = (JSONArray) categories.getJSONObject(i).get("subcategories");
174
175                                 for (int j = 0; j < subcategories.length(); j++) {
176
177                                         subCategories.add((String) subcategories.getJSONObject(j).get("uniqueId"));
178                                 }
179
180                                 break;
181                         }
182                 }
183                 if (subcategories == null) {
184                         subCategories.add(uniqueId);
185                 }
186                 return subCategories;
187         }
188
189         @Test
190         // Get icons by category name
191         public void getSubCategoryIcons() throws IOException, JSONException {
192                 RestResponse allcategoriesJson = CatalogRestUtils.getAllCategoriesTowardsCatalogBe();
193
194                 JSONArray categories = new JSONArray(allcategoriesJson.getResponse());
195                 for (int i = 0; i < categories.length(); i++) {
196                         String subcategoryname = (String) categories.getJSONObject(i).get("name");
197                         if (subcategoryname.contentEquals("Generic")) {
198                                 JSONArray subcategories = (JSONArray) categories.getJSONObject(i).get("subcategories");
199                                 for (int j = 0; j < subcategories.length(); j++) {
200                                         JSONArray icons = (JSONArray) subcategories.getJSONObject(j).get("icons");
201                                         for (int k = 0; k < icons.length(); k++) {
202                                                 System.out.println(icons.get(k));
203                                         }
204                                 }
205                                 break;
206                         }
207                 }
208         }
209         
210         
211         public static WebElement clickOnUpperCategoryCheckbox() throws InterruptedException {
212                 List<WebElement> categorieCheckboxes = GeneralUIUtils.getElementsByCSS("span[data-tests-id*='category']"); // get all categories and subcategories
213                 WebElement categorieCheckbox = categorieCheckboxes.get(0);
214                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Clicking on %s category ...", categorieCheckbox.getText()));
215                 categorieCheckbox.click();
216                 GeneralUIUtils.ultimateWait();
217                 return categorieCheckbox;
218         }
219
220         public static void clickOnLeftPanelElement(DataTestIdEnum.CatalogPageLeftPanelFilterTitle leftPanelElement) throws InterruptedException {
221                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Clicking on %s", leftPanelElement.name()));
222                 GeneralUIUtils.getElementsByCSS(leftPanelElement.getValue()).forEach(WebElement::click);
223         }
224
225         public static WebElement catalogSearchBox(String searchText) {
226                 WebElement searchBox = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.MainMenuButtons.SEARCH_BOX.getValue());
227                 searchBox.clear();
228                 searchBox.sendKeys(searchText);
229                 return searchBox;
230         }
231
232 }