|
| 1 | +package org.mifos.integrationtest.cucumber.stepdef; |
| 2 | + |
| 3 | +import static com.github.tomakehurst.wiremock.client.WireMock.getAllServeEvents; |
| 4 | +import static com.google.common.truth.Truth.assertThat; |
| 5 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 6 | +import static org.awaitility.Awaitility.await; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import com.github.tomakehurst.wiremock.stubbing.ServeEvent; |
| 11 | +import com.icegreen.greenmail.util.GreenMail; |
| 12 | +import com.icegreen.greenmail.util.GreenMailUtil; |
| 13 | +import com.icegreen.greenmail.util.ServerSetupTest; |
| 14 | +import io.cucumber.core.internal.com.fasterxml.jackson.databind.JsonNode; |
| 15 | +import io.cucumber.datatable.DataTable; |
| 16 | +import io.cucumber.java.Before; |
| 17 | +import io.cucumber.java.en.And; |
| 18 | +import io.cucumber.java.en.Given; |
| 19 | +import io.cucumber.java.en.Then; |
| 20 | +import io.cucumber.java.en.When; |
| 21 | +import io.restassured.RestAssured; |
| 22 | +import io.restassured.builder.ResponseSpecBuilder; |
| 23 | +import io.restassured.specification.RequestSpecification; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Objects; |
| 28 | +import org.mifos.integrationtest.common.Utils; |
| 29 | +import org.mifos.integrationtest.common.dto.EmailRequestDTO; |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.beans.factory.annotation.Value; |
| 32 | +import org.springframework.core.env.Environment; |
| 33 | + |
| 34 | +public class EmailStepDef extends BaseStepDef { |
| 35 | + |
| 36 | + @Value("${callback_url}") |
| 37 | + private String callbackURL; |
| 38 | + |
| 39 | + @Value("${messageGateway.contactpoint}") |
| 40 | + private String messageGatewayHost; |
| 41 | + |
| 42 | + @Value("${messageGateway.endpoint.email}") |
| 43 | + private String mgMail; |
| 44 | + @Value("${spring.mail.host}") |
| 45 | + private String smtpHost; |
| 46 | + |
| 47 | + @Value("${spring.mail.port}") |
| 48 | + private int smtpPort; |
| 49 | + |
| 50 | + @Autowired |
| 51 | + private Environment env; |
| 52 | + |
| 53 | + private GreenMail greenMail; |
| 54 | + |
| 55 | + @Before |
| 56 | + public void setUp() { |
| 57 | + |
| 58 | + int port = Integer.parseInt(Objects.requireNonNull(env.getProperty("spring.mail.port"))); |
| 59 | + greenMail = new GreenMail(ServerSetupTest.SMTP); |
| 60 | + greenMail.setUser("greenmail", "greenmail"); |
| 61 | + greenMail.start(); |
| 62 | + } |
| 63 | + |
| 64 | + @Given("the email service is running") |
| 65 | + public void theEmailServiceIsRunning() { |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + @When("I send an email to the following recipients with subject {string} and body {string} with callbackurl as {string} and get {int}") |
| 70 | + public void iSendAnEmailToWithSubjectAndBody(String subject, String body, String url, int expectedStatus, DataTable dataTable) |
| 71 | + throws JsonProcessingException { |
| 72 | + List<String> to = dataTable.asList(String.class); |
| 73 | + EmailRequestDTO emailRequest = new EmailRequestDTO(to, subject, body); |
| 74 | + |
| 75 | + // Convert the payload to JSON |
| 76 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 77 | + String jsonPayload = objectMapper.writeValueAsString(emailRequest); |
| 78 | + |
| 79 | + // Get the default request specification |
| 80 | + RequestSpecification requestSpec = Utils.getDefaultSpec(); |
| 81 | + |
| 82 | + // Send the POST request and get the response |
| 83 | + scenarioScopeState.response = RestAssured.given(requestSpec).header("Content-Type", "application/json") |
| 84 | + .header("X-CallbackUrl", callbackURL + url).header("Correlation-Id", scenarioScopeState.clientCorrelationId) |
| 85 | + .header("Platform-Tenant-Id", scenarioScopeState.tenant).baseUri(messageGatewayHost).body(jsonPayload).expect() |
| 86 | + .spec(new ResponseSpecBuilder().expectStatusCode(expectedStatus).build()).when().post(mgMail).andReturn().asString(); |
| 87 | + |
| 88 | + logger.info("Mail Response {}", scenarioScopeState.response); |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + @Then("the email should be sent to all recipients with subject {string} and body {string}") |
| 93 | + public void theEmailShouldBeSentToWithSubjectAndBody(String subject, String body) throws Exception { |
| 94 | + await().atMost(awaitMost, SECONDS).pollInterval(pollInterval, SECONDS).untilAsserted(() -> { |
| 95 | + logger.info(String.valueOf(greenMail.getReceivedMessages().length)); |
| 96 | + assertThat(greenMail.getReceivedMessages().length == 1).isTrue(); |
| 97 | + |
| 98 | + String receivedTo = GreenMailUtil.getAddressList(greenMail.getReceivedMessages()[0].getAllRecipients()); |
| 99 | + String receivedSubject = greenMail.getReceivedMessages()[0].getSubject(); |
| 100 | + String receivedBody = GreenMailUtil.getBody(greenMail.getReceivedMessages()[0]); |
| 101 | + |
| 102 | + assertThat(receivedTo).isNotNull(); |
| 103 | + assertThat(subject.equals(receivedSubject)).isTrue(); |
| 104 | + assertThat(body.equals(receivedBody)).isTrue(); |
| 105 | + }); |
| 106 | + } |
| 107 | + @Then("I should be able to extract error from response") |
| 108 | + public void iShouldBeAbleToExtractErrorFromResponse() { |
| 109 | + assertThat(scenarioScopeState.response).isNotNull(); |
| 110 | + assertThat(scenarioScopeState.response).containsMatch("Bad Request"); |
| 111 | + } |
| 112 | + |
| 113 | + @And("I can verify callback recieved with value") |
| 114 | + public void iCanVerifyCallbackRecievedWithValue() { |
| 115 | + await().atMost(awaitMost, SECONDS).pollInterval(pollInterval, SECONDS).untilAsserted(() -> { |
| 116 | + boolean flag = false; |
| 117 | + List<ServeEvent> allServeEvents = getAllServeEvents(); |
| 118 | + for (int i = allServeEvents.size() - 1; i >= 0; i--) { |
| 119 | + ServeEvent request = allServeEvents.get(i); |
| 120 | + if (!(request.getRequest().getBodyAsString()).isEmpty() && request.getRequest().getUrl().equals("/sendMail")) { |
| 121 | + JsonNode rootNode = null; |
| 122 | + try { |
| 123 | + rootNode = objectMapper.readTree(request.getRequest().getBody()); |
| 124 | + logger.info("Rootnode value:" + rootNode); |
| 125 | + assertThat(rootNode.toString().contains("Email sent successfully")); |
| 126 | + } catch (IOException e) { |
| 127 | + throw new RuntimeException(e); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + }); |
| 133 | + } |
| 134 | +} |
0 commit comments