summaryrefslogtreecommitdiffstats
path: root/integration-tests/src/test/java/org/onap/sdc/frontend/ci/tests/datatypes/CanvasManager.java
blob: 560f342ea5261275c3fd60a00f82f7d54288c142 (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*-
 * ============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.frontend.ci.tests.datatypes;

import com.aventstack.extentreports.Status;
import com.clearspring.analytics.util.Pair;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.onap.sdc.backend.ci.tests.datatypes.enums.CircleSize;
import org.onap.sdc.frontend.ci.tests.pages.CompositionPage;
import org.onap.sdc.frontend.ci.tests.pages.PropertiesAssignmentPage;
import org.onap.sdc.frontend.ci.tests.pages.PropertyNameBuilder;
import org.onap.sdc.frontend.ci.tests.utilities.GeneralUIUtils;
import org.onap.sdc.frontend.ci.tests.datatypes.DataTestIdEnum.LeftPanelCanvasItems;
import org.onap.sdc.frontend.ci.tests.execute.setup.ExtentTestActions;
import org.onap.sdc.frontend.ci.tests.execute.setup.SetupCDTest;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public final class CanvasManager {
    public static final int VIEW_BUTTON_X_DELTA = 30;
    public static final int VIEW_BUTTON_Y_DELTA = 11;
    public static final int DELTET_BUTTON_X_DELTA = 30;
    public static final int CANVAS_ELEMENT_TYPE_SPLITER = 4;
    public static final int MAX_WAIT_DIVIDER = 1000;
    public static final int THREAD_SLEEP_TIME = 5000;
    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) {
        String prefix = element.getElementType();
        List<CanvasElement> canvasElementsFromSameTemplate = new ArrayList<>();

        // collect all elements from from same template
        for (CanvasElement currElement : canvasElements.values()) {
            if (currElement.getElementNameOnCanvas().toLowerCase().startsWith(prefix.toLowerCase())) {
                canvasElementsFromSameTemplate.add(currElement);
            }
        }

        // match element name to actual name on canvas
        if (canvasElementsFromSameTemplate.size() > 0) {
            String newName = prefix + " " + canvasElementsFromSameTemplate.size();
            element.setElementNameOnCanvas(newName);
        }

        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) {
        final int maxWait = 5000;
        final int napPeriod = 200;
        int sumOfWaiting = 0;
        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) {
        ImmutablePair<Integer, Integer> coordinates = getElementCoordinates(canvasElement.getElementNameOnCanvas());
        actions.moveToElement(canvas, coordinates.left, coordinates.right);
        actions.clickAndHold();
        actions.release();
        actions.perform();
        GeneralUIUtils.ultimateWait();
        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() + VIEW_BUTTON_X_DELTA;
        int y = canvasElement.getLocation().getRight() + VIEW_BUTTON_Y_DELTA;
        clickOnCanvasPosition(x, y);
        GeneralUIUtils.ultimateWait();
    }

    public void openLinkPopupReqsCapsConnection(CanvasElement sourceElement, CanvasElement destElement) {
        ExtentTestActions.log(Status.INFO, "Open Link popup");
        ImmutablePair<Integer, Integer> sourceCoordinates = getElementCoordinates(sourceElement.getElementNameOnCanvas());
        ImmutablePair<Integer, Integer> destCoordinates = getElementCoordinates(destElement.getElementNameOnCanvas());
        ImmutablePair<Integer, Integer> linkPosition = calcMidOfLink(sourceCoordinates, destCoordinates);

        clickOnCanvasPosition(linkPosition.left, linkPosition.right); // click on link
        int x = linkPosition.left + VIEW_BUTTON_X_DELTA;
        int y = linkPosition.right + VIEW_BUTTON_Y_DELTA;
        clickOnCanvasPosition(x, y); // click on view popup
        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) {
        ExtentTestActions.log(Status.INFO, "Delete Link ");
        clickOnCanvasLink(canvasElement);
        int x = canvasElement.getLocation().getLeft() + DELTET_BUTTON_X_DELTA;
        int y = canvasElement.getLocation().getRight() + DELTET_BUTTON_X_DELTA;
        clickOnCanvasPosition(x, y);
    }

    public void deleteLinkPopupReqsCapsConnection(CanvasElement sourceElement, CanvasElement destElement) {
        ExtentTestActions.log(Status.INFO, "Delete Link ");
        ImmutablePair<Integer, Integer> sourceCoordinates = getElementCoordinates(sourceElement.getElementNameOnCanvas());
        ImmutablePair<Integer, Integer> destCoordinates = getElementCoordinates(destElement.getElementNameOnCanvas());
        ImmutablePair<Integer, Integer> linkPosition = calcMidOfLink(sourceCoordinates, destCoordinates);
        clickOnCanvasPosition(linkPosition.left, linkPosition.right); // click on link
        int x = linkPosition.left + DELTET_BUTTON_X_DELTA;
        int y = linkPosition.right + DELTET_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();
        ExtentTestActions.log(Status.INFO, String.format("Removing canvas element %s ", canvasElement.getElementType()));
        GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.DELETE_INSTANCE_BUTTON.getValue())
                .click();
        GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.DELETE_INSTANCE_CANCEL.getValue()).click();
        GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.DELETE_INSTANCE_BUTTON.getValue())
                .click();
        GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.DELETE_INSTANCE_OK.getValue()).click();
        canvasElements.remove(canvasElement.getUniqueId());
        GeneralUIUtils.ultimateWait();
        if (canvasElement.getElementType().contains("-")) {
            ExtentTestActions.log(Status.INFO, String.format(
                    "Canvas element %s is removed",
                    canvasElement.getElementType().split("-")[CANVAS_ELEMENT_TYPE_SPLITER]));
        } else {
            ExtentTestActions.log(Status.INFO, String.format(
                    "Canvas element %s is 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) {
        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 {
        final int offset = 20;
        try {
            CompositionPage.searchForElement(elementDataTestId);
            WebElement element = findClickElement(elementDataTestId);
            ImmutablePair<Integer, Integer> freePosition = getFreePosition();
            actions.moveToElement(element, offset, offset);
            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) {
        return createElementOnCanvas(canvasItem.getValue());
    }

    private ImmutablePair<Integer, Integer> getFreePosition() {
        ImmutablePair<Integer, Integer> randomPosition = null;
        boolean freePosition = false;
        final 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() {
        final 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.getElementNameOnCanvas(), secondElement.getElementNameOnCanvas());
        selectReqAndCapAndConnect();
        ExtentTestActions.log(Status.INFO, String.format("The instances %s and %s should now be connected.", firstElement.getElementType(), secondElement.getElementType()));
    }

    // old version, depricated
    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()));
    }

    public void linkElements(String firstElement, String secondElement) throws Exception {
        drawSimpleLink(firstElement, secondElement);
        selectReqAndCapAndConnect();
        ExtentTestActions.log(Status.INFO, String.format("The instances %s and %s should now be connected.", firstElement, secondElement));
    }

    // use JS to get coordinates of elements
    private void drawSimpleLink(String firstElement, String secondElement) {
        ImmutablePair<Integer, Integer> firstElementCoordinates = getGreenDotCoordinatesOfElement(firstElement);
        ImmutablePair<Integer, Integer> secondElementCoordinates = getElementCoordinates(secondElement);

        actions.moveToElement(canvas, firstElementCoordinates.left, firstElementCoordinates.right);
        actions.perform();
        actions.moveToElement(canvas, firstElementCoordinates.left, firstElementCoordinates.right);
        actions.clickAndHold();
        actions.moveToElement(canvas, secondElementCoordinates.left, secondElementCoordinates.right);
        actions.release();
        actions.perform();
        GeneralUIUtils.ultimateWait();
    }

    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.build();
        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) {
        GeneralUIUtils.ultimateWait();
        clickOnCanvaElement(canvasElement);
        GeneralUIUtils.getWebElementBy(By.id("editPencil")).click();
        GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.RENAME_INSTANCE_CANCEL.getValue()).click();
        GeneralUIUtils.getWebElementBy(By.id("editPencil")).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.RENAME_INSTANCE_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
     */
    private void validateInstanceSelected(CanvasElement canvasElement) {
        final long maxWait = 5000;
        final long napPeriod = 200;
        long sumOfWaiting = 0;
        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("Bug 342260, can't select instance properly, waited for %s seconds after click on instance", (int) (maxWait / MAX_WAIT_DIVIDER)));
            }
        } while (!isInstanceSelected);
    }

    public void validateLinkIsSelected() {
        final long maxWait = 5000;
        final long napPeriod = 200;
        long sumOfWaiting = 0;
        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 / MAX_WAIT_DIVIDER)));
            }
        } 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, CanvasElement secondElement, ConnectionWizardPopUpObject connectionWizardPopUpObject) throws Exception {
        SetupCDTest.getExtendTest().log(Status.INFO, String.format("Creating link between %s and %s", firstElement.getElementType(), secondElement.getElementType()));
        drawSimpleLink(firstElement.getElementNameOnCanvas(), secondElement.getElementNameOnCanvas());
        selectTypeOfReqCap(DataTestIdEnum.LinkMenuItems.REQ_CAP_SELECT_DATA_TESTS_ID.getValue(), connectionWizardPopUpObject.getCapabilityTypeSecondItem());
        addFirstReqOrCapAndPressNext();
        selectReqCapByName(connectionWizardPopUpObject.getCapabilityNameSecondItem());
        linkMenuClickOnNextButton();
        linkMenuClickOnFinishButton();
    }

    public Map<String, String> linkElementsWithCapPropAssignment(CanvasElement firstElement, CanvasElement secondElement, ConnectionWizardPopUpObject connectionWizardPopUpObject) throws Exception {
        SetupCDTest.getExtendTest().log(Status.INFO, String.format("Creating link between %s and %s", firstElement.getElementType(), secondElement.getElementType()));
        drawSimpleLink(firstElement.getElementNameOnCanvas(), secondElement.getElementNameOnCanvas());
        selectTypeOfReqCap(DataTestIdEnum.LinkMenuItems.REQ_CAP_SELECT_DATA_TESTS_ID.getValue(), connectionWizardPopUpObject.getCapabilityTypeSecondItem());
        addFirstReqOrCapAndPressNext();
        selectReqCapByName(connectionWizardPopUpObject.getCapabilityNameSecondItem());
        linkMenuClickOnNextButton();
        Map<String, String> mapOfValues = connectionWizardAssignCapPropValues();
        linkMenuClickOnFinishButton();
        Thread.sleep(THREAD_SLEEP_TIME);
        return mapOfValues;
    }


    public Map<String, String> connectionWizardAssignCapPropValues() throws Exception {
        //get list of capability property value fields data-tests-ids in connection wizard
        List<String> valueField = getListOfValueFieldIDs();
        //get map of field ids and their values, fill in values if empty
        Map<String, String> propValues = getMapOfCapPropValues(valueField, true);
        return propValues;
    }

    public Map<String, String> connectionWizardCollectCapPropValues() throws Exception {
        //get list of capability property value fields data-tests-ids in connection wizard
        List<String> valueField = getListOfValueFieldIDs();
        //get map of field ids and their values, collect existing values
        Map<String, String> propValues = getMapOfCapPropValues(valueField, false);
        return propValues;
    }

    private List<String> getListOfValueFieldIDs() {
        String propName = GeneralUIUtils.getWebElementsListByContainsClassName("multiline-ellipsis-content").get(0).getText();
        List<WebElement> valueNameElement = GeneralUIUtils.findElementsByXpath("//div[@class='dynamic-property-row nested-level-1']/div[1]");
        List<String> valueName = new ArrayList<>();
        for (int i = 0; i < valueNameElement.size(); i++) {
            valueName.add(valueNameElement.get(i).getText());
        }
        //get list of value field names as appear in data-tests-id
        List<String> valueField = new ArrayList<>();
        for (int i = 0; i < valueName.size(); i++) {
            valueField.add(PropertyNameBuilder.buildIComplexField(propName, valueName.get(i)));
        }
        return valueField;
    }

    private Map<String, String> getMapOfCapPropValues(List<String> valueField, boolean isValueAssign) throws Exception {
        SetupCDTest.getExtendTest().log(Status.INFO, String.format("Assigning values to properties of capabilities and/or collecting existing ones"));
        Map<String, String> propValues = new HashMap<>();
        for (int i = 0; i < valueField.size(); i++) {
            String fieldId = valueField.get(i);
            if (GeneralUIUtils.getWebElementByTestID(fieldId).getAttribute("value").isEmpty() && isValueAssign) {
                //add value and put into map
                propValues.put(fieldId, "value" + i);
                PropertiesAssignmentPage.editPropertyValue(fieldId, "value" + i);
            } else {
                //put existing value into map
                propValues.put(fieldId, GeneralUIUtils.getWebElementByTestID(valueField.get(i)).getAttribute("value"));
            }
        }
        return propValues;
    }


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

    public ImmutablePair<Integer, Integer> getElementCoordinates(String elementName) {
        Object position = GeneralUIUtils.getElementPositionOnCanvas(elementName);
        return converJSJsonToCoordinates(position);
    }

    public ImmutablePair<Integer, Integer> getGreenDotCoordinatesOfElement(String elementName) {
        Object position = GeneralUIUtils.getElementGreenDotPositionOnCanvas(elementName);
        return converJSJsonToCoordinates(position);
    }

    public ImmutablePair<Integer, Integer> converJSJsonToCoordinates(Object position) {
        JsonElement root = new JsonParser().parse(position.toString());
        int xElement = root.getAsJsonObject().get("x").getAsInt();
        int yElement = root.getAsJsonObject().get("y").getAsInt();
        return new ImmutablePair<Integer, Integer>(xElement, yElement);
    }
}