Skip to content

Commit 304fea6

Browse files
Add functional tests on Subscription content (#112)
Added Cucumber feature with scenarios for valid, invalid and duplicate subscriptions.
1 parent 370f3b2 commit 304fea6

File tree

9 files changed

+446
-8
lines changed

9 files changed

+446
-8
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package com.ericsson.ei.subscriptions.content;
2+
3+
import com.ericsson.ei.controller.model.Subscription;
4+
import com.ericsson.ei.controller.model.SubscriptionResponse;
5+
import com.ericsson.ei.utils.FunctionalTestBase;
6+
import com.ericsson.ei.utils.HttpRequests;
7+
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import org.junit.Ignore;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import cucumber.api.java.en.And;
13+
import cucumber.api.java.en.Given;
14+
import cucumber.api.java.en.Then;
15+
import cucumber.api.java.en.When;
16+
import org.springframework.boot.web.server.LocalServerPort;
17+
import org.springframework.http.HttpStatus;
18+
import org.springframework.http.ResponseEntity;
19+
20+
import javax.annotation.PostConstruct;
21+
22+
import java.io.IOException;
23+
24+
import static org.junit.Assert.assertEquals;
25+
26+
27+
@Ignore
28+
public class SubscriptionContentSteps extends FunctionalTestBase {
29+
30+
@LocalServerPort
31+
int applicationPort;
32+
33+
private static final Logger LOGGER = LoggerFactory.getLogger(SubscriptionContentSteps.class);
34+
private HttpRequests request;
35+
private ResponseEntity response;
36+
37+
private String validSubscriptionFile = "src/functionaltests/resources/ValidSubscription.json";
38+
private String invalidSubscriptionFile = "src/functionaltests/resources/InvalidSubscription.json";
39+
40+
ObjectMapper mapper = new ObjectMapper();
41+
42+
@PostConstruct
43+
private void setUp() {
44+
request = new HttpRequests(applicationPort);
45+
}
46+
47+
48+
// SCENARIO 1
49+
50+
51+
@Given("^No subscriptions exist$")
52+
public void fetch_subscriptions() {
53+
String url = "http://localhost:" + applicationPort + "/subscriptions";
54+
response = request.makeHttpGetRequest(url);
55+
assertEquals("[]", response.getBody().toString());
56+
}
57+
58+
@When("^Create subscription request$")
59+
public void send_subscription_request() {
60+
String url = "http://localhost:" + applicationPort + "/subscriptions";
61+
response = request.makeHttpPostRequest(url, validSubscriptionFile);
62+
}
63+
64+
@Then("^The subscription is created successfully$")
65+
public void subscription_created_successfully() {
66+
assertEquals(HttpStatus.OK.value(), response.getStatusCodeValue());
67+
}
68+
69+
@And("^Valid subscription exists$")
70+
public void ensure_valid_subscription_exists() {
71+
String url = "http://localhost:" + applicationPort + "/subscriptions/mySubscription";
72+
response = request.makeHttpGetRequest(url);
73+
assertEquals(HttpStatus.OK.value(), response.getStatusCodeValue());
74+
}
75+
76+
77+
// SCENARIO 2
78+
79+
80+
@Given("^Subscription ([A-Za-z0-9_]+) already exists$")
81+
public void check_a_subscription_exists(String subscriptionName) {
82+
String url = "http://localhost:" + applicationPort + "/subscriptions/" + subscriptionName;
83+
response = request.makeHttpGetRequest(url);
84+
assertEquals(HttpStatus.OK.value(), response.getStatusCodeValue());
85+
}
86+
87+
@When("^I create a duplicate subscription$")
88+
public void create_duplicate_subscription() {
89+
String url = "http://localhost:" + applicationPort + "/subscriptions";
90+
response = request.makeHttpPostRequest(url, validSubscriptionFile);
91+
}
92+
93+
@Then("^The new subscription is rejected$")
94+
public void duplicate_subscription_is_rejected() {
95+
assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatusCodeValue());
96+
}
97+
98+
@And("^([A-Za-z0-9_]+) is not duplicated$")
99+
public void check_duplicate_was_not_created(String name) {
100+
String url = "http://localhost:" + applicationPort + "/subscriptions/" + name;
101+
Subscription[] subscription = null;
102+
103+
response = request.makeHttpGetRequest(url);
104+
105+
try {
106+
subscription = mapper.readValue(response.getBody().toString(), Subscription[].class);
107+
} catch(IOException e) {
108+
LOGGER.error(e.getMessage(), e);
109+
}
110+
111+
// Ensure only one subscription exists
112+
assertEquals(1, subscription.length);
113+
}
114+
115+
116+
// SCENARIO 3
117+
118+
@Given("^I delete ([A-Za-z0-9_]+)$")
119+
public void delete_subscription(String subscriptionName) {
120+
SubscriptionResponse subscriptionResponse = null;
121+
String url = "http://localhost:" + applicationPort + "/subscriptions/mySubscription";
122+
123+
subscriptionResponse = request.makeHttpDeleteRequest(url);
124+
assertEquals("Deleted Successfully", subscriptionResponse.getMsg());
125+
}
126+
127+
@And("^Subscriptions does not exist$")
128+
public void get_all_subscriptions() {
129+
ResponseEntity<String> response = null;
130+
String url = "http://localhost:" + applicationPort + "/subscriptions";
131+
132+
response = request.makeHttpGetRequest(url);
133+
134+
assertEquals(HttpStatus.OK.value(), response.getStatusCodeValue());
135+
assertEquals("[]", response.getBody().toString());
136+
}
137+
138+
@When("^I create an invalid subscription$")
139+
public void create_invalid_subscription() {
140+
String url = "http://localhost:" + applicationPort + "/subscriptions";
141+
response = request.makeHttpPostRequest(url, invalidSubscriptionFile);
142+
}
143+
144+
@Then("^The invalid subscription is rejected$")
145+
public void invalid_subscription_is_rejected() {
146+
assertEquals(HttpStatus.PRECONDITION_FAILED.value(), response.getStatusCodeValue());
147+
}
148+
149+
@And("^The invalid subscription does not exist$")
150+
public void ensure_invalid_subscription_not_exists() {
151+
String url = "http://localhost:" + applicationPort + "/subscriptions";
152+
String invalidName = "#Subscription-&-with-&-mal-&-formatted-&-name";
153+
154+
response = request.makeHttpGetRequest(url + invalidName);
155+
assertEquals("[]", response.getBody().toString());
156+
}
157+
158+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ericsson.ei.subscriptions.content;
2+
3+
import org.junit.runner.RunWith;
4+
5+
import cucumber.api.CucumberOptions;
6+
import cucumber.api.junit.Cucumber;
7+
8+
@RunWith(Cucumber.class)
9+
@CucumberOptions(features = "src/functionaltests/resources/features/subscriptionContent.feature", glue = {
10+
"com.ericsson.ei.subscriptions.content" })
11+
public class TestSubscriptionContentRunner {
12+
13+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
*/
4040
@Ignore
4141
@RunWith(SpringRunner.class)
42-
@SpringBootTest(classes = App.class)
42+
@SpringBootTest(classes = App.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
4343
@ContextConfiguration(classes = App.class, loader = SpringBootContextLoader.class, initializers = TestContextInitializer.class)
4444
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class, FunctionalTestBase.class })
4545
public class FunctionalTestBase extends AbstractTestExecutionListener {
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.ericsson.ei.utils;
2+
3+
import com.ericsson.ei.controller.model.SubscriptionResponse;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.apache.commons.io.FileUtils;
6+
import org.apache.http.client.methods.*;
7+
import org.apache.http.entity.StringEntity;
8+
import org.apache.http.impl.client.CloseableHttpClient;
9+
import org.apache.http.impl.client.HttpClientBuilder;
10+
import org.json.JSONArray;
11+
import org.json.JSONException;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
import org.springframework.http.HttpStatus;
15+
import org.springframework.http.ResponseEntity;
16+
17+
import java.io.*;
18+
19+
20+
public class HttpRequests {
21+
private static final Logger LOGGER = LoggerFactory.getLogger(HttpRequests.class);
22+
private int port;
23+
private CloseableHttpClient client = HttpClientBuilder.create().build();
24+
static JSONArray jsonParams = null;
25+
private ObjectMapper mapper = new ObjectMapper();
26+
27+
28+
public HttpRequests(int applicationPort) {
29+
port = applicationPort;
30+
}
31+
32+
/**
33+
* Create a HTTP GET request
34+
*
35+
* @param url
36+
*
37+
* */
38+
public ResponseEntity<String> makeHttpGetRequest(String url) {
39+
ResponseEntity<String> response = null;
40+
41+
HttpGet httpGet = new HttpGet(url);
42+
response = getResponse(httpGet);
43+
44+
return response;
45+
}
46+
47+
48+
/**
49+
* Create a POST request
50+
*
51+
* @param url
52+
* Where to send request
53+
* @param filePath
54+
* Location to json file containing subscription
55+
* */
56+
public ResponseEntity<String> makeHttpPostRequest(String url, String filePath) {
57+
ResponseEntity<String> response = null;
58+
String fileContent = "";
59+
60+
try {
61+
fileContent = FileUtils.readFileToString(new File(filePath), "UTF-8");
62+
} catch(IOException e) {
63+
LOGGER.error(e.getMessage(), e);
64+
System.out.println("ERROR: " + e.getMessage());
65+
}
66+
67+
try {
68+
jsonParams = new JSONArray(fileContent);
69+
} catch(JSONException e) {
70+
LOGGER.error(e.getMessage(), e);
71+
System.out.println("ERROR: " + e.getMessage());
72+
}
73+
74+
HttpPost httpPost = new HttpPost(url);
75+
76+
// add request parameters
77+
StringEntity params = new StringEntity(jsonParams.toString(), "UTF-8");
78+
httpPost.setEntity(params);
79+
80+
// add headers
81+
httpPost.addHeader("content-type", "application/json;charset=UTF-8");
82+
httpPost.addHeader("Accept", "application/json");
83+
84+
response = getResponse(httpPost);
85+
86+
return response;
87+
}
88+
89+
/**
90+
* Delete a given subscription
91+
*
92+
* The url should include the name of subscription which should be deleted
93+
* */
94+
public SubscriptionResponse makeHttpDeleteRequest(String url) {
95+
ResponseEntity<String> response = null;
96+
SubscriptionResponse subscriptionResponse = null;
97+
98+
HttpDelete httpDelete = new HttpDelete(url);
99+
response = getResponse(httpDelete);
100+
101+
try {
102+
subscriptionResponse = mapper.readValue(response.getBody().toString(), SubscriptionResponse.class);
103+
} catch(IOException e) {
104+
LOGGER.error(e.getMessage(), e);
105+
}
106+
107+
return subscriptionResponse;
108+
}
109+
110+
111+
/**
112+
* Handle the response from a HTTP request
113+
* @param request
114+
* A HTTP request method, e.g. httpGet, httpPost
115+
*
116+
* @return ResponseEntity
117+
* containing the json content of the http response and statuscode from request
118+
* */
119+
public ResponseEntity<String> getResponse(HttpRequestBase request) {
120+
int statusCode = HttpStatus.PROCESSING.value();
121+
String jsonContent = "";
122+
123+
try(CloseableHttpResponse httpResponse = client.execute(request)) {
124+
125+
InputStream inStream = httpResponse.getEntity().getContent();
126+
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
127+
128+
for (String line = bufReader.readLine(); line != null; line = bufReader.readLine()) {
129+
jsonContent += line;
130+
}
131+
if (jsonContent.isEmpty()) {
132+
jsonContent = "[]";
133+
}
134+
bufReader.close();
135+
inStream.close();
136+
137+
statusCode = httpResponse.getStatusLine().getStatusCode();
138+
139+
} catch(IOException e) {
140+
LOGGER.error(e.getMessage(), e);
141+
System.out.println("ERROR: " + e.getMessage());
142+
}
143+
144+
return new ResponseEntity<>(jsonContent, HttpStatus.valueOf(statusCode));
145+
}
146+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"subscriptionName": "#Subscription-&-with-&-mal-&-formatted-&-name",
4+
"userName" : "DEF",
5+
"repeat": false,
6+
"created": "data-time",
7+
"notificationType": "wrong notification type",
8+
"notificationMeta": "http://127.0.0.1:3000/ei/test_subscription_rest",
9+
"restPostBodyMediaType": "application/json",
10+
"notificationMessageKeyValues" : [
11+
{
12+
"formkey" : "",
13+
"formvalue" : "@"
14+
}
15+
],
16+
17+
"requirements": [
18+
{
19+
"type": "ARTIFACT_1",
20+
"conditions": [
21+
{"jmespath": "gav.groupId=='com.mycompany.myproduct'"},
22+
{"jmespath": "testCaseExecutions[?testCase.conclusion == 'SUCCESSFUL' && testCase.id=='TC5']"}
23+
]
24+
25+
},
26+
{
27+
"type": "ARTIFACT_1",
28+
"conditions": [
29+
{"jmespath": "gav.groupId=='com.mycompany.myproduct'"},
30+
{"jmespath": "testCaseExecutions[?testCaseStartedEventId == '13af4a14-f951-4346-a1ba-624c79f10e98']"}
31+
]
32+
33+
}
34+
]
35+
}
36+
]

0 commit comments

Comments
 (0)