Search in sources :

Example 1 with TemplateManagementClientException

use of org.wso2.carbon.identity.template.mgt.exception.TemplateManagementClientException in project carbon-identity-framework by wso2.

the class TemplateManagerImplTest method testErrorCodes.

@Test()
public void testErrorCodes() throws Exception {
    DataSource dataSource = mock(DataSource.class);
    mockDataSource(dataSource);
    Template template = new Template(SUPER_TENANT_ID, null, "sample description", sampleScript);
    String errorCode = TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_NAME_REQUIRED.getCode();
    try (Connection connection = getConnection()) {
        Connection spyConnection = spyConnection(connection);
        when(dataSource.getConnection()).thenReturn(spyConnection);
        TemplateManager templateManager = new TemplateManagerImpl();
        try {
            ((TemplateManagerImpl) templateManager).addTemplateUsingTemplateMgtDAO(template);
        } catch (TemplateManagementClientException e) {
            String errorCode1 = e.getErrorCode();
            Assert.assertEquals(errorCode, errorCode1);
        }
    }
}
Also used : Connection(java.sql.Connection) TestUtils.spyConnection(org.wso2.carbon.identity.template.mgt.util.TestUtils.spyConnection) TestUtils.getConnection(org.wso2.carbon.identity.template.mgt.util.TestUtils.getConnection) TemplateManagementClientException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementClientException) DataSource(javax.sql.DataSource) TestUtils.mockDataSource(org.wso2.carbon.identity.template.mgt.util.TestUtils.mockDataSource) Template(org.wso2.carbon.identity.template.mgt.model.Template) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with TemplateManagementClientException

use of org.wso2.carbon.identity.template.mgt.exception.TemplateManagementClientException in project identity-api-server by wso2.

the class ServerIdpManagementService method handleTemplateMgtException.

/**
 * Handle template management exceptions and return related API errors.
 *
 * @param e {@link TemplateManagementException}.
 * @param errorEnum Error message with error code and description.
 * @param data Additional information.
 * @return API error.
 */
private APIError handleTemplateMgtException(TemplateManagementException e, Constants.ErrorMessage errorEnum, String data) {
    ErrorResponse errorResponse;
    Response.Status status;
    if (e instanceof TemplateManagementClientException) {
        if (e.getErrorCode() != null) {
            String errorCode = e.getErrorCode();
            errorResponse = getErrorBuilder(errorCode, e.getMessage(), data).build(log, e.getMessage());
            errorCode = errorCode.contains(TEMPLATE_MGT_ERROR_CODE_DELIMITER) ? errorCode : Constants.IDP_MANAGEMENT_PREFIX + errorCode;
            errorResponse.setCode(errorCode);
        } else {
            errorResponse = getErrorBuilder(errorEnum, data).build(log, e.getMessage());
        }
        errorResponse.setDescription(e.getMessage());
        status = Response.Status.BAD_REQUEST;
    } else if (e instanceof TemplateManagementServerException) {
        if (e.getErrorCode() != null) {
            String errorCode = e.getErrorCode();
            errorResponse = getErrorBuilder(errorCode, e.getMessage(), data).build(log, e, includeData(e.getMessage(), data));
            errorCode = errorCode.contains(TEMPLATE_MGT_ERROR_CODE_DELIMITER) ? errorCode : Constants.IDP_MANAGEMENT_PREFIX + errorCode;
            errorResponse.setCode(errorCode);
        } else {
            errorResponse = getErrorBuilder(errorEnum, data).build(log, e, includeData(e.getMessage(), data));
        }
        errorResponse.setDescription(e.getMessage());
        status = Response.Status.INTERNAL_SERVER_ERROR;
    } else {
        if (e.getErrorCode() != null) {
            errorResponse = getErrorBuilder(e.getErrorCode(), e.getMessage(), data).build(log, e, includeData(e.getMessage(), data));
        } else {
            errorResponse = getErrorBuilder(errorEnum, data).build(log, e, includeData(e.getMessage(), data));
        }
        status = Response.Status.INTERNAL_SERVER_ERROR;
    }
    return new APIError(status, errorResponse);
}
Also used : IdentityProviderTemplateListResponse(org.wso2.carbon.identity.api.server.idp.v1.model.IdentityProviderTemplateListResponse) ErrorResponse(org.wso2.carbon.identity.api.server.common.error.ErrorResponse) IdentityProviderListResponse(org.wso2.carbon.identity.api.server.idp.v1.model.IdentityProviderListResponse) FederatedAuthenticatorListResponse(org.wso2.carbon.identity.api.server.idp.v1.model.FederatedAuthenticatorListResponse) Response(javax.ws.rs.core.Response) IdentityProviderResponse(org.wso2.carbon.identity.api.server.idp.v1.model.IdentityProviderResponse) OutboundConnectorListResponse(org.wso2.carbon.identity.api.server.idp.v1.model.OutboundConnectorListResponse) ProvisioningResponse(org.wso2.carbon.identity.api.server.idp.v1.model.ProvisioningResponse) TemplateManagementServerException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException) APIError(org.wso2.carbon.identity.api.server.common.error.APIError) TemplateManagementClientException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementClientException) ErrorResponse(org.wso2.carbon.identity.api.server.common.error.ErrorResponse)

Aggregations

TemplateManagementClientException (org.wso2.carbon.identity.template.mgt.exception.TemplateManagementClientException)2 Connection (java.sql.Connection)1 DataSource (javax.sql.DataSource)1 Response (javax.ws.rs.core.Response)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 Test (org.testng.annotations.Test)1 APIError (org.wso2.carbon.identity.api.server.common.error.APIError)1 ErrorResponse (org.wso2.carbon.identity.api.server.common.error.ErrorResponse)1 FederatedAuthenticatorListResponse (org.wso2.carbon.identity.api.server.idp.v1.model.FederatedAuthenticatorListResponse)1 IdentityProviderListResponse (org.wso2.carbon.identity.api.server.idp.v1.model.IdentityProviderListResponse)1 IdentityProviderResponse (org.wso2.carbon.identity.api.server.idp.v1.model.IdentityProviderResponse)1 IdentityProviderTemplateListResponse (org.wso2.carbon.identity.api.server.idp.v1.model.IdentityProviderTemplateListResponse)1 OutboundConnectorListResponse (org.wso2.carbon.identity.api.server.idp.v1.model.OutboundConnectorListResponse)1 ProvisioningResponse (org.wso2.carbon.identity.api.server.idp.v1.model.ProvisioningResponse)1 TemplateManagementServerException (org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException)1 Template (org.wso2.carbon.identity.template.mgt.model.Template)1 TestUtils.getConnection (org.wso2.carbon.identity.template.mgt.util.TestUtils.getConnection)1 TestUtils.mockDataSource (org.wso2.carbon.identity.template.mgt.util.TestUtils.mockDataSource)1 TestUtils.spyConnection (org.wso2.carbon.identity.template.mgt.util.TestUtils.spyConnection)1