use of org.wso2.carbon.apimgt.rest.api.gateway.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();
}
use of org.wso2.carbon.apimgt.rest.api.gateway.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;
}
use of org.wso2.carbon.apimgt.rest.api.gateway.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");
}
use of org.wso2.carbon.apimgt.rest.api.gateway.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();
}
}
use of org.wso2.carbon.apimgt.rest.api.gateway.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);
}
Aggregations