[SDC-29] rebase continue work to align source
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / datatypes / CanvasManager.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.datatypes;
22
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Random;
27 import java.util.UUID;
28 import java.util.stream.Collectors;
29
30 import org.apache.commons.lang3.tuple.ImmutablePair;
31 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum.LeftPanelCanvasItems;
32 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
33 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
34 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
35 import org.openqa.selenium.By;
36 import org.openqa.selenium.StaleElementReferenceException;
37 import org.openqa.selenium.WebElement;
38 import org.openqa.selenium.interactions.Actions;
39 import org.testng.Assert;
40
41 import com.aventstack.extentreports.Status;
42
43 public final class CanvasManager {
44         private Map<String, CanvasElement> canvasElements;
45         private Actions actions;
46         private WebElement canvas;
47         private int reduceCanvasWidthFactor;
48         private CanvasElement canvasElement;
49         // Offsets Are used to find upper right corner of canvas element in order to
50         // connect links
51         private static final int CANVAS_ELEMENT_Y_OFFSET = 30;
52         private static final int CANVAS_ELEMENT_X_OFFSET = 18; // 14 - 27
53
54         private CanvasManager() {
55                 canvasElements = new HashMap<>();
56                 actions = new Actions(GeneralUIUtils.getDriver());
57                 canvas = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS.getValue());
58                 try {
59                         WebElement webElement = GeneralUIUtils
60                                         .getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS_RIGHT_PANEL.getValue());
61                         reduceCanvasWidthFactor = webElement.getSize().width;
62                 } catch (Exception e) {
63                         reduceCanvasWidthFactor = 0;
64                 }
65         }
66
67         public static CanvasManager getCanvasManager() {
68                 return new CanvasManager();
69         }
70
71         public List<CanvasElement> getCanvasElements() {
72                 return canvasElements.values().stream().collect(Collectors.toList());
73         }
74
75         private void addCanvasElement(CanvasElement element) {
76                 canvasElements.put(element.getUniqueId(), element);
77         }
78
79         private void moveElementOnCanvas(CanvasElement canvasElement, ImmutablePair<Integer, Integer> newLocation)
80                         throws Exception {
81                 GeneralUIUtils.waitForLoader();
82                 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
83                 actions.clickAndHold();
84                 actions.moveToElement(canvas, newLocation.left, newLocation.right);
85                 actions.release();
86                 actions.perform();
87                 canvasElement.setLocation(newLocation);
88                 GeneralUIUtils.waitForLoader();
89
90         }
91
92         public void moveToFreeLocation(String containerName) {
93                 int maxWait = 5000;
94                 int sumOfWaiting = 0;
95                 int napPeriod = 200;
96                 boolean isKeepWaiting = false;
97                 while (!isKeepWaiting) {
98                         ImmutablePair<Integer, Integer> freePosition = getFreePosition();
99                         actions.moveToElement(canvas, freePosition.left, freePosition.right);
100                         actions.clickAndHold();
101                         actions.release();
102                         actions.perform();
103                         isKeepWaiting = GeneralUIUtils.getWebElementByTestID("selectedCompTitle").getText()
104                                         .equals(containerName);
105                         sumOfWaiting += napPeriod;
106                         if (sumOfWaiting > maxWait) {
107                                 Assert.fail("Can't click on VF");
108                         }
109                 }
110         }
111
112         public void clickOnCanvaElement(CanvasElement canvasElement) {
113                 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
114                 actions.clickAndHold();
115                 actions.release();
116                 actions.perform();
117                 actions.click().perform();
118                 GeneralUIUtils.ultimateWait();
119                 ExtentTestActions.log(Status.INFO, String.format("Canvas element %s selected", canvasElement.getElementType()));
120         }
121
122         public void moveElementOnCanvas(CanvasElement canvasElement) throws Exception {
123                 moveElementOnCanvas(canvasElement, getFreePosition());
124         }
125
126         public void deleteElementFromCanvas(CanvasElement canvasElement) throws Exception {
127                 GeneralUIUtils.waitForLoader();
128                 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
129                 actions.click();
130                 actions.perform();
131                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.DELETE_INSTANCE_BUTTON.getValue())
132                                 .click();
133                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.OK.getValue()).click();
134                 canvasElements.remove(canvasElement.getUniqueId());
135                 GeneralUIUtils.ultimateWait();
136                 if (canvasElement.getElementType().contains("-")){
137                         ExtentTestActions.log(Status.INFO, String.format("Canvas element %s removed", canvasElement.getElementType().split("-")[4]));
138                 }
139                 else{
140                         ExtentTestActions.log(Status.INFO, String.format("Canvas element %s removed", canvasElement.getElementType()));
141                 }
142         }
143
144         private WebElement findClickElement(String dataTestId) {
145                 int attempts = 0;
146                 while (attempts < 2) {
147                         try {
148                                 return GeneralUIUtils.getWebElementByTestID(dataTestId);
149                         } catch (StaleElementReferenceException e) {
150                         }
151                         attempts++;
152                 }
153                 return null;
154         }
155         
156         public CanvasElement createElementOnCanvas(String elementName) throws Exception {
157                 String actionDuration = GeneralUIUtils.getActionDuration(() -> {
158                         try {
159                                 canvasElement = createElementOnCanvasWithoutDuration(elementName);
160                         } catch (Exception e) {
161                                 e.printStackTrace();
162                         }
163                 });
164                 
165                 if (canvasElement != null){
166                         ExtentTestActions.log(Status.INFO, String.format("The element %s should now be on the canvas", elementName), actionDuration);
167                 }
168                 return canvasElement;
169         }
170         
171         private CanvasElement createElementOnCanvasWithoutDuration(String elementDataTestId) throws Exception {
172                 try {
173                         WebElement element = findClickElement(elementDataTestId);
174                         ImmutablePair<Integer, Integer> freePosition = getFreePosition();
175                         actions.moveToElement(element, 20, 20);
176                         actions.clickAndHold();
177                         actions.moveToElement(canvas, freePosition.left, freePosition.right);
178                         actions.release();
179                         actions.perform();
180                         GeneralUIUtils.ultimateWait();
181                         String uniqueId = elementDataTestId + "_" + UUID.randomUUID().toString();
182                         CanvasElement canvasElement = new CanvasElement(uniqueId, freePosition, elementDataTestId);
183                         addCanvasElement(canvasElement);
184                         GeneralUIUtils.ultimateWait();
185                         return canvasElement;
186                 } 
187                 catch (Exception e) {
188                         System.out.println("Can't create element on canvas");
189                         e.printStackTrace();
190                 }
191                 return null;
192         }
193
194         public CanvasElement createElementOnCanvas(LeftPanelCanvasItems canvasItem) throws Exception {
195                 return createElementOnCanvas(canvasItem.getValue());
196         }
197
198         private ImmutablePair<Integer, Integer> getFreePosition() {
199                 ImmutablePair<Integer, Integer> randomPosition = null;
200                 boolean freePosition = false;
201                 int minSpace = 150;
202                 while (!freePosition) {
203                         ImmutablePair<Integer, Integer> tempRandomPosition = getRandomPosition();
204                         freePosition = !canvasElements.values().stream().map(e -> e.getLocation())
205                                         .filter(e -> Math.abs(e.left - tempRandomPosition.left) < minSpace
206                                                         && Math.abs(e.right - tempRandomPosition.right) < minSpace)
207                                         .findAny().isPresent();
208                         randomPosition = tempRandomPosition;
209                 }
210                 return randomPosition;
211         }
212
213         private ImmutablePair<Integer, Integer> getRandomPosition() {
214                 int edgeBuffer = 50;
215                 Random random = new Random();
216                 int xElement = random.nextInt(canvas.getSize().width - 2 * edgeBuffer - reduceCanvasWidthFactor) + edgeBuffer;
217                 int yElement = random.nextInt(canvas.getSize().height - 2 * edgeBuffer) + edgeBuffer;
218                 return new ImmutablePair<Integer, Integer>(xElement, yElement);
219         }
220
221         public void linkElements(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
222                 ExtentTestActions.log(Status.INFO, String.format("Linking between the %s instance and the %s instance.", firstElement.getElementType(), secondElement.getElementType()));
223                 drawSimpleLink(firstElement, secondElement);
224                 selectReqAndCapAndConnect();
225                 ExtentTestActions.log(Status.INFO, String.format("The instances %s and %s should now be connected.", firstElement.getElementType(), secondElement.getElementType()));
226         }
227
228         private void selectReqAndCapAndConnect() throws Exception {
229                 // Select First Cap
230                 GeneralUIUtils.getWebElementsListByTestID(DataTestIdEnum.LinkMenuItems.LINK_ITEM_CAP.getValue()).get(0).click();
231                 // Select First Req
232                 GeneralUIUtils.getWebElementsListByTestID(DataTestIdEnum.LinkMenuItems.LINK_ITEM_REQ.getValue()).get(0).click();
233                 // Connect
234                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.LinkMenuItems.CONNECT_BUTTON.getValue()).click();
235
236                 GeneralUIUtils.waitForLoader();
237         }
238
239         private void drawSimpleLink(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
240                 int yOffset = CANVAS_ELEMENT_Y_OFFSET;
241                 int xOffset = CANVAS_ELEMENT_X_OFFSET;
242
243                 actions.moveToElement(canvas, firstElement.getLocation().left + xOffset,
244                                 firstElement.getLocation().right - yOffset);
245
246                 actions.clickAndHold();
247                 actions.moveToElement(canvas, secondElement.getLocation().left + xOffset, secondElement.getLocation().right - yOffset);
248                 actions.release();
249                 actions.perform();
250                 GeneralUIUtils.ultimateWait();
251         }
252
253         public String updateElementNameInCanvas(CanvasElement canvasElement, String newInstanceName) throws Exception {
254                 GeneralUIUtils.ultimateWait();;
255                 clickOnCanvaElement(canvasElement);
256                 WebElement updateInstanceName = GeneralUIUtils.getWebElementBy(By.id("editPencil"));
257                 updateInstanceName.click();
258                 WebElement instanceNameField = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.INSTANCE_NAME_FIELD.getValue());
259                 String oldInstanceName = instanceNameField.getAttribute("value");
260                 instanceNameField.clear();
261                 instanceNameField.sendKeys(newInstanceName);
262                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.OK.getValue()).click();
263                 GeneralUIUtils.ultimateWait();
264                 GeneralUIUtils.waitForElementInVisibilityByTestId(By.className("w-sdc-modal-resource-instance-name"));
265                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Name of element instance changed from %s to %s", oldInstanceName, newInstanceName));
266                 return oldInstanceName;
267         }
268 }