Search in sources :

Example 1 with ApplicationListDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationListDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsGet.

/**
 * Retrieves all applications that qualifies for the search query
 *
 * @param query       Search query
 * @param limit       Max number of applications to return
 * @param offset      Starting position of pagination
 * @param ifNoneMatch If-None-Match header value
 * @param request     msf4j request object
 * @return A list of qualifying application DTOs as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response applicationsGet(String query, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
    ApplicationListDTO applicationListDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    try {
        APIStore apiConsumer = RestApiUtil.getConsumer(username);
        List<Application> allMatchedApps = new ArrayList<>();
        if (StringUtils.isBlank(query)) {
            allMatchedApps = apiConsumer.getApplications(username);
        } else {
            Application application = apiConsumer.getApplicationByName(username, query);
            if (application != null) {
                allMatchedApps = new ArrayList<>();
                allMatchedApps.add(application);
            }
        }
        // allMatchedApps are already sorted to application name
        applicationListDTO = ApplicationMappingUtil.fromApplicationsToDTO(allMatchedApps, limit, offset);
        ApplicationMappingUtil.setPaginationParams(applicationListDTO, limit, offset, allMatchedApps.size());
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving applications";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_NAME, query);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(applicationListDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) ArrayList(java.util.ArrayList) Application(org.wso2.carbon.apimgt.core.models.Application) ApplicationListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationListDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 2 with ApplicationListDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationListDTO in project carbon-apimgt by wso2.

the class ApplicationMappingUtil method fromApplicationsToDTO.

/**
 * Converts an Application[] array into a corresponding ApplicationListDTO
 *
 * @param applications array of Application objects
 * @param limit limit parameter
 * @param offset starting index
 * @return ApplicationListDTO object corresponding to Application[] array
 */
public static ApplicationListDTO fromApplicationsToDTO(List<Application> applications, int limit, int offset) {
    ApplicationListDTO applicationListDTO = new ApplicationListDTO();
    List<ApplicationInfoDTO> applicationInfoDTOs = applicationListDTO.getList();
    if (applicationInfoDTOs == null) {
        applicationInfoDTOs = new ArrayList<>();
        applicationListDTO.setList(applicationInfoDTOs);
    }
    // identifying the proper start and end indexes
    int start = offset < applications.size() && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= applications.size() - 1 ? offset + limit - 1 : applications.size() - 1;
    for (int i = start; i <= end; i++) {
        applicationInfoDTOs.add(fromApplicationToInfoDTO(applications.get(i)));
    }
    applicationListDTO.setCount(applicationInfoDTOs.size());
    return applicationListDTO;
}
Also used : ApplicationInfoDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationInfoDTO) ApplicationListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationListDTO)

Example 3 with ApplicationListDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationListDTO in project carbon-apimgt by wso2.

the class ApplicationMappingUtilTestCase method testFromApplicationsToDTO.

@Test
public void testFromApplicationsToDTO() {
    Application application1 = new Application("application1", "user1");
    application1.setId(UUID.randomUUID().toString());
    application1.setDescription("application 1");
    application1.setStatus("ACTIVE");
    application1.setPolicy(new APIPolicy("GOLD"));
    Application application2 = new Application("application2", "user1");
    application2.setId(UUID.randomUUID().toString());
    application2.setDescription("application 2");
    application2.setStatus("ACTIVE");
    application2.setPolicy(new APIPolicy("GOLD"));
    Application application3 = new Application("application3", "user1");
    application3.setId(UUID.randomUUID().toString());
    application3.setDescription("application 3");
    application3.setStatus("ACTIVE");
    application3.setPolicy(new APIPolicy("GOLD"));
    List<Application> applicationList = new ArrayList<>();
    applicationList.add(application1);
    applicationList.add(application2);
    applicationList.add(application3);
    ApplicationMappingUtil applicationMappingUtil = new ApplicationMappingUtil();
    ApplicationListDTO applicationListDTO = applicationMappingUtil.fromApplicationsToDTO(applicationList, 10, 0);
    Assert.assertEquals(applicationListDTO.getList().get(0).getName(), "application1");
}
Also used : ArrayList(java.util.ArrayList) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Application(org.wso2.carbon.apimgt.core.models.Application) ApplicationListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationListDTO) Test(org.junit.Test)

Example 4 with ApplicationListDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationListDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsGet.

@Override
public Response applicationsGet(String accept, Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
        List<Application> applicationList = apiMgtAdminService.getAllApplications();
        ApplicationListDTO applicationListDTO = new ApplicationListDTO();
        applicationListDTO.setList(MappingUtil.convertToApplicationDtoList(applicationList));
        return Response.ok(applicationListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving Applications.";
        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) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Application(org.wso2.carbon.apimgt.core.models.Application) ApplicationListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.ApplicationListDTO)

Example 5 with ApplicationListDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationListDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImplTestCase method applicationsGetTestCase.

@Test
public void applicationsGetTestCase() throws Exception {
    ApplicationsApiServiceImpl applicationsApiService = new ApplicationsApiServiceImpl();
    APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
    PowerMockito.mockStatic(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
    Mockito.when(instance.getAPIMgtAdminService()).thenReturn(adminService);
    List<Application> applicationList = new ArrayList<>();
    Application applicationOne = SampleTestObjectCreator.createRandomApplication();
    Application applicationTwo = SampleTestObjectCreator.createRandomApplication();
    Application applicationThree = SampleTestObjectCreator.createRandomApplication();
    applicationList.add(applicationOne);
    applicationList.add(applicationTwo);
    applicationList.add(applicationThree);
    Mockito.when(adminService.getAllApplications()).thenReturn(applicationList);
    Response response = applicationsApiService.applicationsGet(null, getRequest());
    Assert.assertEquals(response.getStatus(), 200);
    Assert.assertEquals(((ApplicationListDTO) response.getEntity()).getList().size(), 3);
}
Also used : Response(javax.ws.rs.core.Response) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) ArrayList(java.util.ArrayList) Application(org.wso2.carbon.apimgt.core.models.Application) ApplicationListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.ApplicationListDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Application (org.wso2.carbon.apimgt.core.models.Application)4 ArrayList (java.util.ArrayList)3 ApplicationListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationListDTO)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 ApplicationListDTO (org.wso2.carbon.apimgt.rest.api.core.dto.ApplicationListDTO)2 Response (javax.ws.rs.core.Response)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)1 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)1 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)1 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)1 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1 ApplicationInfoDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationInfoDTO)1