Skip to content

Commit

Permalink
Add tests for SAMLSSOPersistenceManagerFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Osara-B committed Dec 19, 2024
1 parent 95929a8 commit b2e7bdf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class SAMLSSOPersistenceManagerFactory {

private static final Log LOG = LogFactory.getLog(SAMLSSOPersistenceManagerFactory.class);
private static final String SAML_STORAGE_TYPE = IdentityUtil.getProperty(SAML_STORAGE_CONFIG);
private static String SAML_STORAGE_TYPE = IdentityUtil.getProperty(SAML_STORAGE_CONFIG);
private static final String HYBRID = "hybrid";
private static final String REGISTRY = "registry";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.wso2.carbon.identity.core.dao;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.lang.reflect.Field;

import static org.testng.Assert.assertTrue;

public class SAMLSSOPersistenceManagerFactoryTest {

private SAMLSSOPersistenceManagerFactory factory;

@BeforeMethod
public void setUp() {

factory = new SAMLSSOPersistenceManagerFactory();

}

@AfterMethod
public void tearDown() throws Exception {

setPrivateStaticField(SAMLSSOPersistenceManagerFactory.class, "SAML_STORAGE_TYPE", "");
factory = null;
}

@Test
public void testBuildSSOServiceProviderManagerWithDefaultStorage() throws Exception {

setPrivateStaticField(SAMLSSOPersistenceManagerFactory.class, "SAML_STORAGE_TYPE", "database");
SAMLSSOServiceProviderDAO samlSSOServiceProviderDAO = factory.buildSSOServiceProviderManager();
assertTrue(samlSSOServiceProviderDAO instanceof JDBCSAMLSSOServiceProviderDAOImpl);
}

@Test
public void testBuildSSOServiceProviderManagerWithRegistryStorage() throws Exception {

setPrivateStaticField(SAMLSSOPersistenceManagerFactory.class, "SAML_STORAGE_TYPE", "registry");
SAMLSSOServiceProviderDAO samlSSOServiceProviderDAO = factory.buildSSOServiceProviderManager();
assertTrue(samlSSOServiceProviderDAO instanceof RegistrySAMLSSOServiceProviderDAOImpl);
}

@Test
public void testBuildSSOServiceProviderManagerWithHybridStorage() throws Exception {

setPrivateStaticField(SAMLSSOPersistenceManagerFactory.class, "SAML_STORAGE_TYPE", "hybrid");
SAMLSSOServiceProviderDAO samlSSOServiceProviderDAO = factory.buildSSOServiceProviderManager();
assertTrue(samlSSOServiceProviderDAO instanceof JDBCSAMLSSOServiceProviderDAOImpl);
}

private void setPrivateStaticField(Class<?> clazz, String fieldName, Object newValue)
throws NoSuchFieldException, IllegalAccessException {

Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(null, newValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<class name="org.wso2.carbon.identity.core.IdentityKeyStoreResolverTest"/>
<class name="org.wso2.carbon.identity.core.util.IdentityKeyStoreResolverUtilTest"/>
<class name="org.wso2.carbon.identity.core.SAMLSSOServiceProviderManagerTest"/>
<class name="org.wso2.carbon.identity.core.dao.SAMLSSOPersistenceManagerFactoryTest"/>
</classes>
</test>
</suite>

0 comments on commit b2e7bdf

Please sign in to comment.