Search in sources :

Example 1 with FunctionLibraryManagementService

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

the class JsGraphBuilder method loadLocalLibrary.

/**
 * Loads the required function library from the database.
 *
 * @param functionLibraryName functionLibraryName
 * @return functionLibraryScript
 * @throws FunctionLibraryManagementException
 */
public String loadLocalLibrary(String functionLibraryName) throws FunctionLibraryManagementException {
    FunctionLibraryManagementService functionLibMgtService = FrameworkServiceComponent.getFunctionLibraryManagementService();
    FunctionLibrary functionLibrary;
    String libraryScript = null;
    functionLibrary = functionLibMgtService.getFunctionLibrary(functionLibraryName, CarbonContext.getThreadLocalCarbonContext().getTenantDomain());
    if (functionLibrary != null) {
        libraryScript = functionLibrary.getFunctionLibraryScript();
    } else {
        log.error("No function library available with " + functionLibraryName + "name.");
    }
    return libraryScript;
}
Also used : FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary) FunctionLibraryManagementService(org.wso2.carbon.identity.functions.library.mgt.FunctionLibraryManagementService)

Example 2 with FunctionLibraryManagementService

use of org.wso2.carbon.identity.functions.library.mgt.FunctionLibraryManagementService 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 3 with FunctionLibraryManagementService

use of org.wso2.carbon.identity.functions.library.mgt.FunctionLibraryManagementService 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 4 with FunctionLibraryManagementService

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

the class FunctionLibraryManagementServiceTest method listFunctionLibraries.

@Test(dataProvider = "listFunctionLibraryDataProvider")
public void listFunctionLibraries(List<Object> functionLibraries, String tenantDomain) throws Exception {
    FunctionLibraryManagementService functionLibraryManagementService = FunctionLibraryManagementServiceImpl.getInstance();
    FunctionLibraryDAOImpl functionLibraryDAO = PowerMockito.mock(FunctionLibraryDAOImpl.class);
    PowerMockito.whenNew(FunctionLibraryDAOImpl.class).withNoArguments().thenReturn(functionLibraryDAO);
    for (Object functionLibrary : functionLibraries) {
        when(functionLibraryDAO.isFunctionLibraryExists(((FunctionLibrary) functionLibrary).getFunctionLibraryName(), tenantDomain)).thenReturn(false);
    }
    List<FunctionLibrary> functionLibraries1 = Arrays.asList(new FunctionLibrary[3]);
    int i = 0;
    for (Object functionLibrary : functionLibraries) {
        functionLibraries1.set(i, (FunctionLibrary) functionLibrary);
        i += 1;
    }
    when(functionLibraryDAO.listFunctionLibraries(SAMPLE_TENANT_DOMAIN)).thenReturn(functionLibraries1);
    addFunctionLibraries(functionLibraryManagementService, functionLibraries, tenantDomain);
    List<FunctionLibrary> functionLibrariesList = functionLibraryManagementService.listFunctionLibraries(tenantDomain);
    assertTrue(functionLibrariesList != null && functionLibrariesList.size() != 0, "Failed to retrieve scopes.");
    // Clean after test
    deleteFunctionLibraries(functionLibraryManagementService, functionLibraries, tenantDomain);
}
Also used : FunctionLibraryDAOImpl(org.wso2.carbon.identity.functions.library.mgt.dao.impl.FunctionLibraryDAOImpl) FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary) Test(org.testng.annotations.Test) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with FunctionLibraryManagementService

use of org.wso2.carbon.identity.functions.library.mgt.FunctionLibraryManagementService 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)

Aggregations

PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 Test (org.testng.annotations.Test)7 FunctionLibraryDAOImpl (org.wso2.carbon.identity.functions.library.mgt.dao.impl.FunctionLibraryDAOImpl)7 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)7 FunctionLibraryManagementException (org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException)6 FunctionLibrary (org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary)4 FunctionLibraryManagementService (org.wso2.carbon.identity.functions.library.mgt.FunctionLibraryManagementService)2 Hashtable (java.util.Hashtable)1