From 215dbe617112c453d14f4cd930bd69b28e88a47c Mon Sep 17 00:00:00 2001 From: Thumimku Date: Tue, 27 Aug 2024 11:25:12 +0530 Subject: [PATCH 1/8] inmemory init --- .../email/mgt/EmailTemplateManagerImpl.java | 6 +- .../mgt/internal/I18nMgtServiceComponent.java | 12 +- .../mgt/store/DefaultTemplateManager.java | 211 ++++++++++++++ .../store/InMemoryBasedTemplateManager.java | 263 ++++++++++++++++++ .../InMemoryBasedTemplateManagerTest.java | 194 +++++++++++++ .../src/test/resources/testng.xml | 1 + 6 files changed, 678 insertions(+), 9 deletions(-) create mode 100644 components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DefaultTemplateManager.java create mode 100644 components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java create mode 100644 components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManagerTest.java diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/EmailTemplateManagerImpl.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/EmailTemplateManagerImpl.java index a1f4c11f..aeb90a5a 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/EmailTemplateManagerImpl.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/EmailTemplateManagerImpl.java @@ -22,8 +22,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.email.mgt.constants.I18nMgtConstants; +import org.wso2.carbon.email.mgt.store.DefaultTemplateManager; import org.wso2.carbon.email.mgt.store.TemplatePersistenceManager; -import org.wso2.carbon.email.mgt.store.TemplatePersistenceManagerFactory; import org.wso2.carbon.email.mgt.exceptions.I18nEmailMgtClientException; import org.wso2.carbon.email.mgt.exceptions.I18nEmailMgtException; import org.wso2.carbon.email.mgt.exceptions.I18nEmailMgtInternalException; @@ -78,8 +78,8 @@ public class EmailTemplateManagerImpl implements EmailTemplateManager, Notificat } public EmailTemplateManagerImpl() { - TemplatePersistenceManagerFactory templatePersistenceManagerFactory = new TemplatePersistenceManagerFactory(); - templatePersistenceManager = templatePersistenceManagerFactory.getTemplatePersistenceManager(); + + this.templatePersistenceManager = new DefaultTemplateManager(); } @Override diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java index 9946ba51..42394829 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java @@ -87,6 +87,12 @@ protected void activate(ComponentContext context) { try { BundleContext bundleCtx = context.getBundleContext(); + // Load default notification templates from file + I18nMgtDataHolder.getInstance().setDefaultEmailTemplates( + loadDefaultTemplatesFromFile(NotificationChannels.EMAIL_CHANNEL.getChannelType())); + I18nMgtDataHolder.getInstance().setDefaultSMSTemplates( + loadDefaultTemplatesFromFile(NotificationChannels.SMS_CHANNEL.getChannelType())); + // Register Email Mgt Service as an OSGi service. EmailTemplateManagerImpl emailTemplateManager = new EmailTemplateManagerImpl(); ServiceRegistration emailTemplateSR = bundleCtx.registerService(EmailTemplateManager.class.getName(), @@ -135,12 +141,6 @@ protected void activate(ComponentContext context) { log.error("Error registering SMS Provider Payload Template Mgt Service."); } - // Load default notification templates from file - I18nMgtDataHolder.getInstance().setDefaultEmailTemplates( - loadDefaultTemplatesFromFile(NotificationChannels.EMAIL_CHANNEL.getChannelType())); - I18nMgtDataHolder.getInstance().setDefaultSMSTemplates( - loadDefaultTemplatesFromFile(NotificationChannels.SMS_CHANNEL.getChannelType())); - // Load default notification templates. loadDefaultEmailTemplates(); loadDefaultSMSTemplates(); diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DefaultTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DefaultTemplateManager.java new file mode 100644 index 00000000..9b363472 --- /dev/null +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DefaultTemplateManager.java @@ -0,0 +1,211 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.email.mgt.store; + +import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerServerException; +import org.wso2.carbon.identity.governance.model.NotificationTemplate; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * This class serves as a unified template management system that delegates the template persistence operations + * to both template persistent manger crafted from the factory and an in-memory manager. + * This class will function as a wrapper class for the template manager produced from the factory. + */ +public class DefaultTemplateManager implements TemplatePersistenceManager { + + private final TemplatePersistenceManager templatePersistenceManager; + private final TemplatePersistenceManager inMemoryTemplateManager = new InMemoryBasedTemplateManager(); + + public DefaultTemplateManager() { + + TemplatePersistenceManagerFactory templatePersistenceManagerFactory = new TemplatePersistenceManagerFactory(); + this.templatePersistenceManager = templatePersistenceManagerFactory.getTemplatePersistenceManager(); + } + + @Override + public void addNotificationTemplateType(String displayName, String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + templatePersistenceManager.addNotificationTemplateType(displayName, notificationChannel, tenantDomain); + } + + @Override + public boolean isNotificationTemplateTypeExists(String displayName, String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + return templatePersistenceManager.isNotificationTemplateTypeExists(displayName, notificationChannel, + tenantDomain) || + inMemoryTemplateManager.isNotificationTemplateTypeExists(displayName, notificationChannel, + tenantDomain); + } + + @Override + public List listNotificationTemplateTypes(String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + List dbBasedTemplateTypes = templatePersistenceManager.listNotificationTemplateTypes + (notificationChannel, tenantDomain); + List inMemoryTemplateTypes = inMemoryTemplateManager.listNotificationTemplateTypes(notificationChannel, + tenantDomain); + + return mergeAndRemoveDuplicates(dbBasedTemplateTypes, inMemoryTemplateTypes); + } + + @Override + public void deleteNotificationTemplateType(String displayName, String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + if (templatePersistenceManager.isNotificationTemplateTypeExists(displayName, notificationChannel, + tenantDomain)) { + templatePersistenceManager.deleteNotificationTemplateType(displayName, notificationChannel, tenantDomain); + } + } + + @Override + public void addOrUpdateNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, + String tenantDomain) throws NotificationTemplateManagerServerException { + + templatePersistenceManager.addOrUpdateNotificationTemplate(notificationTemplate, applicationUuid, tenantDomain); + } + + @Override + public boolean isNotificationTemplateExists(String displayName, String locale, String notificationChannel, + String applicationUuid, String tenantDomain) + throws NotificationTemplateManagerServerException { + + return templatePersistenceManager.isNotificationTemplateExists(displayName, locale, notificationChannel, + applicationUuid, tenantDomain) || + inMemoryTemplateManager.isNotificationTemplateExists(displayName, locale, notificationChannel, + applicationUuid, tenantDomain); + } + + @Override + public NotificationTemplate getNotificationTemplate(String displayName, String locale, String notificationChannel, + String applicationUuid, String tenantDomain) + throws NotificationTemplateManagerServerException { + + if (templatePersistenceManager.isNotificationTemplateExists(displayName, locale, notificationChannel, + applicationUuid, tenantDomain)) { + return templatePersistenceManager.getNotificationTemplate(displayName, locale, notificationChannel, + applicationUuid, tenantDomain); + } else { + return inMemoryTemplateManager.getNotificationTemplate(displayName, locale, notificationChannel, + applicationUuid, tenantDomain); + } + } + + @Override + public List listNotificationTemplates(String templateType, String notificationChannel, + String applicationUuid, String tenantDomain) + throws NotificationTemplateManagerServerException { + + List dbBasedTemplates = new ArrayList<>(); + if (templatePersistenceManager.isNotificationTemplateTypeExists(templateType, notificationChannel, + tenantDomain)) { + dbBasedTemplates = + templatePersistenceManager.listNotificationTemplates(templateType, notificationChannel, + applicationUuid, tenantDomain); + } + + List inMemoryBasedTemplates = new ArrayList<>(); + if (inMemoryTemplateManager.isNotificationTemplateTypeExists(templateType, notificationChannel, + tenantDomain)) { + inMemoryBasedTemplates = + inMemoryTemplateManager.listNotificationTemplates(templateType, notificationChannel, + applicationUuid, tenantDomain); + } + + return mergeAndRemoveDuplicateTemplates(dbBasedTemplates, inMemoryBasedTemplates); + } + + @Override + public List listAllNotificationTemplates(String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + List dbBasedTemplates = + templatePersistenceManager.listAllNotificationTemplates(notificationChannel, tenantDomain); + List inMemoryBasedTemplates = + inMemoryTemplateManager.listAllNotificationTemplates(notificationChannel, tenantDomain); + + return mergeAndRemoveDuplicateTemplates(dbBasedTemplates, inMemoryBasedTemplates); + } + + @Override + public void deleteNotificationTemplate(String displayName, String locale, String notificationChannel, + String applicationUuid, String tenantDomain) + throws NotificationTemplateManagerServerException { + + if (templatePersistenceManager.isNotificationTemplateExists(displayName, locale, notificationChannel, + applicationUuid, tenantDomain)) { + templatePersistenceManager.deleteNotificationTemplate(displayName, locale, notificationChannel, applicationUuid, + tenantDomain); + } + } + + @Override + public void deleteNotificationTemplates(String displayName, String notificationChannel, String applicationUuid, + String tenantDomain) throws NotificationTemplateManagerServerException { + + if (templatePersistenceManager.isNotificationTemplateTypeExists(displayName, notificationChannel, + tenantDomain)) { + templatePersistenceManager.deleteNotificationTemplates(displayName, notificationChannel, applicationUuid, + tenantDomain); + } + } + + /** + * Merges two lists and removes duplicates. + * + * @param dbBasedTemplates DbBasedTemplates + * @param inMemoryTemplates InMemoryTemplates + * @return Merged list without duplicates. + */ + private List mergeAndRemoveDuplicates(List dbBasedTemplates, List inMemoryTemplates) { + + Set uniqueElements = new HashSet<>(); + uniqueElements.addAll(dbBasedTemplates); + uniqueElements.addAll(inMemoryTemplates); + return new ArrayList<>(uniqueElements); + } + + /** + * Merges two NotificationTemplate lists and removes duplicate templates. + * + * @param dbBasedTemplates DbBasedTemplates + * @param inMemoryTemplates InMemoryTemplates + * @return Merged list without duplicates. + */ + private List mergeAndRemoveDuplicateTemplates( + List dbBasedTemplates, + List inMemoryTemplates) { + + Map templateMap = new HashMap<>(); + dbBasedTemplates.forEach(template -> templateMap.put(template.getDisplayName(), template)); + + // Add in-memory templates, only if not already present + inMemoryTemplates.forEach(template -> templateMap.putIfAbsent(template.getDisplayName(), template)); + return new ArrayList<>(templateMap.values()); + } +} diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java new file mode 100644 index 00000000..a6fe26dd --- /dev/null +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java @@ -0,0 +1,263 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.email.mgt.store; + +import org.apache.commons.lang.StringUtils; +import org.wso2.carbon.email.mgt.internal.I18nMgtDataHolder; +import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerServerException; +import org.wso2.carbon.identity.governance.model.NotificationTemplate; +import org.wso2.carbon.identity.governance.service.notification.NotificationChannels; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * This class is responsible for storing notification templates from the email-admin-config.xml file. + * This class is primarily used to provide default notification templates without addition/modification, + * allowing for quick access of templates without migration. + * Templates are stored in nested maps, where the outer map is keyed by the template display name and the inner map + * is keyed by locale. This allows for easy retrieval of localized templates. + * This class used by {@link HybridTemplateManager} as a fallback mechanism. + * This class only supports for tenant specific notification templates. + */ +public class InMemoryBasedTemplateManager implements TemplatePersistenceManager { + + private final Map> defaultEmailTemplates; + private final Map> defaultSMSTemplates; + + /** + * Initializes the in-memory template manager by populating default email and SMS templates. + */ + public InMemoryBasedTemplateManager() { + + defaultEmailTemplates = populateTemplates(I18nMgtDataHolder.getInstance().getDefaultEmailTemplates()); + defaultSMSTemplates = populateTemplates(I18nMgtDataHolder.getInstance().getDefaultSMSTemplates()); + } + + @Override + public void addNotificationTemplateType(String displayName, String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + // This method does nothing cause addition is not allowed for in-memory template manager. + } + + /** + * Checks if a notification template type exists in the in-memory store. + * + * @param displayName The display name of the template type. + * @param notificationChannel The channel of the notification (e.g., Email, SMS). + * @param tenantDomain The tenant domain. + * @return true if the template type exists, false otherwise. + * @throws NotificationTemplateManagerServerException if an error occurs during the operation. + */ + @Override + public boolean isNotificationTemplateTypeExists(String displayName, String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + if (!StringUtils.isBlank(displayName)) { + if (NotificationChannels.SMS_CHANNEL.getChannelType().equals(notificationChannel)) { + return defaultSMSTemplates.containsKey(displayName.toLowerCase()); + } + return defaultEmailTemplates.containsKey(displayName.toLowerCase()); + } + return false; + } + + /** + * Lists all notification template types for a given channel in the in-memory store. + * + * @param notificationChannel The channel of the notification (e.g., Email, SMS). + * @param tenantDomain The tenant domain. + * @return A list of template type display names. + * @throws NotificationTemplateManagerServerException if an error occurs during the operation. + */ + + @Override + public List listNotificationTemplateTypes(String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + List displayNames = new ArrayList<>(); + Map> defaultTemplates = getTemplateMap(notificationChannel); + defaultTemplates.forEach((displayName, innerMap) -> + innerMap.forEach((locale, template) -> + displayNames.add(template.getDisplayName()) + ) + ); + return displayNames; + } + + @Override + public void deleteNotificationTemplateType(String displayName, String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + // This method does nothing cause deletion is not allowed for in-memory template manager. + } + + @Override + public void addOrUpdateNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, + String tenantDomain) throws NotificationTemplateManagerServerException { + + // This method does nothing cause modification is not allowed for in-memory template manager. + } + + /** + * Checks if a specific notification template exists in the in-memory store. + * + * @param displayName The display name of the template. + * @param locale The locale of the template. + * @param notificationChannel The channel of the notification (e.g., Email, SMS). + * @param applicationUuid The application UUID. + * @param tenantDomain The tenant domain. + * @return true if the template exists, false otherwise. + * @throws NotificationTemplateManagerServerException if an error occurs during the operation. + */ + @Override + public boolean isNotificationTemplateExists(String displayName, String locale, String notificationChannel, + String applicationUuid, String tenantDomain) + throws NotificationTemplateManagerServerException { + + if (StringUtils.isBlank(applicationUuid) && !StringUtils.isBlank(displayName) && !StringUtils.isBlank(locale)) { + Map> defaultTemplates = getTemplateMap(notificationChannel); + return defaultTemplates.containsKey(displayName.toLowerCase()) && + defaultTemplates.get(displayName.toLowerCase()).containsKey(locale.toLowerCase()); + } + return false; + } + + /** + * Retrieves a specific notification template from the in-memory store. + * + * @param displayName The display name of the template. + * @param locale The locale of the template. + * @param notificationChannel The channel of the notification (e.g., Email, SMS). + * @param applicationUuid The application UUID. + * @param tenantDomain The tenant domain. + * @return The notification template, or null if it doesn't exist. + * @throws NotificationTemplateManagerServerException if an error occurs during the operation. + */ + @Override + public NotificationTemplate getNotificationTemplate(String displayName, String locale, String notificationChannel, + String applicationUuid, String tenantDomain) + throws NotificationTemplateManagerServerException { + + if (StringUtils.isBlank(applicationUuid) && !StringUtils.isBlank(displayName) && !StringUtils.isBlank(locale)) { + + Map> defaultTemplates = getTemplateMap(notificationChannel); + return defaultTemplates.containsKey(displayName.toLowerCase()) ? + defaultTemplates.get(displayName.toLowerCase()).get(locale.toLowerCase()) : null; + } + return null; + + } + + /** + * Lists all notification templates of a specific type for a given channel in the in-memory store. + * + * @param templateType The type of the templates to list. + * @param notificationChannel The channel of the notification (e.g., Email, SMS). + * @param applicationUuid The application UUID. + * @param tenantDomain The tenant domain. + * @return A list of notification templates. + * @throws NotificationTemplateManagerServerException if an error occurs during the operation. + */ + @Override + public List listNotificationTemplates(String templateType, String notificationChannel, + String applicationUuid, String tenantDomain) + throws NotificationTemplateManagerServerException { + + if (StringUtils.isBlank(applicationUuid) && !StringUtils.isBlank(templateType)) { + List notificationTemplates = new ArrayList<>(); + Map> defaultTemplates = getTemplateMap(notificationChannel); + if (defaultTemplates.containsKey(templateType.toLowerCase())) { + defaultTemplates.get(templateType.toLowerCase()).forEach((locale, template) -> + notificationTemplates.add(template)); + return notificationTemplates; + + } + } + return Collections.emptyList(); + } + + /** + * Lists all notification templates for a given channel in the in-memory store. + * + * @param notificationChannel The channel of the notification (e.g., Email, SMS). + * @param tenantDomain The tenant domain. + * @return A list of notification templates. + * @throws NotificationTemplateManagerServerException if an error occurs during the operation. + */ + @Override + public List listAllNotificationTemplates(String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + if (NotificationChannels.SMS_CHANNEL.getChannelType().equals(notificationChannel)) { + return I18nMgtDataHolder.getInstance().getDefaultSMSTemplates(); + } + return I18nMgtDataHolder.getInstance().getDefaultEmailTemplates(); + } + + @Override + public void deleteNotificationTemplate(String displayName, String locale, String notificationChannel, + String applicationUuid, String tenantDomain) + throws NotificationTemplateManagerServerException { + + // This method does nothing cause deletion is not allowed for in-memory template manager. + } + + @Override + public void deleteNotificationTemplates(String displayName, String notificationChannel, String applicationUuid, + String tenantDomain) throws NotificationTemplateManagerServerException { + + // This method does nothing cause deletion is not allowed for in-memory template manager. + } + + /** + * Populates the in-memory store with default templates, organizing them by display name and locale. + * + * @param templates The list of templates to populate the store with. + * @return A map of template display names to maps of locale to templates. + */ + private Map> populateTemplates (List templates) { + + Map> templateMap = new HashMap<>(); + + for (NotificationTemplate template : templates) { + templateMap.computeIfAbsent(template.getDisplayName().toLowerCase(), k -> new HashMap<>()) + .put(template.getLocale().toLowerCase(), template); + } + return templateMap; + } + + /** + * Retrieves the map of templates for a given notification channel (e.g., Email, SMS). + * + * @param notificationChannel The channel of the notification. + * @return The map of templates, organized by display name and locale. + */ + private Map> getTemplateMap(String notificationChannel) { + + if (NotificationChannels.SMS_CHANNEL.getChannelType().equals(notificationChannel)) { + return defaultSMSTemplates; + } + return defaultEmailTemplates; + } +} diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManagerTest.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManagerTest.java new file mode 100644 index 00000000..c77e2464 --- /dev/null +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManagerTest.java @@ -0,0 +1,194 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.email.mgt.store; + +import org.apache.commons.lang.StringUtils; +import org.mockito.Mock; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.testng.PowerMockTestCase; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; +import org.wso2.carbon.email.mgt.internal.I18nMgtDataHolder; +import org.wso2.carbon.email.mgt.internal.I18nMgtServiceComponent; +import org.wso2.carbon.identity.governance.model.NotificationTemplate; +import org.wso2.carbon.identity.governance.service.notification.NotificationChannels; +import org.wso2.carbon.utils.CarbonUtils; + +import java.nio.file.Paths; +import java.util.List; + +import static org.mockito.MockitoAnnotations.initMocks; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +/** + * Class that contains the test cases for {@link InMemoryBasedTemplateManager}. + */ +@PrepareForTest({I18nMgtDataHolder.class, CarbonUtils.class}) +public class InMemoryBasedTemplateManagerTest extends PowerMockTestCase { + + private final String baseDirectoryPath = Paths.get(System.getProperty("user.dir"), + "src", "test", "resources").toString(); + private static final String tenantDomain = "carbon.super"; + private static final String dummyDisplayName = "dummyDisplayName"; + private static final String dummyAppId = "dummyAppId"; + private static final String EN_US = "en_US"; + @Mock + I18nMgtDataHolder i18nMgtDataHolder; + InMemoryBasedTemplateManager inMemoryBasedTemplateManager; + NotificationTemplate positiveNotificationTemplate; + NotificationTemplate negativeNotificationTemplate; + + @BeforeMethod + public void setUp() { + + initMocks(this); + mockStatic(I18nMgtDataHolder.class); + i18nMgtDataHolder = PowerMockito.mock(I18nMgtDataHolder.class); + when(I18nMgtDataHolder.getInstance()).thenReturn(i18nMgtDataHolder); + + List defaultEmailTemplate = loadDefaultTemplatesFromFile(baseDirectoryPath, + NotificationChannels.EMAIL_CHANNEL.getChannelType()); + when(i18nMgtDataHolder.getDefaultEmailTemplates()).thenReturn(defaultEmailTemplate); + + inMemoryBasedTemplateManager = new InMemoryBasedTemplateManager(); + initTestNotificationTemplates(); + } + + private void initTestNotificationTemplates() { + + positiveNotificationTemplate = i18nMgtDataHolder.getDefaultEmailTemplates().get(0); + negativeNotificationTemplate = new NotificationTemplate(); + negativeNotificationTemplate.setNotificationChannel(NotificationChannels.EMAIL_CHANNEL.getChannelType()); + negativeNotificationTemplate.setType("dummyType"); + negativeNotificationTemplate.setDisplayName("dummyDisplayName"); + negativeNotificationTemplate.setLocale("en_US"); + } + + @Test + public void testIsNotificationTemplateTypeExists() throws Exception { + + assertFalse(inMemoryBasedTemplateManager.isNotificationTemplateTypeExists(StringUtils.EMPTY, + NotificationChannels.EMAIL_CHANNEL.getChannelType(), tenantDomain)); + assertFalse(inMemoryBasedTemplateManager.isNotificationTemplateTypeExists( + negativeNotificationTemplate.getDisplayName(), + NotificationChannels.EMAIL_CHANNEL.getChannelType(), tenantDomain)); + assertTrue(inMemoryBasedTemplateManager.isNotificationTemplateTypeExists( + positiveNotificationTemplate.getDisplayName(), + NotificationChannels.EMAIL_CHANNEL.getChannelType(), tenantDomain)); + } + + @Test + public void testListNotificationTemplateTypes() throws Exception { + + List displayNames = inMemoryBasedTemplateManager.listNotificationTemplateTypes( + NotificationChannels.EMAIL_CHANNEL.getChannelType(), tenantDomain); + assertNotNull(displayNames); + assertTrue(displayNames.contains(positiveNotificationTemplate.getDisplayName())); + assertFalse(displayNames.contains(negativeNotificationTemplate.getDisplayName())); + } + + @Test + public void testIsNotificationTemplateExists() throws Exception { + + assertFalse(inMemoryBasedTemplateManager.isNotificationTemplateExists(StringUtils.EMPTY, EN_US, + NotificationChannels.EMAIL_CHANNEL.getChannelType(), dummyAppId, tenantDomain)); + assertFalse(inMemoryBasedTemplateManager.isNotificationTemplateExists(dummyDisplayName, StringUtils.EMPTY, + NotificationChannels.EMAIL_CHANNEL.getChannelType(), dummyAppId, tenantDomain)); + assertFalse(inMemoryBasedTemplateManager.isNotificationTemplateExists(dummyDisplayName, EN_US, + NotificationChannels.EMAIL_CHANNEL.getChannelType(), StringUtils.EMPTY, tenantDomain)); + assertFalse(inMemoryBasedTemplateManager.isNotificationTemplateExists( + negativeNotificationTemplate.getDisplayName(), EN_US, + NotificationChannels.EMAIL_CHANNEL.getChannelType(), StringUtils.EMPTY, tenantDomain)); + assertTrue(inMemoryBasedTemplateManager.isNotificationTemplateExists( + positiveNotificationTemplate.getDisplayName(), EN_US, + NotificationChannels.EMAIL_CHANNEL.getChannelType(), StringUtils.EMPTY, tenantDomain)); + } + + @Test + public void testGetNotificationTemplate() throws Exception { + + assertNull(inMemoryBasedTemplateManager.getNotificationTemplate(StringUtils.EMPTY, EN_US, + NotificationChannels.EMAIL_CHANNEL.getChannelType(), dummyAppId, tenantDomain)); + assertNull(inMemoryBasedTemplateManager.getNotificationTemplate(dummyDisplayName, StringUtils.EMPTY, + NotificationChannels.EMAIL_CHANNEL.getChannelType(), dummyAppId, tenantDomain)); + assertNull(inMemoryBasedTemplateManager.getNotificationTemplate(dummyDisplayName, EN_US, + NotificationChannels.EMAIL_CHANNEL.getChannelType(), StringUtils.EMPTY, tenantDomain)); + assertNull(inMemoryBasedTemplateManager.getNotificationTemplate( + negativeNotificationTemplate.getDisplayName(), EN_US, + NotificationChannels.EMAIL_CHANNEL.getChannelType(), StringUtils.EMPTY, tenantDomain)); + assertNotNull(inMemoryBasedTemplateManager.getNotificationTemplate( + positiveNotificationTemplate.getDisplayName(), EN_US, + NotificationChannels.EMAIL_CHANNEL.getChannelType(), StringUtils.EMPTY, tenantDomain)); + } + + @Test + public void testListNotificationTemplates() throws Exception { + + assertTrue(inMemoryBasedTemplateManager.listNotificationTemplates(StringUtils.EMPTY, + NotificationChannels.EMAIL_CHANNEL.getChannelType(), dummyAppId, tenantDomain).isEmpty()); + assertTrue(inMemoryBasedTemplateManager.listNotificationTemplates(dummyDisplayName, + NotificationChannels.EMAIL_CHANNEL.getChannelType(), StringUtils.EMPTY, tenantDomain).isEmpty()); + assertTrue(inMemoryBasedTemplateManager.listNotificationTemplates( + negativeNotificationTemplate.getDisplayName(), + NotificationChannels.EMAIL_CHANNEL.getChannelType(), StringUtils.EMPTY, tenantDomain).isEmpty()); + assertFalse(inMemoryBasedTemplateManager.listNotificationTemplates( + positiveNotificationTemplate.getDisplayName(), + NotificationChannels.EMAIL_CHANNEL.getChannelType(), StringUtils.EMPTY, tenantDomain).isEmpty()); + } + + @Test + public void testListAllNotificationTemplates() throws Exception { + + assertFalse(inMemoryBasedTemplateManager.listAllNotificationTemplates( + NotificationChannels.EMAIL_CHANNEL.getChannelType(), tenantDomain).isEmpty()); + assertTrue(inMemoryBasedTemplateManager.listAllNotificationTemplates( + NotificationChannels.SMS_CHANNEL.getChannelType(), tenantDomain).isEmpty()); + } + + /** + * Loads the default templates from the file for the channel(EMAIL or SMS) and create list of Notification Template. + * + * @param notificationChannel Channel of the notification. + * @return List of NotificationTemplate. + */ + private List loadDefaultTemplatesFromFile(String baseDirectoryPath, + String notificationChannel) { + + mockNotificationChannelConfigPath(baseDirectoryPath); + I18nMgtServiceComponent component = new I18nMgtServiceComponent(); + return component.loadDefaultTemplatesFromFile(notificationChannel); + } + + /** + * Mock the default config xml path of notification templates. + * + * @param baseDirectoryPath Resource folder location + */ + private void mockNotificationChannelConfigPath(String baseDirectoryPath) { + + mockStatic(CarbonUtils.class); + when(CarbonUtils.getCarbonConfigDirPath()).thenReturn(baseDirectoryPath); + } +} diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/test/resources/testng.xml b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/resources/testng.xml index 78bf58a8..efe76d25 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/test/resources/testng.xml +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/resources/testng.xml @@ -20,6 +20,7 @@ + From d65f33901738badf8949b73afd5f4ba5bf19a670 Mon Sep 17 00:00:00 2001 From: JeethJJ Date: Tue, 27 Aug 2024 16:36:03 +0530 Subject: [PATCH 2/8] Fix testcase failure. --- .../org/wso2/carbon/email/mgt/ApplicationEmailTemplateTest.java | 1 + .../org/wso2/carbon/email/mgt/OrganizationEmailTemplateTest.java | 1 + 2 files changed, 2 insertions(+) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/ApplicationEmailTemplateTest.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/ApplicationEmailTemplateTest.java index 999fa749..bc67216b 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/ApplicationEmailTemplateTest.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/ApplicationEmailTemplateTest.java @@ -472,6 +472,7 @@ private void mockRegistryResource(String notificationChannel, String displayName when(resourceMgtService.getIdentityResource(Matchers.anyString(), Matchers.anyString(), Matchers.anyString())) .thenReturn(resource); + when(resourceMgtService.isResourceExists(Matchers.anyString(), Matchers.anyString())).thenReturn(true); // Mock Resource properties. when(resource.getProperty(I18nMgtConstants.TEMPLATE_TYPE_DISPLAY_NAME)).thenReturn(displayName); diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/OrganizationEmailTemplateTest.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/OrganizationEmailTemplateTest.java index 6c718b98..b671bf25 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/OrganizationEmailTemplateTest.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/OrganizationEmailTemplateTest.java @@ -406,6 +406,7 @@ private void mockRegistryResource(String notificationChannel, String displayName when(resourceMgtService.getIdentityResource(Matchers.anyString(), Matchers.anyString(), Matchers.anyString())) .thenReturn(resource); + when(resourceMgtService.isResourceExists(Matchers.anyString(), Matchers.anyString())).thenReturn(true); // Mock Resource properties. when(resource.getProperty(I18nMgtConstants.TEMPLATE_TYPE_DISPLAY_NAME)).thenReturn(displayName); From 8628c42d5f74b1e58a734be7fc641c3f846af46d Mon Sep 17 00:00:00 2001 From: JeethJJ Date: Wed, 28 Aug 2024 17:40:21 +0530 Subject: [PATCH 3/8] Address comments. --- .../email/mgt/store/DefaultTemplateManager.java | 16 ++++++++-------- .../mgt/store/InMemoryBasedTemplateManager.java | 16 +++++++--------- .../store/InMemoryBasedTemplateManagerTest.java | 5 +++++ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DefaultTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DefaultTemplateManager.java index 9b363472..65fc6da1 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DefaultTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DefaultTemplateManager.java @@ -55,9 +55,9 @@ public void addNotificationTemplateType(String displayName, String notificationC public boolean isNotificationTemplateTypeExists(String displayName, String notificationChannel, String tenantDomain) throws NotificationTemplateManagerServerException { - return templatePersistenceManager.isNotificationTemplateTypeExists(displayName, notificationChannel, + return inMemoryTemplateManager.isNotificationTemplateTypeExists(displayName, notificationChannel, tenantDomain) || - inMemoryTemplateManager.isNotificationTemplateTypeExists(displayName, notificationChannel, + templatePersistenceManager.isNotificationTemplateTypeExists(displayName, notificationChannel, tenantDomain); } @@ -95,9 +95,9 @@ public boolean isNotificationTemplateExists(String displayName, String locale, S String applicationUuid, String tenantDomain) throws NotificationTemplateManagerServerException { - return templatePersistenceManager.isNotificationTemplateExists(displayName, locale, notificationChannel, + return inMemoryTemplateManager.isNotificationTemplateExists(displayName, locale, notificationChannel, applicationUuid, tenantDomain) || - inMemoryTemplateManager.isNotificationTemplateExists(displayName, locale, notificationChannel, + templatePersistenceManager.isNotificationTemplateExists(displayName, locale, notificationChannel, applicationUuid, tenantDomain); } @@ -106,10 +106,10 @@ public NotificationTemplate getNotificationTemplate(String displayName, String l String applicationUuid, String tenantDomain) throws NotificationTemplateManagerServerException { - if (templatePersistenceManager.isNotificationTemplateExists(displayName, locale, notificationChannel, - applicationUuid, tenantDomain)) { - return templatePersistenceManager.getNotificationTemplate(displayName, locale, notificationChannel, - applicationUuid, tenantDomain); + NotificationTemplate notificationTemplate = templatePersistenceManager.getNotificationTemplate(displayName, + locale, notificationChannel, applicationUuid, tenantDomain); + if (notificationTemplate != null) { + return notificationTemplate; } else { return inMemoryTemplateManager.getNotificationTemplate(displayName, locale, notificationChannel, applicationUuid, tenantDomain); diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java index a6fe26dd..fbad66d9 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java @@ -18,6 +18,7 @@ package org.wso2.carbon.email.mgt.store; +import org.apache.commons.lang.NotImplementedException; import org.apache.commons.lang.StringUtils; import org.wso2.carbon.email.mgt.internal.I18nMgtDataHolder; import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerServerException; @@ -36,7 +37,7 @@ * allowing for quick access of templates without migration. * Templates are stored in nested maps, where the outer map is keyed by the template display name and the inner map * is keyed by locale. This allows for easy retrieval of localized templates. - * This class used by {@link HybridTemplateManager} as a fallback mechanism. + * This class used by {@link DefaultTemplateManager} as a fallback mechanism. * This class only supports for tenant specific notification templates. */ public class InMemoryBasedTemplateManager implements TemplatePersistenceManager { @@ -57,7 +58,7 @@ public InMemoryBasedTemplateManager() { public void addNotificationTemplateType(String displayName, String notificationChannel, String tenantDomain) throws NotificationTemplateManagerServerException { - // This method does nothing cause addition is not allowed for in-memory template manager. + throw new NotImplementedException(); } /** @@ -73,11 +74,8 @@ public void addNotificationTemplateType(String displayName, String notificationC public boolean isNotificationTemplateTypeExists(String displayName, String notificationChannel, String tenantDomain) throws NotificationTemplateManagerServerException { - if (!StringUtils.isBlank(displayName)) { - if (NotificationChannels.SMS_CHANNEL.getChannelType().equals(notificationChannel)) { - return defaultSMSTemplates.containsKey(displayName.toLowerCase()); - } - return defaultEmailTemplates.containsKey(displayName.toLowerCase()); + if (StringUtils.isNotBlank(displayName)) { + return getTemplateMap(notificationChannel).containsKey(displayName.toLowerCase()); } return false; } @@ -109,14 +107,14 @@ public List listNotificationTemplateTypes(String notificationChannel, St public void deleteNotificationTemplateType(String displayName, String notificationChannel, String tenantDomain) throws NotificationTemplateManagerServerException { - // This method does nothing cause deletion is not allowed for in-memory template manager. + throw new NotImplementedException(); } @Override public void addOrUpdateNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, String tenantDomain) throws NotificationTemplateManagerServerException { - // This method does nothing cause modification is not allowed for in-memory template manager. + throw new NotImplementedException(); } /** diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManagerTest.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManagerTest.java index c77e2464..b1d5811c 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManagerTest.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManagerTest.java @@ -33,10 +33,13 @@ import java.nio.file.Paths; import java.util.List; +import java.util.HashSet; +import java.util.Set; import static org.mockito.MockitoAnnotations.initMocks; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.when; +import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; @@ -104,9 +107,11 @@ public void testListNotificationTemplateTypes() throws Exception { List displayNames = inMemoryBasedTemplateManager.listNotificationTemplateTypes( NotificationChannels.EMAIL_CHANNEL.getChannelType(), tenantDomain); + Set displayNamesSet = new HashSet<>(displayNames); assertNotNull(displayNames); assertTrue(displayNames.contains(positiveNotificationTemplate.getDisplayName())); assertFalse(displayNames.contains(negativeNotificationTemplate.getDisplayName())); + assertEquals(displayNamesSet.size(), displayNames.size()); } @Test From 7c4bb109ea3a87a977a5e5c94b03534c23aa2499 Mon Sep 17 00:00:00 2001 From: JeethJJ Date: Wed, 28 Aug 2024 18:00:35 +0530 Subject: [PATCH 4/8] Address comments. --- .../carbon/email/mgt/store/InMemoryBasedTemplateManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java index fbad66d9..c565f741 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java @@ -218,14 +218,14 @@ public void deleteNotificationTemplate(String displayName, String locale, String String applicationUuid, String tenantDomain) throws NotificationTemplateManagerServerException { - // This method does nothing cause deletion is not allowed for in-memory template manager. + throw new NotImplementedException(); } @Override public void deleteNotificationTemplates(String displayName, String notificationChannel, String applicationUuid, String tenantDomain) throws NotificationTemplateManagerServerException { - // This method does nothing cause deletion is not allowed for in-memory template manager. + throw new NotImplementedException(); } /** From 42f15c831cce44e60382c0f22dbd09ee09ea3e38 Mon Sep 17 00:00:00 2001 From: JeethJJ Date: Thu, 29 Aug 2024 12:12:37 +0530 Subject: [PATCH 5/8] Address comments. --- .../carbon/email/mgt/store/InMemoryBasedTemplateManager.java | 2 +- .../org/wso2/carbon/email/mgt/ApplicationEmailTemplateTest.java | 1 - .../wso2/carbon/email/mgt/OrganizationEmailTemplateTest.java | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java index c565f741..5b8633ef 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java @@ -38,7 +38,7 @@ * Templates are stored in nested maps, where the outer map is keyed by the template display name and the inner map * is keyed by locale. This allows for easy retrieval of localized templates. * This class used by {@link DefaultTemplateManager} as a fallback mechanism. - * This class only supports for tenant specific notification templates. + * This class keeping set of templates that will be commonly used as default templates across all the tenants. */ public class InMemoryBasedTemplateManager implements TemplatePersistenceManager { diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/ApplicationEmailTemplateTest.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/ApplicationEmailTemplateTest.java index bc67216b..999fa749 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/ApplicationEmailTemplateTest.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/ApplicationEmailTemplateTest.java @@ -472,7 +472,6 @@ private void mockRegistryResource(String notificationChannel, String displayName when(resourceMgtService.getIdentityResource(Matchers.anyString(), Matchers.anyString(), Matchers.anyString())) .thenReturn(resource); - when(resourceMgtService.isResourceExists(Matchers.anyString(), Matchers.anyString())).thenReturn(true); // Mock Resource properties. when(resource.getProperty(I18nMgtConstants.TEMPLATE_TYPE_DISPLAY_NAME)).thenReturn(displayName); diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/OrganizationEmailTemplateTest.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/OrganizationEmailTemplateTest.java index b671bf25..6c718b98 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/OrganizationEmailTemplateTest.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/OrganizationEmailTemplateTest.java @@ -406,7 +406,6 @@ private void mockRegistryResource(String notificationChannel, String displayName when(resourceMgtService.getIdentityResource(Matchers.anyString(), Matchers.anyString(), Matchers.anyString())) .thenReturn(resource); - when(resourceMgtService.isResourceExists(Matchers.anyString(), Matchers.anyString())).thenReturn(true); // Mock Resource properties. when(resource.getProperty(I18nMgtConstants.TEMPLATE_TYPE_DISPLAY_NAME)).thenReturn(displayName); From f3e8a53885fd2347764bd08ff0ff5f304ece79b8 Mon Sep 17 00:00:00 2001 From: JeethJJ Date: Thu, 29 Aug 2024 12:16:03 +0530 Subject: [PATCH 6/8] Update comments. --- .../carbon/email/mgt/store/InMemoryBasedTemplateManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java index 5b8633ef..3978b065 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java @@ -38,7 +38,7 @@ * Templates are stored in nested maps, where the outer map is keyed by the template display name and the inner map * is keyed by locale. This allows for easy retrieval of localized templates. * This class used by {@link DefaultTemplateManager} as a fallback mechanism. - * This class keeping set of templates that will be commonly used as default templates across all the tenants. + * This class keeping set of templates that will be commonly used as default templates across all the tenants. */ public class InMemoryBasedTemplateManager implements TemplatePersistenceManager { From 5d985d4254d5681ad87d157dc8a8a8c37cd69ff9 Mon Sep 17 00:00:00 2001 From: JeethJJ Date: Thu, 29 Aug 2024 14:56:21 +0530 Subject: [PATCH 7/8] Address comments. --- .../store/InMemoryBasedTemplateManager.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java index 3978b065..4d33cbbb 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java @@ -18,7 +18,6 @@ package org.wso2.carbon.email.mgt.store; -import org.apache.commons.lang.NotImplementedException; import org.apache.commons.lang.StringUtils; import org.wso2.carbon.email.mgt.internal.I18nMgtDataHolder; import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerServerException; @@ -32,13 +31,16 @@ import java.util.Map; /** - * This class is responsible for storing notification templates from the email-admin-config.xml file. - * This class is primarily used to provide default notification templates without addition/modification, - * allowing for quick access of templates without migration. - * Templates are stored in nested maps, where the outer map is keyed by the template display name and the inner map - * is keyed by locale. This allows for easy retrieval of localized templates. - * This class used by {@link DefaultTemplateManager} as a fallback mechanism. - * This class keeping set of templates that will be commonly used as default templates across all the tenants. + * This class is responsible for handling default templates provided by the system. + * System default templates are used as default org notification templates in a given tenant and based on the content + * of the email-admin-config.xml. + * Any modifications to system default templates needs to be done via email-admin-config.xml and any such changes will + * be reflected only with the server startup. + * This class have support for get, list, exists operations and any invocations to modification operations + * throws {@link UnsupportedOperationException}. + * This class expected to be use only with conjunction with another {@link TemplatePersistenceManager} implementation + * which supports full CRUD operations, hence {@link DefaultTemplateManager} provides that aggregation using this as a + * fallback provider. */ public class InMemoryBasedTemplateManager implements TemplatePersistenceManager { @@ -58,7 +60,7 @@ public InMemoryBasedTemplateManager() { public void addNotificationTemplateType(String displayName, String notificationChannel, String tenantDomain) throws NotificationTemplateManagerServerException { - throw new NotImplementedException(); + throw new UnsupportedOperationException(); } /** @@ -107,14 +109,14 @@ public List listNotificationTemplateTypes(String notificationChannel, St public void deleteNotificationTemplateType(String displayName, String notificationChannel, String tenantDomain) throws NotificationTemplateManagerServerException { - throw new NotImplementedException(); + throw new UnsupportedOperationException(); } @Override public void addOrUpdateNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, String tenantDomain) throws NotificationTemplateManagerServerException { - throw new NotImplementedException(); + throw new UnsupportedOperationException(); } /** @@ -218,14 +220,14 @@ public void deleteNotificationTemplate(String displayName, String locale, String String applicationUuid, String tenantDomain) throws NotificationTemplateManagerServerException { - throw new NotImplementedException(); + throw new UnsupportedOperationException(); } @Override public void deleteNotificationTemplates(String displayName, String notificationChannel, String applicationUuid, String tenantDomain) throws NotificationTemplateManagerServerException { - throw new NotImplementedException(); + throw new UnsupportedOperationException(); } /** From 2005040ba5bce25c4669c3ce2e13523da67d0a5e Mon Sep 17 00:00:00 2001 From: JeethJJ Date: Thu, 29 Aug 2024 15:25:20 +0530 Subject: [PATCH 8/8] Minor version bump. --- components/email-mgt/org.wso2.carbon.email.mgt.ui/pom.xml | 2 +- components/email-mgt/org.wso2.carbon.email.mgt/pom.xml | 2 +- .../org.wso2.carbon.identity.event.handler.notification/pom.xml | 2 +- .../pom.xml | 2 +- features/org.wso2.carbon.email.mgt.feature/pom.xml | 2 +- features/org.wso2.carbon.email.mgt.server.feature/pom.xml | 2 +- features/org.wso2.carbon.email.mgt.ui.feature/pom.xml | 2 +- .../pom.xml | 2 +- pom.xml | 2 +- service-stubs/identity/org.wso2.carbon.email.mgt.stub/pom.xml | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt.ui/pom.xml b/components/email-mgt/org.wso2.carbon.email.mgt.ui/pom.xml index f11ffa60..b34a66d7 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt.ui/pom.xml +++ b/components/email-mgt/org.wso2.carbon.email.mgt.ui/pom.xml @@ -20,7 +20,7 @@ org.wso2.carbon.identity.event.handler.notification identity-event-handler-notification - 1.8.28-SNAPSHOT + 1.9.0-SNAPSHOT ../../../pom.xml diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/pom.xml b/components/email-mgt/org.wso2.carbon.email.mgt/pom.xml index d73d2847..db790f46 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/pom.xml +++ b/components/email-mgt/org.wso2.carbon.email.mgt/pom.xml @@ -21,7 +21,7 @@ org.wso2.carbon.identity.event.handler.notification identity-event-handler-notification - 1.8.28-SNAPSHOT + 1.9.0-SNAPSHOT ../../../pom.xml diff --git a/components/event-handler-notification/org.wso2.carbon.identity.event.handler.notification/pom.xml b/components/event-handler-notification/org.wso2.carbon.identity.event.handler.notification/pom.xml index e207316e..2de03293 100644 --- a/components/event-handler-notification/org.wso2.carbon.identity.event.handler.notification/pom.xml +++ b/components/event-handler-notification/org.wso2.carbon.identity.event.handler.notification/pom.xml @@ -18,7 +18,7 @@ org.wso2.carbon.identity.event.handler.notification identity-event-handler-notification - 1.8.28-SNAPSHOT + 1.9.0-SNAPSHOT ../../../pom.xml 4.0.0 diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/pom.xml b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/pom.xml index 5557500b..379c819c 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/pom.xml +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/pom.xml @@ -22,7 +22,7 @@ org.wso2.carbon.identity.event.handler.notification identity-event-handler-notification - 1.8.28-SNAPSHOT + 1.9.0-SNAPSHOT ../../../pom.xml diff --git a/features/org.wso2.carbon.email.mgt.feature/pom.xml b/features/org.wso2.carbon.email.mgt.feature/pom.xml index 529922fc..648be7dc 100644 --- a/features/org.wso2.carbon.email.mgt.feature/pom.xml +++ b/features/org.wso2.carbon.email.mgt.feature/pom.xml @@ -21,7 +21,7 @@ org.wso2.carbon.identity.event.handler.notification identity-event-handler-notification - 1.8.28-SNAPSHOT + 1.9.0-SNAPSHOT ../../pom.xml diff --git a/features/org.wso2.carbon.email.mgt.server.feature/pom.xml b/features/org.wso2.carbon.email.mgt.server.feature/pom.xml index b66f9195..4a6238c0 100644 --- a/features/org.wso2.carbon.email.mgt.server.feature/pom.xml +++ b/features/org.wso2.carbon.email.mgt.server.feature/pom.xml @@ -21,7 +21,7 @@ org.wso2.carbon.identity.event.handler.notification identity-event-handler-notification - 1.8.28-SNAPSHOT + 1.9.0-SNAPSHOT ../../pom.xml diff --git a/features/org.wso2.carbon.email.mgt.ui.feature/pom.xml b/features/org.wso2.carbon.email.mgt.ui.feature/pom.xml index 3e30b741..64849082 100644 --- a/features/org.wso2.carbon.email.mgt.ui.feature/pom.xml +++ b/features/org.wso2.carbon.email.mgt.ui.feature/pom.xml @@ -21,7 +21,7 @@ org.wso2.carbon.identity.event.handler.notification identity-event-handler-notification - 1.8.28-SNAPSHOT + 1.9.0-SNAPSHOT ../../pom.xml diff --git a/features/org.wso2.carbon.event.handler.notification.server.feature/pom.xml b/features/org.wso2.carbon.event.handler.notification.server.feature/pom.xml index 56c35df2..8e79a3e4 100644 --- a/features/org.wso2.carbon.event.handler.notification.server.feature/pom.xml +++ b/features/org.wso2.carbon.event.handler.notification.server.feature/pom.xml @@ -21,7 +21,7 @@ org.wso2.carbon.identity.event.handler.notification identity-event-handler-notification - 1.8.28-SNAPSHOT + 1.9.0-SNAPSHOT ../../pom.xml diff --git a/pom.xml b/pom.xml index 7ac08a6d..fe6396a4 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ org.wso2.carbon.identity.event.handler.notification identity-event-handler-notification pom - 1.8.28-SNAPSHOT + 1.9.0-SNAPSHOT 4.0.0 http://wso2.org diff --git a/service-stubs/identity/org.wso2.carbon.email.mgt.stub/pom.xml b/service-stubs/identity/org.wso2.carbon.email.mgt.stub/pom.xml index cae71e40..1dee67c3 100644 --- a/service-stubs/identity/org.wso2.carbon.email.mgt.stub/pom.xml +++ b/service-stubs/identity/org.wso2.carbon.email.mgt.stub/pom.xml @@ -21,7 +21,7 @@ org.wso2.carbon.identity.event.handler.notification identity-event-handler-notification - 1.8.28-SNAPSHOT + 1.9.0-SNAPSHOT ../../../pom.xml