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