Skip to content

Commit cdbf9e4

Browse files
Replace mockMvc in subscription bulk tests (#155)
1 parent 313f3ed commit cdbf9e4

File tree

2 files changed

+81
-51
lines changed

2 files changed

+81
-51
lines changed

src/functionaltests/java/com/ericsson/ei/subscriptions/bulk/SubscriptionBulkSteps.java

Lines changed: 76 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.ericsson.ei.controller.model.GetSubscriptionResponse;
44
import com.ericsson.ei.utils.FunctionalTestBase;
5+
import com.ericsson.ei.utils.HttpRequest;
6+
import com.ericsson.ei.utils.HttpRequest.HttpMethod;
57
import com.fasterxml.jackson.databind.ObjectMapper;
68

79
import cucumber.api.java.en.Given;
@@ -11,31 +13,27 @@
1113
import org.json.JSONArray;
1214
import org.json.JSONObject;
1315
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;
2019

2120
import java.io.File;
2221

2322
import static org.junit.Assert.assertEquals;
2423

2524
@Ignore
26-
@AutoConfigureMockMvc
25+
@TestPropertySource(properties = {"logging.level.com.ericsson.ei.subscriptions.bulk=DEBUG"})
2726
public class SubscriptionBulkSteps extends FunctionalTestBase {
2827

2928
private static final String TEST_RESOURCES_PATH = "src/functionaltests/resources";
3029

31-
@Autowired
32-
private MockMvc mockMvc;
33-
34-
private MvcResult mvcResult;
30+
@LocalServerPort
31+
private int port;
3532

33+
private String hostName = getHostName();
3634
private JSONArray subscriptions;
37-
3835
private JSONArray retrievedSubscriptions;
36+
private ResponseEntity response;
3937

4038
@Given("^file with subscriptions \"([^\"]*)\"$")
4139
public void file_with_subscriptions(String subscriptionsFileName) throws Throwable {
@@ -45,69 +43,102 @@ public void file_with_subscriptions(String subscriptionsFileName) throws Throwab
4543

4644
@When("^make a POST request with list of subscriptions to the subscription REST API \"([^\"]*)\"$")
4745
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();
5354
}
5455

5556
@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();
6066
}
6167

6268
@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();
6778
}
6879

6980
@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();
7692
}
7793

7894
@Then("^get response code of (\\d+)$")
7995
public void get_response_code_of(int statusCode) throws Throwable {
80-
assertEquals(statusCode, mvcResult.getResponse().getStatus());
96+
assertEquals(statusCode, response.getStatusCode().value());
8197
}
8298

8399
@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));
88110
}
89111

90112
@Then("^get in response content subscription \"([^\"]*)\"$")
91113
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"));
94116
}
95117

96118
@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());
100129
assertEquals(subscriptionsNumber, retrievedSubscriptions.length());
101130
}
102131

103132
@Then("^retrieved subscriptions are same as given$")
104133
public void retrieved_subscriptions_are_same_as_given() throws Throwable {
105134
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"));
109141
assertEquals(true, retrievedSubscriptions.getJSONObject(i).has("userName"));
110142
}
111143
}
112-
113144
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77
import java.util.Map;
88

99
import org.apache.commons.io.FileUtils;
10-
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
11-
import org.apache.http.client.methods.HttpGet;
12-
import org.apache.http.client.methods.HttpPost;
13-
import org.apache.http.client.methods.HttpDelete;
14-
import org.apache.http.client.methods.HttpRequestBase;
10+
import org.apache.http.client.methods.*;
1511
import org.apache.http.client.utils.URIBuilder;
1612
import org.apache.http.entity.StringEntity;
1713
import org.slf4j.Logger;
@@ -29,7 +25,7 @@ public class HttpRequest {
2925
private HttpExecutor executor = HttpExecutor.getInstance();
3026

3127
public enum HttpMethod {
32-
GET, POST, DELETE
28+
GET, POST, DELETE, PUT
3329
}
3430

3531
@Getter
@@ -57,6 +53,9 @@ public HttpRequest(HttpMethod method) {
5753
case DELETE:
5854
request = new HttpDelete();
5955
break;
56+
case PUT:
57+
request = new HttpPut();
58+
break;
6059
}
6160
}
6261

0 commit comments

Comments
 (0)