Sync Integ to Master
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / datatypes / 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.datatypes;
22
23 import com.aventstack.extentreports.Status;
24 import com.clearspring.analytics.util.Pair;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum.LeftPanelCanvasItems;
27 import org.openecomp.sdc.ci.tests.datatypes.enums.CircleSize;
28 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
29 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
30 import org.openecomp.sdc.ci.tests.pages.CompositionPage;
31 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
32 import org.openqa.selenium.By;
33 import org.openqa.selenium.StaleElementReferenceException;
34 import org.openqa.selenium.WebElement;
35 import org.openqa.selenium.interactions.Actions;
36 import org.testng.Assert;
37 import org.testng.SkipException;
38
39 import java.util.*;
40 import java.util.concurrent.TimeUnit;
41 import java.util.stream.Collectors;
42
43 public final class CanvasManager {
44         private Map<String, CanvasElement> canvasElements;
45         private Actions actions;
46         private WebElement canvas;
47         private int reduceCanvasWidthFactor;
48         private CanvasElement canvasElement;
49         // Offsets Are used to find upper right corner of canvas element in order to
50         // connect links
51         private static final int CANVAS_VF_Y_OFFSET = 30;
52         private static final int CANVAS_VF_X_OFFSET = 18; // 14 - 27
53
54         private static final int CANVAS_NORMATIVE_ELEMENT_Y_OFFSET = 12;
55         private static final int CANVAS_NORMATIVE_ELEMENT_X_OFFSET = 7;
56
57     private static final int CANVAS_SERVICE_Y_OFFSET = 27;
58         private static final int CANVAS_SERVICE_X_OFFSET = 16;
59
60         private CanvasManager() {
61                 canvasElements = new HashMap<>();
62                 actions = new Actions(GeneralUIUtils.getDriver());
63                 canvas = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS.getValue());
64                 try {
65                         WebElement webElement = GeneralUIUtils
66                                         .getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS_RIGHT_PANEL.getValue());
67                         reduceCanvasWidthFactor = webElement.getSize().width;
68                 } catch (Exception e) {
69                         reduceCanvasWidthFactor = 0;
70                 }
71         }
72
73         public static CanvasManager getCanvasManager() {
74                 return new CanvasManager();
75         }
76
77         public List<CanvasElement> getCanvasElements() {
78                 return canvasElements.values().stream().collect(Collectors.toList());
79         }
80
81         private void addCanvasElement(CanvasElement element) {
82                 canvasElements.put(element.getUniqueId(), element);
83         }
84
85         private void moveElementOnCanvas(CanvasElement canvasElement, ImmutablePair<Integer, Integer> newLocation)
86                         throws Exception {
87                 GeneralUIUtils.waitForLoader();
88                 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
89                 actions.clickAndHold();
90                 actions.moveToElement(canvas, newLocation.left, newLocation.right);
91                 actions.release();
92                 actions.perform();
93                 canvasElement.setLocation(newLocation);
94                 GeneralUIUtils.waitForLoader();
95
96         }
97
98         public void moveToFreeLocation(String containerName) {
99                 int maxWait = 5000;
100                 int sumOfWaiting = 0;
101                 int napPeriod = 200;
102                 boolean isKeepWaiting = false;
103                 while (!isKeepWaiting) {
104                         ImmutablePair<Integer, Integer> freePosition = getFreePosition();
105                         actions.moveToElement(canvas, freePosition.left, freePosition.right);
106                         actions.clickAndHold();
107                         actions.release();
108                         actions.perform();
109                         isKeepWaiting = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.CompositionRightPanel.COMPONENT_TITLE.getValue()).getText()
110                                         .equals(containerName);
111                         sumOfWaiting += napPeriod;
112                         if (sumOfWaiting > maxWait) {
113                                 Assert.fail("Can't click on VF");
114                         }
115                 }
116         }
117
118         public void clickOnCanvaElement(CanvasElement canvasElement) {
119                 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
120                 actions.clickAndHold();
121                 actions.release();
122                 actions.perform();
123                 actions.click().perform();
124                 GeneralUIUtils.ultimateWait();
125
126             validateInstanceSelected(canvasElement);
127                 ExtentTestActions.log(Status.INFO, String.format("Canvas element %s selected", canvasElement.getElementType()));
128         }
129
130         public void openLinkPopupReqsCapsConnection(CanvasElement canvasElement)
131         {
132                 ExtentTestActions.log(Status.INFO, "Open Link popup");
133                 clickOnCanvasLink(canvasElement);
134                 int x = canvasElement.getLocation().getLeft() + 30; // view button x delta
135                 int y = canvasElement.getLocation().getRight() + 11; // view button y delta
136                 clickOnCanvasPosition(x,y);
137                 GeneralUIUtils.ultimateWait();
138         }
139         public void closeLinkPopupReqsCapsConnection()
140         {
141                 GeneralUIUtils.clickOnElementByTestId("Cancel");
142                 GeneralUIUtils.ultimateWait();
143         }
144
145         public void clickSaveOnLinkPopup()
146         {
147                 ExtentTestActions.log(Status.INFO, "Click save on link popup");
148                 GeneralUIUtils.clickOnElementByTestId("Save");
149                 GeneralUIUtils.ultimateWait();
150         }
151
152         public void deleteLinkPopupReqsCapsConnection(CanvasElement canvasElement)
153         {
154                 clickOnCanvasLink(canvasElement);
155                 int x = canvasElement.getLocation().getLeft() + 30; // delete button x delta
156                 int y = canvasElement.getLocation().getRight() + 30; // delete button x delta
157                 clickOnCanvasPosition(x,y);
158         }
159
160         public void clickOnCanvasLink(CanvasElement canvasElement) {
161                 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
162                 actions.click().perform();
163                 GeneralUIUtils.ultimateWait();
164         }
165
166         public void clickOnCanvasPosition(int x, int y) {
167
168                 try {
169                         actions.moveToElement(canvas, x, y);
170                         actions.click();
171                         actions.perform();
172                         GeneralUIUtils.ultimateWait();
173                 }
174                 catch (Exception e)
175                 {
176                         System.out.println(e);
177                 }
178         }
179
180         public void moveElementOnCanvas(CanvasElement canvasElement) throws Exception {
181                 moveElementOnCanvas(canvasElement, getFreePosition());
182         }
183
184         public void deleteElementFromCanvas(CanvasElement canvasElement) throws Exception {
185                 GeneralUIUtils.waitForLoader();
186                 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
187                 actions.click();
188                 actions.perform();
189                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.DELETE_INSTANCE_BUTTON.getValue())
190                                 .click();
191                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.OK.getValue()).click();
192                 canvasElements.remove(canvasElement.getUniqueId());
193                 GeneralUIUtils.ultimateWait();
194                 if (canvasElement.getElementType().contains("-")){
195                         ExtentTestActions.log(Status.INFO, String.format("Canvas element %s removed", canvasElement.getElementType().split("-")[4]));
196                 }
197                 else{
198                         ExtentTestActions.log(Status.INFO, String.format("Canvas element %s removed", canvasElement.getElementType()));
199                 }
200         }
201
202         private WebElement findClickElement(String dataTestId) {
203                 int attempts = 0;
204                 while (attempts < 2) {
205                         try {
206                                 return GeneralUIUtils.getWebElementByTestID(dataTestId);
207                         } catch (StaleElementReferenceException e) {
208                         }
209                         attempts++;
210                 }
211                 return null;
212         }
213
214         public CanvasElement createElementOnCanvas(String elementName) throws Exception {
215                 String actionDuration = GeneralUIUtils.getActionDuration(() -> {
216                         try {
217                                 canvasElement = createElementOnCanvasWithoutDuration(elementName);
218                         } catch (Exception e) {
219                                 e.printStackTrace();
220                         }
221                 });
222
223                 if (canvasElement != null){
224                         ExtentTestActions.log(Status.INFO, String.format("The element %s should now be on the canvas", elementName), actionDuration);
225                 }
226                 return canvasElement;
227         }
228
229         private CanvasElement createElementOnCanvasWithoutDuration(String elementDataTestId) throws Exception {
230                 try {
231                         CompositionPage.searchForElement(elementDataTestId);
232                         WebElement element = findClickElement(elementDataTestId);
233                         ImmutablePair<Integer, Integer> freePosition = getFreePosition();
234                         actions.moveToElement(element, 20, 20);
235                         actions.clickAndHold();
236                         actions.moveToElement(canvas, freePosition.left, freePosition.right);
237                         actions.release();
238                         actions.perform();
239                         GeneralUIUtils.ultimateWait();
240                         String uniqueId = elementDataTestId + "_" + UUID.randomUUID().toString();
241                         CanvasElement canvasElement = new CanvasElement(uniqueId, freePosition, elementDataTestId);
242                         addCanvasElement(canvasElement);
243                         GeneralUIUtils.ultimateWait();
244                         return canvasElement;
245                 }
246                 catch (Exception e) {
247                         System.out.println("Can't create element on canvas");
248                         e.printStackTrace();
249                 }
250                 return null;
251         }
252
253         public CanvasElement createElementOnCanvas(LeftPanelCanvasItems canvasItem) throws Exception {
254                 return createElementOnCanvas(canvasItem.getValue());
255         }
256
257         private ImmutablePair<Integer, Integer> getFreePosition() {
258                 ImmutablePair<Integer, Integer> randomPosition = null;
259                 boolean freePosition = false;
260                 int minSpace = 150;
261                 while (!freePosition) {
262                         ImmutablePair<Integer, Integer> tempRandomPosition = getRandomPosition();
263                         freePosition = !canvasElements.values().stream().map(e -> e.getLocation())
264                                         .filter(e -> Math.abs(e.left - tempRandomPosition.left) < minSpace
265                                                         && Math.abs(e.right - tempRandomPosition.right) < minSpace)
266                                         .findAny().isPresent();
267                         randomPosition = tempRandomPosition;
268                 }
269                 return randomPosition;
270         }
271
272         private ImmutablePair<Integer, Integer> getRandomPosition() {
273                 int edgeBuffer = 50;
274                 Random random = new Random();
275                 int xElement = random.nextInt(canvas.getSize().width - 2 * edgeBuffer - reduceCanvasWidthFactor) + edgeBuffer;
276                 int yElement = random.nextInt(canvas.getSize().height - 2 * edgeBuffer) + edgeBuffer;
277                 return new ImmutablePair<Integer, Integer>(xElement, yElement);
278         }
279
280         // Will work only if 2 elements are big sized (VF size), if one of the elements is Small use the function linkElements
281         public void linkElements(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
282                 ExtentTestActions.log(Status.INFO, String.format("Linking between the %s instance and the %s instance.", firstElement.getElementType(), secondElement.getElementType()));
283                 drawSimpleLink(firstElement, secondElement);
284                 selectReqAndCapAndConnect();
285                 ExtentTestActions.log(Status.INFO, String.format("The instances %s and %s should now be connected.", firstElement.getElementType(), secondElement.getElementType()));
286         }
287
288         public void linkElements(CanvasElement firstElement, CircleSize firstElementSize, CanvasElement secondElement, CircleSize secondElementSize) throws Exception {
289                 drawSimpleLink(firstElement,firstElementSize, secondElement,secondElementSize);
290                 selectReqAndCapAndConnect();
291                 ExtentTestActions.log(Status.INFO, String.format("The instances %s and %s should now be connected.", firstElement.getElementType(), secondElement.getElementType()));
292         }
293
294         private void selectReqAndCapAndConnect() throws Exception {
295                 addFirstReqOrCapAndPressNext();
296                 addFirstReqOrCapAndPressNext();
297                 linkMenuClickOnFinishButton();
298         }
299
300         private void addFirstReqOrCapAndPressNext() throws Exception {
301                 addFirstReqOrCap();
302                 linkMenuClickOnNextButton();
303         }
304
305         private void addFirstReqOrCap() {
306                 GeneralUIUtils.getWebElementsListByClassName(DataTestIdEnum.LinkMenuItems.LINK_ITEM_CAP_Or_REQ.getValue()).get(0).click();
307         }
308
309         private void linkMenuClickOnNextButton() throws Exception {
310                 GeneralUIUtils.clickOnElementByText("Next");
311                 GeneralUIUtils.ultimateWait();
312         }
313
314         private void linkMenuClickOnFinishButton() throws Exception {
315                 GeneralUIUtils.clickOnElementByText("Finish");
316                 GeneralUIUtils.ultimateWait();
317         }
318
319
320         private void drawSimpleLink(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
321                 int yOffset = CANVAS_VF_Y_OFFSET;
322                 int xOffset = CANVAS_VF_X_OFFSET;
323
324                 actions.moveToElement(canvas, firstElement.getLocation().left + xOffset,
325                                 firstElement.getLocation().right - yOffset);
326
327                 actions.clickAndHold();
328                 actions.moveToElement(canvas, secondElement.getLocation().left + xOffset, secondElement.getLocation().right - yOffset);
329                 actions.release();
330                 actions.perform();
331                 GeneralUIUtils.ultimateWait();
332         }
333
334         private void drawSimpleLink(CanvasElement firstElement, CircleSize firstElementSize, CanvasElement secondElement, CircleSize secondElementSize) throws Exception {
335                 ExtentTestActions.log(Status.INFO, String.format("Linking between the %s instance and the %s instance.", firstElement.getElementType(), secondElement.getElementType()));
336                 Integer yOffset = getCircleOffset(firstElementSize).right;
337                 Integer xOffset = getCircleOffset(firstElementSize).left;
338                 firstElement.getElementType();
339
340
341                                 actions.moveToElement(canvas, firstElement.getLocation().left + xOffset,
342                                 firstElement.getLocation().right - yOffset);
343
344                 actions.clickAndHold();
345
346                 yOffset = getCircleOffset(secondElementSize).right;
347                 xOffset = getCircleOffset(secondElementSize).left;
348
349                 actions.moveToElement(canvas, secondElement.getLocation().left + xOffset, secondElement.getLocation().right - yOffset);
350                 actions.release();
351                 actions.perform();
352                 GeneralUIUtils.ultimateWait();
353         }
354
355         private Pair<Integer,Integer> getCircleOffset(CircleSize circleSize)
356         {
357                 Pair<Integer,Integer> circleSizes;
358                 if(circleSize.equals(CircleSize.VF))
359                 {
360                         circleSizes = new Pair <Integer,Integer> (CANVAS_VF_X_OFFSET,CANVAS_VF_Y_OFFSET);
361                 }
362                 else if (circleSize.equals(CircleSize.NORMATIVE))
363                 {
364                         circleSizes = new Pair <Integer,Integer> (CANVAS_NORMATIVE_ELEMENT_X_OFFSET,CANVAS_NORMATIVE_ELEMENT_Y_OFFSET);
365                 }
366                 else
367                 {
368                         circleSizes = new Pair <Integer,Integer> (CANVAS_SERVICE_X_OFFSET,CANVAS_SERVICE_Y_OFFSET);
369                 }
370                 return circleSizes;
371         }
372
373         public String updateElementNameInCanvas(CanvasElement canvasElement, String newInstanceName) throws Exception {
374                 GeneralUIUtils.ultimateWait();;
375                 clickOnCanvaElement(canvasElement);
376                 WebElement updateInstanceName = GeneralUIUtils.getWebElementBy(By.id("editPencil"));
377                 updateInstanceName.click();
378                 WebElement instanceNameField = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.INSTANCE_NAME_FIELD.getValue());
379                 String oldInstanceName = instanceNameField.getAttribute("value");
380                 instanceNameField.clear();
381                 instanceNameField.sendKeys(newInstanceName);
382                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.OK.getValue()).click();
383                 GeneralUIUtils.ultimateWait();
384                 GeneralUIUtils.waitForElementInVisibilityByTestId(By.className("w-sdc-modal-resource-instance-name"));
385                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Name of element instance changed from %s to %s", oldInstanceName, newInstanceName));
386                 return oldInstanceName;
387         }
388
389         /**
390          * @param canvasElement
391          * Validate that instance was selected on right sidebar
392          */
393         public void validateInstanceSelected(CanvasElement canvasElement) {
394                 long maxWait = 5000;
395                 long sumOfWaiting = 0;
396                 long napPeriod = 200;
397                 boolean isInstanceSelected;
398                 do {
399                         isInstanceSelected = CompositionPage.getSelectedInstanceName().toLowerCase().contains(canvasElement.getElementType().toLowerCase());
400
401                         if (!isInstanceSelected) {
402                                 try {
403                                         TimeUnit.MILLISECONDS.sleep(napPeriod);
404                                 } catch (InterruptedException e) {
405                                         e.printStackTrace();
406                                 }
407                         }
408
409                         sumOfWaiting += napPeriod;
410                         if (sumOfWaiting > maxWait) {
411                                 throw new SkipException(String.format("Open bug 342260, can't select instance properly, waited for %s seconds", (int) (maxWait/1000)));
412                         }
413                 } while (!isInstanceSelected);
414         }
415
416         public void validateLinkIsSelected() {
417                 long maxWait = 5000;
418                 long sumOfWaiting = 0;
419                 long napPeriod = 200;
420                 boolean isInstanceSelected;
421                 do {
422                         isInstanceSelected = GeneralUIUtils.isWebElementExistByClass("w-sdc-menu-item w-sdc-canvas-menu-item-view");
423
424                         if (!isInstanceSelected) {
425                                 try {
426                                         TimeUnit.MILLISECONDS.sleep(napPeriod);
427                                 } catch (InterruptedException e) {
428                                         e.printStackTrace();
429                                 }
430                         }
431
432                         sumOfWaiting += napPeriod;
433                         if (sumOfWaiting > maxWait) {
434                                 Assert.fail(String.format("Can't select link properly, waited for %s seconds", (int) (maxWait/1000)));          
435                         }
436                 } while (!isInstanceSelected);
437         }
438
439         private void selectReqCapByName(String reqCapName)
440         {
441             GeneralUIUtils.clickOnElementByText(reqCapName);
442         GeneralUIUtils.ultimateWait();
443     }
444
445         private void selectTypeOfReqCap(String dataTestId, String reqCapType)
446         {
447         GeneralUIUtils.selectByValueTextContained(dataTestId, reqCapType);
448         }
449
450         public void linkElementsAndSelectCapReqTypeAndCapReqName(CanvasElement firstElement, CircleSize firstElementSize, CanvasElement secondElement, CircleSize secondElementSize, ConnectionWizardPopUpObject connectionWizardPopUpObject) throws Exception {
451                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Creating link between %s and %s", firstElement.getElementType(), secondElement.getElementType()));
452                 drawSimpleLink(firstElement, firstElementSize, secondElement, secondElementSize);
453         selectTypeOfReqCap(DataTestIdEnum.LinkMenuItems.REQ_CAP_SELECT_DATA_TESTS_ID.getValue(),connectionWizardPopUpObject.getCapabilityTypeSecondItem());
454                 addFirstReqOrCapAndPressNext();
455                 selectReqCapByName(connectionWizardPopUpObject.getCapabilityNameSecondItem());
456                 linkMenuClickOnNextButton();
457         linkMenuClickOnFinishButton();
458     }
459
460         public ImmutablePair<Integer, Integer> calcMidOfLink(ImmutablePair<Integer, Integer> location1, ImmutablePair<Integer, Integer> location2)
461         {
462                 int x = (location1.getLeft()+location2.getLeft())/2;
463                 int y = (location1.getRight()+location2.getRight())/2;
464
465                 ImmutablePair<Integer, Integer> location = new ImmutablePair<>(x,y);
466                 return location;
467         }
468 }