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);
}
}
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);
}
}
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);
}
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);
}
}
}
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);
}
}
Aggregations