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 Dec 2, 2024
1 parent aad55b6 commit 03b1325
Showing 1 changed file with 37 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException;
import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.Property;
import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.AuthenticationType;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType;
import org.wso2.carbon.identity.common.testng.WithAxisConfiguration;
Expand Down Expand Up @@ -58,10 +59,10 @@ public class ApplicationAuthenticatorServiceTest {
private String tenantDomain;

private ApplicationAuthenticatorService authenticatorService;;
private LocalAuthenticatorConfig authenticatorConfig1;
private LocalAuthenticatorConfig authenticatorConfig2;
private LocalAuthenticatorConfig nonExistAuthenticatorConfig;
private LocalAuthenticatorConfig systemAuthenticatorConfig;
private UserDefinedLocalAuthenticatorConfig authenticatorConfig1;
private UserDefinedLocalAuthenticatorConfig authenticatorConfig2;
private UserDefinedLocalAuthenticatorConfig nonExistAuthenticatorConfig;
private UserDefinedLocalAuthenticatorConfig systemAuthenticatorConfig;

private static final String authenticator1Name = "auth1";
private static final String authenticator2Name = "auth2";
Expand All @@ -85,8 +86,7 @@ public void setUpClass() throws Exception {
private void addSystemAuthenticator() throws AuthenticatorMgtException {

AuthenticatorManagementDAOImpl dao = new AuthenticatorManagementDAOImpl();
dao.addUserDefinedLocalAuthenticator(systemAuthenticatorConfig, MultitenantConstants.SUPER_TENANT_ID,
AuthenticationType.IDENTIFICATION);
dao.addUserDefinedLocalAuthenticator(systemAuthenticatorConfig, MultitenantConstants.SUPER_TENANT_ID);

}

Expand All @@ -100,11 +100,11 @@ public Object[][] authenticatorConfigForCreation() {
}

@Test(priority = 1, dataProvider = "authenticatorConfigForCreation")
public void testCreateUserDefinedLocalAuthenticator(LocalAuthenticatorConfig config, AuthenticationType type)
throws AuthenticatorMgtException {
public void testCreateUserDefinedLocalAuthenticator(UserDefinedLocalAuthenticatorConfig config,
AuthenticationType type) throws AuthenticatorMgtException {

LocalAuthenticatorConfig createdAuthenticator = authenticatorService.createUserDefinedLocalAuthenticator(
config, type, tenantDomain);
UserDefinedLocalAuthenticatorConfig createdAuthenticator = authenticatorService
.addUserDefinedLocalAuthenticator(config, tenantDomain);

Assert.assertEquals(createdAuthenticator.getName(), config.getName());
Assert.assertEquals(createdAuthenticator.getDisplayName(), config.getDisplayName());
Expand All @@ -118,37 +118,37 @@ public void testCreateUserDefinedLocalAuthenticator(LocalAuthenticatorConfig con
}

@Test(priority = 2, dataProvider = "authenticatorConfigForCreation", expectedExceptions =
AuthenticatorMgtException.class, expectedExceptionsMessageRegExp = "There is already an authenticator.")
public void testCreateUserDefinedLocalAuthenticatorWithExistingAuthenticator(LocalAuthenticatorConfig config,
AuthenticationType type) throws AuthenticatorMgtException {
AuthenticatorMgtException.class, expectedExceptionsMessageRegExp = "An authenticator already exists.")
public void testCreateUserDefinedLocalAuthenticatorWithExistingAuthenticator(
UserDefinedLocalAuthenticatorConfig config, AuthenticationType type) throws AuthenticatorMgtException {

authenticatorService.createUserDefinedLocalAuthenticator(config, type, tenantDomain);
authenticatorService.addUserDefinedLocalAuthenticator(config, tenantDomain);
}

@Test(priority = 3, expectedExceptions = AuthenticatorMgtException.class,
expectedExceptionsMessageRegExp = "No operations allowed on system authenticators.")
public void testCreateUserDefinedLocalAuthenticatorWithSystemAuthenticator() throws AuthenticatorMgtException {

authenticatorService.createUserDefinedLocalAuthenticator(createAuthenticatorConfig(systemAuthenticator + "new",
DefinedByType.SYSTEM), AuthenticationType.IDENTIFICATION, tenantDomain);
authenticatorService.addUserDefinedLocalAuthenticator(createAuthenticatorConfig(systemAuthenticator + "new",
DefinedByType.SYSTEM), tenantDomain);
}

@Test(priority = 3, expectedExceptions = AuthenticatorMgtException.class,
expectedExceptionsMessageRegExp = "Blank field value is provided.")
expectedExceptionsMessageRegExp = "Invalid empty or blank value.")
public void testCreateUserDefinedLocalAuthenticatorWithBlankDisplayName() throws AuthenticatorMgtException {

LocalAuthenticatorConfig config = createAuthenticatorConfig("withBlankDisplayName", DefinedByType.USER);
UserDefinedLocalAuthenticatorConfig config = createAuthenticatorConfig("withBlankDisplayName",
DefinedByType.USER);
config.setDisplayName("");
authenticatorService.createUserDefinedLocalAuthenticator(config, AuthenticationType.IDENTIFICATION,
tenantDomain);
authenticatorService.addUserDefinedLocalAuthenticator(config, tenantDomain);
}

@Test(priority = 3, expectedExceptions = AuthenticatorMgtException.class,
expectedExceptionsMessageRegExp = "Invalid authenticator name is provided.")
expectedExceptionsMessageRegExp = "Authenticator name is invalid.")
public void testCreateUserDefinedLocalAuthenticatorWithInvalidName() throws AuthenticatorMgtException {

authenticatorService.createUserDefinedLocalAuthenticator(createAuthenticatorConfig("323#2@dwd",
DefinedByType.USER), AuthenticationType.IDENTIFICATION, tenantDomain);
authenticatorService.addUserDefinedLocalAuthenticator(createAuthenticatorConfig("323#2@dwd",
DefinedByType.USER), tenantDomain);
}

@DataProvider(name = "authenticatorConfigToModify")
Expand All @@ -171,11 +171,11 @@ public Object[][] authenticatorConfigToModify() {
}

@Test(priority = 4, dataProvider = "authenticatorConfigToModify")
public void testUpdateUserDefinedLocalAuthenticator(LocalAuthenticatorConfig config)
public void testUpdateUserDefinedLocalAuthenticator(UserDefinedLocalAuthenticatorConfig config)
throws AuthenticatorMgtException {

LocalAuthenticatorConfig updatedAuthenticator = authenticatorService.updateUserDefinedLocalAuthenticator(
config, tenantDomain);
UserDefinedLocalAuthenticatorConfig updatedAuthenticator = authenticatorService
.updateUserDefinedLocalAuthenticator(config, tenantDomain);

Assert.assertEquals(updatedAuthenticator.getName(), config.getName());
Assert.assertEquals(updatedAuthenticator.getDisplayName(), config.getDisplayName());
Expand All @@ -185,7 +185,7 @@ public void testUpdateUserDefinedLocalAuthenticator(LocalAuthenticatorConfig con
}

@Test(priority = 5, expectedExceptions = AuthenticatorMgtException.class,
expectedExceptionsMessageRegExp = "No Authenticator is found.")
expectedExceptionsMessageRegExp = "No Authenticator found.")
public void testUpdateUserDefinedLocalAuthenticatorWithNonExistingAuthenticator() throws AuthenticatorMgtException {

authenticatorService.updateUserDefinedLocalAuthenticator(nonExistAuthenticatorConfig, tenantDomain);
Expand All @@ -210,10 +210,10 @@ public Object[][] authenticatorConfigToRetrieve() {
}

@Test(priority = 7, dataProvider = "authenticatorConfigToRetrieve")
public void testGetUserDefinedLocalAuthenticator(LocalAuthenticatorConfig configToBeRetrieved,
LocalAuthenticatorConfig expectedConfig, String type) throws AuthenticatorMgtException {
public void testGetUserDefinedLocalAuthenticator(UserDefinedLocalAuthenticatorConfig configToBeRetrieved,
UserDefinedLocalAuthenticatorConfig expectedConfig, String type) throws AuthenticatorMgtException {

LocalAuthenticatorConfig retrievedConfig =
UserDefinedLocalAuthenticatorConfig retrievedConfig =
authenticatorService.getUserDefinedLocalAuthenticator(configToBeRetrieved.getName(), tenantDomain);
Assert.assertEquals(retrievedConfig, expectedConfig);
if (expectedConfig != null) {
Expand All @@ -231,7 +231,7 @@ public void testGetUserDefinedLocalAuthenticator(LocalAuthenticatorConfig config
@Test(priority = 8)
public void testGetAllUserDefinedLocalAuthenticator() throws AuthenticatorMgtException {

List<LocalAuthenticatorConfig> retrievedConfig = authenticatorService.getLocalAuthenticators(tenantDomain);
List<LocalAuthenticatorConfig> retrievedConfig = authenticatorService.getLocalAuthenticators();
Assert.assertEquals(retrievedConfig.size(), 3);
}

Expand All @@ -248,15 +248,15 @@ public void testGetUserDefinedLocalAuthenticatorWithSystemAuthenticator() throws
}

@Test(priority = 10, dataProvider = "authenticatorConfigToModify")
public void testDeleteUserDefinedLocalAuthenticator(LocalAuthenticatorConfig config)
public void testDeleteUserDefinedLocalAuthenticator(UserDefinedLocalAuthenticatorConfig config)
throws AuthenticatorMgtException {

authenticatorService.deleteUserDefinedLocalAuthenticator(config.getName(), tenantDomain);
Assert.assertNull(authenticatorService.getLocalAuthenticatorByName(config.getName()));
}

@Test(priority = 11, expectedExceptions = AuthenticatorMgtException.class,
expectedExceptionsMessageRegExp = "No Authenticator is found.")
expectedExceptionsMessageRegExp = "No Authenticator found.")
public void testDeleteUserDefinedLocalAuthenticatorWithNonExistingAuthenticator() throws AuthenticatorMgtException {

authenticatorService.deleteUserDefinedLocalAuthenticator(nonExistAuthenticatorConfig.getName(), tenantDomain);
Expand All @@ -269,9 +269,11 @@ public void testDeleteUserDefinedLocalAuthenticatorWithSystemAuthenticator() thr
authenticatorService.deleteUserDefinedLocalAuthenticator(systemAuthenticatorConfig.getName(), tenantDomain);
}

private LocalAuthenticatorConfig createAuthenticatorConfig(String uniqueIdentifier, DefinedByType definedByType) {
private UserDefinedLocalAuthenticatorConfig createAuthenticatorConfig(String uniqueIdentifier,
DefinedByType definedByType) {

LocalAuthenticatorConfig authenticatorConfig = new LocalAuthenticatorConfig();
UserDefinedLocalAuthenticatorConfig authenticatorConfig = new
UserDefinedLocalAuthenticatorConfig(AuthenticationType.IDENTIFICATION);
authenticatorConfig.setName(uniqueIdentifier);
authenticatorConfig.setDisplayName("Custom " + uniqueIdentifier);
authenticatorConfig.setEnabled(true);
Expand Down

0 comments on commit 03b1325

Please sign in to comment.