Search in sources :

Example 21 with FunctionLibraryManagementException

use of org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException in project carbon-identity-framework by wso2.

the class FunctionLibraryManagementServiceTest method testFunctionLibraryNameRequired.

@Test(dataProvider = "testFunctionLibraryNameRequiredDataProvider")
public void testFunctionLibraryNameRequired(Object functionLibrary, String tenantDomain) {
    FunctionLibraryDAOImpl functionLibraryDAO = PowerMockito.mock(FunctionLibraryDAOImpl.class);
    try {
        PowerMockito.whenNew(FunctionLibraryDAOImpl.class).withNoArguments().thenReturn(functionLibraryDAO);
        when(functionLibraryDAO.getFunctionLibrary(((FunctionLibrary) functionLibrary).getFunctionLibraryName(), tenantDomain)).thenReturn((FunctionLibrary) functionLibrary);
        FunctionLibraryManagementService functionLibraryManagementService = FunctionLibraryManagementServiceImpl.getInstance();
        functionLibraryManagementService.createFunctionLibrary((FunctionLibrary) functionLibrary, tenantDomain);
    } catch (FunctionLibraryManagementException e) {
        assertEquals(e.getMessage(), "Script library name is required");
    } catch (Exception e) {
        fail("Exception", e);
    }
}
Also used : FunctionLibraryDAOImpl(org.wso2.carbon.identity.functions.library.mgt.dao.impl.FunctionLibraryDAOImpl) FunctionLibraryManagementException(org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException) FunctionLibraryManagementException(org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException) Test(org.testng.annotations.Test) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 22 with FunctionLibraryManagementException

use of org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException in project carbon-identity-framework by wso2.

the class FunctionLibraryManagementServiceTest method testIsRegexValidated.

@Test(dataProvider = "testIsRegexValidatedDataProvider")
public void testIsRegexValidated(Object functionLibrary, String tenantDomain) {
    FunctionLibraryDAOImpl functionLibraryDAO = PowerMockito.mock(FunctionLibraryDAOImpl.class);
    try {
        PowerMockito.whenNew(FunctionLibraryDAOImpl.class).withNoArguments().thenReturn(functionLibraryDAO);
        when(functionLibraryDAO.getFunctionLibrary(((FunctionLibrary) functionLibrary).getFunctionLibraryName(), tenantDomain)).thenReturn((FunctionLibrary) functionLibrary);
        FunctionLibraryManagementService functionLibraryManagementService = FunctionLibraryManagementServiceImpl.getInstance();
        functionLibraryManagementService.createFunctionLibrary((FunctionLibrary) functionLibrary, tenantDomain);
    } catch (FunctionLibraryManagementException e) {
        assertEquals(e.getMessage(), "The script library name is not valid! It is not adhering to the regex " + FunctionLibraryMgtUtil.FUNCTION_LIBRARY_NAME_VALIDATING_REGEX + ".");
    } catch (Exception e) {
        fail("Exception", e);
    }
}
Also used : FunctionLibraryDAOImpl(org.wso2.carbon.identity.functions.library.mgt.dao.impl.FunctionLibraryDAOImpl) FunctionLibraryManagementException(org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException) FunctionLibraryManagementException(org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException) Test(org.testng.annotations.Test) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 23 with FunctionLibraryManagementException

use of org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException in project identity-api-server by wso2.

the class ServerScriptLibrariesService method handleScriptLibraryError.

/**
 * Handle FunctionLibraryManagementExceptions, error description and status code to be sent
 * in the response.
 *
 * @param e         FunctionLibraryManagementException
 * @param errorEnum Error Message information.
 * @return APIError.
 */
private APIError handleScriptLibraryError(FunctionLibraryManagementException e, Constants.ErrorMessage errorEnum) {
    ErrorResponse errorResponse;
    Response.Status status;
    if (e instanceof FunctionLibraryManagementClientException) {
        errorResponse = getErrorBuilder(errorEnum).build(log, e.getMessage());
        createErrorResponse(e, errorResponse);
        status = Response.Status.BAD_REQUEST;
    } else if (e instanceof FunctionLibraryManagementServerException) {
        errorResponse = getErrorBuilder(errorEnum).build(log, e, errorEnum.getDescription());
        createErrorResponse(e, errorResponse);
        status = Response.Status.INTERNAL_SERVER_ERROR;
    } else {
        errorResponse = getErrorBuilder(errorEnum).build(log, e, errorEnum.getDescription());
        status = Response.Status.INTERNAL_SERVER_ERROR;
    }
    return new APIError(status, errorResponse);
}
Also used : ErrorResponse(org.wso2.carbon.identity.api.server.common.error.ErrorResponse) ScriptLibraryResponse(org.wso2.carbon.identity.api.server.script.library.v1.model.ScriptLibraryResponse) ScriptLibraryListResponse(org.wso2.carbon.identity.api.server.script.library.v1.model.ScriptLibraryListResponse) Response(javax.ws.rs.core.Response) FunctionLibraryManagementClientException(org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementClientException) APIError(org.wso2.carbon.identity.api.server.common.error.APIError) ErrorResponse(org.wso2.carbon.identity.api.server.common.error.ErrorResponse) FunctionLibraryManagementServerException(org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementServerException)

Example 24 with FunctionLibraryManagementException

use of org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException in project identity-api-server by wso2.

the class ServerScriptLibrariesService method addScriptLibrary.

/**
 * Add a script library.
 *
 * @param name               Name of the script library.
 * @param contentInputStream Content of the script library code.
 * @param description        Description of the script library
 */
public void addScriptLibrary(String name, InputStream contentInputStream, String description) {
    ScriptLibraryPOSTRequest scriptLibraryPOSTRequest = new ScriptLibraryPOSTRequest();
    scriptLibraryPOSTRequest.setName(name);
    scriptLibraryPOSTRequest.setDescription(description);
    try {
        scriptLibraryPOSTRequest.setContent(new File(IOUtils.toString(contentInputStream, StandardCharsets.UTF_8.name())));
    } catch (IOException e) {
        log.error("Error occurred while reading contentInputStream: " + e);
        throw handleScriptLibraryClientError(Constants.ErrorMessage.ERROR_CODE_ERROR_ADDING_SCRIPT_LIBRARY, Response.Status.INTERNAL_SERVER_ERROR);
    }
    if (isScriptLibraryAvailable(scriptLibraryPOSTRequest.getName())) {
        throw handleScriptLibraryClientError(Constants.ErrorMessage.ERROR_SCRIPT_LIBRARY_ALREADY_FOUND, Response.Status.CONFLICT, scriptLibraryPOSTRequest.getName(), ContextLoader.getTenantDomainFromContext());
    } else {
        FunctionLibrary functionLibrary = createScriptLibrary(scriptLibraryPOSTRequest);
        try {
            if (scriptLibraryPOSTRequest.getName().contains(Constants.SCRIPT_LIBRARY_EXTENSION)) {
                ScriptLibraryServiceHolder.getScriptLibraryManagementService().createFunctionLibrary(functionLibrary, ContextLoader.getTenantDomainFromContext());
            } else {
                throw handleScriptLibraryClientError(Constants.ErrorMessage.ERROR_SCRIPT_LIBRARY_NAME_VALIDATION, Response.Status.BAD_REQUEST);
            }
        } catch (FunctionLibraryManagementException e) {
            throw handleScriptLibraryError(e, Constants.ErrorMessage.ERROR_CODE_ERROR_ADDING_SCRIPT_LIBRARY);
        }
    }
}
Also used : FunctionLibraryManagementException(org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException) ScriptLibraryPOSTRequest(org.wso2.carbon.identity.api.server.script.library.v1.model.ScriptLibraryPOSTRequest) FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary) IOException(java.io.IOException) File(java.io.File)

Example 25 with FunctionLibraryManagementException

use of org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException in project identity-api-server by wso2.

the class ServerScriptLibrariesService method createScriptLibraryResponse.

/**
 * Create the script library response.
 *
 * @param scriptLibrary script library object.
 * @return scriptLibraryResponse
 */
private ScriptLibraryResponse createScriptLibraryResponse(FunctionLibrary scriptLibrary) {
    ScriptLibraryResponse scriptLibraryResponse = new ScriptLibraryResponse();
    scriptLibraryResponse.setName(scriptLibrary.getFunctionLibraryName());
    scriptLibraryResponse.setDescription(scriptLibrary.getDescription());
    try {
        String displayName = URLEncoder.encode(scriptLibrary.getFunctionLibraryName(), StandardCharsets.UTF_8.name());
        scriptLibraryResponse.setContentRef(ContextLoader.buildURIForBody(String.format(V1_API_PATH_COMPONENT + SCRIPT_LIBRARY_PATH_COMPONENT + "/%s" + SCRIPT_LIBRARY_CONTENT_PATH, displayName)).toString().replace("+", "%20"));
        return scriptLibraryResponse;
    } catch (UnsupportedEncodingException e) {
        FunctionLibraryManagementException error = new FunctionLibraryManagementException(Constants.ErrorMessage.ERROR_CODE_ERROR_ENCODING_URL.getMessage(), e);
        throw handleScriptLibraryError(error, Constants.ErrorMessage.ERROR_CODE_ERROR_ENCODING_URL);
    }
}
Also used : ScriptLibraryResponse(org.wso2.carbon.identity.api.server.script.library.v1.model.ScriptLibraryResponse) FunctionLibraryManagementException(org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

FunctionLibrary (org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 Test (org.testng.annotations.Test)12 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)12 FunctionLibraryDAO (org.wso2.carbon.identity.functions.library.mgt.dao.FunctionLibraryDAO)11 Connection (java.sql.Connection)10 FunctionLibraryManagementException (org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException)9 FunctionLibraryDAOImpl (org.wso2.carbon.identity.functions.library.mgt.dao.impl.FunctionLibraryDAOImpl)7 SQLException (java.sql.SQLException)6 IOException (java.io.IOException)4 PreparedStatement (java.sql.PreparedStatement)4 IdentityRuntimeException (org.wso2.carbon.identity.base.IdentityRuntimeException)4 File (java.io.File)2 ResultSet (java.sql.ResultSet)2 ScriptLibraryResponse (org.wso2.carbon.identity.api.server.script.library.v1.model.ScriptLibraryResponse)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Response (javax.ws.rs.core.Response)1 APIError (org.wso2.carbon.identity.api.server.common.error.APIError)1 ErrorResponse (org.wso2.carbon.identity.api.server.common.error.ErrorResponse)1