Skip to content

Commit d2d5490

Browse files
Updated Subscriptions REST API to support bulk operations (#119)
1 parent 8ba2298 commit d2d5490

29 files changed

+1023
-517
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
package com.ericsson.ei.subscriptions.authentication;
22

33
import com.ericsson.ei.controller.AuthControllerImpl;
4+
import com.ericsson.ei.controller.model.GetSubscriptionResponse;
45
import com.ericsson.ei.utils.FunctionalTestBase;
56
import com.ericsson.ei.utils.TestLDAPInitializer;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
68
import cucumber.api.java.en.Given;
79
import cucumber.api.java.en.Then;
810
import cucumber.api.java.en.When;
911
import org.apache.commons.io.FileUtils;
1012
import org.apache.tomcat.util.codec.binary.Base64;
1113
import org.apache.tomcat.util.codec.binary.StringUtils;
14+
import org.json.JSONArray;
1215
import org.json.JSONObject;
1316
import org.junit.Ignore;
1417
import org.slf4j.Logger;
1518
import org.slf4j.LoggerFactory;
1619
import org.springframework.beans.factory.annotation.Autowired;
1720
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1821
import org.springframework.http.HttpHeaders;
22+
import org.springframework.http.HttpStatus;
1923
import org.springframework.http.MediaType;
2024
import org.springframework.test.context.ContextConfiguration;
2125
import org.springframework.test.web.servlet.MockMvc;
@@ -69,11 +73,13 @@ public void make_a_post_request_to_the_subscription_rest_api_without_credentials
6973
@Then("^get response code of (\\d+) and subscription with name \"(\\w+)\" is not created$")
7074
public void get_response_code_of_and_subscription_with_name_is_not_created(int statusCode, String subscriptionName) throws Throwable {
7175
assertEquals(statusCode, mvcResult.getResponse().getStatus());
72-
mockMvc.perform(MockMvcRequestBuilders.get("/subscriptions/" + subscriptionName)
76+
mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/subscriptions/" + subscriptionName)
7377
.accept(MediaType.APPLICATION_JSON_VALUE))
74-
.andExpect(status().isBadRequest())
75-
.andExpect(content().string("[]"))
7678
.andReturn();
79+
GetSubscriptionResponse response = new ObjectMapper().readValue(mvcResult.getResponse().getContentAsString(), GetSubscriptionResponse.class);
80+
assertEquals(HttpStatus.NOT_FOUND.value(), mvcResult.getResponse().getStatus());
81+
assertEquals(true, response.getFoundSubscriptions().isEmpty());
82+
assertEquals(subscriptionName, response.getNotFoundSubscriptions().get(0));
7783
}
7884
///Scenario:1 ends ===============================================================================
7985

@@ -93,10 +99,13 @@ public void make_a_post_request_to_the_subscription_rest_api_with_username_and_p
9399
@Then("^get response code of (\\d+) and subscription with name \"(\\w+)\" is created$")
94100
public void get_response_code_of_and_subscription_with_name_is_created(int statusCode, String subscriptionName) throws Throwable {
95101
assertEquals(statusCode, mvcResult.getResponse().getStatus());
96-
mockMvc.perform(MockMvcRequestBuilders.get("/subscriptions/" + subscriptionName)
102+
mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/subscriptions/" + subscriptionName)
97103
.accept(MediaType.APPLICATION_JSON_VALUE))
98-
.andExpect(status().isOk())
99104
.andReturn();
105+
GetSubscriptionResponse response = new ObjectMapper().readValue(mvcResult.getResponse().getContentAsString(), GetSubscriptionResponse.class);
106+
assertEquals(HttpStatus.OK.value(), mvcResult.getResponse().getStatus());
107+
assertEquals(true, response.getNotFoundSubscriptions().isEmpty());
108+
assertEquals(subscriptionName, response.getFoundSubscriptions().get(0).getSubscriptionName());
100109
}
101110
///Scenario:2 ends ===============================================================================
102111

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.ericsson.ei.subscriptions.bulk;
2+
3+
import com.ericsson.ei.controller.model.GetSubscriptionResponse;
4+
import com.ericsson.ei.utils.FunctionalTestBase;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import cucumber.api.java.en.Given;
7+
import cucumber.api.java.en.Then;
8+
import cucumber.api.java.en.When;
9+
import org.apache.commons.io.FileUtils;
10+
import org.json.JSONArray;
11+
import org.json.JSONObject;
12+
import org.junit.Ignore;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
17+
import org.springframework.http.MediaType;
18+
import org.springframework.test.web.servlet.MockMvc;
19+
import org.springframework.test.web.servlet.MvcResult;
20+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
21+
22+
import java.io.File;
23+
24+
import static org.junit.Assert.assertEquals;
25+
26+
@Ignore
27+
@AutoConfigureMockMvc
28+
public class SubscriptionBulkSteps extends FunctionalTestBase {
29+
30+
private static final Logger LOGGER = LoggerFactory.getLogger(SubscriptionBulkSteps.class);
31+
32+
private static final String TEST_RESOURCES_PATH = "src/functionaltests/resources";
33+
34+
@Autowired
35+
private MockMvc mockMvc;
36+
37+
private MvcResult mvcResult;
38+
39+
private JSONArray subscriptions;
40+
41+
private JSONArray retrievedSubscriptions;
42+
43+
@Given("^file with subscriptions \"([^\"]*)\"$")
44+
public void file_with_subscriptions(String subscriptionsFileName) throws Throwable {
45+
String fileContent = FileUtils.readFileToString(new File(TEST_RESOURCES_PATH + subscriptionsFileName), "UTF-8");
46+
subscriptions = new JSONArray(fileContent);
47+
}
48+
49+
@When("^make a POST request with list of subscriptions to the subscription REST API \"([^\"]*)\"$")
50+
public void make_a_POST_request_with_list_of_subscriptions_to_the_subscription_REST_API(String endpoint) throws Throwable {
51+
mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(endpoint)
52+
.accept(MediaType.APPLICATION_JSON)
53+
.content(subscriptions.toString())
54+
.contentType(MediaType.APPLICATION_JSON))
55+
.andReturn();
56+
}
57+
58+
@When("^make a GET request with list of subscriptions names \"([^\"]*)\" to the subscription REST API \"([^\"]*)\"$")
59+
public void make_a_GET_request_with_list_of_subscriptions_names_to_the_subscription_REST_API(String subscriptionsNamesList, String endpoint) throws Throwable {
60+
mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(endpoint + "/" + subscriptionsNamesList)
61+
.accept(MediaType.APPLICATION_JSON))
62+
.andReturn();
63+
}
64+
65+
@When("^make a DELETE request with list of subscriptions names \"([^\"]*)\" to the subscription REST API \"([^\"]*)\"$")
66+
public void make_a_DELETE_request_with_list_of_subscriptions_names_to_the_subscription_REST_API(String subscriptionsNamesList, String endpoint) throws Throwable {
67+
mvcResult = mockMvc.perform(MockMvcRequestBuilders.delete(endpoint + "/" + subscriptionsNamesList)
68+
.accept(MediaType.APPLICATION_JSON))
69+
.andReturn();
70+
}
71+
72+
@When("^make a PUT request with list of subscriptions to the subscription REST API \"([^\"]*)\"$")
73+
public void make_a_PUT_request_with_list_of_subscriptions_to_the_subscription_REST_API(String endpoint) throws Throwable {
74+
mvcResult = mockMvc.perform(MockMvcRequestBuilders.put(endpoint)
75+
.accept(MediaType.APPLICATION_JSON)
76+
.content(subscriptions.toString())
77+
.contentType(MediaType.APPLICATION_JSON))
78+
.andReturn();
79+
}
80+
81+
@Then("^get response code of (\\d+)$")
82+
public void get_response_code_of(int statusCode) throws Throwable {
83+
assertEquals(statusCode, mvcResult.getResponse().getStatus());
84+
}
85+
86+
@Then("^get in response content (\\d+) found subscriptions and not found subscription name \"([^\"]*)\"$")
87+
public void get_in_response_content_found_subscriptions_and_not_found_subscription_name(int foundSubscriptionsNumber, String notFoundSubscriptionsName) throws Throwable {
88+
GetSubscriptionResponse response = new ObjectMapper().readValue(mvcResult.getResponse().getContentAsString(), GetSubscriptionResponse.class);
89+
assertEquals(foundSubscriptionsNumber, response.getFoundSubscriptions().size());
90+
assertEquals(notFoundSubscriptionsName, response.getNotFoundSubscriptions().get(0));
91+
}
92+
93+
@Then("^get in response content subscription \"([^\"]*)\"$")
94+
public void get_in_response_content_subscription_and_reason(String subscriptionName) throws Throwable {
95+
JSONObject response = new JSONArray(mvcResult.getResponse().getContentAsString()).getJSONObject(0);
96+
assertEquals(subscriptionName, response.getString("subscription"));
97+
}
98+
99+
@Then("^number of retrieved subscriptions using REST API \"([^\"]*)\" is (\\d+)$")
100+
public void number_of_retrieved_subscriptions_using_REST_API_is(String endpoint, int subscriptionsNumber) throws Throwable {
101+
mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(endpoint).accept(MediaType.APPLICATION_JSON)).andReturn();
102+
retrievedSubscriptions = new JSONArray(mvcResult.getResponse().getContentAsString());
103+
assertEquals(subscriptionsNumber, retrievedSubscriptions.length());
104+
}
105+
106+
@Then("^retrieved subscriptions are same as given$")
107+
public void retrieved_subscriptions_are_same_as_given() throws Throwable {
108+
for (int i = 0; i < subscriptions.length(); i++) {
109+
assertEquals(subscriptions.getJSONObject(i).get("subscriptionName"), retrievedSubscriptions.getJSONObject(i).get("subscriptionName"));
110+
assertEquals(subscriptions.getJSONObject(i).get("notificationType"), retrievedSubscriptions.getJSONObject(i).get("notificationType"));
111+
assertEquals(subscriptions.getJSONObject(i).get("notificationMeta"), retrievedSubscriptions.getJSONObject(i).get("notificationMeta"));
112+
assertEquals(true, retrievedSubscriptions.getJSONObject(i).has("userName"));
113+
}
114+
}
115+
116+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ericsson.ei.subscriptions.bulk;
2+
3+
import cucumber.api.CucumberOptions;
4+
import cucumber.api.junit.Cucumber;
5+
import org.junit.runner.RunWith;
6+
7+
@RunWith(Cucumber.class)
8+
@CucumberOptions(features = "src/functionaltests/resources/features/subscriptionBulk.feature", glue = {
9+
"com.ericsson.ei.subscriptions.bulk" }, plugin = { "pretty",
10+
"html:target/cucumber-reports/TestSubscriptionBulkRunner" }, monochrome = false)
11+
public class TestSubscriptionBulkRunner {
12+
13+
}

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

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
package com.ericsson.ei.subscriptions.content;
22

3-
import com.ericsson.ei.controller.model.Subscription;
4-
import com.ericsson.ei.controller.model.SubscriptionResponse;
3+
import com.ericsson.ei.controller.model.GetSubscriptionResponse;
54
import com.ericsson.ei.utils.FunctionalTestBase;
65
import com.ericsson.ei.utils.HttpDeleteRequest;
7-
import com.ericsson.ei.utils.HttpPostRequest;
86
import com.ericsson.ei.utils.HttpGetRequest;
9-
7+
import com.ericsson.ei.utils.HttpPostRequest;
108
import com.fasterxml.jackson.databind.ObjectMapper;
11-
import org.junit.Ignore;
12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
149
import cucumber.api.java.en.And;
1510
import cucumber.api.java.en.Given;
1611
import cucumber.api.java.en.Then;
1712
import cucumber.api.java.en.When;
13+
import org.junit.Ignore;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
1816
import org.springframework.boot.web.server.LocalServerPort;
1917
import org.springframework.http.HttpStatus;
2018
import org.springframework.http.ResponseEntity;
@@ -29,9 +27,10 @@
2927
@Ignore
3028
public class SubscriptionContentSteps extends FunctionalTestBase {
3129

30+
private static final Logger LOGGER = LoggerFactory.getLogger(SubscriptionContentSteps.class);
31+
3232
@LocalServerPort
3333
private int applicationPort;
34-
private static final Logger LOGGER = LoggerFactory.getLogger(SubscriptionContentSteps.class);
3534
private HttpGetRequest getRequest;
3635
private HttpPostRequest postRequest;
3736
private HttpDeleteRequest deleteRequest;
@@ -102,28 +101,20 @@ public void duplicate_subscription_is_rejected() {
102101
}
103102

104103
@And("^\"([A-Za-z0-9_]+)\" is not duplicated$")
105-
public void is_not_duplicated(String name) {
106-
Subscription[] subscription = null;
104+
public void is_not_duplicated(String name) throws IOException {
107105
getRequest.setEndpoint("/subscriptions/" + name);
108106
response = getRequest.build();
109-
110-
try {
111-
subscription = mapper.readValue(response.getBody().toString(), Subscription[].class);
112-
} catch(IOException e) {
113-
LOGGER.error(e.getMessage(), e);
114-
}
115-
// Ensure only one subscription exists
116-
assertEquals(1, subscription.length);
107+
GetSubscriptionResponse getSubscriptionResponse = mapper.readValue(response.getBody().toString(), GetSubscriptionResponse.class);
108+
assertEquals(1, getSubscriptionResponse.getFoundSubscriptions().size());
117109
}
118110

119111
// SCENARIO 3
120112

121113
@Given("^I delete \"([A-Za-z0-9_]+)\"$")
122114
public void delete_subscription(String subscriptionName) {
123-
SubscriptionResponse subscriptionResponse = null;
124115
deleteRequest.setEndpoint("/subscriptions/" + subscriptionName);
125-
subscriptionResponse = deleteRequest.build();
126-
assertEquals("Deleted Successfully", subscriptionResponse.getMsg());
116+
response = deleteRequest.build();
117+
assertEquals(HttpStatus.OK.value(), response.getStatusCodeValue());
127118
}
128119

129120
@And("^Subscriptions does not exist$")
@@ -142,14 +133,15 @@ public void create_invalid_subscription_with(String invalidSubscriptionFile) {
142133

143134
@Then("^The invalid subscription is rejected$")
144135
public void invalid_subscription_is_rejected() {
145-
assertEquals(HttpStatus.PRECONDITION_FAILED.value(), response.getStatusCodeValue());
136+
assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatusCodeValue());
146137
}
147138

148139
@And("^The invalid subscription does not exist$")
149140
public void invalid_subscription_does_not_exist() {
150141
String invalidName = "#Subscription-&-with-&-mal-&-formatted-&-name";
151142
getRequest.setEndpoint("/subscriptions/" + invalidName);
152143
response = getRequest.build();
144+
assertEquals(HttpStatus.OK.value(), response.getStatusCodeValue());
153145
assertEquals("[]", response.getBody().toString());
154146
}
155147

0 commit comments

Comments
 (0)