Skip to content

Commit 4a4d9f7

Browse files
Refactor functional tests (#217)
* Split long test methods into smaller pieces
1 parent a4a902f commit 4a4d9f7

13 files changed

+504
-482
lines changed

src/functionaltest/java/com/ericsson/ei/frontend/TestAlarm.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,50 @@
55
import java.io.IOException;
66

77
import org.apache.http.impl.client.CloseableHttpClient;
8+
import org.junit.Before;
89
import org.junit.Test;
910
import org.openqa.selenium.By;
10-
import org.openqa.selenium.JavascriptExecutor;
1111
import org.springframework.boot.test.mock.mockito.MockBean;
1212

13-
import com.ericsson.ei.frontend.pageobjects.IndexPage;
1413
import com.ericsson.ei.frontend.pageobjects.TestRulesPage;
1514

1615
public class TestAlarm extends SeleniumBaseClass {
1716

1817
@MockBean
1918
protected CloseableHttpClient mockedHttpClient;
2019

20+
private TestRulesPage testRulesPage;
21+
22+
@Before
23+
public void before() throws IOException {
24+
initBaseMocks(mockedHttpClient);
25+
testRulesPage = new TestRulesPage(null, driver, baseUrl);
26+
testRulesPage.loadPage();
27+
}
28+
29+
/**
30+
* This test checks that the alarm functionality is able to log error messages.
31+
*
32+
* @throws IOException
33+
*/
2134
@Test
2235
public void testAlarm() throws IOException {
23-
initBaseMocks(mockedHttpClient);
24-
JavascriptExecutor js = (JavascriptExecutor) driver;
36+
enableTestRulesButtons();
37+
clickTestRulesButtons();
38+
verifyAlarmFunctionality();
39+
}
2540

26-
//Load index page
27-
IndexPage indexPageObject = new IndexPage(null, driver, baseUrl);
28-
indexPageObject.loadPage();
41+
private void enableTestRulesButtons() {
42+
driver.executeScript("$('button.btn').prop(\"disabled\", false);");
43+
}
2944

30-
//Generate exception
31-
TestRulesPage testRulesPage = indexPageObject.clickTestRulesPage();
32-
//Enable buttons
33-
js.executeScript("$('button.btn').prop(\"disabled\", false);");
45+
private void clickTestRulesButtons() {
3446
testRulesPage.clickRemoveRuleNumber(0);
3547
testRulesPage.clickRemoveEventNumber(0);
48+
}
3649

37-
//Click alarm button few times
38-
for (int i = 0; i < 5; i++) {
39-
indexPageObject.clickAlarmButton();
40-
}
50+
private void verifyAlarmFunctionality() {
51+
testRulesPage.clickAlarmButton();
4152
assertTrue(driver.findElements(By.cssSelector(".alert-list .dropdown-item")).size() >= 2);
4253
}
4354
}

src/functionaltest/java/com/ericsson/ei/frontend/TestBridgeCurlFunctions.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,11 @@ public class TestBridgeCurlFunctions extends TestBaseClass {
3737
@Before
3838
public void before() throws Exception {
3939
backEndInstancesUtils.setDefaultBackEndInstance("test", BASE_URL, mockServer1.getLocalPort(), "", true);
40-
41-
mockClient1.when(request().withMethod("GET")).respond(response().withStatusCode(200));
42-
mockClient1.when(request().withMethod("POST")).respond(response().withStatusCode(200));
43-
mockClient1.when(request().withMethod("PUT")).respond(response().withStatusCode(200));
44-
45-
mockClient2.when(request().withMethod("GET")).respond(response().withStatusCode(200));
46-
mockClient2.when(request().withMethod("POST")).respond(response().withStatusCode(200));
47-
mockClient2.when(request().withMethod("PUT")).respond(response().withStatusCode(200));
40+
setupMockEndpoints();
4841
}
4942

5043
/**
51-
* This test does a normal request to the bridge and ensures it uses the
52-
* default specified back end.
44+
* This test does a normal request to the bridge and ensures it uses the default specified back end.
5345
*
5446
* @throws Exception
5547
*/
@@ -65,24 +57,21 @@ public void testHandleSubscriptionsDefaultBackEnd() throws Exception {
6557
mockClient1.verify(request().withMethod("GET").withPath(SUBSCRIPTION_ENDPOINT));
6658

6759
mockClient2.verifyZeroInteractions();
68-
6960
}
7061

7162
/**
72-
* This test ensures that a back end specified by a URL parameter is
73-
* overriding the default back end and uses the input one instead.
63+
* This test ensures that a back end specified by a URL parameter is overriding the default back end and uses the input one instead.
7464
*
7565
* @throws Exception
7666
*/
7767
@Test
7868
public void testHandleSubscriptionsUserSpecifiedBackend() throws Exception {
7969
URIBuilder builder = new URIBuilder();
80-
builder.setParameter(BACKEND_PARAM, mockServer2Url).setPath(SUBSCRIPTION_ENDPOINT).setHost(BASE_URL)
81-
.setPort(testServerPort).setScheme("http");
70+
builder.setParameter(BACKEND_PARAM, mockServer2Url).setPath(SUBSCRIPTION_ENDPOINT).setHost(BASE_URL).setPort(testServerPort)
71+
.setScheme("http");
8272

8373
/**
84-
* Since MockMvc has problem including real parameters in a request we
85-
* use a real http request and sends this request to the bridge.
74+
* Since MockMvc has problem including real parameters in a request we use a real http request and sends this request to the bridge.
8675
*/
8776
HttpClientBuilder.create().build().execute(new HttpGet(builder.toString()));
8877
mockClient2.verify(request().withMethod("GET").withPath(SUBSCRIPTION_ENDPOINT));
@@ -118,4 +107,14 @@ public static void tearDownMocks() throws IOException {
118107
mockClient1.stop();
119108
mockClient2.stop();
120109
}
110+
111+
private void setupMockEndpoints() {
112+
mockClient1.when(request().withMethod("GET")).respond(response().withStatusCode(200));
113+
mockClient1.when(request().withMethod("POST")).respond(response().withStatusCode(200));
114+
mockClient1.when(request().withMethod("PUT")).respond(response().withStatusCode(200));
115+
116+
mockClient2.when(request().withMethod("GET")).respond(response().withStatusCode(200));
117+
mockClient2.when(request().withMethod("POST")).respond(response().withStatusCode(200));
118+
mockClient2.when(request().withMethod("PUT")).respond(response().withStatusCode(200));
119+
}
121120
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.ericsson.ei.frontend;
2+
3+
import java.io.IOException;
4+
5+
import org.apache.http.impl.client.CloseableHttpClient;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.springframework.boot.test.mock.mockito.MockBean;
9+
10+
import com.ericsson.ei.frontend.pageobjects.IndexPage;
11+
12+
public class TestNavigation extends SeleniumBaseClass {
13+
14+
@MockBean
15+
protected CloseableHttpClient mockedHttpClient;
16+
17+
private IndexPage indexPage;
18+
19+
@Before
20+
public void before() {
21+
indexPage = new IndexPage(mockedHttpClient, driver, baseUrl);
22+
indexPage.loadPage();
23+
}
24+
25+
@Test
26+
public void testPageNavigationButtons() throws IOException {
27+
indexPage.clickTestRulesPage();
28+
indexPage.clickEiInfoBtn();
29+
indexPage.clickAdminBackendInstancesBtn();
30+
indexPage.clickAddBackendInstanceBtn();
31+
indexPage.clickSwitchBackendInstanceBtn();
32+
indexPage.clickSubscriptionPage();
33+
}
34+
}

src/functionaltest/java/com/ericsson/ei/frontend/TestRulesFunctionality.java

Lines changed: 92 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@
1212

1313
import org.apache.http.impl.client.CloseableHttpClient;
1414
import org.junit.AfterClass;
15+
import org.junit.Before;
1516
import org.junit.BeforeClass;
1617
import org.junit.Test;
1718
import org.mockserver.client.MockServerClient;
1819
import org.mockserver.integration.ClientAndServer;
19-
import org.openqa.selenium.JavascriptExecutor;
2020
import org.openqa.selenium.support.ui.WebDriverWait;
2121
import org.springframework.boot.test.mock.mockito.MockBean;
2222

2323
import com.ericsson.ei.config.SeleniumConfig;
24-
import com.ericsson.ei.frontend.pageobjects.IndexPage;
2524
import com.ericsson.ei.frontend.pageobjects.TestRulesPage;
2625

2726
public class TestRulesFunctionality extends SeleniumBaseClass {
@@ -30,113 +29,123 @@ public class TestRulesFunctionality extends SeleniumBaseClass {
3029
private static ClientAndServer mockServer;
3130
private static final String BASE_URL = "localhost";
3231

33-
private static final String DOWNLOADED_RULES_TEMPLATE_FILE_PATH = String.join(
34-
File.separator, SeleniumConfig.getTempDownloadDirectory().getPath(), "rulesTemplate.json");
35-
private static final String RULES_TEMPLATE_FILE_PATH = String.join(
36-
File.separator, "src", "functionaltest", "resources", "responses", "RulesTemplateObject.json");
37-
private static final String DOWNLOADED_RULES_FILE_PATH = String.join(
38-
File.separator, SeleniumConfig.getTempDownloadDirectory().getPath(), "rules.json");
39-
private static final String DOWNLOADED_EVENTS_TEMPLATE_FILE_PATH = String.join(
40-
File.separator, SeleniumConfig.getTempDownloadDirectory().getPath(), "eventsTemplate.json");
41-
private static final String EVENTS_TEMPLATE_FILE_PATH = String.join(
42-
File.separator, "src", "functionaltest", "resources", "responses", "EventsTemplateObject.json");
43-
private static final String AGGREGATED_OBJECT_FILE_PATH = String.join(
44-
File.separator, "src", "functionaltest", "resources", "responses", "AggregatedObjectResponse.json");
32+
private static final String DOWNLOADED_RULES_TEMPLATE_FILE_PATH = String.join(File.separator, SeleniumConfig.getTempDownloadDirectory().getPath(),
33+
"rulesTemplate.json");
34+
private static final String RULES_TEMPLATE_FILE_PATH = String.join(File.separator, "src", "functionaltest", "resources", "responses",
35+
"RulesTemplateObject.json");
36+
private static final String DOWNLOADED_RULES_FILE_PATH = String.join(File.separator, SeleniumConfig.getTempDownloadDirectory().getPath(),
37+
"rules.json");
38+
private static final String DOWNLOADED_EVENTS_TEMPLATE_FILE_PATH = String.join(File.separator,
39+
SeleniumConfig.getTempDownloadDirectory().getPath(), "eventsTemplate.json");
40+
private static final String EVENTS_TEMPLATE_FILE_PATH = String.join(File.separator, "src", "functionaltest", "resources", "responses",
41+
"EventsTemplateObject.json");
42+
private static final String AGGREGATED_OBJECT_FILE_PATH = String.join(File.separator, "src", "functionaltest", "resources", "responses",
43+
"AggregatedObjectResponse.json");
4544

4645
@MockBean
4746
protected CloseableHttpClient mockedHttpClient;
4847

49-
@Test
50-
public void testJourneyToFindAggregatedObjectButton() throws Exception {
51-
// Set up
52-
int portServer = mockServer.getLocalPort();
53-
backEndInstancesUtils.setDefaultBackEndInstanceToNull();
54-
backEndInstancesUtils.setDefaultBackEndInstance("new_instance_default", "localhost", portServer, "", true);
55-
JavascriptExecutor js = driver;
48+
private TestRulesPage testRulesPage;
49+
private String downloadedRulesTemplate = "";
50+
private String downloadedEventsTemplate = "";
5651

57-
// Open indexpage and verify that it is opened
58-
IndexPage indexPageObject = new IndexPage(null, driver, baseUrl);
59-
indexPageObject.loadPage();
60-
61-
// Verify that we can navigate to test rules page
62-
TestRulesPage testRulesPage = indexPageObject.clickTestRulesPage();
63-
assert(new WebDriverWait(driver, 10).until((webdriver) -> testRulesPage.presenceOfTestRulesHeader()));
64-
65-
//Enable buttons
66-
js.executeScript("$('button.btn').prop(\"disabled\", false);");
52+
@Before
53+
public void before() throws IOException {
54+
int serverPort = mockServer.getLocalPort();
55+
backEndInstancesUtils.setDefaultBackEndInstanceToNull();
56+
backEndInstancesUtils.setDefaultBackEndInstance("new_instance_default", "localhost", serverPort, "", true);
57+
testRulesPage = new TestRulesPage(null, driver, baseUrl);
58+
testRulesPage.loadPage();
59+
}
6760

68-
// Verify that "download rules template" button works
69-
String downloadedRulesTemplate = "";
70-
String mockedResponse = getJSONStringFromFile(RULES_TEMPLATE_FILE_PATH);
71-
mockClient.when(request().withMethod("GET").withPath("/download/rulesTemplate"))
72-
.respond(response().withStatusCode(200).withBody(mockedResponse));
61+
@Test
62+
public void testJourneyToFindAggregatedObjectButton() throws Exception {
63+
enableTestRulesButtons();
64+
verifyDownloadRulesTemplateButton();
65+
verifyUploadRulesFile();
66+
verifyDownloadRulesButton();
67+
verifyAddAndRemoveRuleButton();
68+
verifyDownloadEventsTemplateButton();
69+
verifyUploadEventsFile();
70+
verifyAddAndRemoveEventButton();
71+
verifyAggregatedObjectButton();
72+
}
7373

74-
testRulesPage.clickDownloadRulesTemplate();
75-
new WebDriverWait(driver, 10).until((webdriver) -> Files.exists(Paths.get(DOWNLOADED_RULES_TEMPLATE_FILE_PATH)));
76-
downloadedRulesTemplate = getJSONStringFromFile(DOWNLOADED_RULES_TEMPLATE_FILE_PATH);
74+
@BeforeClass
75+
public static void setUpMocks() throws IOException {
76+
mockServer = startClientAndServer();
77+
mockClient = new MockServerClient(BASE_URL, mockServer.getLocalPort());
78+
}
7779

78-
assertEquals(mockedResponse, downloadedRulesTemplate);
80+
@AfterClass
81+
public static void tearDownMocks() throws IOException {
82+
mockClient.stop();
83+
}
7984

80-
// Verify that uploading the downloaded template file works.
81-
testRulesPage.uploadRulesTemplate(DOWNLOADED_RULES_TEMPLATE_FILE_PATH);
82-
String firstRule = testRulesPage.getFirstRuleText();
83-
assertEquals(true, downloadedRulesTemplate.contains(firstRule));
85+
private void verifyAggregatedObjectButton() throws IOException {
86+
String findAggregatedObjectResponse = getJSONStringFromFile(AGGREGATED_OBJECT_FILE_PATH);
87+
mockClient.when(request().withMethod("POST").withPath("/rules/rule-check/aggregation"))
88+
.respond(response().withStatusCode(200).withBody(findAggregatedObjectResponse));
8489

85-
// Verify that it is possible to download rules
86-
mockClient.when(request().withMethod("GET").withPath("/download/rules"))
87-
.respond(response().withStatusCode(200).withBody(downloadedRulesTemplate));
88-
testRulesPage.clickDownloadRulesButton();
89-
new WebDriverWait(driver, 10).until((webdriver) -> Files.exists(Paths.get(DOWNLOADED_RULES_FILE_PATH)));
90-
String downloadedRules = getJSONStringFromFile(DOWNLOADED_RULES_FILE_PATH);
91-
assertEquals(downloadedRulesTemplate, downloadedRules);
90+
testRulesPage.clickFindAggregatedObject();
91+
assertEquals(findAggregatedObjectResponse, testRulesPage.getAggregatedResultData());
92+
}
9293

93-
// Verify that add rule button works
94-
testRulesPage.clickAddRuleButton();
94+
private void verifyAddAndRemoveEventButton() {
95+
testRulesPage.clickAddEventButton();
96+
testRulesPage.clickRemoveEventNumber(3);
97+
assert (new WebDriverWait(driver, 10).until((webdriver) -> !testRulesPage.presenceOfEventNumber(3)));
98+
}
9599

96-
// Verify that removing a rule works
97-
testRulesPage.clickRemoveRuleNumber(3);
98-
assert(new WebDriverWait(driver, 10).until((webdriver) -> !testRulesPage.presenceOfRuleNumber(3)));
100+
private void verifyUploadEventsFile() {
101+
testRulesPage.uploadEventsTemplate(DOWNLOADED_EVENTS_TEMPLATE_FILE_PATH);
102+
String firstEvent = testRulesPage.getFirstEventText();
103+
assertEquals(true, downloadedEventsTemplate.contains(firstEvent));
104+
}
99105

100-
// Verify that "download events template" button works
106+
private void verifyDownloadEventsTemplateButton() throws IOException {
101107
String downloadEventsTemplateMockedResponse = getJSONStringFromFile(EVENTS_TEMPLATE_FILE_PATH);
102108
mockClient.when(request().withMethod("GET").withPath("/download/eventsTemplate"))
103109
.respond(response().withStatusCode(200).withBody(downloadEventsTemplateMockedResponse));
104110

105111
testRulesPage.clickDownloadEventsTemplate();
106112
new WebDriverWait(driver, 10).until((webdriver) -> Files.exists(Paths.get(DOWNLOADED_EVENTS_TEMPLATE_FILE_PATH)));
107-
String downloadedEventsTemplate = getJSONStringFromFile(DOWNLOADED_EVENTS_TEMPLATE_FILE_PATH);
113+
downloadedEventsTemplate = getJSONStringFromFile(DOWNLOADED_EVENTS_TEMPLATE_FILE_PATH);
108114
assertEquals(downloadEventsTemplateMockedResponse, downloadedEventsTemplate);
115+
}
109116

110-
// Verify that uploading the downloaded template file works.
111-
testRulesPage.uploadEventsTemplate(DOWNLOADED_EVENTS_TEMPLATE_FILE_PATH);
112-
String firstEvent = testRulesPage.getFirstEventText();
113-
assertEquals(true, downloadedEventsTemplate.contains(firstEvent));
114-
115-
// Verify that add rule button works
116-
testRulesPage.clickAddEventButton();
117-
118-
// Verify that removing a rule works
119-
testRulesPage.clickRemoveEventNumber(3);
120-
assert(new WebDriverWait(driver, 10).until((webdriver) -> !testRulesPage.presenceOfEventNumber(3)));
121-
122-
// Verify that find aggregated object button works
123-
String findAggregatedObjectResponse = getJSONStringFromFile(AGGREGATED_OBJECT_FILE_PATH);
124-
mockClient.when(request().withMethod("POST").withPath("/rules/rule-check/aggregation"))
125-
.respond(response().withStatusCode(200).withBody(findAggregatedObjectResponse));
117+
private void verifyAddAndRemoveRuleButton() {
118+
testRulesPage.clickAddRuleButton();
119+
testRulesPage.clickRemoveRuleNumber(3);
120+
assert (new WebDriverWait(driver, 10).until((webdriver) -> !testRulesPage.presenceOfRuleNumber(3)));
121+
}
126122

127-
testRulesPage.clickFindAggregatedObject();
128-
assertEquals(findAggregatedObjectResponse, testRulesPage.getAggregatedResultData());
123+
private void verifyDownloadRulesButton() throws IOException {
124+
mockClient.when(request().withMethod("GET").withPath("/download/rules"))
125+
.respond(response().withStatusCode(200).withBody(downloadedRulesTemplate));
126+
testRulesPage.clickDownloadRulesButton();
127+
new WebDriverWait(driver, 10).until((webdriver) -> Files.exists(Paths.get(DOWNLOADED_RULES_FILE_PATH)));
128+
String downloadedRules = getJSONStringFromFile(DOWNLOADED_RULES_FILE_PATH);
129+
assertEquals(downloadedRulesTemplate, downloadedRules);
129130
}
130131

131-
@BeforeClass
132-
public static void setUpMocks() throws IOException {
133-
mockServer = startClientAndServer();
134-
mockClient = new MockServerClient(BASE_URL, mockServer.getLocalPort());
132+
private void verifyUploadRulesFile() {
133+
testRulesPage.uploadRulesTemplate(DOWNLOADED_RULES_TEMPLATE_FILE_PATH);
134+
String firstRule = testRulesPage.getFirstRuleText();
135+
assertEquals(true, downloadedRulesTemplate.contains(firstRule));
135136
}
136137

137-
@AfterClass
138-
public static void tearDownMocks() throws IOException {
139-
mockClient.stop();
138+
private void verifyDownloadRulesTemplateButton() throws IOException {
139+
String mockedResponse = getJSONStringFromFile(RULES_TEMPLATE_FILE_PATH);
140+
mockClient.when(request().withMethod("GET").withPath("/download/rulesTemplate"))
141+
.respond(response().withStatusCode(200).withBody(mockedResponse));
142+
testRulesPage.clickDownloadRulesTemplate();
143+
new WebDriverWait(driver, 10).until((webdriver) -> Files.exists(Paths.get(DOWNLOADED_RULES_TEMPLATE_FILE_PATH)));
144+
downloadedRulesTemplate = getJSONStringFromFile(DOWNLOADED_RULES_TEMPLATE_FILE_PATH);
145+
assertEquals(mockedResponse, downloadedRulesTemplate);
140146
}
141147

148+
private void enableTestRulesButtons() {
149+
driver.executeScript("$('button.btn').prop(\"disabled\", false);");
150+
}
142151
}

0 commit comments

Comments
 (0)