use of org.wso2.carbon.apimgt.api.model.Label in project carbon-apimgt by wso2.
the class LabelsApiServiceImpl method labelsLabelIdPut.
/**
* Update the label
* @param body The body of the label with fields to be modified
* @param contentType The content type of the body
* @param request The ms4j request object
* @return 200 OK response.
* @throws NotFoundException
*/
@Override
public Response labelsLabelIdPut(String labelId, LabelDTO body, String contentType, Request request) throws NotFoundException {
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
body.labelUUID(labelId);
Label updatedLabel = apiMgtAdminService.updateLabel(LabelMappingUtil.fromDTOTLabel(body));
return Response.status(Response.Status.OK).entity(LabelMappingUtil.fromLabelToDTO(updatedLabel)).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while adding label, label name: " + body.getName();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.api.model.Label 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.model.Label 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();
}
use of org.wso2.carbon.apimgt.api.model.Label in project carbon-apimgt by wso2.
the class LabelMappingUtil method fromLabelArrayToListDTO.
/**
* Converts an array of label model objects into REST API DTO objects.
*
* @param labelList An array of Label model objects
* @return A List DTO of Label DTOs derived from the array of model objects
*/
public static LabelListDTO fromLabelArrayToListDTO(List<Label> labelList) {
LabelListDTO listDTO = new LabelListDTO();
List<LabelDTO> labelDTOList = new ArrayList<>();
if (labelList != null) {
for (Label label : labelList) {
LabelDTO dto = fromLabelToDTO(label);
labelDTOList.add(dto);
}
}
listDTO.setCount(labelDTOList.size());
listDTO.setList(labelDTOList);
return listDTO;
}
use of org.wso2.carbon.apimgt.api.model.Label in project carbon-apimgt by wso2.
the class LabelsApiServiceImplTest method testLabelsGetWithoutLabelId.
@Test
public void testLabelsGetWithoutLabelId() throws Exception {
APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
List<Label> labels = new ArrayList<>();
Label label1 = new Label.Builder().id("1").name("label1").type("GATEWAY").build();
Label label2 = new Label.Builder().id("2").name("label2").type("STORE").build();
labels.add(label1);
labels.add(label2);
LabelsApiServiceImpl labelService = new LabelsApiServiceImpl();
Mockito.when(labelService.labelsGet(null, null, getRequest())).thenReturn(Response.status(Response.Status.OK).entity(LabelMappingUtil.fromLabelArrayToListDTO(labels)).build());
Response response = labelService.labelsGet(null, null, getRequest());
Assert.assertEquals(response.getEntity(), LabelMappingUtil.fromLabelArrayToListDTO(labels));
}
Aggregations