aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/org/onap/sdc/ci/tests/datatypes/CanvasManager.java
blob: 999a63bb3e8264e59abe99dd6b74bc6697640b1c (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*-
 * ============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.datatypes;

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.LeftPanelCanvasItems;
import org.onap.sdc.ci.tests.execute.setup.ExtentTestActions;
import org.onap.sdc.ci.tests.execute.setup.SetupCDTest;
import org.onap.sdc.ci.tests.utilities.GeneralUIUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;

import com.aventstack.extentreports.Status;

public final class CanvasManager {
	private Map<String, CanvasElement> canvasElements;
	private Actions actions;
	private WebElement canvas;
	private int reduceCanvasWidthFactor;
	private CanvasElement canvasElement;
	// Offsets Are used to find upper right corner of canvas element in order to
	// connect links
	private static final int CANVAS_ELEMENT_Y_OFFSET = 30;
	private static final int CANVAS_ELEMENT_X_OFFSET = 18; // 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();
		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 moveToFreeLocation(String containerName) {
		int maxWait = 5000;
		int sumOfWaiting = 0;
		int napPeriod = 200;
		boolean isKeepWaiting = false;
		while (!isKeepWaiting) {
			ImmutablePair<Integer, Integer> freePosition = getFreePosition();
			actions.moveToElement(canvas, freePosition.left, freePosition.right);
			actions.clickAndHold();
			actions.release();
			actions.perform();
			isKeepWaiting = GeneralUIUtils.getWebElementByTestID("selectedCompTitle").getText()
					.equals(containerName);
			sumOfWaiting += napPeriod;
			if (sumOfWaiting > maxWait) {
				Assert.fail("Can't click on VF");
			}
		}
	}

	public void clickOnCanvaElement(CanvasElement canvasElement) {
		actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
		actions.clickAndHold();
		actions.release();
		actions.perform();
		actions.click().perform();
		GeneralUIUtils.ultimateWait();
		ExtentTestActions.log(Status.INFO, String.format("Canvas element %s selected", canvasElement.getElementType()));
	}

	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.ultimateWait();
		if (canvasElement.getElementType().contains("-")){
			ExtentTestActions.log(Status.INFO, String.format("Canvas element %s removed", canvasElement.getElementType().split("-")[4]));
		}
		else{
			ExtentTestActions.log(Status.INFO, String.format("Canvas element %s removed", canvasElement.getElementType()));
		}
	}

	private WebElement findClickElement(String dataTestId) {
		int attempts = 0;
		while (attempts < 2) {
			try {
				return GeneralUIUtils.getWebElementByTestID(dataTestId);
			} catch (StaleElementReferenceException e) {
			}
			attempts++;
		}
		return null;
	}
	
	public CanvasElement createElementOnCanvas(String elementName) throws Exception {
		String actionDuration = GeneralUIUtils.getActionDuration(() -> {
			try {
				canvasElement = createElementOnCanvasWithoutDuration(elementName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		
		if (canvasElement != null){
			ExtentTestActions.log(Status.INFO, String.format("The element %s should now be on the canvas", elementName), actionDuration);
		}
		return canvasElement;
	}
	
	private CanvasElement createElementOnCanvasWithoutDuration(String elementDataTestId) throws Exception {
		try {
			WebElement element = findClickElement(elementDataTestId);
			ImmutablePair<Integer, Integer> freePosition = getFreePosition();
			actions.moveToElement(element, 20, 20);
			actions.clickAndHold();
			actions.moveToElement(canvas, freePosition.left, freePosition.right);
			actions.release();
			actions.perform();
			GeneralUIUtils.ultimateWait();
			String uniqueId = elementDataTestId + "_" + UUID.randomUUID().toString();
			CanvasElement canvasElement = new CanvasElement(uniqueId, freePosition, elementDataTestId);
			addCanvasElement(canvasElement);
			GeneralUIUtils.ultimateWait();
			return canvasElement;
		} 
		catch (Exception e) {
			System.out.println("Can't create element on canvas");
			e.printStackTrace();
		}
		return null;
	}

	public CanvasElement createElementOnCanvas(LeftPanelCanvasItems canvasItem) throws Exception {
		return createElementOnCanvas(canvasItem.getValue());
	}

	private ImmutablePair<Integer, Integer> getFreePosition() {
		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 {
		ExtentTestActions.log(Status.INFO, String.format("Linking between the %s instance and the %s instance.", firstElement.getElementType(), secondElement.getElementType()));
		drawSimpleLink(firstElement, secondElement);
		selectReqAndCapAndConnect();
		ExtentTestActions.log(Status.INFO, String.format("The instances %s and %s should now be connected.", firstElement.getElementType(), secondElement.getElementType()));
	}

	private void selectReqAndCapAndConnect() throws Exception {
		// 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();

		GeneralUIUtils.waitForLoader();
	}

	private void drawSimpleLink(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
		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();
		GeneralUIUtils.ultimateWait();
	}

	public String updateElementNameInCanvas(CanvasElement canvasElement, String newInstanceName) throws Exception {
		GeneralUIUtils.ultimateWait();;
		clickOnCanvaElement(canvasElement);
		WebElement updateInstanceName = GeneralUIUtils.getWebElementBy(By.id("editPencil"));
		updateInstanceName.click();
		WebElement instanceNameField = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.INSTANCE_NAME_FIELD.getValue());
		String oldInstanceName = instanceNameField.getAttribute("value");
		instanceNameField.clear();
		instanceNameField.sendKeys(newInstanceName);
		GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.OK.getValue()).click();
		GeneralUIUtils.ultimateWait();
		GeneralUIUtils.waitForElementInVisibilityByTestId(By.className("w-sdc-modal-resource-instance-name"));
		SetupCDTest.getExtendTest().log(Status.INFO, String.format("Name of element instance changed from %s to %s", oldInstanceName, newInstanceName));
		return oldInstanceName;
	}
}