summaryrefslogtreecommitdiffstats
path: root/ui-ci-dev/src/main/java/org/openecomp/sdc/uici/tests/execute/base/SetupCDTest.java
blob: f9e294cbc782f4b339de2d4cb973675775ecc38d (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
package org.openecomp.sdc.uici.tests.execute.base;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang.NotImplementedException;
import org.apache.log4j.Logger;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.junit.rules.TestName;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
import org.openecomp.sdc.ci.tests.config.Config;
import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
import org.openecomp.sdc.ci.tests.run.StartTest;
import org.openecomp.sdc.ci.tests.utils.Utils;
import org.openecomp.sdc.common.datastructure.FunctionalInterfaces;
import org.openecomp.sdc.uici.tests.datatypes.CleanTypeEnum;
import org.openecomp.sdc.uici.tests.datatypes.UserCredentials;
import org.openecomp.sdc.uici.tests.utilities.FileHandling;
import org.openecomp.sdc.uici.tests.utilities.GeneralUIUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.AssertJUnit;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;

import com.google.common.collect.Lists;
import com.thinkaurelius.titan.core.TitanGraph;

public abstract class SetupCDTest extends ComponentBaseTest {

	private TitanSnapshot snapshot;
	private static CleanTypeEnum cleanType;

	public SetupCDTest() {
		super(new TestName(), SetupCDTest.class.getName());
	}

	public SetupCDTest(TestName name, String className) {
		super(name, className);
	}

	public static Logger logger = Logger.getLogger(SetupCDTest.class.getName());

	/**************** CONSTANTS ****************/
	private static final String CREDENTIALS_FILE = "src/main/resources/ci/conf/credentials.yaml";
	public static final String SELENIUM_NODE_URL = "http://%s:%s/wd/hub";

	/**************** PRIVATES ****************/
	public static Config config;
	private Map<?, ?> credentialsYamlFileMap;

	private static String devUrl, cdUrl;

	@BeforeSuite(alwaysRun = true)
	public static void openTitan() throws FileNotFoundException {
		try {
			openTitanLogic();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@AfterSuite(alwaysRun = true)
	public static void shutdownTitan() {
		shutdownTitanLogic();
	}
	
	@BeforeSuite(alwaysRun = true)
	@Parameters({ "clean-type" })
	public void setEnvParameters(@Optional("PARTIAL") String cleanType) throws FileNotFoundException {
		this.cleanType = CleanTypeEnum.findByName(cleanType);
		System.out.println("setup before class");
		config = Utils.getConfig();
		loadCredentialsFile();
		setUrl();
	}

	@BeforeMethod(alwaysRun = true)
	public void setBrowserBeforeTest() {
		setBrowserBeforeTest(getRole());
	}

	/**************** AFTER ****************/
	@AfterMethod(alwaysRun = true)
	public void quitAfterTest() {
		System.out.println("closing browser");
		GeneralUIUtils.getDriver().quit();
	}

	@BeforeMethod(alwaysRun = true)
	public void beforeState(java.lang.reflect.Method method) throws Exception  {
		CleanTypeEnum cleanType = getCleanMode();
		switch (cleanType) {
		case FULL: {
			super.performeClean();
			break;
		}
		case PARTIAL: {
			takeTitanSnapshot();
			break;
		}
		case NONE: {
			// No Clean Up
			break;
		}
		default: {
			throw new NotImplementedException("Enum Value:" + cleanType.name() + " Is not handled");
		}
		}

	}

	@AfterMethod(alwaysRun = true)
	public void afterState(ITestResult result) throws Exception {
		CleanTypeEnum cleanType = getCleanMode();
		switch (cleanType) {
		case FULL: {
			super.performeClean();
			break;
		}
		case PARTIAL: {
			resetToOriginalSnapshot();
			break;
		}
		case NONE: {
			// No Clean Up
			break;
		}
		default: {
			throw new NotImplementedException("Enum Value:" + cleanType.name() + " Is not handled");
		}
		}

	}

	private void takeTitanSnapshot() {
		List<Edge> edgeList = Lists.newArrayList(getTitanGraph().edges(null));
		List<Vertex> verList = Lists.newArrayList(getTitanGraph().vertices(null));
		setSnapshot(new TitanSnapshot(edgeList, verList));

	}

	private static class TitanSnapshot {
		List<Edge> edges;
		List<Vertex> vertices;

		public List<Edge> getEdges() {
			return edges;
		}

		public List<Vertex> getVertices() {
			return vertices;
		}

		private TitanSnapshot(List<Edge> edges, List<Vertex> vertices) {
			super();
			this.edges = edges;
			this.vertices = vertices;
		}
	}

	private void resetToOriginalSnapshot() {

		List<Edge> joinedEdges = new ArrayList<>();
		List<Vertex> joinedVertices = new ArrayList<>();
		TitanSnapshot original = getSnapshot();
		takeTitanSnapshot();
		TitanSnapshot current = getSnapshot();

		original.getEdges().stream().forEach(e -> addIfIdInList(e, current.getEdges(), joinedEdges, e2 -> e2.id()));
		original.getVertices().stream()
				.forEach(e -> addIfIdInList(e, current.getVertices(), joinedVertices, e2 -> e2.id()));

		List<Edge> edgesToRemove = removeFromList(current.getEdges(), joinedEdges, e2 -> e2.id());
		List<Vertex> verticesToRemove = removeFromList(current.getVertices(), joinedVertices, e2 -> e2.id());

		List<Edge> edgesToAdd = removeFromList(original.getEdges(), joinedEdges, e2 -> e2.id());
		List<Vertex> verticesToAdd = removeFromList(original.getVertices(), joinedVertices, e2 -> e2.id());

		if (edgesToAdd.isEmpty() && verticesToAdd.isEmpty()) {
			edgesToRemove.stream().forEach(e -> e.remove());
			verticesToRemove.stream().forEach(v -> v.remove());
		}

	}

	private <Element, ID> List<Element> removeFromList(List<Element> listToRemoveFrom, List<Element> elementsToRemove,
			Function<Element, ID> idGetter) {
		Set<ID> idSet = new HashSet<>();
		// Fill The Set
		elementsToRemove.stream().map(e -> idGetter.apply(e)).forEach(e2 -> idSet.add(e2));
		return listToRemoveFrom.stream().filter(p -> !idSet.contains(idGetter.apply(p))).collect(Collectors.toList());

	}

	private <Element, ID> void addIfIdInList(Element e, List<Element> listToCheck, List<Element> listToAddTo,
			Function<Element, ID> idGetter) {
		Stream<Element> matchingElements = listToCheck.stream()
				.filter(p -> idGetter.apply(e).equals(idGetter.apply(p)));
		listToAddTo.addAll(matchingElements.collect(Collectors.toList()));
	}

	/**************** MAIN ****************/
	public static void main(String[] args) {
		System.out.println("---------------------");
		System.out.println("running test from CLI");
		System.out.println("---------------------");
		args = new String[] { "ui-ci.xml" };
		StartTest.main(args);
	}

	/***********************************************************************************/

	protected void setBrowserBeforeTest(UserRoleEnum role) {
		System.out.println("setup before test");
		GeneralUIUtils.initDriver();
		setDevUrl(role);
		loginWithUser(role);
	}

	protected void setUrl() {
		cdUrl = config.getUrl();
		setDevUrl(getRole());
	}

	private Map<String, String> loadCredentialsFile() {
		final String credintialsFile = (System.getProperty("credentials.file") != null)
				? System.getProperty("credentials.file") : CREDENTIALS_FILE;
		System.out.println("credentials file is : " + credintialsFile);
		FunctionalInterfaces.swallowException(
				() -> credentialsYamlFileMap = (Map<String, String>) FileHandling.parseYamlFile(credintialsFile));
		System.out.println(credentialsYamlFileMap.toString());
		return (Map<String, String>) credentialsYamlFileMap;
	}

	protected UserCredentials getUserCredentialsFromFile(String userRole) throws Exception {
		Map<String, String> credentialsMap = (Map<String, String>) credentialsYamlFileMap.get(userRole);
		String user = (String) credentialsMap.get("username");
		String password = (String) credentialsMap.get("password");
		String firstname = (String) credentialsMap.get("firstname");
		String lastname = (String) credentialsMap.get("lastname");

		return new UserCredentials(user, password, firstname, lastname);
	}

	public void navigateToUrl(String url) throws InterruptedException {
		WebDriver driver = GeneralUIUtils.getDriver();
		System.out.println("navigating to URL :" + url);
		driver.navigate().to(url);
		driver.manage().window().maximize();
		driver.manage().deleteAllCookies();
	}

	protected void loginToSystem(UserCredentials credentials) throws Exception {

		sendUserAndPasswordKeys(credentials);
		WebElement submitButton = GeneralUIUtils.getDriver().findElement(By.name("btnSubmit"));
		submitButton.click();
		WebElement buttonOK = GeneralUIUtils.getDriver().findElement(By.name("successOK"));
		AssertJUnit.assertTrue(buttonOK.isDisplayed());
		buttonOK.click();
		System.out.println("Entering to design studio");
		Thread.sleep(2000);
		WebElement enterToUserWorkspaceButton = GeneralUIUtils.getDriver()
				.findElement(By.xpath("//button[@data-tests-id='Design Studio']"));
		enterToUserWorkspaceButton.click();
	}

	private void sendUserAndPasswordKeys(UserCredentials userId) {
		System.out.println("Login to system with user : " + userId.getUserId());
		WebElement userNameTextbox = GeneralUIUtils.getDriver().findElement(By.name("userid"));
		userNameTextbox.sendKeys(userId.getUserId());
		WebElement passwordTextbox = GeneralUIUtils.getDriver().findElement(By.name("password"));
		passwordTextbox.sendKeys(userId.getPassword());
	}

	public String getUrl() {
		String url;
		final CleanTypeEnum workMode = getCleanMode();
		switch (workMode) {
		case FULL: {
			url = devUrl;
			break;
		}
		case PARTIAL: {
			url = devUrl;
			break;
		}
		case NONE: {
			url = cdUrl;
			break;
		}
		default: {
			throw new NotImplementedException(workMode.name());
		}

		}
		return url;
	}

	public static void setDevUrl(UserRoleEnum role) {
		String url = SetupCDTest.devUrl;
		switch (role) {
		case ADMIN: {
			url = "http://localhost:8181/sdc1/proxy-admin1#/dashboard";
			break;
		}
		case DESIGNER: {
			url = "http://localhost:8181/sdc1/proxy-designer1#/dashboard";
			// url = "http://localhost:9000/#/dashboard";
			break;
		}
		case GOVERNOR: {
			url = "http://localhost:8181/sdc1/proxy-governor1#/dashboard";
			break;
		}
		case OPS: {
			url = "http://localhost:8181/sdc1/proxy-ops1#/dashboard";
			break;
		}
		case TESTER: {
			url = "http://localhost:8181/sdc1/proxy-tester1#/dashboard";
			break;
		}
		default: {
			break;
		}
		}
		SetupCDTest.devUrl = url;
	}

	public static Config getConfig() {
		return config;
	}

	private User user;

	public void loginWithUser(UserRoleEnum role) {

		setUser(role);
		String url = getUrl();
		System.out.println("URL is : " + url);
		try {
			navigateToUrl(url);
			if (url.contains("https://www.e-access.att.com")) {
				System.out.println("going to update designer user to mechIDs form...");
				UserCredentials credentials = getUserCredentialsFromFile(role.name().toLowerCase());
				loginToSystem(credentials);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private void setUser(UserRoleEnum role) {
		user = new User();
		user.setUserId(role.getUserId());
		user.setFirstName(role.getFirstName());
		user.setRole(role.name());
	}

	/**
	 * Current User Role
	 * 
	 * @return
	 */
	public UserRoleEnum getRole() {
		return UserRoleEnum.DESIGNER;
	}

	/**
	 * To change clean type update configuration.<br>
	 * Do not override this method.
	 * 
	 * @return
	 */
	protected final CleanTypeEnum getCleanMode() {
		return cleanType;
	}

	public User getUser() {
		return user;
	}

	protected void quitAndReLogin(UserRoleEnum role) {
		quitAfterTest();
		setBrowserBeforeTest(role);
		GeneralUIUtils.waitForLoader(30);
	}

	public TitanSnapshot getSnapshot() {
		return snapshot;
	}

	public void setSnapshot(TitanSnapshot snapshot) {
		this.snapshot = snapshot;
	}

	public static TitanGraph getTitanGraph() {
		return titanGraph;
	}

}