Search in sources :

Example 6 with FunctionLibraryManagementException

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

the class FunctionLibraryManagementServiceTest method testAlreadyExistFunctionLibrary.

@Test(dataProvider = "tesAlreadyExistFunctionLibraryDataProvider")
public void testAlreadyExistFunctionLibrary(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);
        assertEquals(functionLibraryManagementService.getFunctionLibrary(((FunctionLibrary) functionLibrary).getFunctionLibraryName(), tenantDomain).getFunctionLibraryName(), ((FunctionLibrary) functionLibrary).getFunctionLibraryName());
    } catch (FunctionLibraryManagementException e) {
        assertEquals(e.getMessage(), "Already a script library available with the name: " + ((FunctionLibrary) functionLibrary).getFunctionLibraryName() + ".");
    } 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) FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary) 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 7 with FunctionLibraryManagementException

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

the class FunctionLibraryManagementServiceTest method updateFunctionLibrary.

@Test(dataProvider = "updateFunctionLibraryDataProvider")
public void updateFunctionLibrary(Object functionLibrary, String tenantDomain) throws Exception {
    FunctionLibraryManagementService functionLibraryManagementService = FunctionLibraryManagementServiceImpl.getInstance();
    FunctionLibraryDAOImpl functionLibraryDAO = PowerMockito.mock(FunctionLibraryDAOImpl.class);
    PowerMockito.whenNew(FunctionLibraryDAOImpl.class).withNoArguments().thenReturn(functionLibraryDAO);
    addFunctionLibraries(functionLibraryManagementService, Collections.singletonList(functionLibrary), tenantDomain);
    FunctionLibrary funLib = (FunctionLibrary) functionLibrary;
    String oldName = funLib.getFunctionLibraryName();
    if (oldName.equals("sample11")) {
        funLib.setFunctionLibraryName("");
    } else if (oldName.equals("sample12")) {
        funLib.setFunctionLibraryName("#$%^%^");
    } else if (oldName.equals("sample13")) {
        funLib.setFunctionLibraryName("sample");
    } else {
        funLib.setFunctionLibraryName("updated");
    }
    try {
        when(functionLibraryDAO.isFunctionLibraryExists("sample", tenantDomain)).thenReturn(true);
        when(functionLibraryDAO.getFunctionLibrary(funLib.getFunctionLibraryName(), tenantDomain)).thenReturn(funLib);
        functionLibraryManagementService.updateFunctionLibrary(oldName, funLib, tenantDomain);
        assertNotNull(functionLibraryManagementService.getFunctionLibrary(funLib.getFunctionLibraryName(), tenantDomain), "Failed to update script library.");
        // Clean after test
        deleteFunctionLibraries(functionLibraryManagementService, Collections.singletonList(functionLibrary), tenantDomain);
    } catch (FunctionLibraryManagementException e) {
        if (!funLib.getFunctionLibraryName().equals(oldName) && functionLibraryDAO.isFunctionLibraryExists(funLib.getFunctionLibraryName(), tenantDomain)) {
            assertEquals(e.getMessage(), "Already a script library available with the name: " + ((FunctionLibrary) functionLibrary).getFunctionLibraryName() + ".");
        }
        if (!isRegexValidated(funLib.getFunctionLibraryName())) {
            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) FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary) 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 8 with FunctionLibraryManagementException

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

the class FunctionLibraryDAOImplTest method deleteFunctionLibrary.

@Test(dataProvider = "deleteFunctionLibraryData")
public void deleteFunctionLibrary(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()).thenReturn(connection1);
        functionLibraryDAO.deleteFunctionLibrary(((FunctionLibrary) functionLibrary).getFunctionLibraryName(), tenantDomain);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection2);
        assertNull(functionLibraryDAO.getFunctionLibrary(((FunctionLibrary) functionLibrary).getFunctionLibraryName(), tenantDomain), "Failed to delete the functionLibrary by name.");
    }
}
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 9 with FunctionLibraryManagementException

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

the class FunctionLibraryDAOImplTest method createFunctionLibrary.

@Test(dataProvider = "createFunctionLibraryDataProvider")
public void createFunctionLibrary(Object functionLibrary, String tenantDomain) {
    try (Connection connection1 = DAOUtils.getConnection(DB_NAME);
        Connection connection2 = DAOUtils.getConnection(DB_NAME);
        Connection connection3 = DAOUtils.getConnection(DB_NAME)) {
        FunctionLibraryDAO functionLibraryDAO = new FunctionLibraryDAOImpl();
        FunctionLibrary functionLibrary1 = new FunctionLibrary();
        functionLibrary1.setFunctionLibraryName("sample1");
        functionLibrary1.setDescription("sample1");
        functionLibrary1.setFunctionLibraryScript("samplefunction1");
        try (Connection connection = DAOUtils.getConnection(DB_NAME)) {
            when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
            functionLibraryDAO.createFunctionLibrary(functionLibrary1, tenantDomain);
        } catch (FunctionLibraryManagementException e) {
            log.error("FunctionLibraryManagementException");
        } catch (SQLException e) {
            log.error("SQLException");
        }
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection1);
        functionLibraryDAO.createFunctionLibrary((FunctionLibrary) functionLibrary, tenantDomain);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection2);
        assertEquals(((FunctionLibrary) functionLibrary).getFunctionLibraryName(), functionLibraryDAO.getFunctionLibrary(((FunctionLibrary) functionLibrary).getFunctionLibraryName(), tenantDomain).getFunctionLibraryName());
        // Clean after test
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection3);
        functionLibraryDAO.deleteFunctionLibrary(((FunctionLibrary) functionLibrary).getFunctionLibraryName(), tenantDomain);
    } catch (SQLException e) {
        log.error("SQLException");
    } catch (FunctionLibraryManagementException e) {
        assertEquals(e.getMessage(), "Error while creating the script library: " + ((FunctionLibrary) functionLibrary).getFunctionLibraryName() + ".");
    }
}
Also used : FunctionLibraryManagementException(org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException) 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 10 with FunctionLibraryManagementException

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

the class FunctionLibraryDAOImplTest method getFunctionLibrary.

@Test(dataProvider = "getFunctionLibraryDataProvider")
public void getFunctionLibrary(Object functionLibrary, String tenantDomain) throws SQLException, FunctionLibraryManagementException {
    try (Connection connection = DAOUtils.getConnection(DB_NAME)) {
        FunctionLibraryDAO functionLibraryDAO = new FunctionLibraryDAOImpl();
        addFunctionLibraries(functionLibraryDAO, Collections.singletonList(functionLibrary), tenantDomain);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection);
        assertTrue(functionLibraryDAO.getFunctionLibrary(((FunctionLibrary) functionLibrary).getFunctionLibraryName(), tenantDomain) != null, "Failed to retrieve script library");
        // Clean after test
        deleteFunctionLibraries(functionLibraryDAO, Collections.singletonList(functionLibrary), tenantDomain);
    }
}
Also used : Connection(java.sql.Connection) 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