Search in sources :

Example 1 with APIListDTO

use of org.wso2.carbon.apimgt.internal.service.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());
}
Also used : ArrayList(java.util.ArrayList) APIListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.APIListDTO) API(org.wso2.carbon.apimgt.core.models.API) Test(org.testng.annotations.Test)

Example 2 with APIListDTO

use of org.wso2.carbon.apimgt.internal.service.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;
}
Also used : APIListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.APIListDTO)

Example 3 with APIListDTO

use of org.wso2.carbon.apimgt.internal.service.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);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) ArrayList(java.util.ArrayList) API(org.wso2.carbon.apimgt.core.models.API) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 4 with APIListDTO

use of org.wso2.carbon.apimgt.internal.service.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();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) APIListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.APIListDTO) FileBasedApiImportExportManager(org.wso2.carbon.apimgt.rest.api.publisher.utils.FileBasedApiImportExportManager)

Example 5 with APIListDTO

use of org.wso2.carbon.apimgt.internal.service.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();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) APIListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.APIListDTO) FileBasedApiImportExportManager(org.wso2.carbon.apimgt.rest.api.publisher.utils.FileBasedApiImportExportManager)

Aggregations

ArrayList (java.util.ArrayList)13 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)8 API (org.wso2.carbon.apimgt.core.models.API)7 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)6 APIListDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.APIListDTO)5 HashMap (java.util.HashMap)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 API (org.wso2.carbon.apimgt.api.model.API)4 APIListDTO (org.wso2.carbon.apimgt.rest.api.core.dto.APIListDTO)4 JSONObject (org.json.simple.JSONObject)3 APIListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.APIListDTO)3 File (java.io.File)2 Set (java.util.Set)2 Test (org.junit.Test)2 Test (org.testng.annotations.Test)2 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)2 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)2 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)2 APIMgtEntityImportExportException (org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)2 APIDetails (org.wso2.carbon.apimgt.core.models.APIDetails)2