Skip to content

Commit 9635d5d

Browse files
committed
Initialize mailer client in constructor
1 parent 8a6cb56 commit 9635d5d

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/main/java/cf/maybelambda/httpvalidator/springboot/service/EmailNotificationService.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
1010
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.beans.factory.annotation.Value;
1112
import org.springframework.core.env.Environment;
1213
import org.springframework.lang.NonNull;
1314
import org.springframework.stereotype.Service;
@@ -18,7 +19,6 @@
1819
import static cf.maybelambda.httpvalidator.springboot.util.HttpSendOutcomeWrapper.NET_ERR_CODE;
1920
import static io.micrometer.common.util.StringUtils.isBlank;
2021
import static io.micrometer.common.util.StringUtils.truncate;
21-
import static java.util.Objects.isNull;
2222

2323
/**
2424
* Service to send email notifications using external SMTP service.
@@ -37,6 +37,10 @@ public class EmailNotificationService {
3737
@Autowired
3838
private Environment env;
3939

40+
public EmailNotificationService(@Value("${" + APIKEY_PROPERTY + "}") String apiKey) {
41+
this.client = MailgunClient.config(apiKey).createApi(MailgunMessagesApi.class);
42+
}
43+
4044
/**
4145
* Builds the email body content from a list of validation results.
4246
*
@@ -70,8 +74,6 @@ String buildMailBody(List<String[]> contents) {
7074
void sendPlainTextEmail(String subject, String body) throws ConnectIOException {
7175
if (!this.isValidConfig()) return;
7276

73-
if (isNull(this.client)) this.client = MailgunClient.config(this.getApiKey()).createApi(MailgunMessagesApi.class);
74-
7577
Message message = Message.builder()
7678
.from(this.getFrom())
7779
.to(this.getTo())

src/test/java/cf/maybelambda/httpvalidator/springboot/service/EmailNotificationServiceTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class EmailNotificationServiceTests {
3939

4040
@BeforeEach
4141
void setUp() {
42-
this.mailServ = new EmailNotificationService();
42+
this.mailServ = new EmailNotificationService("apiKey");
4343
this.mailServ.setClient(this.cl);
4444
this.mailServ.setEnv(this.env);
4545
}
@@ -171,7 +171,7 @@ void emailNotificationServiceInstanceUsesNewMailerClientToSendEmailWhenNoneWasSe
171171
given(this.env.getProperty(APIKEY_PROPERTY)).willReturn("apiKey");
172172
given(this.env.getProperty(FROM_PROPERTY)).willReturn("[email protected]");
173173
given(this.env.getProperty(TO_PROPERTY)).willReturn("[email protected]");
174-
EmailNotificationService serv = new EmailNotificationService();
174+
EmailNotificationService serv = new EmailNotificationService("apiKey");
175175
serv.setEnv(this.env);
176176
serv.setLogger(this.logger);
177177

0 commit comments

Comments
 (0)