Skip to content

Commit ecb6701

Browse files
Functional Tests Subscription CRUD (#108)
* JMESPath REST API * JMESPath API with fixed reviews * partial work jmespath rest * JMESPath API with RAML 0.8 specification * remove componet comment * replace tab with spaces * fix usage examples * prepare for new asserts in flow test * fix unit tests * Subscription for individual events * pom file * subscription individual objects * pom * error removal * Subscription Individual Object * deleting unused import * jmespath comment correction * space removal and new clean build * review changes * review change file deleted * review change * review changes * log.info to log.error * required changes * raml * raml files * resolving conflict * self generated code raml * controllers auto generated * auto generated * removing ttl * adding ttl * copied pom * changes in app.properties * resolving conflict with upstream * adding more files * added time lapse variable * rebased... * new clone and update * reviewed changes * review changes-evasiba * review changes 2:evasiba * handling of authorization by Subscription REST API: 9584 * changing property name * pom updated * method change * reviewed changes-1 * some review changes regarding comments * some changes ´to get pass throguh tests * review changes * Update application.properties * Fix test * development * changes * code changes related to story 9561 * error * error removal * requested changes * reviewed change * reviewed changes * ongoing * functional tests CRUD * removed unwanted feature file * removed unwanted eclipse file * simple comment change
1 parent a21dcf1 commit ecb6701

File tree

4 files changed

+288
-26
lines changed

4 files changed

+288
-26
lines changed
Lines changed: 179 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,202 @@
11
package com.ericsson.ei.subscriptions.crud;
22

3-
import com.ericsson.ei.utils.FunctionalTestBase;
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.io.UnsupportedEncodingException;
48

9+
import org.apache.commons.io.FileUtils;
10+
import org.json.JSONArray;
11+
import org.json.JSONException;
12+
import org.junit.Assert;
513
import org.junit.Ignore;
614
import org.slf4j.Logger;
715
import org.slf4j.LoggerFactory;
8-
import org.springframework.test.context.TestExecutionListeners;
9-
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
16+
import org.springframework.beans.factory.annotation.Autowired;
17+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
18+
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.RequestBuilder;
23+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
24+
25+
import com.ericsson.ei.controller.model.Subscription;
26+
import com.ericsson.ei.controller.model.SubscriptionResponse;
27+
import com.ericsson.ei.utils.FunctionalTestBase;
28+
import com.fasterxml.jackson.databind.ObjectMapper;
1029

1130
import cucumber.api.java.en.Given;
1231
import cucumber.api.java.en.Then;
1332
import cucumber.api.java.en.When;
1433

1534
@Ignore
35+
@AutoConfigureMockMvc
1636
public class SubscriptionCRUDSteps extends FunctionalTestBase {
37+
38+
private static final String subscriptionJsonPath = "src/functionaltests/resources/subscription_single.json";
39+
private static final String subscriptionJsonPathUpdated = "src/functionaltests/resources/subscription_single_updated.json";
40+
41+
@Autowired
42+
private MockMvc mockMvc;
43+
MvcResult result;
44+
ObjectMapper mapper = new ObjectMapper();
45+
static JSONArray jsonArray = null;
1746

1847
private static final Logger LOGGER = LoggerFactory.getLogger(SubscriptionCRUDSteps.class);
48+
49+
50+
@Given("^The REST API \"([^\"]*)\" is up and running$")
51+
public void the_REST_API_is_up_and_running(String endPoint) {
52+
RequestBuilder requestBuilder = MockMvcRequestBuilders.get(endPoint).accept(MediaType.APPLICATION_JSON);
53+
try {
54+
result = mockMvc.perform(requestBuilder).andReturn();
55+
} catch (Exception e) {
56+
LOGGER.error(e.getMessage(), e);
57+
}
58+
Assert.assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus());
59+
}
60+
61+
@When("^I make a POST request with valid \"([^\"]*)\" to the subscription REST API \"([^\"]*)\"$")
62+
public void i_make_a_POST_request_with_valid_to_the_subscription_REST_API(String arg1, String endPoint) {
63+
String readFileToString = "";
64+
try {
65+
readFileToString = FileUtils.readFileToString(new File(subscriptionJsonPath), "UTF-8");
66+
} catch (IOException e) {
67+
LOGGER.error(e.getMessage(), e);
68+
}
69+
try {
70+
jsonArray = new JSONArray(readFileToString);
71+
} catch (JSONException e) {
72+
LOGGER.error(e.getMessage(), e);
73+
}
74+
RequestBuilder requestBuilder = MockMvcRequestBuilders.post(endPoint).accept(MediaType.APPLICATION_JSON)
75+
.content(jsonArray.toString()).contentType(MediaType.APPLICATION_JSON);
76+
try {
77+
result = mockMvc.perform(requestBuilder).andReturn();
78+
} catch (Exception e) {
79+
LOGGER.error(e.getMessage(), e);
80+
}
81+
}
82+
83+
@Then("^I get response code of (\\d+)$")
84+
public void i_get_response_code_of(int statusCode) {
85+
Assert.assertEquals(statusCode, result.getResponse().getStatus());
86+
}
87+
///Scenario:1 ends ===============================================================================
1988

20-
@Given("^Subscription is created$")
21-
public void subscription_is_created() throws Throwable {
22-
LOGGER.debug("Creating subscription");
23-
System.out.println("MongoDB port for " + this.getClass().getName() + " is: " + getMongoDbPort());
89+
@When("^I make a GET request with subscription name \"([^\"]*)\" to the subscription REST API \"([^\"]*)\"$")
90+
public void i_make_a_GET_request_with_subscription_name_to_the_subscription_REST_API(String name, String endPoint) {
91+
RequestBuilder requestBuilder = MockMvcRequestBuilders.get(endPoint).accept(MediaType.APPLICATION_JSON);
92+
try {
93+
result = mockMvc.perform(requestBuilder).andReturn();
94+
} catch (Exception e) {
95+
LOGGER.error(e.getMessage(), e);
96+
}
2497
}
2598

26-
@When("^Update the subscription$")
27-
public void update_the_subscription() throws Throwable {
28-
LOGGER.debug("Updating subscription");
99+
@Then("^I get response code of (\\d+) and subscription name \"([^\"]*)\"$")
100+
public void i_get_response_code_of_and_subscription_name(int statusCode, String name) {
101+
Subscription[] subscription = null;
102+
try {
103+
subscription = mapper.readValue(result.getResponse().getContentAsString(), Subscription[].class);
104+
} catch (IOException e) {
105+
LOGGER.error(e.getMessage(), e);
106+
}
107+
Assert.assertEquals(statusCode, result.getResponse().getStatus());
108+
Assert.assertEquals(name, subscription[0].getSubscriptionName());
29109
}
110+
// Scenario:2 ends=========================================================================================
30111

31-
@Then("^Delete the updated subscription$")
32-
public void delete_the_updated_subscription() throws Throwable {
112+
@When("^I make a PUT request with modified notificationType as \"([^\"]*)\" to REST API \"([^\"]*)\"$")
113+
public void i_make_a_PUT_request_with_modified_notificationType_as_to_REST_API(String notificationType,
114+
String endPoint){
115+
String readFileToString = null;
116+
try {
117+
readFileToString = FileUtils.readFileToString(new File(subscriptionJsonPathUpdated), "UTF-8");
118+
} catch (IOException e) {
119+
LOGGER.error(e.getMessage(), e);
120+
}
121+
try {
122+
jsonArray = new JSONArray(readFileToString);
123+
} catch (JSONException e) {
124+
LOGGER.error(e.getMessage(), e);
125+
}
126+
127+
RequestBuilder requestBuilder = MockMvcRequestBuilders.put(endPoint).accept(MediaType.APPLICATION_JSON)
128+
.content(jsonArray.toString()).contentType(MediaType.APPLICATION_JSON);
129+
try {
130+
result = mockMvc.perform(requestBuilder).andReturn();
131+
} catch (Exception e) {
132+
LOGGER.error(e.getMessage(), e);
133+
}
134+
LOGGER.info("Modified notificationType is:" + notificationType);
135+
}
136+
137+
@Then("^I get response code of (\\d+) for successful updation$")
138+
public void i_get_response_code_of_for_successful_updation(int statusCode) {
139+
Assert.assertEquals(statusCode, result.getResponse().getStatus());
140+
}
141+
142+
@Then("^I can validate modified notificationType \"([^\"]*)\" with GET request at \"([^\"]*)\"$")
143+
public void i_can_validate_modified_notificationType_with_GET_request_at(String notificationType, String endPoint) {
144+
RequestBuilder requestBuilder = MockMvcRequestBuilders.get(endPoint).accept(MediaType.APPLICATION_JSON);
145+
try {
146+
result = mockMvc.perform(requestBuilder).andReturn();
147+
} catch (Exception e) {
148+
LOGGER.error(e.getMessage(), e);
149+
}
150+
Subscription[] subscription = null;
151+
try {
152+
subscription = mapper.readValue(result.getResponse().getContentAsString(), Subscription[].class);
153+
} catch (IOException e) {
154+
LOGGER.error(e.getMessage(), e);
155+
}
156+
assertEquals(notificationType, subscription[0].getNotificationType());
157+
}
158+
//Scenario:3 ends ==========================================================================================
159+
160+
@When("^I make a DELETE request with subscription name \"([^\"]*)\" to the subscription REST API \"([^\"]*)\"$")
161+
public void i_make_a_DELETE_request_with_subscription_name_to_the_subscription_REST_API(String name,
162+
String endPoint) {
163+
endPoint = endPoint + name;
164+
RequestBuilder requestBuilder = MockMvcRequestBuilders.delete(endPoint).accept(MediaType.APPLICATION_JSON);
165+
try {
166+
result = mockMvc.perform(requestBuilder).andReturn();
167+
} catch (Exception e) {
168+
LOGGER.error(e.getMessage(), e);
169+
}
170+
}
171+
172+
@Then("^I get response code of (\\d+) for successful delete$")
173+
public void i_get_response_code_of_for_successful_delete(int statusCode) {
174+
Assert.assertEquals(statusCode, result.getResponse().getStatus());
175+
SubscriptionResponse subscriptionResponse = null;
176+
try {
177+
subscriptionResponse = mapper.readValue(result.getResponse().getContentAsString().toString(),
178+
SubscriptionResponse.class);
179+
} catch (IOException e) {
180+
LOGGER.error(e.getMessage(), e);
181+
}
182+
assertEquals("Deleted Successfully", subscriptionResponse.getMsg());
183+
}
33184

185+
@Then("^My GET request with subscription name \"([^\"]*)\" at REST API \"([^\"]*)\" returns empty String \"([^\"]*)\"$")
186+
public void my_GET_request_with_subscription_name_at_REST_API_returns_empty_String(String name, String endPoint, String emptyString) {
187+
endPoint = endPoint + name;
188+
RequestBuilder requestBuilder = MockMvcRequestBuilders.get(endPoint)
189+
.accept(MediaType.APPLICATION_JSON);
190+
try {
191+
result = mockMvc.perform(requestBuilder).andReturn();
192+
} catch (Exception e) {
193+
LOGGER.error(e.getMessage(), e);
194+
}
195+
try {
196+
assertEquals(emptyString, result.getResponse().getContentAsString());
197+
} catch (UnsupportedEncodingException e) {
198+
LOGGER.error(e.getMessage(), e);
199+
}
34200
}
201+
////Scenario:4 ends ==========================================================================================
35202
}

src/functionaltests/resources/features/subscriptionCRUD.feature

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,29 @@
2020
Feature: Test Subscription CRUD
2121

2222
@tag1
23-
Scenario: Title of your scenario
24-
Given Subscription is created
25-
When Update the subscription
26-
Then Delete the updated subscription
23+
Scenario: Create subscription with JSON object using REST API by POST method
24+
Given The REST API "/subscriptions" is up and running
25+
When I make a POST request with valid "JSON" to the subscription REST API "/subscriptions"
26+
Then I get response code of 200
27+
28+
#@tag2
29+
Scenario: Read subscription using REST API by GET method
30+
Given The REST API "/subscriptions" is up and running
31+
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"
33+
34+
#@tag3
35+
Scenario: Update subscription using REST API by PUT method and validate updation
36+
Given The REST API "/subscriptions" is up and running
37+
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+
And I can validate modified notificationType "MAIL" with GET request at "/subscriptions/Subscription_Test"
40+
41+
#@tag4
42+
Scenario: Delete subscription using REST API by DELETE method and validate deletion
43+
Given The REST API "/subscriptions" is up and running
44+
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 "[]"
47+
2748

28-
# @tag2
29-
# Scenario Outline: Title of your scenario outline
30-
# Given I want to write a step with <name>
31-
# When I check for the <value> in step
32-
# Then I verify the <status> in step
33-
#
34-
# Examples:
35-
# | name | value | status |
36-
# | name1 | 5 | success |
37-
# | name2 | 7 | Fail |
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[
2+
{
3+
"created" : "2017-07-26",
4+
"notificationMeta" : "http://127.0.0.1:3000/ei/test_subscription_rest",
5+
"notificationType" : "REST_POST",
6+
"restPostBodyMediaType" : "application/x-www-form-urlencoded",
7+
"notificationMessageKeyValues" : [
8+
{
9+
"formkey" : "e",
10+
"formvalue" : "{parameter: [{ name: 'jsonparams', value : to_string(@) }, { name: 'runpipeline', value : 'mybuildstep' }]}"
11+
}
12+
],
13+
"repeat" : false,
14+
"requirements" : [
15+
{
16+
"conditions" : [
17+
{
18+
"jmespath" : "gav.groupId=='com.mycompany.myproduct'"
19+
},
20+
{
21+
"jmespath" : "testCaseExecutions[?testCase.conclusion == 'SUCCESSFUL' && testCase.id=='TC5']"
22+
}
23+
],
24+
"type" : "ARTIFACT_1"
25+
},
26+
{
27+
"conditions" : [
28+
{
29+
"jmespath" : "gav.groupId=='com.mycompany.myproduct'"
30+
},
31+
{
32+
"jmespath" : "testCaseExecutions[?testCaseStartedEventId == '13af4a14-f951-4346-a1ba-624c79f10e98']"
33+
}
34+
],
35+
"type" : "ARTIFACT_1"
36+
}
37+
],
38+
"subscriptionName" : "Subscription_Test",
39+
"userName" : "ABC"
40+
41+
}
42+
]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[
2+
{
3+
"created" : "2017-07-26",
4+
"notificationMeta" : "http://127.0.0.1:3000/ei/test_subscription_rest",
5+
"notificationType" : "MAIL",
6+
"restPostBodyMediaType" : "application/x-www-form-urlencoded",
7+
"notificationMessageKeyValues" : [
8+
{
9+
"formkey" : "e",
10+
"formvalue" : "{parameter: [{ name: 'jsonparams', value : to_string(@) }, { name: 'runpipeline', value : 'mybuildstep' }]}"
11+
}
12+
],
13+
"repeat" : false,
14+
"requirements" : [
15+
{
16+
"conditions" : [
17+
{
18+
"jmespath" : "gav.groupId=='com.mycompany.myproduct'"
19+
},
20+
{
21+
"jmespath" : "testCaseExecutions[?testCase.conclusion == 'SUCCESSFUL' && testCase.id=='TC5']"
22+
}
23+
],
24+
"type" : "ARTIFACT_1"
25+
},
26+
{
27+
"conditions" : [
28+
{
29+
"jmespath" : "gav.groupId=='com.mycompany.myproduct'"
30+
},
31+
{
32+
"jmespath" : "testCaseExecutions[?testCaseStartedEventId == '13af4a14-f951-4346-a1ba-624c79f10e98']"
33+
}
34+
],
35+
"type" : "ARTIFACT_1"
36+
}
37+
],
38+
"subscriptionName" : "Subscription_Test",
39+
"userName" : "ABC"
40+
41+
}
42+
]

0 commit comments

Comments
 (0)