Skip to content

Commit 78ada8e

Browse files
Replaced MvcMock with HttpRequest in SubscriptionCRUD (#159)
Replaced MvcMock with HttpRequest in SubscriptionCRUD
1 parent 9d5e698 commit 78ada8e

File tree

4 files changed

+93
-68
lines changed

4 files changed

+93
-68
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public class AuthenticationSteps extends FunctionalTestBase {
4444
public void beforeScenario() throws Throwable {
4545
httpRequest = new HttpRequest(HttpMethod.GET);
4646
httpRequest.setHost(hostName).setPort(applicationPort).setEndpoint("/auth/logout");
47-
4847
String auth = "gauss:password";
4948
String encodedAuth = new String(Base64.encodeBase64(auth.getBytes()), "UTF-8");
5049
httpRequest.addHeader("Authorization", "Basic " + encodedAuth);
@@ -61,7 +60,6 @@ public void ldap_is_activated() throws Throwable {
6160
String expectedContent = new JSONObject().put("security", true).toString();
6261
httpRequest = new HttpRequest(HttpMethod.GET);
6362
httpRequest.setHost(hostName).setPort(applicationPort).setEndpoint("/auth");
64-
6563
response = httpRequest.performRequest();
6664
assertEquals(HttpStatus.OK, response.getStatusCode());
6765
assertEquals(expectedContent, response.getBody().toString());

src/functionaltests/java/com/ericsson/ei/subscriptions/crud/SubscriptionCRUDSteps.java

Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@
22

33
import com.ericsson.ei.controller.model.Subscription;
44
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;
58
import com.fasterxml.jackson.databind.ObjectMapper;
69
import cucumber.api.java.en.Given;
710
import cucumber.api.java.en.Then;
811
import cucumber.api.java.en.When;
912
import org.apache.commons.io.FileUtils;
1013
import org.json.JSONArray;
11-
import org.json.JSONObject;
12-
import org.junit.Assert;
1314
import org.junit.Ignore;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
16-
import org.springframework.beans.factory.annotation.Autowired;
1717
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
18+
import org.springframework.boot.web.server.LocalServerPort;
1819
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;
2321

2422
import java.io.File;
2523

@@ -34,87 +32,105 @@ public class SubscriptionCRUDSteps extends FunctionalTestBase {
3432
private static final String SUBSCRIPTION_FILE_PATH = "src/functionaltests/resources/subscription_single.json";
3533
private static final String SUBSCRIPTION_UPDATED_FILE_PATH = "src/functionaltests/resources/subscription_single_updated.json";
3634

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;
3940

40-
private MvcResult result;
4141
private ObjectMapper mapper = new ObjectMapper();
4242
private static JSONArray jsonArray = null;
4343

4444
@Given("^The REST API \"([^\"]*)\" is up and running$")
4545
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());
4850
}
4951

5052
@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 {
5255
String readFileToString = FileUtils.readFileToString(new File(SUBSCRIPTION_FILE_PATH), "UTF-8");
5356
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();
5662
}
5763

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());
6167
}
62-
///Scenario:1 ends ===============================================================================
68+
/// Scenario:1 ends ==================================================================
6369

6470
@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();
6776
}
6877

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());
7484
}
75-
// Scenario:2 ends=========================================================================================
85+
// Scenario:2 ends ==================================================================
7686

7787
@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 {
7990
String readFileToString = FileUtils.readFileToString(new File(SUBSCRIPTION_UPDATED_FILE_PATH), "UTF-8");
8091
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();
8397
LOGGER.info("Modified notificationType is:" + notificationType);
8498
}
8599

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-
91100
@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);
96109
assertEquals(notificationType, subscription.getNotificationType());
97110
}
98-
//Scenario:3 ends ==========================================================================================
111+
// Scenario:3 ends ==================================================================
99112

100113
@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();
108120
}
109121

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);
118134
}
119-
////Scenario:4 ends ==========================================================================================
120-
}
135+
// Scenario:4 ends ==================================================================
136+
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,27 @@ public List<String> getEventsIdList(String eiffelEventsJsonPath, List<String> ev
7171
}
7272

7373
/**
74-
* Converts a JSON string into a tree model.
74+
* Converts a JSON file into a tree model.
7575
*
7676
* @param filePath
7777
* path to JSON file
7878
* @return JsonNode tree model
7979
* @throws IOException
8080
*/
8181
public JsonNode getJSONFromFile(String filePath) throws IOException {
82-
ObjectMapper objectMapper = new ObjectMapper();
83-
String expectedDocument = FileUtils.readFileToString(new File(filePath), "UTF-8");
84-
return objectMapper.readTree(expectedDocument);
82+
return getJSONFromString(FileUtils.readFileToString(new File(filePath), "UTF-8"));
83+
}
84+
85+
/**
86+
* Converts a JSON string into a tree model.
87+
*
88+
* @param document
89+
* string of json
90+
* @return JsonNode tree model
91+
* @throws IOException
92+
*/
93+
public JsonNode getJSONFromString(String document) throws IOException {
94+
return new ObjectMapper().readTree(document);
8595
}
8696

8797
}

src/functionaltests/resources/features/subscriptionCRUD.feature

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,27 @@ Feature: Test Subscription CRUD
2323
Scenario: Create subscription with JSON object using REST API by POST method
2424
Given The REST API "/subscriptions" is up and running
2525
When I make a POST request with valid "JSON" to the subscription REST API "/subscriptions"
26-
Then I get response code of 200
26+
Then I get response code of 200
2727

2828
#@tag2
2929
Scenario: Read subscription using REST API by GET method
3030
Given The REST API "/subscriptions" is up and running
3131
When I make a GET request with subscription name "Subscription_Test" to the subscription REST API "/subscriptions/"
32-
Then I get response code of 200 and subscription name "Subscription_Test"
32+
Then I get response code of 200
33+
And Subscription name is "Subscription_Test"
3334

3435
#@tag3
3536
Scenario: Update subscription using REST API by PUT method and validate updation
3637
Given The REST API "/subscriptions" is up and running
3738
When I make a PUT request with modified notificationType as "MAIL" to REST API "/subscriptions"
38-
Then I get response code of 200 for successful updation
39+
Then I get response code of 200
3940
And I can validate modified notificationType "MAIL" with GET request at "/subscriptions/Subscription_Test"
4041

4142
#@tag4
4243
Scenario: Delete subscription using REST API by DELETE method and validate deletion
4344
Given The REST API "/subscriptions" is up and running
4445
When I make a DELETE request with subscription name "Subscription_Test" to the subscription REST API "/subscriptions/"
45-
Then I get response code of 200 for successful delete
46-
And My GET request with subscription name "Subscription_Test" at REST API "/subscriptions/" returns empty String "[]"
46+
Then I get response code of 200
47+
And My GET request with subscription name "Subscription_Test" at REST API "/subscriptions/" returns an empty String
4748

4849

0 commit comments

Comments
 (0)