[SDC-29] rebase continue work to align source
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / utilities / 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.utilities;
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;
32 import org.openqa.selenium.WebElement;
33 import org.openqa.selenium.interactions.Actions;
34
35 public final class CanvasManager {
36         private Map<String, CanvasElement> canvasElements;
37         private Actions actions;
38         private WebElement canvas;
39         private int reduceCanvasWidthFactor;
40         // Offsets Are used to find upper right corner of canvas element in order to
41         // connect links
42         private static final int CANVAS_ELEMENT_Y_OFFSET = 40;
43         private static final int CANVAS_ELEMENT_X_OFFSET = 21; // 14 - 27
44
45         private CanvasManager() {
46                 canvasElements = new HashMap<>();
47                 actions = new Actions(GeneralUIUtils.getDriver());
48                 canvas = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS.getValue());
49                 try {
50                         WebElement webElement = GeneralUIUtils
51                                         .getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS_RIGHT_PANEL.getValue());
52                         reduceCanvasWidthFactor = webElement.getSize().width;
53                 } catch (Exception e) {
54                         reduceCanvasWidthFactor = 0;
55                 }
56         }
57
58         public static CanvasManager getCanvasManager() {
59                 return new CanvasManager();
60         }
61
62         public List<CanvasElement> getCanvasElements() {
63                 return canvasElements.values().stream().collect(Collectors.toList());
64         }
65
66         private void addCanvasElement(CanvasElement element) {
67                 canvasElements.put(element.getUniqueId(), element);
68         }
69
70         private void moveElementOnCanvas(CanvasElement canvasElement, ImmutablePair<Integer, Integer> newLocation)
71                         throws Exception {
72                 GeneralUIUtils.waitForLoader();
73                 Thread.sleep(500);
74                 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
75                 actions.clickAndHold();
76                 actions.moveToElement(canvas, newLocation.left, newLocation.right);
77                 actions.release();
78                 actions.perform();
79                 canvasElement.setLocation(newLocation);
80                 GeneralUIUtils.waitForLoader();
81
82         }
83
84         public void moveElementOnCanvas(CanvasElement canvasElement) throws Exception {
85                 moveElementOnCanvas(canvasElement, getFreePosition());
86         }
87
88         public void deleteElementFromCanvas(CanvasElement canvasElement) throws Exception {
89                 GeneralUIUtils.waitForLoader();
90                 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
91                 actions.click();
92                 actions.perform();
93                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.DELETE_INSTANCE_BUTTON.getValue())
94                                 .click();
95                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.OK.getValue()).click();
96                 canvasElements.remove(canvasElement.getUniqueId());
97                 GeneralUIUtils.waitForLoader();
98         }
99
100         private String getItemName(WebElement canvasItem) {
101                 String canvasItemName = canvasItem.getAttribute("data-tests-id");
102
103                 return canvasItemName.substring(canvasItemName.lastIndexOf("-"));
104         }
105
106         public CanvasElement createElementOnCanvas(WebElement canvasItem) throws Exception {
107                 GeneralUIUtils.waitForLoader();
108                 ImmutablePair<Integer, Integer> freePosition = getFreePosition();
109                 actions.moveToElement(canvasItem, 0, 0);
110                 actions.clickAndHold();
111                 actions.moveToElement(canvas, freePosition.left, freePosition.right);
112                 actions.release();
113                 actions.perform();
114
115                 String uniqueId = getItemName(canvasItem) + "_" + UUID.randomUUID().toString();
116                 CanvasElement canvasElement = new CanvasElement(uniqueId, freePosition, canvasItem);
117                 addCanvasElement(canvasElement);
118                 GeneralUIUtils.waitForLoader();
119                 return canvasElement;
120         }
121
122         private ImmutablePair<Integer, Integer> getFreePosition() {
123                 // TODO mshitrit use better method
124                 ImmutablePair<Integer, Integer> randomPosition = null;
125                 boolean freePosition = false;
126                 int minSpace = 150;
127                 while (!freePosition) {
128                         ImmutablePair<Integer, Integer> tempRandomPosition = getRandomPosition();
129                         freePosition = !canvasElements.values().stream().map(e -> e.getLocation())
130                                         .filter(e -> Math.abs(e.left - tempRandomPosition.left) < minSpace
131                                                         && Math.abs(e.right - tempRandomPosition.right) < minSpace)
132                                         .findAny().isPresent();
133                         randomPosition = tempRandomPosition;
134                 }
135                 return randomPosition;
136         }
137
138         private ImmutablePair<Integer, Integer> getRandomPosition() {
139                 int edgeBuffer = 50;
140                 Random random = new Random();
141                 int xElement = random.nextInt(canvas.getSize().width - 2 * edgeBuffer - reduceCanvasWidthFactor) + edgeBuffer;
142                 int yElement = random.nextInt(canvas.getSize().height - 2 * edgeBuffer) + edgeBuffer;
143                 return new ImmutablePair<Integer, Integer>(xElement, yElement);
144         }
145
146         public void linkElements(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
147                 GeneralUIUtils.waitForLoader();
148                 drawSimpleLink(firstElement, secondElement);
149
150                 selectReqAndCapAndConnect();
151
152                 GeneralUIUtils.waitForLoader();
153
154         }
155
156         private void selectReqAndCapAndConnect() {
157                 // Select First Cap
158                 GeneralUIUtils.getWebElementsListByTestID(DataTestIdEnum.LinkMenuItems.LINK_ITEM_CAP.getValue()).get(0).click();
159                 // Select First Req
160                 GeneralUIUtils.getWebElementsListByTestID(DataTestIdEnum.LinkMenuItems.LINK_ITEM_REQ.getValue()).get(0).click();
161                 // Connect
162                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.LinkMenuItems.CONNECT_BUTTON.getValue()).click();
163         }
164
165         private void drawSimpleLink(CanvasElement firstElement, CanvasElement secondElement) {
166
167                 int yOffset = CANVAS_ELEMENT_Y_OFFSET;
168                 int xOffset = CANVAS_ELEMENT_X_OFFSET;
169
170                 actions.moveToElement(canvas, firstElement.getLocation().left + xOffset,
171                                 firstElement.getLocation().right - yOffset);
172
173                 actions.clickAndHold();
174                 actions.moveToElement(canvas, secondElement.getLocation().left + xOffset,
175                                 secondElement.getLocation().right - yOffset);
176                 actions.release();
177                 actions.perform();
178         }
179 }