aboutsummaryrefslogtreecommitdiffstats
path: root/ui-ci/src/main/java/org/openecomp/sdc/ci/tests/datatypes/CanvasManager.java
blob: 910cc54dad8500096b71687658247fdb06cc95f6 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*-
 * ============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.openecomp.sdc.ci.tests.datatypes;

import com.aventstack.extentreports.Status;
import com.clearspring.analytics.util.Pair;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum.LeftPanelCanvasItems;
import org.openecomp.sdc.ci.tests.datatypes.enums.CircleSize;
import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
import org.openecomp.sdc.ci.tests.pages.CompositionPage;
import org.openecomp.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 org.testng.SkipException;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

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_VF_Y_OFFSET = 30;
	private static final int CANVAS_VF_X_OFFSET = 18; // 14 - 27

	private static final int CANVAS_NORMATIVE_ELEMENT_Y_OFFSET = 12;
	private static final int CANVAS_NORMATIVE_ELEMENT_X_OFFSET = 7;

    private static final int CANVAS_SERVICE_Y_OFFSET = 27;
	private static final int CANVAS_SERVICE_X_OFFSET = 16;

	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(DataTestIdEnum.CompositionRightPanel.COMPONENT_TITLE.getValue()).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();

	    validateInstanceSelected(canvasElement);
		ExtentTestActions.log(Status.INFO, String.format("Canvas element %s selected", canvasElement.getElementType()));
	}

	public void openLinkPopupReqsCapsConnection(CanvasElement canvasElement)
	{
		ExtentTestActions.log(Status.INFO, "Open Link popup");
		clickOnCanvasLink(canvasElement);
		int x = canvasElement.getLocation().getLeft() + 30; // view button x delta
		int y = canvasElement.getLocation().getRight() + 11; // view button y delta
		clickOnCanvasPosition(x,y);
		GeneralUIUtils.ultimateWait();
	}
	public void closeLinkPopupReqsCapsConnection()
	{
		GeneralUIUtils.clickOnElementByTestId("Cancel");
		GeneralUIUtils.ultimateWait();
	}

	public void clickSaveOnLinkPopup()
	{
		ExtentTestActions.log(Status.INFO, "Click save on link popup");
		GeneralUIUtils.clickOnElementByTestId("Save");
		GeneralUIUtils.ultimateWait();
	}

	public void deleteLinkPopupReqsCapsConnection(CanvasElement canvasElement)
	{
		clickOnCanvasLink(canvasElement);
		int x = canvasElement.getLocation().getLeft() + 30; // delete button x delta
		int y = canvasElement.getLocation().getRight() + 30; // delete button x delta
		clickOnCanvasPosition(x,y);
	}

	public void clickOnCanvasLink(CanvasElement canvasElement) {
		actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
		actions.click().perform();
		GeneralUIUtils.ultimateWait();
	}

	public void clickOnCanvasPosition(int x, int y) {

		try {
			actions.moveToElement(canvas, x, y);
			actions.click();
			actions.perform();
			GeneralUIUtils.ultimateWait();
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
	}

	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 {
			CompositionPage.searchForElement(elementDataTestId);
			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);
	}

	// Will work only if 2 elements are big sized (VF size), if one of the elements is Small use the function linkElements
	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()));
	}

	public void linkElements(CanvasElement firstElement, CircleSize firstElementSize, CanvasElement secondElement, CircleSize secondElementSize) throws Exception {
		drawSimpleLink(firstElement,firstElementSize, secondElement,secondElementSize);
		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 {
		addFirstReqOrCapAndPressNext();
		addFirstReqOrCapAndPressNext();
		linkMenuClickOnFinishButton();
	}

	private void addFirstReqOrCapAndPressNext() throws Exception {
		addFirstReqOrCap();
		linkMenuClickOnNextButton();
	}

	private void addFirstReqOrCap() {
		GeneralUIUtils.getWebElementsListByClassName(DataTestIdEnum.LinkMenuItems.LINK_ITEM_CAP_Or_REQ.getValue()).get(0).click();
	}

	private void linkMenuClickOnNextButton() throws Exception {
		GeneralUIUtils.clickOnElementByText("Next");
		GeneralUIUtils.ultimateWait();
	}

	private void linkMenuClickOnFinishButton() throws Exception {
		GeneralUIUtils.clickOnElementByText("Finish");
		GeneralUIUtils.ultimateWait();
	}


	private void drawSimpleLink(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
		int yOffset = CANVAS_VF_Y_OFFSET;
		int xOffset = CANVAS_VF_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();
	}

	private void drawSimpleLink(CanvasElement firstElement, CircleSize firstElementSize, CanvasElement secondElement, CircleSize secondElementSize) throws Exception {
		ExtentTestActions.log(Status.INFO, String.format("Linking between the %s instance and the %s instance.", firstElement.getElementType(), secondElement.getElementType()));
		Integer yOffset = getCircleOffset(firstElementSize).right;
		Integer xOffset = getCircleOffset(firstElementSize).left;
		firstElement.getElementType();


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

		actions.clickAndHold();

		yOffset = getCircleOffset(secondElementSize).right;
		xOffset = getCircleOffset(secondElementSize).left;

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

	private Pair<Integer,Integer> getCircleOffset(CircleSize circleSize)
	{
		Pair<Integer,Integer> circleSizes;
		if(circleSize.equals(CircleSize.VF))
		{
			circleSizes = new Pair <Integer,Integer> (CANVAS_VF_X_OFFSET,CANVAS_VF_Y_OFFSET);
		}
		else if (circleSize.equals(CircleSize.NORMATIVE))
		{
			circleSizes = new Pair <Integer,Integer> (CANVAS_NORMATIVE_ELEMENT_X_OFFSET,CANVAS_NORMATIVE_ELEMENT_Y_OFFSET);
		}
		else
		{
			circleSizes = new Pair <Integer,Integer> (CANVAS_SERVICE_X_OFFSET,CANVAS_SERVICE_Y_OFFSET);
		}
		return circleSizes;
	}

	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;
	}

	/**
	 * @param canvasElement
	 * Validate that instance was selected on right sidebar
	 */
	public void validateInstanceSelected(CanvasElement canvasElement) {
		long maxWait = 5000;
		long sumOfWaiting = 0;
		long napPeriod = 200;
		boolean isInstanceSelected;
		do {
			isInstanceSelected = CompositionPage.getSelectedInstanceName().toLowerCase().contains(canvasElement.getElementType().toLowerCase());

			if (!isInstanceSelected) {
				try {
					TimeUnit.MILLISECONDS.sleep(napPeriod);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			sumOfWaiting += napPeriod;
			if (sumOfWaiting > maxWait) {
				throw new SkipException(String.format("Open bug 342260, can't select instance properly, waited for %s seconds", (int) (maxWait/1000)));
			}
		} while (!isInstanceSelected);
	}

	public void validateLinkIsSelected() {
		long maxWait = 5000;
		long sumOfWaiting = 0;
		long napPeriod = 200;
		boolean isInstanceSelected;
		do {
			isInstanceSelected = GeneralUIUtils.isWebElementExistByClass("w-sdc-menu-item w-sdc-canvas-menu-item-view");

			if (!isInstanceSelected) {
				try {
					TimeUnit.MILLISECONDS.sleep(napPeriod);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			sumOfWaiting += napPeriod;
			if (sumOfWaiting > maxWait) {
				Assert.fail(String.format("Can't select link properly, waited for %s seconds", (int) (maxWait/1000)));		
			}
		} while (!isInstanceSelected);
	}

	private void selectReqCapByName(String reqCapName)
	{
	    GeneralUIUtils.clickOnElementByText(reqCapName);
        GeneralUIUtils.ultimateWait();
    }

	private void selectTypeOfReqCap(String dataTestId, String reqCapType)
	{
        GeneralUIUtils.selectByValueTextContained(dataTestId, reqCapType);
	}

	public void linkElementsAndSelectCapReqTypeAndCapReqName(CanvasElement firstElement, CircleSize firstElementSize, CanvasElement secondElement, CircleSize secondElementSize, ConnectionWizardPopUpObject connectionWizardPopUpObject) throws Exception {
		SetupCDTest.getExtendTest().log(Status.INFO, String.format("Creating link between %s and %s", firstElement.getElementType(), secondElement.getElementType()));
		drawSimpleLink(firstElement, firstElementSize, secondElement, secondElementSize);
        selectTypeOfReqCap(DataTestIdEnum.LinkMenuItems.REQ_CAP_SELECT_DATA_TESTS_ID.getValue(),connectionWizardPopUpObject.getCapabilityTypeSecondItem());
		addFirstReqOrCapAndPressNext();
		selectReqCapByName(connectionWizardPopUpObject.getCapabilityNameSecondItem());
		linkMenuClickOnNextButton();
        linkMenuClickOnFinishButton();
    }

	public ImmutablePair<Integer, Integer> calcMidOfLink(ImmutablePair<Integer, Integer> location1, ImmutablePair<Integer, Integer> location2)
	{
		int x = (location1.getLeft()+location2.getLeft())/2;
		int y = (location1.getRight()+location2.getRight())/2;

		ImmutablePair<Integer, Integer> location = new ImmutablePair<>(x,y);
		return location;
	}
}