Search in sources :

Example 1 with LabelListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.dto.LabelListDTO in project carbon-apimgt by wso2.

the class TestMappingUtilTestCase method testLabelsListToLabelListDTOMapping.

@Test(description = "Label list to Label list DTO mapping ")
void testLabelsListToLabelListDTOMapping() {
    Label label1 = SampleTestObjectCreator.createLabel("label1").build();
    Label label2 = SampleTestObjectCreator.createLabel("label1").build();
    List<Label> labels = new ArrayList<>();
    labels.add(label1);
    labels.add(label2);
    LabelListDTO labelListDTO = MappingUtil.toLabelListDTO(labels);
    assertEquals((Integer) labels.size(), labelListDTO.getCount());
    assertEquals(label1.getId(), labelListDTO.getList().get(0).getLabelId());
    assertEquals(label1.getName(), labelListDTO.getList().get(0).getName());
    assertEquals(label2.getId(), labelListDTO.getList().get(1).getLabelId());
    assertEquals(label2.getName(), labelListDTO.getList().get(1).getName());
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) LabelListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.LabelListDTO) Test(org.testng.annotations.Test)

Example 2 with LabelListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.dto.LabelListDTO in project carbon-apimgt by wso2.

the class LabelInfoApiServiceImpl method labelInfoGet.

/**
 * Get label information for labels provided.
 *
 * @param labels          List of labels
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return Label List
 * @throws NotFoundException If failed to get the label values
 */
@Override
public Response labelInfoGet(String labels, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    LabelListDTO labelListDTO;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        if (labels != null) {
            List<String> labelNames = Arrays.asList(labels.split(","));
            List<Label> labelList = apiStore.getLabelInfo(labelNames, username);
            labelListDTO = LabelMappingUtil.toLabelListDTO(labelList);
        } else {
            // mandatory parameters not provided
            String errorMessage = "Labels 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 retrieving label information";
        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.ok().entity(labelListDTO).build();
}
Also used : 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) Label(org.wso2.carbon.apimgt.core.models.Label) LabelListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.LabelListDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 3 with LabelListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.dto.LabelListDTO in project carbon-apimgt by wso2.

the class LabelsApiServiceImpl method labelsGet.

/**
 * Get all the labels.
 *
 * @param labelType       the type of the labels to be fetched
 * @param accept          Accept header value
 * @param ifNoneMatch     If-None-Match header value
 * @param request         ms4j request object
 * @return Lable List
 * @throws NotFoundException If failed to get the label values
 */
@Override
public Response labelsGet(String labelType, String accept, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        List<Label> labels;
        if (labelType == null) {
            labels = apiStore.getAllLabels();
        } else {
            labels = apiStore.getLabelsByType(labelType);
        }
        LabelListDTO labelListDTO = LabelMappingUtil.toLabelListDTO(labels);
        return Response.ok().entity(labelListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while retrieving Labels";
        HashMap<String, String> paramList = new HashMap<String, String>();
        org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : HashMap(java.util.HashMap) Label(org.wso2.carbon.apimgt.core.models.Label) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) LabelListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.LabelListDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 4 with LabelListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.dto.LabelListDTO in project carbon-apimgt by wso2.

the class LabelMappingUtil method toLabelListDTO.

/**
 * Convert List of labels to LabelListDTO
 *
 * @param labels List of Labels
 * @return LabelListDTO
 */
public static LabelListDTO toLabelListDTO(List<Label> labels) {
    LabelListDTO labelListDTO = new LabelListDTO();
    labelListDTO.setCount(labels.size());
    labelListDTO.setList(toLabelDTO(labels));
    return labelListDTO;
}
Also used : LabelListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.LabelListDTO)

Example 5 with LabelListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.dto.LabelListDTO 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;
}
Also used : ArrayList(java.util.ArrayList) Label(org.wso2.carbon.apimgt.core.models.Label) LabelDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.LabelDTO) LabelListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.LabelListDTO)

Aggregations

Label (org.wso2.carbon.apimgt.core.models.Label)7 ArrayList (java.util.ArrayList)4 LabelListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.LabelListDTO)4 HashMap (java.util.HashMap)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)3 LabelListDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.LabelListDTO)3 Test (org.testng.annotations.Test)2 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)2 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)2 ErrorHandler (org.wso2.carbon.apimgt.core.exception.ErrorHandler)1 LabelDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.LabelDTO)1 LabelListDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.LabelListDTO)1 LabelDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.LabelDTO)1