2
2
3
3
import com .ericsson .ei .controller .model .GetSubscriptionResponse ;
4
4
import com .ericsson .ei .utils .FunctionalTestBase ;
5
+ import com .ericsson .ei .utils .HttpRequest ;
6
+ import com .ericsson .ei .utils .HttpRequest .HttpMethod ;
5
7
import com .fasterxml .jackson .databind .ObjectMapper ;
6
8
7
9
import cucumber .api .java .en .Given ;
11
13
import org .json .JSONArray ;
12
14
import org .json .JSONObject ;
13
15
import org .junit .Ignore ;
14
- import org .springframework .beans .factory .annotation .Autowired ;
15
- import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
16
- import org .springframework .http .MediaType ;
17
- import org .springframework .test .web .servlet .MockMvc ;
18
- import org .springframework .test .web .servlet .MvcResult ;
19
- import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
16
+ import org .springframework .boot .web .server .LocalServerPort ;
17
+ import org .springframework .http .ResponseEntity ;
18
+ import org .springframework .test .context .TestPropertySource ;
20
19
21
20
import java .io .File ;
22
21
23
22
import static org .junit .Assert .assertEquals ;
24
23
25
24
@ Ignore
26
- @ AutoConfigureMockMvc
25
+ @ TestPropertySource ( properties = { "logging.level.com.ericsson.ei.subscriptions.bulk=DEBUG" })
27
26
public class SubscriptionBulkSteps extends FunctionalTestBase {
28
27
29
28
private static final String TEST_RESOURCES_PATH = "src/functionaltests/resources" ;
30
29
31
- @ Autowired
32
- private MockMvc mockMvc ;
33
-
34
- private MvcResult mvcResult ;
30
+ @ LocalServerPort
31
+ private int port ;
35
32
33
+ private String hostName = getHostName ();
36
34
private JSONArray subscriptions ;
37
-
38
35
private JSONArray retrievedSubscriptions ;
36
+ private ResponseEntity response ;
39
37
40
38
@ Given ("^file with subscriptions \" ([^\" ]*)\" $" )
41
39
public void file_with_subscriptions (String subscriptionsFileName ) throws Throwable {
@@ -45,69 +43,102 @@ public void file_with_subscriptions(String subscriptionsFileName) throws Throwab
45
43
46
44
@ When ("^make a POST request with list of subscriptions to the subscription REST API \" ([^\" ]*)\" $" )
47
45
public void make_a_POST_request_with_list_of_subscriptions_to_the_subscription_REST_API (String endpoint ) throws Throwable {
48
- mvcResult = mockMvc .perform (MockMvcRequestBuilders .post (endpoint )
49
- .accept (MediaType .APPLICATION_JSON )
50
- .content (subscriptions .toString ())
51
- .contentType (MediaType .APPLICATION_JSON ))
52
- .andReturn ();
46
+ HttpRequest postRequest = new HttpRequest (HttpMethod .POST );
47
+ response = postRequest .setHost (hostName )
48
+ .setPort (port )
49
+ .setEndpoint (endpoint )
50
+ .setHeaders ("content-type" , "application/json" )
51
+ .setHeaders ("Accept" , "application/json" )
52
+ .setBody (subscriptions .toString ())
53
+ .performRequest ();
53
54
}
54
55
55
56
@ When ("^make a GET request with list of subscriptions names \" ([^\" ]*)\" to the subscription REST API \" ([^\" ]*)\" $" )
56
- public void make_a_GET_request_with_list_of_subscriptions_names_to_the_subscription_REST_API (String subscriptionsNamesList , String endpoint ) throws Throwable {
57
- mvcResult = mockMvc .perform (MockMvcRequestBuilders .get (endpoint + "/" + subscriptionsNamesList )
58
- .accept (MediaType .APPLICATION_JSON ))
59
- .andReturn ();
57
+ public void make_a_GET_request_with_list_of_subscriptions_names_to_the_subscription_REST_API (
58
+ String subscriptionsNamesList , String endpoint ) throws Throwable {
59
+
60
+ HttpRequest getRequest = new HttpRequest (HttpMethod .GET );
61
+ response = getRequest .setHost (hostName )
62
+ .setPort (port )
63
+ .setEndpoint (endpoint + "/" + subscriptionsNamesList )
64
+ .setHeaders ("Accept" , "application/json" )
65
+ .performRequest ();
60
66
}
61
67
62
68
@ When ("^make a DELETE request with list of subscriptions names \" ([^\" ]*)\" to the subscription REST API \" ([^\" ]*)\" $" )
63
- public void make_a_DELETE_request_with_list_of_subscriptions_names_to_the_subscription_REST_API (String subscriptionsNamesList , String endpoint ) throws Throwable {
64
- mvcResult = mockMvc .perform (MockMvcRequestBuilders .delete (endpoint + "/" + subscriptionsNamesList )
65
- .accept (MediaType .APPLICATION_JSON ))
66
- .andReturn ();
69
+ public void make_a_DELETE_request_with_list_of_subscriptions_names_to_the_subscription_REST_API (
70
+ String subscriptionsNamesList , String endpoint ) throws Throwable {
71
+
72
+ HttpRequest deleteRequest = new HttpRequest (HttpMethod .DELETE );
73
+ response = deleteRequest .setHost (hostName )
74
+ .setPort (port )
75
+ .setEndpoint (endpoint + "/" + subscriptionsNamesList )
76
+ .setHeaders ("Accept" , "application/json" )
77
+ .performRequest ();
67
78
}
68
79
69
80
@ When ("^make a PUT request with list of subscriptions to the subscription REST API \" ([^\" ]*)\" $" )
70
- public void make_a_PUT_request_with_list_of_subscriptions_to_the_subscription_REST_API (String endpoint ) throws Throwable {
71
- mvcResult = mockMvc .perform (MockMvcRequestBuilders .put (endpoint )
72
- .accept (MediaType .APPLICATION_JSON )
73
- .content (subscriptions .toString ())
74
- .contentType (MediaType .APPLICATION_JSON ))
75
- .andReturn ();
81
+ public void make_a_PUT_request_with_list_of_subscriptions_to_the_subscription_REST_API (
82
+ String endpoint ) throws Throwable {
83
+
84
+ HttpRequest putRequest = new HttpRequest (HttpMethod .PUT );
85
+ response = putRequest .setHost (hostName )
86
+ .setPort (port )
87
+ .setEndpoint (endpoint )
88
+ .setHeaders ("content-type" , "application/json" )
89
+ .setHeaders ("Accept" , "application/json" )
90
+ .setBody (subscriptions .toString ())
91
+ .performRequest ();
76
92
}
77
93
78
94
@ Then ("^get response code of (\\ d+)$" )
79
95
public void get_response_code_of (int statusCode ) throws Throwable {
80
- assertEquals (statusCode , mvcResult . getResponse ().getStatus ());
96
+ assertEquals (statusCode , response . getStatusCode ().value ());
81
97
}
82
98
83
99
@ Then ("^get in response content (\\ d+) found subscriptions and not found subscription name \" ([^\" ]*)\" $" )
84
- public void get_in_response_content_found_subscriptions_and_not_found_subscription_name (int foundSubscriptionsNumber , String notFoundSubscriptionsName ) throws Throwable {
85
- GetSubscriptionResponse response = new ObjectMapper ().readValue (mvcResult .getResponse ().getContentAsString (), GetSubscriptionResponse .class );
86
- assertEquals (foundSubscriptionsNumber , response .getFoundSubscriptions ().size ());
87
- assertEquals (notFoundSubscriptionsName , response .getNotFoundSubscriptions ().get (0 ));
100
+ public void get_in_response_content_found_subscriptions_and_not_found_subscription_name (
101
+ int foundSubscriptionsNumber , String notFoundSubscriptionsName ) throws Throwable {
102
+
103
+ GetSubscriptionResponse subscriptionResponse = new ObjectMapper ().readValue (
104
+ response .getBody ().toString (), GetSubscriptionResponse .class );
105
+
106
+ assertEquals (foundSubscriptionsNumber ,
107
+ subscriptionResponse .getFoundSubscriptions ().size ());
108
+ assertEquals (notFoundSubscriptionsName ,
109
+ subscriptionResponse .getNotFoundSubscriptions ().get (0 ));
88
110
}
89
111
90
112
@ Then ("^get in response content subscription \" ([^\" ]*)\" $" )
91
113
public void get_in_response_content_subscription_and_reason (String subscriptionName ) throws Throwable {
92
- JSONObject response = new JSONArray (mvcResult . getResponse ().getContentAsString ()).getJSONObject (0 );
93
- assertEquals (subscriptionName , response .getString ("subscription" ));
114
+ JSONObject jsonResponse = new JSONArray (response . getBody ().toString ()).getJSONObject (0 );
115
+ assertEquals (subscriptionName , jsonResponse .getString ("subscription" ));
94
116
}
95
117
96
118
@ Then ("^number of retrieved subscriptions using REST API \" ([^\" ]*)\" is (\\ d+)$" )
97
- public void number_of_retrieved_subscriptions_using_REST_API_is (String endpoint , int subscriptionsNumber ) throws Throwable {
98
- mvcResult = mockMvc .perform (MockMvcRequestBuilders .get (endpoint ).accept (MediaType .APPLICATION_JSON )).andReturn ();
99
- retrievedSubscriptions = new JSONArray (mvcResult .getResponse ().getContentAsString ());
119
+ public void number_of_retrieved_subscriptions_using_REST_API_is (
120
+ String endpoint , int subscriptionsNumber ) throws Throwable {
121
+
122
+ HttpRequest getRequest = new HttpRequest (HttpMethod .GET );
123
+ response = getRequest .setHost (hostName )
124
+ .setPort (port )
125
+ .setEndpoint (endpoint )
126
+ .setHeaders ("Accept" , "application/json" )
127
+ .performRequest ();
128
+ retrievedSubscriptions = new JSONArray (response .getBody ().toString ());
100
129
assertEquals (subscriptionsNumber , retrievedSubscriptions .length ());
101
130
}
102
131
103
132
@ Then ("^retrieved subscriptions are same as given$" )
104
133
public void retrieved_subscriptions_are_same_as_given () throws Throwable {
105
134
for (int i = 0 ; i < subscriptions .length (); i ++) {
106
- assertEquals (subscriptions .getJSONObject (i ).get ("subscriptionName" ), retrievedSubscriptions .getJSONObject (i ).get ("subscriptionName" ));
107
- assertEquals (subscriptions .getJSONObject (i ).get ("notificationType" ), retrievedSubscriptions .getJSONObject (i ).get ("notificationType" ));
108
- assertEquals (subscriptions .getJSONObject (i ).get ("notificationMeta" ), retrievedSubscriptions .getJSONObject (i ).get ("notificationMeta" ));
135
+ assertEquals (subscriptions .getJSONObject (i ).get ("subscriptionName" ),
136
+ retrievedSubscriptions .getJSONObject (i ).get ("subscriptionName" ));
137
+ assertEquals (subscriptions .getJSONObject (i ).get ("notificationType" ),
138
+ retrievedSubscriptions .getJSONObject (i ).get ("notificationType" ));
139
+ assertEquals (subscriptions .getJSONObject (i ).get ("notificationMeta" ),
140
+ retrievedSubscriptions .getJSONObject (i ).get ("notificationMeta" ));
109
141
assertEquals (true , retrievedSubscriptions .getJSONObject (i ).has ("userName" ));
110
142
}
111
143
}
112
-
113
144
}
0 commit comments