2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.sdc.frontend.ci.tests.pages.component.workspace;
22 import static org.onap.sdc.backend.ci.tests.datatypes.enums.UserRoleEnum.DESIGNER;
24 import com.aventstack.extentreports.Status;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
27 import java.time.Duration;
28 import java.util.ArrayList;
29 import java.util.HashSet;
30 import java.util.Optional;
31 import java.util.Random;
33 import lombok.AllArgsConstructor;
35 import org.apache.commons.lang3.tuple.ImmutablePair;
36 import org.onap.sdc.backend.ci.tests.utils.general.AtomicOperationUtils;
37 import org.onap.sdc.frontend.ci.tests.datatypes.CanvasNodeElement;
38 import org.onap.sdc.frontend.ci.tests.exception.CompositionCanvasRuntimeException;
39 import org.onap.sdc.frontend.ci.tests.execute.setup.ExtentTestActions;
40 import org.onap.sdc.frontend.ci.tests.pages.AbstractPageObject;
41 import org.openecomp.sdc.be.model.ComponentInstance;
42 import org.openecomp.sdc.be.model.Resource;
43 import org.openecomp.sdc.be.model.Service;
44 import org.openqa.selenium.Dimension;
45 import org.openqa.selenium.JavascriptExecutor;
46 import org.openqa.selenium.Point;
47 import org.openqa.selenium.WebDriver;
48 import org.openqa.selenium.WebElement;
49 import org.openqa.selenium.interactions.Actions;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class CompositionCanvasComponent extends AbstractPageObject {
55 private static final Logger LOGGER = LoggerFactory.getLogger(CompositionCanvasComponent.class);
56 private static final String nodePositionJs = "var cy = window.jQuery('.sdc-composition-graph-wrapper').cytoscape('get');%n"
57 + "var n = cy.nodes('[name=\"%s\"]');%n"
58 + "var nPos = n.renderedPosition();%n"
59 + "return JSON.stringify({%n"
64 private static final String getNodesJs = "var cy = window.jQuery('.sdc-composition-graph-wrapper').cytoscape('get');\n"
66 + "cy.nodes().forEach((node) => {nodes.push(JSON.stringify({name: node.data('name'), position: node.renderedPosition()}))});\n"
69 private final CompositionElementsComponent compositionElementsComponent;
70 private final CompositionDetailSideBarComponent compositionDetailSideBarComponent;
72 private WebElement canvasWebElement;
73 private Set<CanvasNodeElement> canvasElementList;
74 private int canvasCenterX;
75 private int canvasCenterY;
76 private int canvasWidth;
77 private int canvasHeight;
79 public CompositionCanvasComponent(final WebDriver webDriver) {
81 compositionElementsComponent = new CompositionElementsComponent(webDriver);
82 compositionDetailSideBarComponent = new CompositionDetailSideBarComponent(webDriver);
86 public void isLoaded() {
87 //waiting the canvas data to be load and animation finishes.
88 new Actions(webDriver).pause(Duration.ofSeconds(2)).perform();
89 canvasWebElement = waitToBeClickable(XpathSelector.CANVAS_ELEMENT.getXpath());
90 compositionElementsComponent.isLoaded();
91 compositionDetailSideBarComponent.isLoaded();
96 private void loadCanvas() {
97 canvasWidth = canvasWebElement.getSize().getWidth();
98 canvasHeight = canvasWebElement.getSize().getHeight();
99 canvasCenterX = canvasWidth / 2;
100 canvasCenterY = canvasHeight / 2;
101 LOGGER.debug("Canvas with size [{}, {}] and center [{}, {}]", canvasWidth, canvasHeight, canvasCenterX, canvasCenterY);
102 final String scriptJS = "var cy = window.jQuery('.sdc-composition-graph-wrapper').cytoscape('get');\n"
103 + "return JSON.stringify({width: cy.width(), height: cy.height()});";
104 final Object sizeObj = ((JavascriptExecutor) webDriver).executeScript(scriptJS);
105 final JsonObject size = new JsonParser().parse(sizeObj.toString()).getAsJsonObject();
107 LOGGER.debug("Canvas with size [{}, {}]", size.get("width"), size.get("height"));
110 private void loadElements() {
111 canvasElementList = new HashSet<>();
112 final Object nodeListObj = ((JavascriptExecutor) webDriver).executeScript(getNodesJs);
113 if (!(nodeListObj instanceof ArrayList)) {
116 final ArrayList<String> nodeList = (ArrayList<String>) nodeListObj;
117 if (nodeList.isEmpty()) {
120 nodeList.forEach(nodeString -> {
121 final JsonObject node = new JsonParser().parse(nodeString).getAsJsonObject();
122 final JsonObject position = node.get("position").getAsJsonObject();
123 final CanvasNodeElement canvasElement =
124 new CanvasNodeElement(node.get("name").getAsString(), position.get("x").getAsInt(), position.get("y").getAsInt());
125 canvasElementList.add(canvasElement);
129 public void selectNode(final String elementName) {
130 final Optional<CanvasNodeElement> canvasElementOptional = canvasElementList.stream()
131 .filter(canvasNodeElement -> canvasNodeElement.getName().equals(elementName))
133 if (canvasElementOptional.isEmpty()) {
134 throw new CompositionCanvasRuntimeException(String.format("Given element '%s' does not exist on the element list", elementName));
136 final CanvasNodeElement canvasNodeElement = canvasElementOptional.get();
137 final Point positionFromCenter = calculateOffsetFromCenter(canvasNodeElement.getPositionX(),
138 canvasNodeElement.getPositionY());
139 final Actions actions = new Actions(webDriver);
140 int offsetFromElementCenter = 10;
141 actions.moveToElement(canvasWebElement, positionFromCenter.getX() - offsetFromElementCenter,
142 positionFromCenter.getY() + offsetFromElementCenter)
143 .pause(Duration.ofSeconds(1))
146 ExtentTestActions.takeScreenshot(Status.INFO, "canvas-node-selected", String.format("'%s' was selected", elementName));
149 public ComponentInstance createNodeOnServiceCanvas(final String serviceName, final String serviceVersion, final String resourceName,
150 final String resourceVersion) {
151 final Point freePositionInCanvas = getFreePositionInCanvas(20);
152 final Point pointFromCanvasCenter = calculateOffsetFromCenter(freePositionInCanvas);
154 final Service service =
155 AtomicOperationUtils.getServiceObjectByNameAndVersion(DESIGNER, serviceName, serviceVersion);
156 final Resource resourceToAdd =
157 AtomicOperationUtils.getResourceObjectByNameAndVersion(DESIGNER, resourceName, resourceVersion);
158 final ComponentInstance componentInstance = AtomicOperationUtils
159 .addComponentInstanceToComponentContainer(resourceToAdd, service, DESIGNER, true,
160 String.valueOf(pointFromCanvasCenter.getX()), String.valueOf(pointFromCanvasCenter.getY()))
163 LOGGER.debug("Created instance {} in the Service {}", componentInstance.getName(), serviceName);
164 return componentInstance;
165 } catch (final Exception e) {
166 throw new CompositionCanvasRuntimeException("Could not create node through the API", e);
170 public ComponentInstance createNodeOnResourceCanvas(final String serviceName, final String serviceVersion, final String resourceName,
171 final String resourceVersion) {
172 final Point freePositionInCanvas = getFreePositionInCanvas(20);
173 final Point pointFromCanvasCenter = calculateOffsetFromCenter(freePositionInCanvas);
175 final Resource service = AtomicOperationUtils.getResourceObjectByNameAndVersion(DESIGNER, serviceName, serviceVersion);
176 final Resource resourceToAdd = AtomicOperationUtils.getResourceObjectByNameAndVersion(DESIGNER, resourceName, resourceVersion);
177 final ComponentInstance componentInstance =
178 AtomicOperationUtils.addComponentInstanceToComponentContainer(resourceToAdd, service, DESIGNER, true,
179 String.valueOf(pointFromCanvasCenter.getX()), String.valueOf(pointFromCanvasCenter.getY())).left().value();
181 LOGGER.debug("Created instance {} in the Service {}", componentInstance.getName(), serviceName);
182 return componentInstance;
183 } catch (final Exception e) {
184 throw new CompositionCanvasRuntimeException("Could not create node through the API", e);
188 private Point getFreePositionInCanvas(int maxAttempts) {
189 boolean isPositionFree;
190 final int minSpace = 150;
191 for (int attemptCount = 0; attemptCount < maxAttempts; attemptCount++) {
192 final Point randomPositionInCanvas = getRandomPositionInCanvas();
193 isPositionFree = canvasElementList.stream()
194 .noneMatch(canvasNodeElement -> Math.abs(canvasNodeElement.getPositionX() - randomPositionInCanvas.getX()) < minSpace
195 && Math.abs(canvasNodeElement.getPositionX() - randomPositionInCanvas.getY()) < minSpace);
196 if (isPositionFree) {
197 return randomPositionInCanvas;
200 throw new CompositionCanvasRuntimeException("Could not find a free Canvas position");
203 private Point getRandomPositionInCanvas() {
204 final Random random = new Random();
205 int x = random.nextInt(canvasWidth);
206 final int maxAllowedWidth = canvasWidth - getRightMarginWidth();
207 final int minAllowedWidth = 30;
208 if (x > maxAllowedWidth) {
209 x = x - getRightMarginWidth();
210 } else if (x < minAllowedWidth) {
211 x = x + minAllowedWidth;
213 int bottomMargin = 0;
214 int heightTopMargin = 100;
215 int y = random.nextInt(canvasHeight);
216 int maxAllowedHeight = canvasHeight - bottomMargin;
218 if (y > maxAllowedHeight) {
219 y = y - bottomMargin;
220 } else if (y < heightTopMargin) {
221 y = y + heightTopMargin;
223 LOGGER.debug("Generated random position in canvas [{},{}]", x, y);
225 return new Point(x, y);
228 private int getRightMarginWidth() {
229 int canvasIconsOffset = 100;
230 final Dimension sideBarSize = compositionDetailSideBarComponent.getSize();
231 return sideBarSize.getWidth() + canvasIconsOffset;
234 private Point calculateOffsetFromCenter(final Point point) {
235 return calculateOffsetFromCenter(point.getX(), point.getY());
238 private Point calculateOffsetFromCenter(final int xPosition, final int yPosition) {
239 final int positionX = xPosition - canvasCenterX;
240 final int positionY = yPosition - canvasCenterY;
241 return new Point(positionX, positionY);
244 public ImmutablePair<Integer, Integer> getElementPositionByName(final String elementName) {
245 final String scriptJs = String.format(nodePositionJs, elementName);
246 final Object position = ((JavascriptExecutor) webDriver).executeScript(scriptJs);
247 final JsonObject positionAsJson = new JsonParser().parse(position.toString()).getAsJsonObject();
248 int xElement = positionAsJson.get("x").getAsInt();
249 int yElement = positionAsJson.get("y").getAsInt();
250 return new ImmutablePair<>(xElement, yElement);
254 * Enum that contains identifiers and xpath expressions to elements related to the enclosing page object.
257 private enum XpathSelector {
258 CANVAS_ELEMENT("canvas", "//*[@data-tests-id='%s']//canvas[1]");
261 private final String id;
262 private final String xpathFormat;
264 public String getXpath() {
265 return String.format(xpathFormat, id);