aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/org/onap/sdc/ci/tests/utilities/CanvasManager.java
blob: 91011f3338ae831fb649c84b69b36f56848779b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdc.ci.tests.utilities;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.onap.sdc.ci.tests.datatypes.DataTestIdEnum;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

public final class CanvasManager {
	private Map<String, CanvasElement> canvasElements;
	private Actions actions;
	private WebElement canvas;
	private int reduceCanvasWidthFactor;
	// Offsets Are used to find upper right corner of canvas element in order to
	// connect links
	private static final int CANVAS_ELEMENT_Y_OFFSET = 40;
	private static final int CANVAS_ELEMENT_X_OFFSET = 21; // 14 - 27

	private CanvasManager() {
		canvasElements = new HashMap<>();
		actions = new Actions(GeneralUIUtils.getDriver());
		canvas = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS.getValue());
		try {
			WebElement webElement = GeneralUIUtils
					.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS_RIGHT_PANEL.getValue());
			reduceCanvasWidthFactor = webElement.getSize().width;
		} catch (Exception e) {
			reduceCanvasWidthFactor = 0;
		}
	}

	public static CanvasManager getCanvasManager() {
		return new CanvasManager();
	}

	public List<CanvasElement> getCanvasElements() {
		return canvasElements.values().stream().collect(Collectors.toList());
	}

	private void addCanvasElement(CanvasElement element) {
		canvasElements.put(element.getUniqueId(), element);
	}

	private void moveElementOnCanvas(CanvasElement canvasElement, ImmutablePair<Integer, Integer> newLocation)
			throws Exception {
		GeneralUIUtils.waitForLoader();
		Thread.sleep(500);
		actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
		actions.clickAndHold();
		actions.moveToElement(canvas, newLocation.left, newLocation.right);
		actions.release();
		actions.perform();
		canvasElement.setLocation(newLocation);
		GeneralUIUtils.waitForLoader();

	}

	public void moveElementOnCanvas(CanvasElement canvasElement) throws Exception {
		moveElementOnCanvas(canvasElement, getFreePosition());
	}

	public void deleteElementFromCanvas(CanvasElement canvasElement) throws Exception {
		GeneralUIUtils.waitForLoader();
		actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
		actions.click();
		actions.perform();
		GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.DELETE_INSTANCE_BUTTON.getValue())
				.click();
		GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.OK.getValue()).click();
		canvasElements.remove(canvasElement.getUniqueId());
		GeneralUIUtils.waitForLoader();
	}

	private String getItemName(WebElement canvasItem) {
		String canvasItemName = canvasItem.getAttribute("data-tests-id");

		return canvasItemName.substring(canvasItemName.lastIndexOf("-"));
	}

	public CanvasElement createElementOnCanvas(WebElement canvasItem) throws Exception {
		GeneralUIUtils.waitForLoader();
		ImmutablePair<Integer, Integer> freePosition = getFreePosition();
		actions.moveToElement(canvasItem, 0, 0);
		actions.clickAndHold();
		actions.moveToElement(canvas, freePosition.left, freePosition.right);
		actions.release();
		actions.perform();

		String uniqueId = getItemName(canvasItem) + "_" + UUID.randomUUID().toString();
		CanvasElement canvasElement = new CanvasElement(uniqueId, freePosition, canvasItem);
		addCanvasElement(canvasElement);
		GeneralUIUtils.waitForLoader();
		return canvasElement;
	}

	private ImmutablePair<Integer, Integer> getFreePosition() {
		// TODO mshitrit use better method
		ImmutablePair<Integer, Integer> randomPosition = null;
		boolean freePosition = false;
		int minSpace = 150;
		while (!freePosition) {
			ImmutablePair<Integer, Integer> tempRandomPosition = getRandomPosition();
			freePosition = !canvasElements.values().stream().map(e -> e.getLocation())
					.filter(e -> Math.abs(e.left - tempRandomPosition.left) < minSpace
							&& Math.abs(e.right - tempRandomPosition.right) < minSpace)
					.findAny().isPresent();
			randomPosition = tempRandomPosition;
		}
		return randomPosition;
	}

	private ImmutablePair<Integer, Integer> getRandomPosition() {
		int edgeBuffer = 50;
		Random random = new Random();
		int xElement = random.nextInt(canvas.getSize().width - 2 * edgeBuffer - reduceCanvasWidthFactor) + edgeBuffer;
		int yElement = random.nextInt(canvas.getSize().height - 2 * edgeBuffer) + edgeBuffer;
		return new ImmutablePair<Integer, Integer>(xElement, yElement);
	}

	public void linkElements(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
		GeneralUIUtils.waitForLoader();
		drawSimpleLink(firstElement, secondElement);

		selectReqAndCapAndConnect();

		GeneralUIUtils.waitForLoader();

	}

	private void selectReqAndCapAndConnect() {
		// Select First Cap
		GeneralUIUtils.getWebElementsListByTestID(DataTestIdEnum.LinkMenuItems.LINK_ITEM_CAP.getValue()).get(0).click();
		// Select First Req
		GeneralUIUtils.getWebElementsListByTestID(DataTestIdEnum.LinkMenuItems.LINK_ITEM_REQ.getValue()).get(0).click();
		// Connect
		GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.LinkMenuItems.CONNECT_BUTTON.getValue()).click();
	}

	private void drawSimpleLink(CanvasElement firstElement, CanvasElement secondElement) {

		int yOffset = CANVAS_ELEMENT_Y_OFFSET;
		int xOffset = CANVAS_ELEMENT_X_OFFSET;

		actions.moveToElement(canvas, firstElement.getLocation().left + xOffset,
				firstElement.getLocation().right - yOffset);

		actions.clickAndHold();
		actions.moveToElement(canvas, secondElement.getLocation().left + xOffset,
				secondElement.getLocation().right - yOffset);
		actions.release();
		actions.perform();
	}
}