Search in sources :

Example 21 with ErrorHandler

use of org.wso2.carbon.apimgt.api.ErrorHandler in project carbon-apimgt by wso2.

the class RestApiUtilTestCase method testGetErrorDTO.

@Test(description = "Testing get Error DTO")
public void testGetErrorDTO() throws Exception {
    ErrorHandler errorHandler = Mockito.mock(ErrorHandler.class);
    when(errorHandler.getErrorCode()).thenReturn((long) 900300);
    when(errorHandler.getErrorMessage()).thenReturn("Lifecycle exception occurred");
    when(errorHandler.getErrorDescription()).thenReturn("Error occurred while changing lifecycle state");
    ErrorDTO errorDTOExpected = new ErrorDTO();
    errorDTOExpected.setCode((long) 900300);
    errorDTOExpected.setMessage("Lifecycle exception occurred");
    errorDTOExpected.setDescription("Error occurred while changing lifecycle state");
    ErrorDTO errorDTO1 = RestApiUtil.getErrorDTO(errorHandler);
    Assert.assertEquals(errorDTO1.getCode(), errorDTOExpected.getCode());
    Assert.assertEquals(errorDTO1.getMessage(), errorDTOExpected.getMessage());
    Assert.assertEquals(errorDTO1.getDescription(), errorDTOExpected.getDescription());
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 22 with ErrorHandler

use of org.wso2.carbon.apimgt.api.ErrorHandler in project carbon-apimgt by wso2.

the class RestApiUtilTestCase method testGetErrorDTO1.

@Test(description = "Test get Error DTO")
public void testGetErrorDTO1() throws Exception {
    Map<String, String> paramList = new HashMap<>();
    ErrorDTO errorDTOExpected = new ErrorDTO();
    errorDTOExpected.setCode((long) 900300);
    errorDTOExpected.setMessage("Lifecycle exception occurred");
    errorDTOExpected.setDescription("Error occurred while changing lifecycle state");
    ErrorHandler errorHandler = Mockito.mock(ErrorHandler.class);
    when(errorHandler.getErrorCode()).thenReturn((long) 900300);
    when(errorHandler.getErrorMessage()).thenReturn("Lifecycle exception occurred");
    when(errorHandler.getErrorDescription()).thenReturn("Error occurred while changing lifecycle state");
    ErrorDTO errorDTO1 = RestApiUtil.getErrorDTO(errorHandler, paramList);
    Assert.assertEquals(errorDTO1.getCode(), errorDTOExpected.getCode());
    Assert.assertEquals(errorDTO1.getMessage(), errorDTOExpected.getMessage());
    Assert.assertEquals(errorDTO1.getMoreInfo(), new HashMap<String, String>());
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 23 with ErrorHandler

use of org.wso2.carbon.apimgt.api.ErrorHandler in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method subscriptionsGet.

/**
 * Get all subscriptions.
 * {@code <p/>}
 * If apiId is specified this will return the subscribed applications of that api
 * If application id is specified this will return the api subscriptions of that application
 *
 * @param apiId         ID of the API
 * @param applicationId ID of the Application
 * @param offset        offset value
 * @param limit         limit value
 * @param ifNoneMatch   If-None-Match header value
 * @param request       msf4j request object
 * @return Subscription List
 * @throws NotFoundException If failed to get the subscription
 */
@Override
public Response subscriptionsGet(String apiId, String applicationId, String apiType, Integer offset, Integer limit, String ifNoneMatch, Request request) throws NotFoundException {
    List<Subscription> subscribedApiList = null;
    SubscriptionListDTO subscriptionListDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        if (!StringUtils.isEmpty(apiId)) {
            subscribedApiList = apiStore.getSubscriptionsByAPI(apiId);
            subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedApiList, limit, offset);
        } else if (!StringUtils.isEmpty(applicationId)) {
            Application application = apiStore.getApplicationByUuid(applicationId);
            if (application != null) {
                if (!StringUtils.isEmpty(apiType)) {
                    ApiType apiTypeEnum = ApiType.fromString(apiType);
                    if (apiTypeEnum == null) {
                        throw new APIManagementException("API Type specified is invalid", ExceptionCodes.API_TYPE_INVALID);
                    }
                    subscribedApiList = apiStore.getAPISubscriptionsByApplication(application, apiTypeEnum);
                } else {
                    subscribedApiList = apiStore.getAPISubscriptionsByApplication(application);
                }
                subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedApiList, limit, offset);
            } else {
                String errorMessage = "Application not found: " + applicationId;
                APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.APPLICATION_NOT_FOUND);
                HashMap<String, String> paramList = new HashMap<String, String>();
                paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
                log.error(errorMessage, e);
                return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
            }
        } else {
            // mandatory parameters not provided
            String errorMessage = "Either applicationId or apiId should be provided";
            ErrorHandler errorHandler = ExceptionCodes.PARAMETER_NOT_PROVIDED;
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
            log.error(errorMessage);
            return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving subscriptions";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, applicationId);
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(subscriptionListDTO).build();
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ApiType(org.wso2.carbon.apimgt.core.dao.ApiType) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 24 with ErrorHandler

use of org.wso2.carbon.apimgt.api.ErrorHandler in project carbon-apimgt by wso2.

the class LabelsApiServiceImpl method labelsLabelIdDelete.

/**
 * Delete label by label id
 *
 * @param labelId           Id of the label
 * @param request           msf4j request object
 * @return 200 OK if the operation is successful
 * @throws NotFoundException If failed to find the particular resource
 */
@Override
public Response labelsLabelIdDelete(String labelId, Request request) throws NotFoundException {
    try {
        if (labelId != null) {
            APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
            apiMgtAdminService.deleteLabel(labelId);
        } else {
            // mandatory parameters not provided
            String errorMessage = "Label Id parameter should be provided";
            ErrorHandler errorHandler = ExceptionCodes.PARAMETER_NOT_PROVIDED;
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
            log.error(errorMessage);
            return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while deleting the label [labelId] " + labelId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.status(Response.Status.NO_CONTENT).build();
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 25 with ErrorHandler

use of org.wso2.carbon.apimgt.api.ErrorHandler in project carbon-apimgt by wso2.

the class LabelsApiServiceImpl method labelsGet.

/**
 * Gets all labels
 * @param accept Accept header value
 * @param request ms4j request object
 * @return  a list of label objects
 * @throws NotFoundException
 */
@Override
public Response labelsGet(String labelId, String accept, Request request) throws NotFoundException {
    List<Label> labels = new ArrayList<>();
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        // get all labels
        if (labelId == null) {
            labels = apiMgtAdminService.getLabels();
        } else {
            Label label = apiMgtAdminService.getLabelByID(labelId);
            labels.add(label);
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while retrieving all labels";
        ErrorHandler errorHandler = ExceptionCodes.LABEL_EXCEPTION;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
        log.error(errorMessage, e);
        return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.status(Response.Status.OK).entity(LabelMappingUtil.fromLabelArrayToListDTO(labels)).build();
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList)

Aggregations

ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)21 ErrorHandler (org.wso2.carbon.apimgt.core.exception.ErrorHandler)16 HashMap (java.util.HashMap)13 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)12 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)7 URI (java.net.URI)6 URISyntaxException (java.net.URISyntaxException)6 Map (java.util.Map)5 ErrorHandler (org.wso2.carbon.apimgt.api.ErrorHandler)5 Application (org.wso2.carbon.apimgt.core.models.Application)4 ArrayList (java.util.ArrayList)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Test (org.testng.annotations.Test)3 WorkflowResponseDTO (org.wso2.carbon.apimgt.rest.api.store.dto.WorkflowResponseDTO)3 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)2 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)2 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)2 ExceptionCodes (org.wso2.carbon.apimgt.core.exception.ExceptionCodes)2 Label (org.wso2.carbon.apimgt.core.models.Label)2 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)2