2
2
3
3
import com .ericsson .ei .controller .model .Subscription ;
4
4
import com .ericsson .ei .utils .FunctionalTestBase ;
5
+ import com .ericsson .ei .utils .HttpRequest ;
6
+ import com .ericsson .ei .utils .HttpRequest .HttpMethod ;
7
+ import com .fasterxml .jackson .databind .JsonNode ;
5
8
import com .fasterxml .jackson .databind .ObjectMapper ;
6
9
import cucumber .api .java .en .Given ;
7
10
import cucumber .api .java .en .Then ;
8
11
import cucumber .api .java .en .When ;
9
12
import org .apache .commons .io .FileUtils ;
10
13
import org .json .JSONArray ;
11
- import org .json .JSONObject ;
12
- import org .junit .Assert ;
13
14
import org .junit .Ignore ;
14
15
import org .slf4j .Logger ;
15
16
import org .slf4j .LoggerFactory ;
16
- import org .springframework .beans .factory .annotation .Autowired ;
17
17
import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
18
+ import org .springframework .boot .web .server .LocalServerPort ;
18
19
import org .springframework .http .HttpStatus ;
19
- import org .springframework .http .MediaType ;
20
- import org .springframework .test .web .servlet .MockMvc ;
21
- import org .springframework .test .web .servlet .MvcResult ;
22
- import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
20
+ import org .springframework .http .ResponseEntity ;
23
21
24
22
import java .io .File ;
25
23
@@ -34,87 +32,105 @@ public class SubscriptionCRUDSteps extends FunctionalTestBase {
34
32
private static final String SUBSCRIPTION_FILE_PATH = "src/functionaltests/resources/subscription_single.json" ;
35
33
private static final String SUBSCRIPTION_UPDATED_FILE_PATH = "src/functionaltests/resources/subscription_single_updated.json" ;
36
34
37
- @ Autowired
38
- private MockMvc mockMvc ;
35
+ @ LocalServerPort
36
+ private int applicationPort ;
37
+ private String hostName = getHostName ();
38
+ private HttpRequest httpRequest ;
39
+ private ResponseEntity <String > response ;
39
40
40
- private MvcResult result ;
41
41
private ObjectMapper mapper = new ObjectMapper ();
42
42
private static JSONArray jsonArray = null ;
43
43
44
44
@ Given ("^The REST API \" ([^\" ]*)\" is up and running$" )
45
45
public void the_REST_API_is_up_and_running (String endPoint ) throws Throwable {
46
- result = mockMvc .perform (MockMvcRequestBuilders .get (endPoint ).accept (MediaType .APPLICATION_JSON )).andReturn ();
47
- Assert .assertEquals (HttpStatus .OK .value (), result .getResponse ().getStatus ());
46
+ httpRequest = new HttpRequest (HttpMethod .GET );
47
+ httpRequest .setHost (hostName ).setPort (applicationPort ).setEndpoint (endPoint );
48
+ response = httpRequest .performRequest ();
49
+ assertEquals (HttpStatus .OK , response .getStatusCode ());
48
50
}
49
51
50
52
@ When ("^I make a POST request with valid \" ([^\" ]*)\" to the subscription REST API \" ([^\" ]*)\" $" )
51
- public void i_make_a_POST_request_with_valid_to_the_subscription_REST_API (String arg1 , String endPoint ) throws Throwable {
53
+ public void i_make_a_POST_request_with_valid_to_the_subscription_REST_API (String arg1 , String endPoint )
54
+ throws Throwable {
52
55
String readFileToString = FileUtils .readFileToString (new File (SUBSCRIPTION_FILE_PATH ), "UTF-8" );
53
56
jsonArray = new JSONArray (readFileToString );
54
- result = mockMvc .perform (MockMvcRequestBuilders .post (endPoint ).accept (MediaType .APPLICATION_JSON ).content (jsonArray .toString ())
55
- .contentType (MediaType .APPLICATION_JSON )).andReturn ();
57
+ httpRequest = new HttpRequest (HttpMethod .POST );
58
+ httpRequest .setHost (hostName ).setPort (applicationPort ).setEndpoint (endPoint )
59
+ .addHeader ("content-type" , "application/json" ).addHeader ("Accept" , "application/json" )
60
+ .setBody (jsonArray .toString ());
61
+ response = httpRequest .performRequest ();
56
62
}
57
63
58
- @ Then ("^I get response code of (\\ d+)$ " )
59
- public void i_get_response_code_of (int statusCode ) throws Throwable {
60
- Assert . assertEquals (statusCode , result . getResponse (). getStatus ());
64
+ @ Then ("^I get response code of (\\ d+)" )
65
+ public void i_get_response_code_of (int statusCode ) {
66
+ assertEquals (HttpStatus . valueOf ( statusCode ), response . getStatusCode ());
61
67
}
62
- ///Scenario:1 ends ============= ==================================================================
68
+ /// Scenario:1 ends ==================================================================
63
69
64
70
@ When ("^I make a GET request with subscription name \" ([^\" ]*)\" to the subscription REST API \" ([^\" ]*)\" $" )
65
- public void i_make_a_GET_request_with_subscription_name_to_the_subscription_REST_API (String name , String endPoint ) throws Throwable {
66
- result = mockMvc .perform (MockMvcRequestBuilders .get (endPoint ).accept (MediaType .APPLICATION_JSON )).andReturn ();
71
+ public void i_make_a_GET_request_with_subscription_name_to_the_subscription_REST_API (String name , String endPoint )
72
+ throws Throwable {
73
+ httpRequest = new HttpRequest (HttpMethod .GET );
74
+ httpRequest .setHost (hostName ).setPort (applicationPort ).setEndpoint (endPoint + name );
75
+ response = httpRequest .performRequest ();
67
76
}
68
77
69
- @ Then ("^I get response code of (\\ d+) and subscription name \" ([^\" ]*)\" $" )
70
- public void i_get_response_code_of_and_subscription_name (int statusCode , String name ) throws Throwable {
71
- Subscription [] subscription = mapper .readValue (result .getResponse ().getContentAsString (), Subscription [].class );
72
- Assert .assertEquals (statusCode , result .getResponse ().getStatus ());
73
- Assert .assertEquals (name , subscription [0 ].getSubscriptionName ());
78
+ @ Then ("^Subscription name is \" ([^\" ]*)\" $" )
79
+ public void i_get_response_code_of_and_subscription_name (String name ) throws Throwable {
80
+ JsonNode node = eventManager .getJSONFromString (response .getBody ());
81
+ String found = node .get ("foundSubscriptions" ).get (0 ).toString ();
82
+ Subscription subscription = mapper .readValue (found , Subscription .class );
83
+ assertEquals (name , subscription .getSubscriptionName ());
74
84
}
75
- // Scenario:2 ends======================= ==================================================================
85
+ // Scenario:2 ends ==================================================================
76
86
77
87
@ When ("^I make a PUT request with modified notificationType as \" ([^\" ]*)\" to REST API \" ([^\" ]*)\" $" )
78
- public void i_make_a_PUT_request_with_modified_notificationType_as_to_REST_API (String notificationType , String endPoint ) throws Throwable {
88
+ public void i_make_a_PUT_request_with_modified_notificationType_as_to_REST_API (String notificationType ,
89
+ String endPoint ) throws Throwable {
79
90
String readFileToString = FileUtils .readFileToString (new File (SUBSCRIPTION_UPDATED_FILE_PATH ), "UTF-8" );
80
91
jsonArray = new JSONArray (readFileToString );
81
- result = mockMvc .perform (MockMvcRequestBuilders .put (endPoint ).accept (MediaType .APPLICATION_JSON ).content (jsonArray .toString ())
82
- .contentType (MediaType .APPLICATION_JSON )).andReturn ();
92
+ httpRequest = new HttpRequest (HttpMethod .PUT );
93
+ httpRequest .setHost (hostName ).setPort (applicationPort ).setEndpoint (endPoint )
94
+ .addHeader ("content-type" , "application/json" ).addHeader ("Accept" , "application/json" )
95
+ .setBody (jsonArray .toString ());
96
+ response = httpRequest .performRequest ();
83
97
LOGGER .info ("Modified notificationType is:" + notificationType );
84
98
}
85
99
86
- @ Then ("^I get response code of (\\ d+) for successful updation$" )
87
- public void i_get_response_code_of_for_successful_updation (int statusCode ) throws Throwable {
88
- Assert .assertEquals (statusCode , result .getResponse ().getStatus ());
89
- }
90
-
91
100
@ Then ("^I can validate modified notificationType \" ([^\" ]*)\" with GET request at \" ([^\" ]*)\" $" )
92
- public void i_can_validate_modified_notificationType_with_GET_request_at (String notificationType , String endPoint ) throws Throwable {
93
- result = mockMvc .perform (MockMvcRequestBuilders .get (endPoint ).accept (MediaType .APPLICATION_JSON )).andReturn ();
94
- JSONArray foundSubscriptions = new JSONObject (result .getResponse ().getContentAsString ()).getJSONArray ("foundSubscriptions" );
95
- Subscription subscription = mapper .readValue (foundSubscriptions .getJSONObject (0 ).toString (), Subscription .class );
101
+ public void i_can_validate_modified_notificationType_with_GET_request_at (String notificationType , String endPoint )
102
+ throws Throwable {
103
+ httpRequest = new HttpRequest (HttpMethod .GET );
104
+ httpRequest .setHost (hostName ).setPort (applicationPort ).setEndpoint (endPoint );
105
+ response = httpRequest .performRequest ();
106
+ JsonNode node = eventManager .getJSONFromString (response .getBody ());
107
+ String found = node .get ("foundSubscriptions" ).get (0 ).toString ();
108
+ Subscription subscription = mapper .readValue (found , Subscription .class );
96
109
assertEquals (notificationType , subscription .getNotificationType ());
97
110
}
98
- //Scenario:3 ends ======================== ==================================================================
111
+ // Scenario:3 ends ==================================================================
99
112
100
113
@ When ("^I make a DELETE request with subscription name \" ([^\" ]*)\" to the subscription REST API \" ([^\" ]*)\" $" )
101
- public void i_make_a_DELETE_request_with_subscription_name_to_the_subscription_REST_API (String name , String endPoint ) throws Throwable {
102
- result = mockMvc .perform (MockMvcRequestBuilders .delete (endPoint + name ).accept (MediaType .APPLICATION_JSON )).andReturn ();
103
- }
104
-
105
- @ Then ("^I get response code of (\\ d+) for successful delete$" )
106
- public void i_get_response_code_of_for_successful_delete (int statusCode ) throws Throwable {
107
- Assert .assertEquals (statusCode , result .getResponse ().getStatus ());
114
+ public void i_make_a_DELETE_request_with_subscription_name_to_the_subscription_REST_API (String name ,
115
+ String endPoint ) throws Throwable {
116
+ httpRequest = new HttpRequest (HttpMethod .DELETE );
117
+ httpRequest .setHost (hostName ).setPort (applicationPort ).setEndpoint (endPoint + name )
118
+ .addHeader ("Accept" , "application/json" );
119
+ response = httpRequest .performRequest ();
108
120
}
109
121
110
- @ Then ("^My GET request with subscription name \" ([^\" ]*)\" at REST API \" ([^\" ]*)\" returns empty String \" ([^\" ]*)\" $" )
111
- public void my_GET_request_with_subscription_name_at_REST_API_returns_empty_String (String name , String endPoint , String emptyString ) throws Throwable {
112
- result = mockMvc .perform (MockMvcRequestBuilders .get (endPoint + name )
113
- .accept (MediaType .APPLICATION_JSON ))
114
- .andReturn ();
115
- JSONObject response = new JSONObject (result .getResponse ().getContentAsString ());
116
- assertEquals (emptyString , response .getJSONArray ("foundSubscriptions" ).toString ());
117
- assertEquals (name , response .getJSONArray ("notFoundSubscriptions" ).getString (0 ));
122
+ @ Then ("^My GET request with subscription name \" ([^\" ]*)\" at REST API \" ([^\" ]*)\" returns an empty String$" )
123
+ public void my_GET_request_with_subscription_name_at_REST_API_returns_empty_String (String name , String endPoint )
124
+ throws Throwable {
125
+ httpRequest = new HttpRequest (HttpMethod .GET );
126
+ httpRequest .setHost (hostName ).setPort (applicationPort ).setEndpoint (endPoint + name )
127
+ .addHeader ("Accept" , "application/json" );
128
+ response = httpRequest .performRequest ();
129
+ JsonNode node = eventManager .getJSONFromString (response .getBody ());
130
+ int size = node .get ("foundSubscriptions" ).size ();
131
+ String notFound = node .get ("notFoundSubscriptions" ).get (0 ).asText ();
132
+ assertEquals ("Subscription was found, should be empty" ,0 , size );
133
+ assertEquals (name , notFound );
118
134
}
119
- //// Scenario:4 ends ======================== ==================================================================
120
- }
135
+ // Scenario:4 ends ==================================================================
136
+ }
0 commit comments