File tree Expand file tree Collapse file tree 2 files changed +7
-5
lines changed
main/java/cf/maybelambda/httpvalidator/springboot/service
test/java/cf/maybelambda/httpvalidator/springboot/service Expand file tree Collapse file tree 2 files changed +7
-5
lines changed Original file line number Diff line number Diff line change 88import org .slf4j .Logger ;
99import org .slf4j .LoggerFactory ;
1010import org .springframework .beans .factory .annotation .Autowired ;
11+ import org .springframework .beans .factory .annotation .Value ;
1112import org .springframework .core .env .Environment ;
1213import org .springframework .lang .NonNull ;
1314import org .springframework .stereotype .Service ;
1819import static cf .maybelambda .httpvalidator .springboot .util .HttpSendOutcomeWrapper .NET_ERR_CODE ;
1920import static io .micrometer .common .util .StringUtils .isBlank ;
2021import 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 ())
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments