Skip to content

Commit f93df2b

Browse files
committed
[PHEE-296A] Email Api Tc
1 parent abab0a9 commit f93df2b

File tree

6 files changed

+201
-0
lines changed

6 files changed

+201
-0
lines changed

.jit/jit-integration.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ dependencies {
172172
testImplementation 'org.apache.commons:commons-csv:1.5'
173173
testImplementation 'org.awaitility:awaitility:4.2.0'
174174
implementation 'commons-validator:commons-validator:1.7'
175+
testImplementation 'com.icegreen:greenmail-junit5:1.6.1'
175176
}
176177

177178
tasks.named('test') {

src/main/resources/application.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,22 @@ fspconfig:
271271
payerfsp2: "gorilla"
272272

273273
totalvouchers: 30
274+
275+
276+
messageGateway:
277+
contactpoint: "https://messagegateway.sandbox.fynarfin.io"
278+
endpoint:
279+
email: "/emails"
280+
281+
spring:
282+
mail:
283+
host: ${MY_POD_IP:localhost}
284+
port: 3025
285+
username: greenmail
286+
password: greenmail
287+
properties:
288+
mail:
289+
smtp:
290+
auth: false
291+
starttls:
292+
enable: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.mifos.integrationtest.common.dto;
2+
3+
import java.util.List;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@Getter
8+
@Setter
9+
public class EmailRequestDTO {
10+
11+
private List<String> to;
12+
private String subject;
13+
private String body;
14+
15+
public EmailRequestDTO(List<String> to, String subject, String body) {
16+
this.to = to;
17+
this.subject = subject;
18+
this.body = body;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
}

src/test/java/resources/email.feature

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Feature: Send Email
2+
3+
@gov
4+
Scenario: Sending an email to the recipient with success
5+
Given I can inject MockServer
6+
And I can start mock server
7+
And I can register the stub with "/sendMail" endpoint for "POST" request with status of 200
8+
And the email service is running
9+
And I have tenant as "paymentBB2"
10+
And I generate clientCorrelationId
11+
When I send an email to the following recipients with subject "Test Email" and body "This is a test email" with callbackurl as "/sendMail" and get 202
12+
| recipient1@example.com |
13+
And I can verify callback recieved with value
14+
Then the email should be sent to all recipients with subject "Test Email" and body "This is a test email"
15+
16+
@gov
17+
Scenario: Sending an email to the recipient
18+
Given I can inject MockServer
19+
And I can start mock server
20+
And I can register the stub with "/sendMail" endpoint for "POST" request with status of 200
21+
And the email service is running
22+
And I have tenant as "paymentBB2"
23+
And I generate clientCorrelationId
24+
When I send an email to the following recipients with subject "" and body "This is a test email" with callbackurl as "/sendMail" and get 400
25+
| recipient1@example.com |
26+
Then I should be able to extract error from response

0 commit comments

Comments
 (0)