|
| 1 | +package com.ericsson.ei.frontend; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.nio.file.Paths; |
| 8 | +import java.util.concurrent.TimeoutException; |
| 9 | + |
| 10 | +import org.apache.commons.io.FileUtils; |
| 11 | +import org.apache.tomcat.util.codec.binary.Base64; |
| 12 | +import org.json.JSONArray; |
| 13 | +import org.json.JSONObject; |
| 14 | +import org.junit.Ignore; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | +import org.springframework.boot.test.context.SpringBootContextLoader; |
| 19 | +import org.springframework.boot.test.context.SpringBootTest; |
| 20 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 21 | +import org.springframework.boot.web.server.LocalServerPort; |
| 22 | +import org.springframework.http.HttpStatus; |
| 23 | +import org.springframework.http.ResponseEntity; |
| 24 | +import org.springframework.test.context.ContextConfiguration; |
| 25 | +import org.springframework.test.context.TestExecutionListeners; |
| 26 | +import org.springframework.test.context.junit4.SpringRunner; |
| 27 | +import org.springframework.test.context.support.AbstractTestExecutionListener; |
| 28 | +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; |
| 29 | + |
| 30 | +import com.ericsson.ei.utils.AMQPCommunication; |
| 31 | +import com.ericsson.ei.utils.HttpRequest; |
| 32 | +import com.ericsson.ei.utils.HttpRequest.HttpMethod; |
| 33 | + |
| 34 | +import cucumber.api.java.Before; |
| 35 | +import cucumber.api.java.en.Given; |
| 36 | +import cucumber.api.java.en.Then; |
| 37 | +import cucumber.api.java.en.When; |
| 38 | + |
| 39 | +@Ignore |
| 40 | +@RunWith(SpringRunner.class) |
| 41 | +@SpringBootTest(classes = EIFrontendApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) |
| 42 | +@ContextConfiguration(classes = EIFrontendApplication.class, loader = SpringBootContextLoader.class) |
| 43 | +@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class, CommonSteps.class }) |
| 44 | +public class CommonSteps extends AbstractTestExecutionListener { |
| 45 | + |
| 46 | + @LocalServerPort |
| 47 | + private int frontendPort; |
| 48 | + private String frontendHost = "localhost"; |
| 49 | + private String rabbitHost; |
| 50 | + private int rabbitPort; |
| 51 | + private String rabbitUsername; |
| 52 | + private String rabbitPassword; |
| 53 | + private String rabbitExchange; |
| 54 | + private String rabbitKey; |
| 55 | + |
| 56 | + private HttpRequest httpRequest; |
| 57 | + private ResponseEntity<String> response; |
| 58 | + |
| 59 | + private static final String RESOURCE_PATH = "src/integrationtest/resources/"; |
| 60 | + private static final String BODIES_PATH = "bodies/"; |
| 61 | + private static final String RESPONSES_PATH = "responses/"; |
| 62 | + private static final String EIFFEL_EVENT_FILE = "eiffel_event.json"; |
| 63 | + |
| 64 | + private static final Logger LOGGER = LoggerFactory.getLogger(CommonSteps.class); |
| 65 | + |
| 66 | + @Before("@QueryByIdScenario, @QueryFreestyleScenario") |
| 67 | + public void beforeQueryScenario() { |
| 68 | + rabbitHost = System.getProperty("rabbit.host"); |
| 69 | + rabbitPort = Integer.getInteger("rabbit.port"); |
| 70 | + rabbitUsername = System.getProperty("rabbit.username"); |
| 71 | + rabbitPassword = System.getProperty("rabbit.password"); |
| 72 | + rabbitExchange = System.getProperty("rabbit.exchange"); |
| 73 | + rabbitKey = System.getProperty("rabbit.key"); |
| 74 | + } |
| 75 | + |
| 76 | + @Given("^frontend is up and running$") |
| 77 | + public void frontend_running() { |
| 78 | + LOGGER.info("Front-end port: {}", frontendPort); |
| 79 | + assertEquals(true, frontendPort != 0); |
| 80 | + } |
| 81 | + |
| 82 | + @Given("^an aggregated object is created$") |
| 83 | + public void aggregated_object_created() throws IOException, TimeoutException { |
| 84 | + LOGGER.debug("Sending Eiffel events for aggregation."); |
| 85 | + String eventFilePath = Paths.get(RESOURCE_PATH, EIFFEL_EVENT_FILE).toString(); |
| 86 | + String eventFileContent = FileUtils.readFileToString(new File(eventFilePath), "UTF-8"); |
| 87 | + |
| 88 | + AMQPCommunication amqp = new AMQPCommunication(rabbitHost, rabbitPort, rabbitUsername, rabbitPassword); |
| 89 | + assertEquals(true, amqp.produceMessage(eventFileContent, rabbitExchange, rabbitKey)); |
| 90 | + amqp.closeConnection(); |
| 91 | + LOGGER.debug("Eiffel events sent."); |
| 92 | + } |
| 93 | + |
| 94 | + @When("^a \'(\\w+)\' request is prepared for REST API \'(.*)\'$") |
| 95 | + public void request_to_rest_api(String method, String endpoint) throws Throwable { |
| 96 | + LOGGER.info("Method: {}, Endpoint: {}", method, endpoint); |
| 97 | + httpRequest = new HttpRequest(HttpMethod.valueOf(method)); |
| 98 | + httpRequest.setHost(frontendHost).setPort(frontendPort).setEndpoint(endpoint); |
| 99 | + } |
| 100 | + |
| 101 | + @When("^\'(.*)\' is appended to endpoint$") |
| 102 | + public void append_to_endpoint(String append) throws Throwable { |
| 103 | + String endpoint = httpRequest.getEndpoint() + append; |
| 104 | + httpRequest.setEndpoint(endpoint); |
| 105 | + } |
| 106 | + |
| 107 | + @When("^param key \'(.*)\' with value \'(.*)\' is added$") |
| 108 | + public void add_param(String key, String value) throws Throwable { |
| 109 | + httpRequest.addParam(key, value); |
| 110 | + } |
| 111 | + |
| 112 | + @When("^body is set to file \'(.*)\'$") |
| 113 | + public void set_body(String filename) throws Throwable { |
| 114 | + String filePath = Paths.get(RESOURCE_PATH, BODIES_PATH, filename).toString(); |
| 115 | + String fileContent = FileUtils.readFileToString(new File(filePath), "UTF-8"); |
| 116 | + httpRequest.addHeader("Content-type", "application/json").setBody(fileContent); |
| 117 | + } |
| 118 | + |
| 119 | + @When("^aggregation is prepared with rules file \'(.*)\' and events file \'(.*)\'$") |
| 120 | + public void aggregation_is_prepared(String rulesFileName, String eventsFileName) throws Throwable { |
| 121 | + String rulesPath = Paths.get(RESOURCE_PATH, BODIES_PATH, rulesFileName).toString(); |
| 122 | + String eventsPath = Paths.get(RESOURCE_PATH, BODIES_PATH, eventsFileName).toString(); |
| 123 | + String rules = FileUtils.readFileToString(new File(rulesPath), "UTF-8"); |
| 124 | + String events = FileUtils.readFileToString(new File(eventsPath), "UTF-8"); |
| 125 | + String body = new JSONObject().put("listRulesJson", new JSONArray(rules)) |
| 126 | + .put("listEventsJson", new JSONArray(events)).toString(); |
| 127 | + httpRequest.setBody(body); |
| 128 | + } |
| 129 | + |
| 130 | + @When("^username \"(\\w+)\" and password \"(\\w+)\" is used as credentials$") |
| 131 | + public void with_credentials(String username, String password) throws Throwable { |
| 132 | + String auth = username + ":" + password; |
| 133 | + String encodedAuth = new String(Base64.encodeBase64(auth.getBytes()), "UTF-8"); |
| 134 | + httpRequest.addHeader("Authorization", "Basic " + encodedAuth); |
| 135 | + } |
| 136 | + |
| 137 | + @When("^request is sent$") |
| 138 | + public void request_sent() throws Throwable { |
| 139 | + response = httpRequest.performRequest(); |
| 140 | + } |
| 141 | + |
| 142 | + @Then("^response code (\\d+) is received$") |
| 143 | + public void get_response_code(int statusCode) throws Throwable { |
| 144 | + LOGGER.info("Response code: {}", response.getStatusCode()); |
| 145 | + assertEquals(HttpStatus.valueOf(statusCode), response.getStatusCode()); |
| 146 | + } |
| 147 | + |
| 148 | + @Then("^response body \'(.*)\' is received$") |
| 149 | + public void get_response_body(String body) throws Throwable { |
| 150 | + LOGGER.info("Response body: {}", response.getBody()); |
| 151 | + assertEquals(body, response.getBody()); |
| 152 | + } |
| 153 | + |
| 154 | + @Then("^response body from file \'(.*)\' is received$") |
| 155 | + public void get_response_body_from_file(String filename) throws Throwable { |
| 156 | + String filePath = Paths.get(RESOURCE_PATH, RESPONSES_PATH, filename).toString(); |
| 157 | + String fileContent = FileUtils.readFileToString(new File(filePath), "UTF-8"); |
| 158 | + LOGGER.info("File path: {}", filePath); |
| 159 | + LOGGER.info("Response body: {}", response.getBody()); |
| 160 | + assertEquals(fileContent.replaceAll("\\s+", ""), response.getBody().replaceAll("\\s+", "")); |
| 161 | + } |
| 162 | + |
| 163 | + @Then("^response body contains \'(.*)\'$") |
| 164 | + public void response_body_contains(String contains) throws Throwable { |
| 165 | + LOGGER.info("Response body: {}", response.getBody()); |
| 166 | + assertEquals(true, response.getBody().contains(contains)); |
| 167 | + } |
| 168 | +} |
0 commit comments