2
2
3
3
import com .ericsson .ei .controller .RuleCheckController ;
4
4
import com .ericsson .ei .utils .FunctionalTestBase ;
5
+ import com .ericsson .ei .utils .HttpRequest ;
6
+ import com .ericsson .ei .utils .HttpRequest .HttpMethod ;
7
+
5
8
import cucumber .api .java .en .Given ;
6
9
import cucumber .api .java .en .Then ;
7
10
import cucumber .api .java .en .When ;
11
+
8
12
import org .apache .commons .io .FileUtils ;
9
13
import org .json .JSONArray ;
10
14
import org .json .JSONObject ;
15
+ import org .json .JSONTokener ;
11
16
import org .junit .Ignore ;
12
17
import org .springframework .beans .factory .annotation .Autowired ;
13
- import org .springframework .boot .test . autoconfigure . web .servlet . AutoConfigureMockMvc ;
14
- import org .springframework .http .MediaType ;
18
+ import org .springframework .boot .web .server . LocalServerPort ;
19
+ import org .springframework .http .ResponseEntity ;
15
20
import org .springframework .test .util .ReflectionTestUtils ;
16
- import org .springframework .test .web .servlet .MockMvc ;
17
- import org .springframework .test .web .servlet .MvcResult ;
18
-
19
21
import java .io .File ;
20
-
21
22
import static org .junit .Assert .assertEquals ;
22
23
import static org .skyscreamer .jsonassert .JSONAssert .assertEquals ;
23
- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
24
24
25
25
@ Ignore
26
- @ AutoConfigureMockMvc
27
26
public class RuleCheckSteps extends FunctionalTestBase {
28
27
29
28
private static final String TEST_RESOURCES_PATH = "src/test/resources" ;
30
29
31
30
@ Autowired
32
31
private RuleCheckController ruleCheckController ;
33
32
34
- @ Autowired
35
- private MockMvc mockMvc ;
36
-
37
- private MvcResult mvcResult ;
38
-
39
33
private String rules ;
40
34
private String events ;
41
35
36
+ @ LocalServerPort
37
+ private int applicationPort ;
38
+ private String hostName = getHostName ();
39
+ private ResponseEntity response ;
40
+
42
41
@ Given ("^rules checking is enabled$" )
43
42
public void rules_checking_is_enabled () throws Throwable {
44
43
ReflectionTestUtils .setField (ruleCheckController , "testEnable" , true );
@@ -57,12 +56,15 @@ public void file_with_JMESPath_rules_and_file_with_events(String rulesFileName,
57
56
58
57
@ When ("^make a POST request to the REST API \" ([^\" ]*)\" with request parameter \" ([^\" ]*)\" $" )
59
58
public void make_a_POST_request_to_the_REST_API_with_request_parameter (String endpoint , String requestParam ) throws Throwable {
60
- mvcResult = mockMvc .perform (post (endpoint )
61
- .param (requestParam , rules )
62
- .accept (MediaType .APPLICATION_JSON )
63
- .content (events )
64
- .contentType (MediaType .APPLICATION_JSON ))
65
- .andReturn ();
59
+ HttpRequest postRequest = new HttpRequest (HttpMethod .POST );
60
+ response = postRequest .setPort (applicationPort )
61
+ .setHost (hostName )
62
+ .setHeaders ("content-type" , "application/json" )
63
+ .setHeaders ("Accept" , "application/json" )
64
+ .setEndpoint (endpoint )
65
+ .setBody (events )
66
+ .setParam (requestParam , rules )
67
+ .performRequest ();
66
68
}
67
69
68
70
@ When ("^make a POST request to the REST API \" ([^\" ]*)\" $" )
@@ -71,22 +73,36 @@ public void make_a_POST_request_to_the_REST_API(String endpoint) throws Throwabl
71
73
.put ("listRulesJson" , new JSONArray (rules ))
72
74
.put ("listEventsJson" , new JSONArray (events ))
73
75
.toString ();
74
- mvcResult = mockMvc .perform (post (endpoint )
75
- .accept (MediaType .APPLICATION_JSON )
76
- .content (requestBody )
77
- .contentType (MediaType .APPLICATION_JSON ))
78
- .andReturn ();
76
+
77
+ HttpRequest postRequest = new HttpRequest (HttpMethod .POST );
78
+ response = postRequest .setPort (applicationPort )
79
+ .setHost (hostName )
80
+ .setHeaders ("content-type" , "application/json" )
81
+ .setHeaders ("Accept" , "application/json" )
82
+ .setEndpoint (endpoint )
83
+ .setBody (requestBody )
84
+ .performRequest ();
79
85
}
80
86
81
87
@ Then ("^get response code of (\\ d+)$" )
82
88
public void get_response_code_of (int statusCode ) throws Throwable {
83
- assertEquals (statusCode , mvcResult . getResponse (). getStatus ());
89
+ assertEquals (statusCode , response . getStatusCodeValue ());
84
90
}
85
91
86
92
@ Then ("^get content \" ([^\" ]*)\" $" )
87
93
public void get_content (String contentFileName ) throws Throwable {
88
94
String responseBody = FileUtils .readFileToString (new File (TEST_RESOURCES_PATH + contentFileName ), "UTF-8" );
89
- assertEquals (responseBody , mvcResult .getResponse ().getContentAsString (), true );
95
+
96
+ Object expectedResponse = new JSONTokener (responseBody ).nextValue ();
97
+ if (expectedResponse instanceof JSONArray ) {
98
+ JSONArray expectedArray = new JSONArray (responseBody );
99
+ JSONArray responseArray = new JSONArray (response .getBody ().toString ());
100
+ assertEquals (expectedArray , responseArray , true );
101
+ } else {
102
+ JSONObject expectedObject = new JSONObject (responseBody );
103
+ JSONObject responseObject = new JSONObject (response .getBody ().toString ());
104
+ assertEquals (expectedObject , responseObject , true );
105
+ }
90
106
}
91
107
92
108
}
0 commit comments