fix(manager-api-notifications): fix issues with template pat trie
MODIFY
manager/api/rest-impl/src/main/java/io/apiman/manager/api/notifications/email/SimpleMailNotificationService.java
manager/api/rest-impl/src/main/java/io/apiman/manager/api/notifications/email/SimpleMailNotificationService.java
@@ -11,12 +11,11 @@ import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
-import java.util.SortedMap;
-import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
@@ -32,9 +31,11 @@ import org.jetbrains.annotations.NotNull;
* <li>Send notification emails as configured via file in {@link ApiManagerConfig#getEmailNotificationProperties()} ()}.</li>
* <li>Get email notification templates (from a static file only, at present) by reason (or prefix thereof) or
* category.</li>
+ * <li>A convenient builder is available with {@link SimpleEmail#builder()}.</li>
* </ul>
*
* @see SmtpEmailConfiguration
+ * @see SimpleEmail
* @author Marc Savy {@literal <marc@blackparrotlabs.io>}
*/
@ApplicationScoped
@@ -71,15 +72,6 @@ public class SimpleMailNotificationService {
public SimpleMailNotificationService() {}
- @PostConstruct
- public void post() {
- System.out.println("hello");
- }
-
- public boolean isConfigured() {
- return isConfigured;
- }
-
/**
* Send an email
*/
@@ -109,6 +101,7 @@ public class SimpleMailNotificationService {
sendHtml(email.getToEmail(), email.getToName(), subject, html, plain, email.getHeaders());
}
}
+
/**
* Send a plaintext email
*/
@@ -175,8 +168,18 @@ public class SimpleMailNotificationService {
*
* @return Sorted map of prefix and corresponding template. Empty if no matches are found.
*/
- public SortedMap<String, EmailNotificationTemplate> findAllTemplatesFor(@NotNull String reasonKey) {
- return reasonTrie.subMap(reasonKey.substring(0, 1), reasonKey + "*"); // Add a character, as headMap only matches prefixes shorter
+ public List<EmailNotificationTemplate> findAllTemplatesFor(@NotNull String reasonKey) {
+ String fromKey = null;
+ String[] elems = reasonKey.split("\\.");
+ if (elems.length == 1) {
+ fromKey = reasonKey.substring(0, 1);
+ } else {
+ fromKey = elems[0];
+ }
+
+ List<EmailNotificationTemplate> list = new ArrayList<>(reasonTrie.subMap(fromKey, reasonKey.substring(0, reasonKey.length()-1)).values());
+ list.add(reasonTrie.select(reasonKey).getValue());
+ return list;
}
/**

MODIFY
manager/api/rest-impl/src/main/java/io/apiman/manager/api/service/OrganizationService.java
manager/api/rest-impl/src/main/java/io/apiman/manager/api/service/OrganizationService.java
@@ -14,7 +14,6 @@ import io.apiman.manager.api.beans.clients.ClientVersionBean;
import io.apiman.manager.api.beans.contracts.ContractBean;
import io.apiman.manager.api.beans.gateways.GatewayBean;
import io.apiman.manager.api.beans.idm.GrantRolesBean;
-import io.apiman.manager.api.beans.idm.PermissionType;
import io.apiman.manager.api.beans.idm.RoleBean;
import io.apiman.manager.api.beans.idm.RoleMembershipBean;
import io.apiman.manager.api.beans.idm.UserBean;
@@ -54,7 +53,6 @@ import io.apiman.manager.api.rest.exceptions.util.ExceptionFactory;
import io.apiman.manager.api.rest.impl.audit.AuditUtils;
import io.apiman.manager.api.rest.impl.util.DataAccessUtilMixin;
import io.apiman.manager.api.rest.impl.util.FieldValidator;
-import io.apiman.manager.api.rest.impl.util.RestHelper;
import io.apiman.manager.api.security.ISecurityContext;
import java.util.ArrayList;
@@ -101,8 +99,7 @@ public class OrganizationService implements DataAccessUtilMixin {
IRoleResource roles,
ISecurityContext securityContext,
IGatewayLinkFactory gatewayLinkFactory,
- ClientAppService clientService,
- SimpleMailNotificationService smns
+ ClientAppService clientService
) {
this.config = config;
this.storage = storage;
@@ -112,7 +109,6 @@ public class OrganizationService implements DataAccessUtilMixin {
this.securityContext = securityContext;
this.gatewayLinkFactory = gatewayLinkFactory;
this.clientService = clientService;
- smns.isConfigured();
}
public OrganizationService() {

MODIFY
manager/test/api/src/test/java/io/apiman/manager/api/notifications/email/SimpleMailNotificationServiceTest.java
manager/test/api/src/test/java/io/apiman/manager/api/notifications/email/SimpleMailNotificationServiceTest.java
@@ -12,7 +12,6 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
-import java.util.SortedMap;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Part;
@@ -32,12 +31,10 @@ import static org.assertj.core.api.Assertions.tuple;
* @author Marc Savy {@literal <marc@blackparrotlabs.io>}
*/
public class SimpleMailNotificationServiceTest {
+ // Pull templates from resources
+ File file = new File(getClass().getClassLoader().getResource("apiman/config/email-notification-templates.json").getFile());
- @Rule
- public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP);
-
- ClassLoader classLoader = getClass().getClassLoader();
- File file = new File(classLoader.getResource("apiman/config/email-notification-templates.json").getFile());
+ // Override default config with our test config
ApiManagerConfig config = new WarApiManagerConfig() {
public Path getConfigDirectory() {
return file.getParentFile().toPath();
@@ -45,11 +42,11 @@ public class SimpleMailNotificationServiceTest {
@Override
public Map<String, String> getEmailNotificationProperties() {
- return MOCK_CONFIG;
+ return TEST_CONFIG;
}
};
- Map<String, String> MOCK_CONFIG = Map.of(
+ Map<String, String> TEST_CONFIG = Map.of(
"smtp.mock", "false",
"smtp.ssl", "false",
"smtp.startTLSMode", "DISABLED",
@@ -61,6 +58,9 @@ public class SimpleMailNotificationServiceTest {
"smtp.password", "don't_look!"
);
+ @Rule
+ public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP);
+
@Before
public void beforeEach() throws Exception {
greenMail.getUserManager().createUser("marc@blackparrotlabs.io", "marc", "don't_look!");
@@ -202,16 +202,16 @@ public class SimpleMailNotificationServiceTest {
@Test
public void All_templates_matching_reason_prefix_are_returned_in_order() {
SimpleMailNotificationService service = new SimpleMailNotificationService(config, new QteTemplateEngine());
- SortedMap<String, EmailNotificationTemplate> allTemplates = service.findAllTemplatesFor("test.notification.reason");
+ List<EmailNotificationTemplate> allTemplates = service.findAllTemplatesFor("test.notification.reason");
assertThat(allTemplates)
- .containsOnlyKeys("test.notification", "test.notification.reason")
- .extractingFromEntries(
- e -> e.getValue().getNotificationReason(),
- e -> e.getValue().getSubject(),
- e -> e.getValue().getHtmlBody(),
- e -> e.getValue().getPlainBody(),
- e -> e.getValue().getCategory()
+ //.containsOnlyKeys("test.notification", "test.notification.reason")
+ .extracting(
+ e -> e.getNotificationReason(),
+ e -> e.getSubject(),
+ e -> e.getHtmlBody(),
+ e -> e.getPlainBody(),
+ e -> e.getCategory()
)
.containsOnly(
tuple(

MODIFY
manager/test/api/src/test/resources/apiman/config/email-notification-templates.json
manager/test/api/src/test/resources/apiman/config/email-notification-templates.json
"plainBody": "Hi {notification.recipient.fullName}",
"category": "API_ADMINISTRATION"
},
+ {
+ "notificationReason": "test.notification.reasob", // Should not match
+ "subject": "Pay attention!",
+ "htmlBody": "Salut {name}",
+ "plainBody": "Hola {name}",
+ "category": "OTHER"
+ },
+ {
+ "notificationReason": "test.notification.reasonn", // Should not match
+ "subject": "Pay attention!",
+ "htmlBody": "Salut {name}",
+ "plainBody": "Hola {name}",
+ "category": "OTHER"
+ },
{
"notificationReason": "test.notification.reason",
"subject": "Pay attention!",

DELETE
manager/test/api/tmlog.lck
manager/test/api/tmlog.lck
fix(manager-api-notifications): fix issues with template pat trie