Skip to content

Commit a6c9f30

Browse files
authored
Params and Headers HTTPRequest improvements (#154)
1 parent cdbf9e4 commit a6c9f30

File tree

6 files changed

+90
-31
lines changed

6 files changed

+90
-31
lines changed

src/functionaltests/java/com/ericsson/ei/files/DownloadFilesTestSteps.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void eiffel_intelligence_instance_is_up_and_running() throws Exception {
4646
LOGGER.debug("Checking if Eiffel Intelligence instance is up and running.");
4747
httpRequest.setHost(hostName)
4848
.setPort(applicationPort)
49-
.setHeaders("Content-type:", MediaType.APPLICATION_JSON_VALUE.toString())
49+
.addHeader("Content-type:", MediaType.APPLICATION_JSON_VALUE.toString())
5050
.setEndpoint("/subscriptions");
5151

5252
response = httpRequest.performRequest();

src/functionaltests/java/com/ericsson/ei/query/QueryAggregatedObjectsTestSteps.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ public void perform_valid_query_on_newly_created_aggregated_object() throws Thro
102102
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
103103
response = getRequest.setPort(applicationPort)
104104
.setHost(hostName)
105-
.setHeaders("content-type", "application/json")
106-
.setHeaders("Accept", "application/json")
105+
.addHeader("content-type", "application/json")
106+
.addHeader("Accept", "application/json")
107107
.setEndpoint(entryPoint)
108-
.setParam("ID", documentId)
108+
.addParam("ID", documentId)
109109
.performRequest();
110110

111111
LOGGER.debug("Response of /queryAggregatedObject RestApi, Status Code: " + response.getStatusCodeValue() +
@@ -137,10 +137,10 @@ public void perform_invalid_query_on_created_aggregated_object() throws Throwabl
137137
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
138138
response = getRequest.setPort(applicationPort)
139139
.setHost(hostName)
140-
.setHeaders("content-type", "application/json")
141-
.setHeaders("Accept", "application/json")
140+
.addHeader("content-type", "application/json")
141+
.addHeader("Accept", "application/json")
142142
.setEndpoint(entryPoint)
143-
.setParam("ID", invalidDocumentId)
143+
.addParam("ID", invalidDocumentId)
144144
.performRequest();
145145

146146
String responseAsString = response.getBody().toString();
@@ -165,10 +165,10 @@ public void perform_valid_freestyle_query_on_created_aggregated_object() throws
165165
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
166166
response = getRequest.setPort(applicationPort)
167167
.setHost(hostName)
168-
.setHeaders("content-type", "application/json")
169-
.setHeaders("Accept", "application/json")
168+
.addHeader("content-type", "application/json")
169+
.addHeader("Accept", "application/json")
170170
.setEndpoint(entryPoint)
171-
.setParam("request", queryAggrObj)
171+
.addParam("request", queryAggrObj)
172172
.performRequest();
173173

174174
LOGGER.debug("Response of /query RestApi, Status Code: " + response.getStatusCodeValue() +
@@ -199,10 +199,10 @@ public void perform_invalid_freestyle_query_on_created_aggregated_object() throw
199199
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
200200
response = getRequest.setPort(applicationPort)
201201
.setHost(hostName)
202-
.setHeaders("content-type", "application/json")
203-
.setHeaders("Accept", "application/json")
202+
.addHeader("content-type", "application/json")
203+
.addHeader("Accept", "application/json")
204204
.setEndpoint(entryPoint)
205-
.setParam("request", queryAggrObj)
205+
.addParam("request", queryAggrObj)
206206
.performRequest();
207207

208208
String responseAsString = response.getBody().toString();
@@ -236,10 +236,10 @@ public void perform_a_query_for_missed_notification() throws Throwable {
236236
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
237237
response = getRequest.setPort(applicationPort)
238238
.setHost(hostName)
239-
.setHeaders("content-type", "application/json")
240-
.setHeaders("Accept", "application/json")
239+
.addHeader("content-type", "application/json")
240+
.addHeader("Accept", "application/json")
241241
.setEndpoint(entryPoint)
242-
.setParam("SubscriptionName", subscriptionName)
242+
.addParam("SubscriptionName", subscriptionName)
243243
.performRequest();
244244

245245
String responseAsString = response.getBody().toString();
@@ -275,10 +275,10 @@ public void check_missed_notification_has_been_returned() throws Throwable {
275275
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
276276
response = getRequest.setPort(applicationPort)
277277
.setHost(hostName)
278-
.setHeaders("content-type", "application/json")
279-
.setHeaders("Accept", "application/json")
278+
.addHeader("content-type", "application/json")
279+
.addHeader("Accept", "application/json")
280280
.setEndpoint(entryPoint)
281-
.setParam("SubscriptionName", subscriptionName)
281+
.addParam("SubscriptionName", subscriptionName)
282282
.performRequest();
283283

284284
String responseAsString = response.getBody().toString();

src/functionaltests/java/com/ericsson/ei/rules/RuleCheckSteps.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ public void make_a_POST_request_to_the_REST_API_with_request_parameter(String en
5959
HttpRequest postRequest = new HttpRequest(HttpMethod.POST);
6060
response = postRequest.setPort(applicationPort)
6161
.setHost(hostName)
62-
.setHeaders("content-type", "application/json")
63-
.setHeaders("Accept", "application/json")
62+
.addHeader("content-type", "application/json")
63+
.addHeader("Accept", "application/json")
6464
.setEndpoint(endpoint)
6565
.setBody(events)
66-
.setParam(requestParam, rules)
66+
.addParam(requestParam, rules)
6767
.performRequest();
6868
}
6969

@@ -77,8 +77,8 @@ public void make_a_POST_request_to_the_REST_API(String endpoint) throws Throwabl
7777
HttpRequest postRequest = new HttpRequest(HttpMethod.POST);
7878
response = postRequest.setPort(applicationPort)
7979
.setHost(hostName)
80-
.setHeaders("content-type", "application/json")
81-
.setHeaders("Accept", "application/json")
80+
.addHeader("content-type", "application/json")
81+
.addHeader("Accept", "application/json")
8282
.setEndpoint(endpoint)
8383
.setBody(requestBody)
8484
.performRequest();

src/functionaltests/java/com/ericsson/ei/subscriptions/authentication/AuthenticationSteps.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ public class AuthenticationSteps extends FunctionalTestBase {
4141
public void beforeScenario() throws Throwable {
4242
httpRequest = new HttpRequest(HttpMethod.GET);
4343
httpRequest.setHost(hostName).setPort(applicationPort).setEndpoint("/auth/logout");
44+
4445
String auth = "gauss:password";
4546
String encodedAuth = new String(Base64.encodeBase64(auth.getBytes()), "UTF-8");
46-
httpRequest.setHeaders("Authorization", "Basic " + encodedAuth);
47+
httpRequest.addHeader("Authorization", "Basic " + encodedAuth);
4748
httpRequest.performRequest();
4849
}
4950

@@ -52,6 +53,7 @@ public void ldap_is_activated() throws Throwable {
5253
String expectedContent = new JSONObject().put("security", true).toString();
5354
httpRequest = new HttpRequest(HttpMethod.GET);
5455
httpRequest.setHost(hostName).setPort(applicationPort).setEndpoint("/auth");
56+
5557
response = httpRequest.performRequest();
5658
assertEquals(HttpStatus.OK, response.getStatusCode());
5759
assertEquals(expectedContent, response.getBody().toString());
@@ -64,11 +66,12 @@ public void request_to_rest_api(String method, String endpoint) throws Throwable
6466
String requestBody = FileUtils.readFileToString(new File(SUBSCRIPTION), "UTF-8");
6567
httpRequest = new HttpRequest(HttpMethod.POST);
6668
httpRequest.setHost(hostName).setPort(applicationPort).setEndpoint(endpoint)
67-
.setHeaders("Content-type", "application/json").setBody(requestBody);
69+
.addHeader("Content-type", "application/json").setBody(requestBody);
6870
break;
6971
case "GET":
7072
httpRequest = new HttpRequest(HttpMethod.GET);
7173
httpRequest.setHost(hostName).setPort(applicationPort).setEndpoint(endpoint);
74+
7275
break;
7376
}
7477
}
@@ -77,7 +80,7 @@ public void request_to_rest_api(String method, String endpoint) throws Throwable
7780
public void with_credentials(String username, String password) throws Throwable {
7881
String auth = username + ":" + password;
7982
String encodedAuth = new String(Base64.encodeBase64(auth.getBytes()), "UTF-8");
80-
httpRequest.setHeaders("Authorization", "Basic " + encodedAuth);
83+
httpRequest.addHeader("Authorization", "Basic " + encodedAuth);
8184
}
8285

8386
@When("^request is sent$")
@@ -94,6 +97,7 @@ public void get_response_code(int statusCode) throws Throwable {
9497
public void subscription_with_name_created(String check) throws Throwable {
9598
httpRequest = new HttpRequest(HttpMethod.GET);
9699
httpRequest.setHost(hostName).setPort(applicationPort).setEndpoint("/subscriptions/" + SUBSCRIPTION_NAME);
100+
97101
response = httpRequest.performRequest();
98102
GetSubscriptionResponse subscription = new ObjectMapper().readValue(response.getBody().toString(),
99103
GetSubscriptionResponse.class);

src/functionaltests/java/com/ericsson/ei/subscriptions/content/SubscriptionContentSteps.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ public class SubscriptionContentSteps extends FunctionalTestBase {
3636
@PostConstruct
3737
private void setUp() {
3838
getRequest = new HttpRequest(HttpMethod.GET);
39+
3940
getRequest.setHost(hostName).setPort(applicationPort).setEndpoint("/subscriptions");
4041

4142
deleteRequest = new HttpRequest(HttpMethod.DELETE);
4243
deleteRequest.setHost(hostName).setPort(applicationPort).setEndpoint("/subscriptions");
4344

4445
postRequest = new HttpRequest(HttpMethod.POST);
4546
postRequest.setHost(hostName).setPort(applicationPort).setEndpoint("/subscriptions")
46-
.setHeaders("content-type", "application/json").setHeaders("Accept", "application/json");
47+
.addHeader("content-type", "application/json").addHeader("Accept", "application/json");
4748
}
4849

4950
// SCENARIO 1

src/functionaltests/java/com/ericsson/ei/utils/HttpRequest.java

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,18 @@ public enum HttpMethod {
3434
@Getter
3535
@Setter
3636
protected String host;
37+
3738
@Getter
3839
@Setter
3940
protected String endpoint;
4041
@Getter
41-
protected Map<String, String> params = new HashMap<>();
42+
protected Map<String, String> params;
4243

4344
private static final Logger LOGGER = LoggerFactory.getLogger(HttpRequest.class);
4445

4546
public HttpRequest(HttpMethod method) {
47+
params = new HashMap<>();
48+
4649
switch (method) {
4750
case POST:
4851
request = new HttpPost();
@@ -58,22 +61,66 @@ public HttpRequest(HttpMethod method) {
5861
break;
5962
}
6063
}
64+
65+
/*
66+
* Function that clean parameters field only.
67+
*/
68+
public void cleanParams() {
69+
params.clear();
70+
}
71+
72+
/*
73+
* Function that resets the HTTP Request object so it can be reused.
74+
*
75+
*/
76+
public void resetHttpRequestObject() {
77+
this.cleanParams();
78+
request.reset();
79+
}
6180

62-
public HttpRequest setHeaders(String key, String value) {
81+
/*
82+
* Function for adding headers to the http request.
83+
*
84+
* @param key , the key of the header
85+
* @param value, the value of the header
86+
*
87+
* @return HTTPRequest
88+
*/
89+
public HttpRequest addHeader(String key, String value) {
6390
request.addHeader(key, value);
6491
return this;
6592
}
6693

67-
public HttpRequest setParam(String key, String value) {
94+
/*
95+
* Function for adding parameters to the http request.
96+
*
97+
* @param key , the key of the parameter
98+
* @param value, the value of the parameter
99+
*
100+
* @return HTTPRequest
101+
*/
102+
public HttpRequest addParam(String key, String value) {
68103
params.put(key, value);
69104
return this;
70105
}
71106

107+
/*
108+
* Function that set the body of the http request.
109+
*
110+
* @param body , the body to be set in the http request.
111+
*
112+
* @return HTTPRequest
113+
*/
72114
public HttpRequest setBody(String body) {
73115
((HttpEntityEnclosingRequestBase) request).setEntity(new StringEntity(body, "UTF-8"));
74116
return this;
75117
}
76118

119+
/*
120+
* Function that set the body of the http request.
121+
*
122+
* @param body , the file with body content to be set in the http request.
123+
*/
77124
public void setBody(File file) {
78125
String fileContent = "";
79126
try {
@@ -84,8 +131,15 @@ public void setBody(File file) {
84131
setBody(fileContent);
85132
}
86133

134+
/*
135+
* Function that execute http request.
136+
*
137+
* @return ResponseEntity<String> , the response of the performed http request.
138+
*/
87139
public ResponseEntity<String> performRequest() throws URISyntaxException {
140+
88141
URIBuilder builder = new URIBuilder("http://" + host + ":" + port + endpoint);
142+
89143
if (!params.isEmpty()) {
90144
for (Map.Entry<String, String> entry : params.entrySet()) {
91145
builder.addParameter(entry.getKey(), entry.getValue());

0 commit comments

Comments
 (0)