|
| 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 | +} |
0 commit comments