1 package org.openecomp.sdc.uici.tests.execute.base;
3 import java.io.FileNotFoundException;
4 import java.util.ArrayList;
5 import java.util.HashSet;
9 import java.util.function.Function;
10 import java.util.stream.Collectors;
11 import java.util.stream.Stream;
13 import org.apache.commons.lang.NotImplementedException;
14 import org.apache.log4j.Logger;
15 import org.apache.tinkerpop.gremlin.structure.Edge;
16 import org.apache.tinkerpop.gremlin.structure.Vertex;
17 import org.junit.rules.TestName;
18 import org.openecomp.sdc.be.model.User;
19 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
20 import org.openecomp.sdc.ci.tests.config.Config;
21 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
22 import org.openecomp.sdc.ci.tests.run.StartTest;
23 import org.openecomp.sdc.ci.tests.utils.Utils;
24 import org.openecomp.sdc.common.datastructure.FunctionalInterfaces;
25 import org.openecomp.sdc.uici.tests.datatypes.CleanTypeEnum;
26 import org.openecomp.sdc.uici.tests.datatypes.UserCredentials;
27 import org.openecomp.sdc.uici.tests.utilities.FileHandling;
28 import org.openecomp.sdc.uici.tests.utilities.GeneralUIUtils;
29 import org.openqa.selenium.By;
30 import org.openqa.selenium.WebDriver;
31 import org.openqa.selenium.WebElement;
32 import org.testng.AssertJUnit;
33 import org.testng.ITestResult;
34 import org.testng.annotations.AfterMethod;
35 import org.testng.annotations.AfterSuite;
36 import org.testng.annotations.BeforeMethod;
37 import org.testng.annotations.BeforeSuite;
38 import org.testng.annotations.Optional;
39 import org.testng.annotations.Parameters;
41 import com.google.common.collect.Lists;
42 import com.thinkaurelius.titan.core.TitanGraph;
44 public abstract class SetupCDTest extends ComponentBaseTest {
46 private TitanSnapshot snapshot;
47 private static CleanTypeEnum cleanType;
49 public SetupCDTest() {
50 super(new TestName(), SetupCDTest.class.getName());
53 public SetupCDTest(TestName name, String className) {
54 super(name, className);
57 public static Logger logger = Logger.getLogger(SetupCDTest.class.getName());
59 /**************** CONSTANTS ****************/
60 private static final String CREDENTIALS_FILE = "src/main/resources/ci/conf/credentials.yaml";
61 public static final String SELENIUM_NODE_URL = "http://%s:%s/wd/hub";
63 /**************** PRIVATES ****************/
64 public static Config config;
65 private Map<?, ?> credentialsYamlFileMap;
67 private static String devUrl, cdUrl;
69 @BeforeSuite(alwaysRun = true)
70 public static void openTitan() throws FileNotFoundException {
73 } catch (Exception e) {
74 // TODO Auto-generated catch block
79 @AfterSuite(alwaysRun = true)
80 public static void shutdownTitan() {
84 @BeforeSuite(alwaysRun = true)
85 @Parameters({ "clean-type" })
86 public void setEnvParameters(@Optional("PARTIAL") String cleanType) throws FileNotFoundException {
87 this.cleanType = CleanTypeEnum.findByName(cleanType);
88 System.out.println("setup before class");
89 config = Utils.getConfig();
90 loadCredentialsFile();
94 @BeforeMethod(alwaysRun = true)
95 public void setBrowserBeforeTest() {
96 setBrowserBeforeTest(getRole());
99 /**************** AFTER ****************/
100 @AfterMethod(alwaysRun = true)
101 public void quitAfterTest() {
102 System.out.println("closing browser");
103 GeneralUIUtils.getDriver().quit();
106 @BeforeMethod(alwaysRun = true)
107 public void beforeState(java.lang.reflect.Method method) throws Exception {
108 CleanTypeEnum cleanType = getCleanMode();
111 super.performeClean();
123 throw new NotImplementedException("Enum Value:" + cleanType.name() + " Is not handled");
129 @AfterMethod(alwaysRun = true)
130 public void afterState(ITestResult result) throws Exception {
131 CleanTypeEnum cleanType = getCleanMode();
134 super.performeClean();
138 resetToOriginalSnapshot();
146 throw new NotImplementedException("Enum Value:" + cleanType.name() + " Is not handled");
152 private void takeTitanSnapshot() {
153 List<Edge> edgeList = Lists.newArrayList(getTitanGraph().edges(null));
154 List<Vertex> verList = Lists.newArrayList(getTitanGraph().vertices(null));
155 setSnapshot(new TitanSnapshot(edgeList, verList));
159 private static class TitanSnapshot {
161 List<Vertex> vertices;
163 public List<Edge> getEdges() {
167 public List<Vertex> getVertices() {
171 private TitanSnapshot(List<Edge> edges, List<Vertex> vertices) {
174 this.vertices = vertices;
178 private void resetToOriginalSnapshot() {
180 List<Edge> joinedEdges = new ArrayList<>();
181 List<Vertex> joinedVertices = new ArrayList<>();
182 TitanSnapshot original = getSnapshot();
184 TitanSnapshot current = getSnapshot();
186 original.getEdges().stream().forEach(e -> addIfIdInList(e, current.getEdges(), joinedEdges, e2 -> e2.id()));
187 original.getVertices().stream()
188 .forEach(e -> addIfIdInList(e, current.getVertices(), joinedVertices, e2 -> e2.id()));
190 List<Edge> edgesToRemove = removeFromList(current.getEdges(), joinedEdges, e2 -> e2.id());
191 List<Vertex> verticesToRemove = removeFromList(current.getVertices(), joinedVertices, e2 -> e2.id());
193 List<Edge> edgesToAdd = removeFromList(original.getEdges(), joinedEdges, e2 -> e2.id());
194 List<Vertex> verticesToAdd = removeFromList(original.getVertices(), joinedVertices, e2 -> e2.id());
196 if (edgesToAdd.isEmpty() && verticesToAdd.isEmpty()) {
197 edgesToRemove.stream().forEach(e -> e.remove());
198 verticesToRemove.stream().forEach(v -> v.remove());
203 private <Element, ID> List<Element> removeFromList(List<Element> listToRemoveFrom, List<Element> elementsToRemove,
204 Function<Element, ID> idGetter) {
205 Set<ID> idSet = new HashSet<>();
207 elementsToRemove.stream().map(e -> idGetter.apply(e)).forEach(e2 -> idSet.add(e2));
208 return listToRemoveFrom.stream().filter(p -> !idSet.contains(idGetter.apply(p))).collect(Collectors.toList());
212 private <Element, ID> void addIfIdInList(Element e, List<Element> listToCheck, List<Element> listToAddTo,
213 Function<Element, ID> idGetter) {
214 Stream<Element> matchingElements = listToCheck.stream()
215 .filter(p -> idGetter.apply(e).equals(idGetter.apply(p)));
216 listToAddTo.addAll(matchingElements.collect(Collectors.toList()));
219 /**************** MAIN ****************/
220 public static void main(String[] args) {
221 System.out.println("---------------------");
222 System.out.println("running test from CLI");
223 System.out.println("---------------------");
224 args = new String[] { "ui-ci.xml" };
225 StartTest.main(args);
228 /***********************************************************************************/
230 protected void setBrowserBeforeTest(UserRoleEnum role) {
231 System.out.println("setup before test");
232 GeneralUIUtils.initDriver();
237 protected void setUrl() {
238 cdUrl = config.getUrl();
239 setDevUrl(getRole());
242 private Map<String, String> loadCredentialsFile() {
243 final String credintialsFile = (System.getProperty("credentials.file") != null)
244 ? System.getProperty("credentials.file") : CREDENTIALS_FILE;
245 System.out.println("credentials file is : " + credintialsFile);
246 FunctionalInterfaces.swallowException(
247 () -> credentialsYamlFileMap = (Map<String, String>) FileHandling.parseYamlFile(credintialsFile));
248 System.out.println(credentialsYamlFileMap.toString());
249 return (Map<String, String>) credentialsYamlFileMap;
252 protected UserCredentials getUserCredentialsFromFile(String userRole) throws Exception {
253 Map<String, String> credentialsMap = (Map<String, String>) credentialsYamlFileMap.get(userRole);
254 String user = (String) credentialsMap.get("username");
255 String password = (String) credentialsMap.get("password");
256 String firstname = (String) credentialsMap.get("firstname");
257 String lastname = (String) credentialsMap.get("lastname");
259 return new UserCredentials(user, password, firstname, lastname);
262 public void navigateToUrl(String url) throws InterruptedException {
263 WebDriver driver = GeneralUIUtils.getDriver();
264 System.out.println("navigating to URL :" + url);
265 driver.navigate().to(url);
266 driver.manage().window().maximize();
267 driver.manage().deleteAllCookies();
270 protected void loginToSystem(UserCredentials credentials) throws Exception {
272 sendUserAndPasswordKeys(credentials);
273 WebElement submitButton = GeneralUIUtils.getDriver().findElement(By.name("btnSubmit"));
274 submitButton.click();
275 WebElement buttonOK = GeneralUIUtils.getDriver().findElement(By.name("successOK"));
276 AssertJUnit.assertTrue(buttonOK.isDisplayed());
278 System.out.println("Entering to design studio");
280 WebElement enterToUserWorkspaceButton = GeneralUIUtils.getDriver()
281 .findElement(By.xpath("//button[@data-tests-id='Design Studio']"));
282 enterToUserWorkspaceButton.click();
285 private void sendUserAndPasswordKeys(UserCredentials userId) {
286 System.out.println("Login to system with user : " + userId.getUserId());
287 WebElement userNameTextbox = GeneralUIUtils.getDriver().findElement(By.name("userid"));
288 userNameTextbox.sendKeys(userId.getUserId());
289 WebElement passwordTextbox = GeneralUIUtils.getDriver().findElement(By.name("password"));
290 passwordTextbox.sendKeys(userId.getPassword());
293 public String getUrl() {
295 final CleanTypeEnum workMode = getCleanMode();
310 throw new NotImplementedException(workMode.name());
317 public static void setDevUrl(UserRoleEnum role) {
318 String url = SetupCDTest.devUrl;
321 url = "http://localhost:8181/sdc1/proxy-admin1#/dashboard";
325 url = "http://localhost:8181/sdc1/proxy-designer1#/dashboard";
326 // url = "http://localhost:9000/#/dashboard";
330 url = "http://localhost:8181/sdc1/proxy-governor1#/dashboard";
334 url = "http://localhost:8181/sdc1/proxy-ops1#/dashboard";
338 url = "http://localhost:8181/sdc1/proxy-tester1#/dashboard";
345 SetupCDTest.devUrl = url;
348 public static Config getConfig() {
354 public void loginWithUser(UserRoleEnum role) {
357 String url = getUrl();
358 System.out.println("URL is : " + url);
361 if (url.contains("https://www.e-access.att.com")) {
362 System.out.println("going to update designer user to mechIDs form...");
363 UserCredentials credentials = getUserCredentialsFromFile(role.name().toLowerCase());
364 loginToSystem(credentials);
366 } catch (Exception e) {
367 throw new RuntimeException(e);
371 private void setUser(UserRoleEnum role) {
373 user.setUserId(role.getUserId());
374 user.setFirstName(role.getFirstName());
375 user.setRole(role.name());
383 public UserRoleEnum getRole() {
384 return UserRoleEnum.DESIGNER;
388 * To change clean type update configuration.<br>
389 * Do not override this method.
393 protected final CleanTypeEnum getCleanMode() {
397 public User getUser() {
401 protected void quitAndReLogin(UserRoleEnum role) {
403 setBrowserBeforeTest(role);
404 GeneralUIUtils.waitForLoader(30);
407 public TitanSnapshot getSnapshot() {
411 public void setSnapshot(TitanSnapshot snapshot) {
412 this.snapshot = snapshot;
415 public static TitanGraph getTitanGraph() {