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