-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to switch template storage type via config
- Loading branch information
1 parent
056ed1d
commit 8b240c8
Showing
7 changed files
with
200 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...il.mgt/src/main/java/org/wso2/carbon/email/mgt/dao/TemplatePersistenceManagerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...gt/src/test/java/org/wso2/carbon/email/mgt/dao/TemplatePersistenceManagerFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters