|
1 | 1 | package com.ericsson.ei.subscriptions.crud;
|
2 | 2 |
|
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; |
4 | 8 |
|
| 9 | +import org.apache.commons.io.FileUtils; |
| 10 | +import org.json.JSONArray; |
| 11 | +import org.json.JSONException; |
| 12 | +import org.junit.Assert; |
5 | 13 | import org.junit.Ignore;
|
6 | 14 | import org.slf4j.Logger;
|
7 | 15 | 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; |
10 | 29 |
|
11 | 30 | import cucumber.api.java.en.Given;
|
12 | 31 | import cucumber.api.java.en.Then;
|
13 | 32 | import cucumber.api.java.en.When;
|
14 | 33 |
|
15 | 34 | @Ignore
|
| 35 | +@AutoConfigureMockMvc |
16 | 36 | 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; |
17 | 46 |
|
18 | 47 | 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 =============================================================================== |
19 | 88 |
|
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 | + } |
24 | 97 | }
|
25 | 98 |
|
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()); |
29 | 109 | }
|
| 110 | + // Scenario:2 ends========================================================================================= |
30 | 111 |
|
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 | + } |
33 | 184 |
|
| 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 | + } |
34 | 200 | }
|
| 201 | + ////Scenario:4 ends ========================================================================================== |
35 | 202 | }
|
0 commit comments