Skip to content

Commit 54162cf

Browse files
authored
Merge pull request #164 from josdem/feature/163
[small]Feature/163
2 parents 8cb33d3 + 6404f23 commit 54162cf

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Copyright 2025 Jose Morales contact@josdem.io
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.josdem.jmailer.config;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNotNull;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
23+
import lombok.extern.slf4j.Slf4j;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.DisplayName;
26+
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.TestInfo;
28+
import org.mockito.Mock;
29+
import org.mockito.MockitoAnnotations;
30+
import org.springframework.mail.javamail.JavaMailSender;
31+
32+
@Slf4j
33+
class TemplateConfigTest {
34+
35+
private TemplateConfig templateConfig;
36+
37+
@Mock private JavaMailSender defaultMailSender;
38+
39+
@Mock private JavaMailSender vetlogMailSender;
40+
41+
@BeforeEach
42+
void setUp() {
43+
MockitoAnnotations.openMocks(this);
44+
templateConfig = new TemplateConfig(defaultMailSender, vetlogMailSender);
45+
}
46+
47+
@Test
48+
@DisplayName("getting a template strategies")
49+
void shouldGetTemplateStrategies(TestInfo testInfo) {
50+
log.info(testInfo.getDisplayName());
51+
var templateStrategy = templateConfig.templateStrategy();
52+
assertNotNull(templateStrategy);
53+
assertEquals(8, templateStrategy.size());
54+
assertTrue(templateStrategy.containsKey("welcome.ftl"));
55+
assertTrue(templateStrategy.containsKey("welcome_es.ftl"));
56+
assertTrue(templateStrategy.containsKey("adoption.ftl"));
57+
assertTrue(templateStrategy.containsKey("adoption_es.ftl"));
58+
assertTrue(templateStrategy.containsKey("forgotPassword.ftl"));
59+
assertTrue(templateStrategy.containsKey("forgotPassword_es.ftl"));
60+
assertTrue(templateStrategy.containsKey("message.ftl"));
61+
assertTrue(templateStrategy.containsKey("register.ftl"));
62+
}
63+
64+
@Test
65+
@DisplayName("getting a default client")
66+
void shouldGetDefaultClient(TestInfo testInfo) {
67+
log.info(testInfo.getDisplayName());
68+
var templateStrategy = templateConfig.templateStrategy();
69+
var defaultClient = templateStrategy.get("message.ftl");
70+
assertNotNull(defaultClient);
71+
}
72+
73+
@Test
74+
@DisplayName("getting a vetlog client")
75+
void shouldGetVetlogClient(TestInfo testInfo) {
76+
log.info(testInfo.getDisplayName());
77+
var templateStrategy = templateConfig.templateStrategy();
78+
var vetlogClient = templateStrategy.get("welcome.ftl");
79+
assertNotNull(vetlogClient);
80+
}
81+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2025 Jose Morales contact@josdem.io
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.josdem.jmailer.exception;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
21+
import lombok.extern.slf4j.Slf4j;
22+
import org.junit.jupiter.api.DisplayName;
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.TestInfo;
25+
26+
@Slf4j
27+
class BusinessExceptionTest {
28+
29+
@Test
30+
@DisplayName("creating a business exception")
31+
void shouldCreateBusinessException(TestInfo testInfo) {
32+
log.info(testInfo.getDisplayName());
33+
var exception = new BusinessException("message", new RuntimeException("cause"));
34+
assertEquals("message", exception.getMessage());
35+
assertEquals("cause", exception.getCause().getMessage());
36+
}
37+
}

services/src/test/java/com/josdem/jmailer/service/MailServiceTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,29 @@
1313

1414
package com.josdem.jmailer.service;
1515

16+
import static org.junit.jupiter.api.Assertions.assertThrows;
1617
import static org.mockito.BDDMockito.given;
1718
import static org.mockito.Mockito.isA;
1819
import static org.mockito.Mockito.mock;
1920
import static org.mockito.Mockito.verify;
2021

22+
import com.josdem.jmailer.exception.BusinessException;
2123
import com.josdem.jmailer.model.Client;
2224
import com.josdem.jmailer.service.impl.MailServiceImpl;
2325
import freemarker.template.Configuration;
2426
import freemarker.template.Template;
2527
import java.io.IOException;
2628
import java.util.HashMap;
2729
import java.util.Map;
30+
import lombok.extern.slf4j.Slf4j;
2831
import org.junit.jupiter.api.BeforeEach;
2932
import org.junit.jupiter.api.DisplayName;
3033
import org.junit.jupiter.api.Test;
34+
import org.junit.jupiter.api.TestInfo;
3135
import org.springframework.mail.javamail.JavaMailSender;
3236
import org.springframework.mail.javamail.MimeMessagePreparator;
3337

38+
@Slf4j
3439
class MailServiceTest {
3540

3641
private static final String DEFAULT_TEMPLATE = "message.ftl";
@@ -53,6 +58,16 @@ void setup() {
5358
mailService = new MailServiceImpl(configuration, templateStrategy);
5459
}
5560

61+
@Test
62+
@DisplayName("throwing exception when template is not found")
63+
void shouldThrowExceptionWhenTemplateIsNotFound(TestInfo testInfo) {
64+
log.info(testInfo.getDisplayName());
65+
var templateName = "nonexistent.ftl";
66+
assertThrows(
67+
BusinessException.class,
68+
() -> mailService.sendMailWithTemplate(values, model, templateName));
69+
}
70+
5671
@Test
5772
@DisplayName("sending mail with default template")
5873
void shouldSendMailWithDefaultTemplate() throws IOException {

web/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ val freeMarkerVersion = "2.3.34"
1515
val jmsApiVersion = "2.0.1"
1616

1717
group = "com.josdem.jmailer"
18-
version = "1.4.8"
18+
version = "1.4.9"
1919

2020

2121
java {

0 commit comments

Comments
 (0)