Search in sources :

Example 16 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class LabelsApiServiceImplTestCase method testLabelsGetException.

@Test
public void testLabelsGetException() throws Exception {
    printTestMethodName();
    LabelsApiServiceImpl labelsApiService = new LabelsApiServiceImpl();
    APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
    PowerMockito.mockStatic(RestAPIPublisherUtil.class);
    PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
    Mockito.doThrow(new LabelException("Error occurred", ExceptionCodes.LABEL_EXCEPTION)).when(apiPublisher).getAllLabels();
    Response response = labelsApiService.labelsGet(null, null, null, null, getRequest());
    assertEquals(response.getStatus(), 500);
    assertTrue(response.getEntity().toString().contains("Label Error"));
}
Also used : Response(javax.ws.rs.core.Response) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) LabelException(org.wso2.carbon.apimgt.core.exception.LabelException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 17 with Label

use of org.wso2.carbon.apimgt.core.models.Label 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 18 with Label

use of org.wso2.carbon.apimgt.core.models.Label 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 19 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class LabelMappingUtil method toLabelDTO.

/**
 * Converts labels to list of LabelDTO
 *
 * @param labels List of Labels
 * @return List of LabelDTOs
 */
private static List<LabelDTO> toLabelDTO(List<Label> labels) {
    List<LabelDTO> labelDTOs = new ArrayList<>();
    for (Label label : labels) {
        LabelDTO labelDTO = new LabelDTO();
        labelDTO.setLabelId(label.getId());
        labelDTO.setName(label.getName());
        labelDTO.setAccessUrls(label.getAccessUrls());
        labelDTOs.add(labelDTO);
    }
    return labelDTOs;
}
Also used : ArrayList(java.util.ArrayList) Label(org.wso2.carbon.apimgt.core.models.Label) LabelDTO(org.wso2.carbon.apimgt.rest.api.store.dto.LabelDTO)

Example 20 with Label

use of org.wso2.carbon.apimgt.core.models.Label 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)

Aggregations

Label (org.wso2.carbon.apimgt.core.models.Label)65 ArrayList (java.util.ArrayList)52 Test (org.testng.annotations.Test)45 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)32 API (org.wso2.carbon.apimgt.core.models.API)29 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)28 SQLException (java.sql.SQLException)21 PreparedStatement (java.sql.PreparedStatement)20 HashMap (java.util.HashMap)16 Connection (java.sql.Connection)15 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)14 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)13 Test (org.junit.Test)12 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)11 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)11 Map (java.util.Map)10 BeforeTest (org.testng.annotations.BeforeTest)9 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)8 Response (javax.ws.rs.core.Response)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8