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 Nov 25, 2024
1 parent ca69b24 commit 4d242f4
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.wso2.carbon.identity.action.management.ActionManagementService;
import org.wso2.carbon.identity.action.management.exception.ActionMgtException;
import org.wso2.carbon.identity.action.management.model.Action;
import org.wso2.carbon.identity.action.management.model.Authentication;
import org.wso2.carbon.identity.action.management.model.EndpointConfig;
import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService;
import org.wso2.carbon.identity.application.common.ProvisioningConnectorService;
Expand Down Expand Up @@ -64,6 +63,7 @@
import org.wso2.carbon.idp.mgt.dao.CacheBackedIdPMgtDAO;
import org.wso2.carbon.idp.mgt.dao.IdPManagementDAO;
import org.wso2.carbon.idp.mgt.internal.IdpMgtServiceComponentHolder;
import org.wso2.carbon.idp.mgt.util.ActionMgtTestUtil;
import org.wso2.carbon.idp.mgt.util.IdPManagementConstants;
import org.wso2.carbon.idp.mgt.util.IdPManagementConstants.ErrorMessage;
import org.wso2.carbon.idp.mgt.util.MetadataConverter;
Expand Down Expand Up @@ -119,7 +119,7 @@ public class IdentityProviderManagementServiceTest {
private MockedStatic<CryptoUtil> cryptoUtil;
private ActionManagementService actionManagementService;

private static final String ASSOCIATED_ACTION_ID = "Dummp_Action_ID";
private static final String ASSOCIATED_ACTION_ID = "Dummy_Action_ID";
private static final String CUSTOM_IDP_NAME = "customIdP";
private static Action action;
private static EndpointConfig endpointConfig;
Expand Down Expand Up @@ -150,11 +150,13 @@ public void setUpClass() throws Exception {

registerSystemAuthenticators();

endpointConfig = createEndpointConfig("http://localhost", "admin", "admin");
endpointConfigToBeUpdated = createEndpointConfig("http://localhost1", "admin1", "admin1");
action = createAction(endpointConfig);
userDefinedIdP = createIdPWithUserDefinedFederatedAuthenticatorConfig(CUSTOM_IDP_NAME, action.getEndpoint());
idpForErrorScenarios = createIdPWithUserDefinedFederatedAuthenticatorConfig(
endpointConfig = ActionMgtTestUtil.createEndpointConfig("http://localhost", "admin", "admin");
endpointConfigToBeUpdated = ActionMgtTestUtil.createEndpointConfig(
"http://localhost1", "admin1", "admin1");
action = ActionMgtTestUtil.createAction(endpointConfig);
userDefinedIdP = ActionMgtTestUtil.createIdPWithUserDefinedFederatedAuthenticatorConfig(
CUSTOM_IDP_NAME, action.getEndpoint());
idpForErrorScenarios = ActionMgtTestUtil.createIdPWithUserDefinedFederatedAuthenticatorConfig(
CUSTOM_IDP_NAME + "Error", action.getEndpoint());
}

Expand Down Expand Up @@ -733,7 +735,8 @@ public void testForceDeleteIdPException(String idpName) throws Exception {
@Test
public void testUpdateIdPActionException() throws Exception {

IdentityProvider idpForErrorScenariosTobeUpdate = createIdPWithUserDefinedFederatedAuthenticatorConfig(
IdentityProvider idpForErrorScenariosTobeUpdate = ActionMgtTestUtil.
createIdPWithUserDefinedFederatedAuthenticatorConfig(
idpForErrorScenarios.getIdentityProviderName(), endpointConfig);
identityProviderManagementService.addIdP(idpForErrorScenarios);

Expand Down Expand Up @@ -819,10 +822,10 @@ public Object[][] updateIdPData() {
IdentityProvider idp3New = new IdentityProvider();
idp3New.setIdentityProviderName("testIdP3New");

IdentityProvider updateIdPWithExistingUserDefinedFedAuth = createIdPWithUserDefinedFederatedAuthenticatorConfig(
CUSTOM_IDP_NAME + "new", endpointConfigToBeUpdated);
IdentityProvider updateIdPWithNewUserDefinedFedAuth = createIdPWithUserDefinedFederatedAuthenticatorConfig(
CUSTOM_IDP_NAME + "new", endpointConfigToBeUpdated);
IdentityProvider updateIdPWithExistingUserDefinedFedAuth = ActionMgtTestUtil.
createIdPWithUserDefinedFederatedAuthenticatorConfig(CUSTOM_IDP_NAME + "new", endpointConfigToBeUpdated);
IdentityProvider updateIdPWithNewUserDefinedFedAuth = ActionMgtTestUtil.
createIdPWithUserDefinedFederatedAuthenticatorConfig(CUSTOM_IDP_NAME + "new", endpointConfigToBeUpdated);
updateIdPWithNewUserDefinedFedAuth.getFederatedAuthenticatorConfigs()[0].setName("New Fed Auth");


Expand Down Expand Up @@ -1442,48 +1445,6 @@ private FederatedAuthenticatorConfig federatedAuthenticatorConfigWithIdpEntityId
return federatedAuthenticatorConfig;
}

private Action createAction(EndpointConfig endpointConfig) {

Action.ActionResponseBuilder actionResponseBuilder = new Action.ActionResponseBuilder();
actionResponseBuilder.id(ASSOCIATED_ACTION_ID);
actionResponseBuilder.name("SampleAssociatedAction");
actionResponseBuilder.type(Action.ActionTypes.AUTHENTICATION);
actionResponseBuilder.description("SampleDescription");
actionResponseBuilder.status(Action.Status.ACTIVE);
actionResponseBuilder.endpoint(endpointConfig);
return actionResponseBuilder.build();
}

private EndpointConfig createEndpointConfig(String uri, String username, String password) {

EndpointConfig.EndpointConfigBuilder endpointConfigBuilder = new EndpointConfig.EndpointConfigBuilder();
endpointConfigBuilder.uri(uri);
endpointConfigBuilder.authentication(
new Authentication.BasicAuthBuilder(username, password).build());
return endpointConfigBuilder.build();
}

private IdentityProvider createIdPWithUserDefinedFederatedAuthenticatorConfig(String idpName,
EndpointConfig endpointConfig) {

// Initialize Test Identity Provider 4 with custom user defined federated authenticator.
IdentityProvider newUserDefinedIdp = new IdentityProvider();
newUserDefinedIdp.setIdentityProviderName(idpName);

UserDefinedFederatedAuthenticatorConfig userDefinedFederatedAuthenticatorConfig = new
UserDefinedFederatedAuthenticatorConfig();
userDefinedFederatedAuthenticatorConfig.setDisplayName("DisplayName1");
userDefinedFederatedAuthenticatorConfig.setName("customFedAuthenticator");
userDefinedFederatedAuthenticatorConfig.setEnabled(true);
userDefinedFederatedAuthenticatorConfig.setEndpointConfig(
buildUserDefinedAuthenticatorEndpointConfig(endpointConfig));
userDefinedFederatedAuthenticatorConfig.setDefinedByType(DefinedByType.USER);
userDefinedFederatedAuthenticatorConfig.setProperties(new Property[]{});
newUserDefinedIdp.setFederatedAuthenticatorConfigs(
new FederatedAuthenticatorConfig[]{userDefinedFederatedAuthenticatorConfig});
return newUserDefinedIdp;
}

private UserDefinedAuthenticatorEndpointConfig buildUserDefinedAuthenticatorEndpointConfig(
EndpointConfig endpointConfig) {

Expand Down
Loading

0 comments on commit 4d242f4

Please sign in to comment.