2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.utilities;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.Random;
27 import java.util.UUID;
28 import java.util.stream.Collectors;
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;
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
42 private static final int CANVAS_ELEMENT_Y_OFFSET = 40;
43 private static final int CANVAS_ELEMENT_X_OFFSET = 21; // 14 - 27
45 private CanvasManager() {
46 canvasElements = new HashMap<>();
47 actions = new Actions(GeneralUIUtils.getDriver());
48 canvas = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS.getValue());
50 WebElement webElement = GeneralUIUtils
51 .getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS_RIGHT_PANEL.getValue());
52 reduceCanvasWidthFactor = webElement.getSize().width;
53 } catch (Exception e) {
54 reduceCanvasWidthFactor = 0;
58 public static CanvasManager getCanvasManager() {
59 return new CanvasManager();
62 public List<CanvasElement> getCanvasElements() {
63 return canvasElements.values().stream().collect(Collectors.toList());
66 private void addCanvasElement(CanvasElement element) {
67 canvasElements.put(element.getUniqueId(), element);
70 private void moveElementOnCanvas(CanvasElement canvasElement, ImmutablePair<Integer, Integer> newLocation)
72 GeneralUIUtils.waitForLoader();
74 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
75 actions.clickAndHold();
76 actions.moveToElement(canvas, newLocation.left, newLocation.right);
79 canvasElement.setLocation(newLocation);
80 GeneralUIUtils.waitForLoader();
84 public void moveElementOnCanvas(CanvasElement canvasElement) throws Exception {
85 moveElementOnCanvas(canvasElement, getFreePosition());
88 public void deleteElementFromCanvas(CanvasElement canvasElement) throws Exception {
89 GeneralUIUtils.waitForLoader();
90 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
93 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.DELETE_INSTANCE_BUTTON.getValue())
95 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.OK.getValue()).click();
96 canvasElements.remove(canvasElement.getUniqueId());
97 GeneralUIUtils.waitForLoader();
100 private String getItemName(WebElement canvasItem) {
101 String canvasItemName = canvasItem.getAttribute("data-tests-id");
103 return canvasItemName.substring(canvasItemName.lastIndexOf("-"));
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);
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;
122 private ImmutablePair<Integer, Integer> getFreePosition() {
123 // TODO mshitrit use better method
124 ImmutablePair<Integer, Integer> randomPosition = null;
125 boolean freePosition = false;
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;
135 return randomPosition;
138 private ImmutablePair<Integer, Integer> getRandomPosition() {
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);
146 public void linkElements(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
147 GeneralUIUtils.waitForLoader();
148 drawSimpleLink(firstElement, secondElement);
150 selectReqAndCapAndConnect();
152 GeneralUIUtils.waitForLoader();
156 private void selectReqAndCapAndConnect() {
158 GeneralUIUtils.getWebElementsListByTestID(DataTestIdEnum.LinkMenuItems.LINK_ITEM_CAP.getValue()).get(0).click();
160 GeneralUIUtils.getWebElementsListByTestID(DataTestIdEnum.LinkMenuItems.LINK_ITEM_REQ.getValue()).get(0).click();
162 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.LinkMenuItems.CONNECT_BUTTON.getValue()).click();
165 private void drawSimpleLink(CanvasElement firstElement, CanvasElement secondElement) {
167 int yOffset = CANVAS_ELEMENT_Y_OFFSET;
168 int xOffset = CANVAS_ELEMENT_X_OFFSET;
170 actions.moveToElement(canvas, firstElement.getLocation().left + xOffset,
171 firstElement.getLocation().right - yOffset);
173 actions.clickAndHold();
174 actions.moveToElement(canvas, secondElement.getLocation().left + xOffset,
175 secondElement.getLocation().right - yOffset);