use of org.wso2.carbon.apimgt.rest.api.service.catalog.dto.APIListDTO in project carbon-apimgt by wso2.
the class TestMappingUtilTestCase method testAPIListToAPIListInfoMapping.
@Test(description = "API list to API list info mapping")
void testAPIListToAPIListInfoMapping() {
API api1 = SampleTestObjectCreator.createDefaultAPI().id("newId1").name("newName1").context("newContext1").description("newDesc1").provider("newProvider1").lifeCycleStatus("newStatus1").version("newVersion1").workflowStatus("newWorkflowStatus1").build();
API api2 = SampleTestObjectCreator.createDefaultAPI().id("newId2").name("newName2").context("newContext2").description("newDesc2").provider("newProvider2").lifeCycleStatus("newStatus2").version("newVersion2").workflowStatus("newWorkflowStatus2").build();
List<API> apis = new ArrayList<>();
apis.add(api1);
apis.add(api2);
APIListDTO apiListDTO = MappingUtil.toAPIListDTO(apis);
assertEquals((Integer) apis.size(), apiListDTO.getCount());
assertEquals(api1.getId(), apiListDTO.getList().get(0).getId());
assertEquals(api1.getName(), apiListDTO.getList().get(0).getName());
assertEquals(api1.getContext(), apiListDTO.getList().get(0).getContext());
assertEquals(api1.getDescription(), apiListDTO.getList().get(0).getDescription());
assertEquals(api1.getProvider(), apiListDTO.getList().get(0).getProvider());
assertEquals(api1.getLifeCycleStatus(), apiListDTO.getList().get(0).getLifeCycleStatus());
assertEquals(api1.getVersion(), apiListDTO.getList().get(0).getVersion());
assertEquals(api1.getWorkflowStatus(), apiListDTO.getList().get(0).getWorkflowStatus());
assertEquals(api2.getId(), apiListDTO.getList().get(1).getId());
assertEquals(api2.getName(), apiListDTO.getList().get(1).getName());
assertEquals(api2.getContext(), apiListDTO.getList().get(1).getContext());
assertEquals(api2.getDescription(), apiListDTO.getList().get(1).getDescription());
assertEquals(api2.getProvider(), apiListDTO.getList().get(1).getProvider());
assertEquals(api2.getLifeCycleStatus(), apiListDTO.getList().get(1).getLifeCycleStatus());
assertEquals(api2.getVersion(), apiListDTO.getList().get(1).getVersion());
assertEquals(api2.getWorkflowStatus(), apiListDTO.getList().get(1).getWorkflowStatus());
}
use of org.wso2.carbon.apimgt.rest.api.service.catalog.dto.APIListDTO in project carbon-apimgt by wso2.
the class MappingUtil method toAPIListDTO.
/**
* Converts API list to APIListDTO list.
*
* @param apisResult List of APIs
* @return APIListDTO object
*/
public static APIListDTO toAPIListDTO(List<API> apisResult) {
APIListDTO apiListDTO = new APIListDTO();
apiListDTO.setCount(apisResult.size());
// apiListDTO.setNext(next);
// apiListDTO.setPrevious(previous);
apiListDTO.setList(toAPIInfo(apisResult));
return apiListDTO;
}
use of org.wso2.carbon.apimgt.rest.api.service.catalog.dto.APIListDTO in project carbon-apimgt by wso2.
the class FileBasedApiImportExportManager method importAndCreateAPIs.
/**
* Imports and creates a set of new APIs to API Manager by reading and decoding the
* input stream. Will fail if the APIs already exists
*
* @param uploadedApiArchiveInputStream InputStream to be read ana decoded to a set of APIs
* @param provider API provider, if needs to be updated
* @return {@link APIListDTO} object comprising of successfully imported APIs
* @throws APIMgtEntityImportExportException if any error occurs while importing or no APIs are imported successfully
*/
public APIListDTO importAndCreateAPIs(InputStream uploadedApiArchiveInputStream, String provider) throws APIMgtEntityImportExportException {
String apiArchiveLocation = path + File.separator + IMPORTED_APIS_DIRECTORY_NAME + ".zip";
String archiveExtractLocation = null;
try {
archiveExtractLocation = APIFileUtils.extractUploadedArchive(uploadedApiArchiveInputStream, IMPORTED_APIS_DIRECTORY_NAME, apiArchiveLocation, path);
} catch (APIMgtDAOException e) {
String errorMsg = "Error in accessing uploaded API archive" + apiArchiveLocation;
log.error(errorMsg, e);
throw new APIMgtEntityImportExportException(errorMsg, e, ExceptionCodes.API_IMPORT_ERROR);
}
// List to contain newly created/updated APIs
Set<APIDetails> apiDetailsSet = decodeApiInformationFromDirectoryStructure(archiveExtractLocation, provider);
List<API> apis = new ArrayList<>();
for (APIDetails apiDetails : apiDetailsSet) {
try {
apis.add(importAndCreateApi(apiDetails));
} catch (APIManagementException e) {
log.error("Error while importing API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion());
// skip importing the API
continue;
}
log.info("Successfully imported API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion());
}
try {
APIFileUtils.deleteDirectory(path);
} catch (APIMgtDAOException e) {
log.warn("Unable to remove directory " + path);
}
// if no APIs are corrected exported, throw an error
if (apis.isEmpty()) {
String errorMsg = "No APIs imported successfully";
throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
}
return MappingUtil.toAPIListDTO(apis);
}
use of org.wso2.carbon.apimgt.rest.api.service.catalog.dto.APIListDTO in project carbon-apimgt by wso2.
the class ImportApiServiceImpl method importApisPut.
/**
* Imports an updates a set of existing APIs which have been exported as a zip file
*
* @param fileInputStream content stream of the zip file which contains exported API(s)
* @param fileDetail meta information of the zip file
* @param provider provider of the API (if it needs to be updated)
* @param request ms4j request object
* @return List of APIs that were imported
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response importApisPut(InputStream fileInputStream, FileInfo fileDetail, String provider, Request request) throws NotFoundException {
APIPublisher publisher = null;
try {
publisher = RestAPIPublisherUtil.getApiPublisher(RestApiUtil.getLoggedInUsername(request));
FileBasedApiImportExportManager importManager = new FileBasedApiImportExportManager(publisher, System.getProperty("java.io.tmpdir") + File.separator + "imported-api-archives-" + UUID.randomUUID().toString());
APIListDTO apiList = importManager.importAPIs(fileInputStream, provider);
return Response.status(Response.Status.OK).entity(apiList).build();
} catch (APIManagementException e) {
String errorMessage = "Error while importing the APIs";
log.error(errorMessage, e);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.service.catalog.dto.APIListDTO in project carbon-apimgt by wso2.
the class ImportApiServiceImpl method importApisPost.
/**
* Imports a set of new APIs which have been exported as a zip file
*
* @param fileInputStream content stream of the zip file which contains exported API(s)
* @param fileDetail meta information of the zip file
* @param provider provider of the API (if it needs to be updated)
* @param request ms4j request object
* @return List of APIs that were imported
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response importApisPost(InputStream fileInputStream, FileInfo fileDetail, String provider, Request request) throws NotFoundException {
APIPublisher publisher = null;
try {
publisher = RestAPIPublisherUtil.getApiPublisher(RestApiUtil.getLoggedInUsername(request));
FileBasedApiImportExportManager importManager = new FileBasedApiImportExportManager(publisher, System.getProperty("java.io.tmpdir") + File.separator + "imported-api-archives-" + UUID.randomUUID().toString());
APIListDTO apiList = importManager.importAndCreateAPIs(fileInputStream, provider);
return Response.status(Response.Status.OK).entity(apiList).build();
} catch (APIManagementException e) {
String errorMessage = "Error while importing the APIs";
log.error(errorMessage, e);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
Aggregations