Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Jan 27, 2025
1 parent 86b4aae commit b440b28
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public ApplicationAuthenticator getApplicationAuthenticatorByName(String authent
// Check whether the authenticator config is the user defined local authenticator config, if so resolve it.
try {
UserDefinedLocalAuthenticatorConfig localConfig = ApplicationAuthenticatorService.getInstance()
.getUserDefinedLocalAuthenticator(tenantDomain, authenticatorName);
.getUserDefinedLocalAuthenticator(authenticatorName, tenantDomain);
if (localConfig != null) {
return FrameworkServiceDataHolder.getInstance().getUserDefinedAuthenticatorService()
.getUserDefinedLocalAuthenticator(localConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

import org.mockito.MockedStatic;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.application.authentication.framework.AbstractFrameworkTest;
Expand Down Expand Up @@ -86,16 +88,18 @@ public void setUp() throws Exception {

mockIdentityConfigParser = mock(IdentityConfigParser.class);
identityConfigParser = mockStatic(IdentityConfigParser.class);
identityConfigParser.when(IdentityConfigParser::getInstance).thenReturn(mockIdentityConfigParser);
}

@BeforeTest
public void setUpTest() throws Exception {

removeAllSystemDefinedAuthenticators();
applicationAuthenticatorService.addSystemDefinedAuthenticator(systemDefinedAuthenticator);
when(userDefinedAuthenticatorService.getUserDefinedLocalAuthenticator(any())).thenReturn(
userDefinedLocalAuthenticator);
when(userDefinedAuthenticatorService.getUserDefinedFederatedAuthenticator(any())).thenReturn(
userDefinedFederatedAuthenticator);
FrameworkServiceDataHolder.getInstance().setUserDefinedAuthenticatorService(userDefinedAuthenticatorService);

identityConfigParser.when(IdentityConfigParser::getInstance).thenReturn(mockIdentityConfigParser);

mockedAuthenticationService.when(ApplicationAuthenticatorService::getInstance).thenReturn(authenticatorService);
when(authenticatorService.getAllUserDefinedLocalAuthenticators(TENANT_DOMAIN)).thenReturn(
Expand All @@ -119,23 +123,50 @@ public void tearDown() {
public void testGetAllAuthenticatorsWithAuthActionTypeEnabledAndNotNullUserDefinedAuthenticatorService() {

setAuthenticatorActionEnableStatus(true);
FrameworkServiceDataHolder.getInstance().setUserDefinedAuthenticatorService(userDefinedAuthenticatorService);
List<ApplicationAuthenticator> result = applicationAuthenticatorService.getAllAuthenticators(TENANT_DOMAIN);
assertEquals(3, result.size());
}

@Test
public void testGetAllAuthenticatorsWithAuthActionTypeEnabledAndNullUserDefinedAuthenticatorService() {
public void testGetAllAuthenticatorsWithAuthenticationActionTypeDisabled() {

FrameworkServiceDataHolder.getInstance().setUserDefinedAuthenticatorService(null);
setAuthenticatorActionEnableStatus(true);
setAuthenticatorActionEnableStatus(false);
FrameworkServiceDataHolder.getInstance().setUserDefinedAuthenticatorService(userDefinedAuthenticatorService);
List<ApplicationAuthenticator> result = applicationAuthenticatorService.getAllAuthenticators(TENANT_DOMAIN);
assertEquals(1, result.size());
}

@Test
public void testGetAllAuthenticatorsWithAuthenticationActionTypeDisabled() {
public void testGetAuthenticatorByNameWithAuthenticationActionTypeDisabled() {

setAuthenticatorActionEnableStatus(false);
FrameworkServiceDataHolder.getInstance().setUserDefinedAuthenticatorService(userDefinedAuthenticatorService);
Assert.assertNotNull(applicationAuthenticatorService.getApplicationAuthenticatorByName(
SYSTEM_DEFINED_AUTHENTICATOR_NAME, TENANT_DOMAIN));
Assert.assertNull(applicationAuthenticatorService.getApplicationAuthenticatorByName(
USER_DEFINED_FEDERATED_AUTHENTICATOR_NAME, TENANT_DOMAIN));
Assert.assertNull(applicationAuthenticatorService.getApplicationAuthenticatorByName(
USER_DEFINED_LOCAL_AUTHENTICATOR_NAME, TENANT_DOMAIN));
}

@Test
public void testGetAllAuthenticatorsWithAuthActionTypeEnabledAndNullUserDefinedAuthenticatorService() {

FrameworkServiceDataHolder.getInstance().setUserDefinedAuthenticatorService(null);
Assert.assertNotNull(applicationAuthenticatorService.getApplicationAuthenticatorByName(
SYSTEM_DEFINED_AUTHENTICATOR_NAME, TENANT_DOMAIN));
Assert.assertNull(applicationAuthenticatorService.getApplicationAuthenticatorByName(
USER_DEFINED_FEDERATED_AUTHENTICATOR_NAME, TENANT_DOMAIN));
Assert.assertNull(applicationAuthenticatorService.getApplicationAuthenticatorByName(
USER_DEFINED_LOCAL_AUTHENTICATOR_NAME, TENANT_DOMAIN));
}

@Test
public void testGetAuthenticatorByNameWithAuthActionTypeEnabledAndNullUserDefinedAuthenticatorService() {

FrameworkServiceDataHolder.getInstance().setUserDefinedAuthenticatorService(null);
setAuthenticatorActionEnableStatus(true);
List<ApplicationAuthenticator> result = applicationAuthenticatorService.getAllAuthenticators(TENANT_DOMAIN);
assertEquals(1, result.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
Expand Down Expand Up @@ -1138,6 +1139,18 @@ public void testGetEffectiveIdpGroupClaimUriLegacyBehaviourWithNoCustomClaimMapp
assertEquals(result, "rolesClaimInDialect");
}
}

@Test
public void getAppAuthenticatorByNameExistingAuthenticator() {

Assert.assertNotNull(FrameworkUtils.getAppAuthenticatorByName("BasicAuthenticator"));
}

@Test
public void getAppAuthenticatorByNameNonExistingAuthenticator() {

Assert.assertNull(FrameworkUtils.getAppAuthenticatorByName("NonExistAuthenticator"));
}

private void removeAllSystemDefinedAuthenticators() {

Expand Down

0 comments on commit b440b28

Please sign in to comment.