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 48904a2e..392e4e3d 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 @@ -20,8 +20,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.dao.RegistryBasedTemplateManager; import org.wso2.carbon.email.mgt.dao.TemplatePersistenceManager; +import org.wso2.carbon.email.mgt.dao.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; @@ -58,7 +58,7 @@ */ public class EmailTemplateManagerImpl implements EmailTemplateManager, NotificationTemplateManager { - private TemplatePersistenceManager templatePersistenceManager = new RegistryBasedTemplateManager(); + private TemplatePersistenceManager templatePersistenceManager; private static final Log log = LogFactory.getLog(EmailTemplateManagerImpl.class); @@ -70,6 +70,11 @@ public class EmailTemplateManagerImpl implements EmailTemplateManager, Notificat IdentityValidationUtil.addPattern(REGISTRY_INVALID_CHARS, REGISTRY_INVALID_CHARS_EXISTS.getRegex()); } + public EmailTemplateManagerImpl() { + TemplatePersistenceManagerFactory templatePersistenceManagerFactory = new TemplatePersistenceManagerFactory(); + templatePersistenceManager = templatePersistenceManagerFactory.getTemplatePersistenceManager(); + } + @Override public void addEmailTemplateType(String emailTemplateDisplayName, String tenantDomain) throws I18nEmailMgtException { diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/I18nMgtConstants.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/I18nMgtConstants.java index 10d099f1..0cdb4ee0 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/I18nMgtConstants.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/I18nMgtConstants.java @@ -62,6 +62,8 @@ private I18nMgtConstants() {} "conf", "sms",SMS_PROVIDER_POST_BODY_TEMPLATES_FILE); public static final String SMS_PROVIDER= "provider"; + public static final String NOTIFICATION_TEMPLATES_STORAGE_CONFIG = "DataStorageType.NotificationTemplates"; + public static class ErrorMsg { private ErrorMsg() { diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/dao/TemplatePersistenceManagerFactory.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/dao/TemplatePersistenceManagerFactory.java new file mode 100644 index 00000000..685b3e2c --- /dev/null +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/dao/TemplatePersistenceManagerFactory.java @@ -0,0 +1,63 @@ +/* + * 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.dao; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.identity.core.util.IdentityUtil; + +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NOTIFICATION_TEMPLATES_STORAGE_CONFIG; + +/** + * Factory class to get the TemplatePersistenceManager. + */ +public class TemplatePersistenceManagerFactory { + + private static final Log log = LogFactory.getLog(TemplatePersistenceManagerFactory.class); + + /** + * Get the TemplatePersistenceManager based on the configuration. + * @return + */ + public TemplatePersistenceManager getTemplatePersistenceManager() { + + String notificationTemplatesStorageType = IdentityUtil.getProperty(NOTIFICATION_TEMPLATES_STORAGE_CONFIG); + + TemplatePersistenceManager persistenceManager = new RegistryBasedTemplateManager(); + + if (StringUtils.isNotBlank(notificationTemplatesStorageType)) { + switch (notificationTemplatesStorageType) { + case "database": + persistenceManager = new DBBasedTemplateManager(); + break; + case "hybrid": + persistenceManager = new HybridTemplateManager(); + break; + default: + persistenceManager = new RegistryBasedTemplateManager(); + } + } + + if (log.isDebugEnabled()) { + log.debug("Template persistent manager initialized with the type: " + persistenceManager.getClass()); + } + return persistenceManager; + } +} 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 381e6e21..93b7622d 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 @@ -21,6 +21,7 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NOTIFICATION_TEMPLATES_STORAGE_CONFIG; import org.apache.commons.lang.StringUtils; import org.mockito.Matchers; @@ -40,6 +41,7 @@ import org.wso2.carbon.identity.base.IdentityRuntimeException; import org.wso2.carbon.identity.base.IdentityValidationUtil; import org.wso2.carbon.identity.core.persistence.registry.RegistryResourceMgtService; +import org.wso2.carbon.identity.core.util.IdentityUtil; import org.wso2.carbon.identity.governance.IdentityMgtConstants; import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerException; import org.wso2.carbon.identity.governance.model.NotificationTemplate; @@ -50,7 +52,7 @@ /** * Class that contains the test cases for the implementation of Email Template Manager. */ -@PrepareForTest({ IdentityValidationUtil.class, I18nMgtDataHolder.class, CarbonUtils.class}) +@PrepareForTest({ IdentityValidationUtil.class, I18nMgtDataHolder.class, CarbonUtils.class, IdentityUtil.class}) public class ApplicationEmailTemplateTest extends PowerMockTestCase { private EmailTemplateManagerImpl emailTemplateManager; @@ -80,6 +82,9 @@ public void setUp() { i18nMgtDataHolder = PowerMockito.mock(I18nMgtDataHolder.class); when(I18nMgtDataHolder.getInstance()).thenReturn(i18nMgtDataHolder); + mockStatic(IdentityUtil.class); + when(IdentityUtil.getProperty(NOTIFICATION_TEMPLATES_STORAGE_CONFIG)).thenReturn("registry"); + // Mock RegistryResourceMgtService. when(i18nMgtDataHolder.getRegistryResourceMgtService()).thenReturn(resourceMgtService); emailTemplateManager = new EmailTemplateManagerImpl(); 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 28cf6414..4cde4970 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 @@ -35,6 +35,7 @@ import org.wso2.carbon.identity.base.IdentityRuntimeException; import org.wso2.carbon.identity.base.IdentityValidationUtil; import org.wso2.carbon.identity.core.persistence.registry.RegistryResourceMgtService; +import org.wso2.carbon.identity.core.util.IdentityUtil; import org.wso2.carbon.identity.governance.IdentityMgtConstants; import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerException; import org.wso2.carbon.identity.governance.model.NotificationTemplate; @@ -61,11 +62,12 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NOTIFICATION_TEMPLATES_STORAGE_CONFIG; /** * Class that contains the test cases for the implementation of Email Template Manager. */ -@PrepareForTest({IdentityValidationUtil.class, I18nMgtDataHolder.class, CarbonUtils.class}) +@PrepareForTest({IdentityValidationUtil.class, I18nMgtDataHolder.class, CarbonUtils.class, IdentityUtil.class}) public class OrganizationEmailTemplateTest extends PowerMockTestCase { private EmailTemplateManagerImpl emailTemplateManager; @@ -95,6 +97,9 @@ public void setUp() { i18nMgtDataHolder = PowerMockito.mock(I18nMgtDataHolder.class); when(I18nMgtDataHolder.getInstance()).thenReturn(i18nMgtDataHolder); + mockStatic(IdentityUtil.class); + when(IdentityUtil.getProperty(NOTIFICATION_TEMPLATES_STORAGE_CONFIG)).thenReturn("registry"); + // Mock RegistryResourceMgtService. when(i18nMgtDataHolder.getRegistryResourceMgtService()).thenReturn(resourceMgtService); emailTemplateManager = new EmailTemplateManagerImpl(); diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/dao/TemplatePersistenceManagerFactoryTest.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/dao/TemplatePersistenceManagerFactoryTest.java new file mode 100644 index 00000000..131174e0 --- /dev/null +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/test/java/org/wso2/carbon/email/mgt/dao/TemplatePersistenceManagerFactoryTest.java @@ -0,0 +1,115 @@ +/* + * 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.dao; + +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.identity.core.persistence.registry.RegistryResourceMgtService; +import org.wso2.carbon.identity.core.util.IdentityUtil; + +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.assertTrue; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NOTIFICATION_TEMPLATES_STORAGE_CONFIG; + +/** + * Class that contains the test cases for {@link TemplatePersistenceManagerFactory}. + */ +@PrepareForTest({I18nMgtDataHolder.class, IdentityUtil.class}) +public class TemplatePersistenceManagerFactoryTest extends PowerMockTestCase { + + @Mock + RegistryResourceMgtService resourceMgtService; + @Mock + I18nMgtDataHolder i18nMgtDataHolder; + private TemplatePersistenceManagerFactory templatePersistenceManagerFactory; + + @BeforeMethod + public void setUp() { + + initMocks(this); + mockStatic(I18nMgtDataHolder.class); + i18nMgtDataHolder = PowerMockito.mock(I18nMgtDataHolder.class); + when(I18nMgtDataHolder.getInstance()).thenReturn(i18nMgtDataHolder); + when(i18nMgtDataHolder.getRegistryResourceMgtService()).thenReturn(resourceMgtService); + + mockStatic(IdentityUtil.class); + templatePersistenceManagerFactory = new TemplatePersistenceManagerFactory(); + } + + @Test + public void shouldReturnDBBasedTemplateManagerWhenConfigIsDatabase() { + + when(IdentityUtil.getProperty(NOTIFICATION_TEMPLATES_STORAGE_CONFIG)).thenReturn("database"); + TemplatePersistenceManager templatePersistenceManager = + templatePersistenceManagerFactory.getTemplatePersistenceManager(); + assertTrue(templatePersistenceManager instanceof DBBasedTemplateManager); + } + + @Test + public void shouldReturnHybridTemplateManagerWhenConfigIsOnMigration() { + + when(IdentityUtil.getProperty(NOTIFICATION_TEMPLATES_STORAGE_CONFIG)).thenReturn("hybrid"); + TemplatePersistenceManager templatePersistenceManager = + templatePersistenceManagerFactory.getTemplatePersistenceManager(); + assertTrue(templatePersistenceManager instanceof HybridTemplateManager); + } + + @Test + public void shouldReturnRegistryBasedTemplateManagerWhenConfigIsRegistry() { + + when(IdentityUtil.getProperty(NOTIFICATION_TEMPLATES_STORAGE_CONFIG)).thenReturn("registry"); + TemplatePersistenceManager templatePersistenceManager = + templatePersistenceManagerFactory.getTemplatePersistenceManager(); + assertTrue(templatePersistenceManager instanceof RegistryBasedTemplateManager); + } + + @Test + public void shouldReturnRegistryBasedTemplateManagerWhenConfigIsInvalid() { + + when(IdentityUtil.getProperty(NOTIFICATION_TEMPLATES_STORAGE_CONFIG)).thenReturn("invalid"); + TemplatePersistenceManager templatePersistenceManager = + templatePersistenceManagerFactory.getTemplatePersistenceManager(); + assertTrue(templatePersistenceManager instanceof RegistryBasedTemplateManager); + } + + @Test + public void shouldReturnRegistryBasedTemplateManagerWhenConfigIsBlank() { + + when(IdentityUtil.getProperty(NOTIFICATION_TEMPLATES_STORAGE_CONFIG)).thenReturn(""); + TemplatePersistenceManager templatePersistenceManager = + templatePersistenceManagerFactory.getTemplatePersistenceManager(); + assertTrue(templatePersistenceManager instanceof RegistryBasedTemplateManager); + } + + @Test + public void shouldReturnRegistryBasedTemplateManagerWhenConfigIsNull() { + + when(IdentityUtil.getProperty(NOTIFICATION_TEMPLATES_STORAGE_CONFIG)).thenReturn(null); + TemplatePersistenceManager templatePersistenceManager = + templatePersistenceManagerFactory.getTemplatePersistenceManager(); + assertTrue(templatePersistenceManager instanceof RegistryBasedTemplateManager); + } +} \ No newline at end of file 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 d12f5f9e..c7fa613c 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 @@ +