Skip to content

Commit

Permalink
Add ability to switch template storage type via config
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanasbg committed Apr 18, 2024
1 parent 056ed1d commit 8b240c8
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<test name="EmailMgtTests" preserve-order="true" parallel="false">
<classes>
<class name="org.wso2.carbon.email.mgt.dao.TemplatePersistenceManagerFactoryTest"/>
<class name="org.wso2.carbon.email.mgt.OrganizationEmailTemplateTest"/>
<class name="org.wso2.carbon.email.mgt.ApplicationEmailTemplateTest"/>
<class name="org.wso2.carbon.email.mgt.util.I18nEmailUtilTest"/>
Expand Down

0 comments on commit 8b240c8

Please sign in to comment.