1
1
package com .ericsson .ei .frontend ;
2
2
3
3
import static org .junit .Assert .assertEquals ;
4
+ import static org .mockserver .integration .ClientAndServer .startClientAndServer ;
5
+ import static org .mockserver .model .HttpRequest .request ;
6
+ import static org .mockserver .model .HttpResponse .response ;
4
7
5
8
import java .io .File ;
9
+ import java .io .IOException ;
6
10
import java .nio .file .Files ;
7
11
import java .nio .file .Paths ;
8
12
9
13
import org .apache .http .impl .client .CloseableHttpClient ;
14
+ import org .junit .AfterClass ;
15
+ import org .junit .BeforeClass ;
10
16
import org .junit .Test ;
17
+ import org .mockserver .client .MockServerClient ;
18
+ import org .mockserver .integration .ClientAndServer ;
11
19
import org .openqa .selenium .support .ui .WebDriverWait ;
12
20
import org .springframework .boot .test .mock .mockito .MockBean ;
13
21
17
25
18
26
public class TestRulesFunctionality extends SeleniumBaseClass {
19
27
28
+ private static MockServerClient mockClient ;
29
+ private static ClientAndServer mockServer ;
30
+ private static final String BASE_URL = "localhost" ;
31
+
20
32
private static final String DOWNLOADED_RULES_TEMPLATE_FILE_PATH = String .join (
21
33
File .separator , SeleniumConfig .getTempDownloadDirectory ().getPath (), "rulesTemplate.json" );
22
34
private static final String RULES_TEMPLATE_FILE_PATH = String .join (
@@ -35,9 +47,13 @@ public class TestRulesFunctionality extends SeleniumBaseClass {
35
47
36
48
@ Test
37
49
public void testJourneyToFindAggregatedObjectButton () throws Exception {
38
- initBaseMocks (mockedHttpClient );
39
- // Load index page and wait for it to load
40
- IndexPage indexPageObject = new IndexPage (mockedHttpClient , driver , baseUrl );
50
+ // Set up
51
+ int portServer = mockServer .getLocalPort ();
52
+ backEndInstancesUtils .setDefaultBackEndInstanceToNull ();
53
+ backEndInstancesUtils .setDefaultBackEndInstance ("new_instance_default" , "localhost" , portServer , "" , true );
54
+
55
+ // Open indexpage and verify that it is opened
56
+ IndexPage indexPageObject = new IndexPage (null , driver , baseUrl );
41
57
indexPageObject .loadPage ();
42
58
43
59
// Verify that we can navigate to test rules page
@@ -47,15 +63,12 @@ public void testJourneyToFindAggregatedObjectButton() throws Exception {
47
63
// Verify that "download rules template" button works
48
64
String downloadedRulesTemplate = "" ;
49
65
String mockedResponse = getJSONStringFromFile (RULES_TEMPLATE_FILE_PATH );
66
+ mockClient .when (request ().withMethod ("GET" ).withPath ("/download/rulesTemplate" ))
67
+ .respond (response ().withStatusCode (200 ).withBody (mockedResponse ));
50
68
51
- // On windows this may require more then one try due to timing issues.
52
- int i = 0 ;
53
- while (!downloadedRulesTemplate .equals (mockedResponse ) && i <= 5 ) {
54
- i ++;
55
- testRulesPage .clickDownloadRulesTemplate (mockedResponse );
56
- new WebDriverWait (driver , 10 ).until ((webdriver ) -> Files .exists (Paths .get (DOWNLOADED_RULES_TEMPLATE_FILE_PATH )));
57
- downloadedRulesTemplate = getJSONStringFromFile (DOWNLOADED_RULES_TEMPLATE_FILE_PATH );
58
- }
69
+ testRulesPage .clickDownloadRulesTemplate ();
70
+ new WebDriverWait (driver , 10 ).until ((webdriver ) -> Files .exists (Paths .get (DOWNLOADED_RULES_TEMPLATE_FILE_PATH )));
71
+ downloadedRulesTemplate = getJSONStringFromFile (DOWNLOADED_RULES_TEMPLATE_FILE_PATH );
59
72
60
73
assertEquals (mockedResponse , downloadedRulesTemplate );
61
74
@@ -65,6 +78,8 @@ public void testJourneyToFindAggregatedObjectButton() throws Exception {
65
78
assertEquals (true , downloadedRulesTemplate .contains (firstRule ));
66
79
67
80
// Verify that it is possible to download rules
81
+ mockClient .when (request ().withMethod ("GET" ).withPath ("/download/rules" ))
82
+ .respond (response ().withStatusCode (200 ).withBody (downloadedRulesTemplate ));
68
83
testRulesPage .clickDownloadRulesButton ();
69
84
new WebDriverWait (driver , 10 ).until ((webdriver ) -> Files .exists (Paths .get (DOWNLOADED_RULES_FILE_PATH )));
70
85
String downloadedRules = getJSONStringFromFile (DOWNLOADED_RULES_FILE_PATH );
@@ -79,7 +94,10 @@ public void testJourneyToFindAggregatedObjectButton() throws Exception {
79
94
80
95
// Verify that "download events template" button works
81
96
String downloadEventsTemplateMockedResponse = getJSONStringFromFile (EVENTS_TEMPLATE_FILE_PATH );
82
- testRulesPage .clickDownloadEventsTemplate (downloadEventsTemplateMockedResponse );
97
+ mockClient .when (request ().withMethod ("GET" ).withPath ("/download/eventsTemplate" ))
98
+ .respond (response ().withStatusCode (200 ).withBody (downloadEventsTemplateMockedResponse ));
99
+
100
+ testRulesPage .clickDownloadEventsTemplate ();
83
101
new WebDriverWait (driver , 10 ).until ((webdriver ) -> Files .exists (Paths .get (DOWNLOADED_EVENTS_TEMPLATE_FILE_PATH )));
84
102
String downloadedEventsTemplate = getJSONStringFromFile (DOWNLOADED_EVENTS_TEMPLATE_FILE_PATH );
85
103
assertEquals (downloadEventsTemplateMockedResponse , downloadedEventsTemplate );
@@ -98,7 +116,23 @@ public void testJourneyToFindAggregatedObjectButton() throws Exception {
98
116
99
117
// Verify that find aggregated object button works
100
118
String findAggregatedObjectResponse = getJSONStringFromFile (AGGREGATED_OBJECT_FILE_PATH );
101
- testRulesPage .clickFindAggregatedObject (findAggregatedObjectResponse );
119
+ mockClient .when (request ().withMethod ("POST" ).withPath ("/rules/rule-check/aggregation" ))
120
+ .respond (response ().withStatusCode (200 ).withBody (findAggregatedObjectResponse ));
121
+
122
+ testRulesPage .clickFindAggregatedObject ();
102
123
assertEquals (findAggregatedObjectResponse , testRulesPage .getAggregatedResultData ());
103
124
}
125
+
126
+ @ BeforeClass
127
+ public static void setUpMocks () throws IOException {
128
+ mockServer = startClientAndServer ();
129
+ mockClient = new MockServerClient (BASE_URL , mockServer .getLocalPort ());
130
+ }
131
+
132
+ @ AfterClass
133
+ public static void tearDownMocks () throws IOException {
134
+ mockClient .stop ();
135
+ mockServer .stop ();
136
+ }
137
+
104
138
}
0 commit comments