Search in sources :

Example 11 with FunctionLibraryManagementException

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

the class ServerScriptLibrariesService method updateScriptLibrary.

/**
 * Update a script library identified by resource ID.
 *
 * @param scriptLibraryName  Name of the script library.
 * @param contentInputStream Content of the script library code.
 * @param description        Description of the script library
 */
public void updateScriptLibrary(String scriptLibraryName, InputStream contentInputStream, String description) {
    ScriptLibraryPUTRequest scriptLibraryPUTRequest = new ScriptLibraryPUTRequest();
    scriptLibraryPUTRequest.setDescription(description);
    try {
        scriptLibraryPUTRequest.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_UPDATING_SCRIPT_LIBRARY, Response.Status.INTERNAL_SERVER_ERROR);
    }
    if (isScriptLibraryAvailable(scriptLibraryName)) {
        FunctionLibrary functionLibrary = createScriptLibraryPut(scriptLibraryName, scriptLibraryPUTRequest);
        try {
            ScriptLibraryServiceHolder.getScriptLibraryManagementService().updateFunctionLibrary(scriptLibraryName, functionLibrary, ContextLoader.getTenantDomainFromContext());
        } catch (FunctionLibraryManagementException e) {
            throw handleScriptLibraryError(e, Constants.ErrorMessage.ERROR_CODE_ERROR_UPDATING_SCRIPT_LIBRARY);
        }
    } else {
        throw handleScriptLibraryClientError(Constants.ErrorMessage.ERROR_SCRIPT_LIBRARY_NOT_FOUND, Response.Status.NOT_FOUND, scriptLibraryName, ContextLoader.getTenantDomainFromContext());
    }
}
Also used : ScriptLibraryPUTRequest(org.wso2.carbon.identity.api.server.script.library.v1.model.ScriptLibraryPUTRequest) FunctionLibraryManagementException(org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException) FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary) IOException(java.io.IOException) File(java.io.File)

Example 12 with FunctionLibraryManagementException

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

the class FunctionLibraryDAOImplTest method listFunctionLibraries.

@Test(dataProvider = "listFunctionLibraryDataProvider")
public void listFunctionLibraries(List<Object> functionLibraries, String tenantDomain) throws FunctionLibraryManagementException {
    try (Connection connection = DAOUtils.getConnection(DB_NAME)) {
        FunctionLibraryDAO functionLibraryDAO = new FunctionLibraryDAOImpl();
        addFunctionLibraries(functionLibraryDAO, functionLibraries, tenantDomain);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection);
        List<FunctionLibrary> functionLibrariesList = functionLibraryDAO.listFunctionLibraries(tenantDomain);
        assertTrue(functionLibrariesList != null && functionLibrariesList.size() != 0, "Failed to retrieve script libraries.");
        // Clean after test
        deleteFunctionLibraries(functionLibraryDAO, functionLibraries, tenantDomain);
    } catch (SQLException e) {
        log.error("SQLException");
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary) FunctionLibraryDAO(org.wso2.carbon.identity.functions.library.mgt.dao.FunctionLibraryDAO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 13 with FunctionLibraryManagementException

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

the class FunctionLibraryDAOImplTest method updateFunctionLibrary.

@Test(dataProvider = "updateFunctionLibraryDataProvider")
public void updateFunctionLibrary(Object functionLibrary, String tenantDomain) throws SQLException, FunctionLibraryManagementException {
    try (Connection connection1 = DAOUtils.getConnection(DB_NAME);
        Connection connection2 = DAOUtils.getConnection(DB_NAME)) {
        FunctionLibraryDAO functionLibraryDAO = new FunctionLibraryDAOImpl();
        addFunctionLibraries(functionLibraryDAO, Collections.singletonList(functionLibrary), tenantDomain);
        FunctionLibrary funLib = (FunctionLibrary) functionLibrary;
        String oldName = funLib.getFunctionLibraryName();
        funLib.setFunctionLibraryName("updatedName");
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection1);
        functionLibraryDAO.updateFunctionLibrary(oldName, funLib, tenantDomain);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection2);
        assertNotNull(functionLibraryDAO.getFunctionLibrary(funLib.getFunctionLibraryName(), tenantDomain), "Failed to update script library.");
        // Clean after test
        deleteFunctionLibraries(functionLibraryDAO, Collections.singletonList(functionLibrary), tenantDomain);
    }
}
Also used : Connection(java.sql.Connection) FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary) FunctionLibraryDAO(org.wso2.carbon.identity.functions.library.mgt.dao.FunctionLibraryDAO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 14 with FunctionLibraryManagementException

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

the class FunctionLibraryDAOImplTest method updateFunctionLibraryException.

@Test(dataProvider = "updateFunctionLibraryDataProvider", expectedExceptions = FunctionLibraryManagementException.class)
public void updateFunctionLibraryException(Object functionLibrary, String tenantDomain) throws SQLException, FunctionLibraryManagementException {
    FunctionLibraryDAO functionLibraryDAO = new FunctionLibraryDAOImpl();
    addFunctionLibraries(functionLibraryDAO, Collections.singletonList(functionLibrary), tenantDomain);
    FunctionLibrary funLib = (FunctionLibrary) functionLibrary;
    String oldName = funLib.getFunctionLibraryName();
    funLib.setFunctionLibraryName("updatedName");
    functionLibraryDAO.updateFunctionLibrary(oldName, funLib, tenantDomain);
    Assert.fail("Expected: " + FunctionLibraryManagementException.class.getName());
    // Clean after test
    deleteFunctionLibraries(functionLibraryDAO, Collections.singletonList(functionLibrary), tenantDomain);
}
Also used : FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary) FunctionLibraryDAO(org.wso2.carbon.identity.functions.library.mgt.dao.FunctionLibraryDAO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 15 with FunctionLibraryManagementException

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

the class FunctionLibraryDAOImplTest method isFunctionLibraryExists.

@Test(dataProvider = "isFunctionLibraryExistsDataProvider")
public void isFunctionLibraryExists(Object functionLibrary, String tenantDomain) throws SQLException, FunctionLibraryManagementException {
    try (Connection connection1 = DAOUtils.getConnection(DB_NAME);
        Connection connection2 = DAOUtils.getConnection(DB_NAME)) {
        FunctionLibraryDAO functionLibraryDAO = new FunctionLibraryDAOImpl();
        addFunctionLibraries(functionLibraryDAO, Collections.singletonList(functionLibrary), tenantDomain);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection1);
        assertTrue(functionLibraryDAO.isFunctionLibraryExists(((FunctionLibrary) functionLibrary).getFunctionLibraryName(), tenantDomain), "Failed to check existence " + "by script library name.");
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection2);
        assertFalse(functionLibraryDAO.isFunctionLibraryExists("InvalidName", tenantDomain), "Failed to check existence " + "by script library name.");
        // Clean after test
        deleteFunctionLibraries(functionLibraryDAO, Collections.singletonList(functionLibrary), tenantDomain);
    }
}
Also used : Connection(java.sql.Connection) FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary) FunctionLibraryDAO(org.wso2.carbon.identity.functions.library.mgt.dao.FunctionLibraryDAO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

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