Search in sources :

Example 1 with PolicyExportManager

use of org.wso2.carbon.apimgt.rest.api.core.utils.PolicyExportManager in project carbon-apimgt by wso2.

the class PolicyExportManagerTest method testCreateArchiveFromExecutionPlans.

@Test(description = "testing creating archive from execution plans")
public void testCreateArchiveFromExecutionPlans() throws APIManagementException {
    APIMgtAdminService apiMgtAdminService = Mockito.mock(APIMgtAdminService.class);
    List<APIPolicy> apiPolicies = new ArrayList<>();
    List<ApplicationPolicy> applicationPolicies = new ArrayList<>();
    List<SubscriptionPolicy> subscriptionPolicies = new ArrayList<>();
    List<CustomPolicy> customPolicies = new ArrayList<>();
    apiPolicies.add(SampleTestObjectCreator.createDefaultAPIPolicy());
    applicationPolicies.add(SampleTestObjectCreator.createDefaultApplicationPolicy());
    subscriptionPolicies.add(SampleTestObjectCreator.createDefaultSubscriptionPolicy());
    customPolicies.add(SampleTestObjectCreator.createDefaultCustomPolicy());
    Mockito.when(apiMgtAdminService.getApiPolicies()).thenReturn(apiPolicies);
    Mockito.when(apiMgtAdminService.getApplicationPolicies()).thenReturn(applicationPolicies);
    Mockito.when(apiMgtAdminService.getCustomRules()).thenReturn(customPolicies);
    Mockito.when(apiMgtAdminService.getSubscriptionPolicies()).thenReturn(subscriptionPolicies);
    PolicyExportManager policyExportManager = new PolicyExportManager(apiMgtAdminService);
    String path = policyExportManager.createArchiveFromExecutionPlans("exportDir", exportRootDirectory, "policies");
    String unzippedPath = exportRootDirectory + File.separator + "export-policies";
    APIFileUtils.extractArchive(path, unzippedPath);
    Assert.assertEquals(new File(APIFileUtils.getDirectoryList(unzippedPath).iterator().next()).listFiles().length == 6, true, "Exported policy count is not equal to ");
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) ArrayList(java.util.ArrayList) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) File(java.io.File) Test(org.testng.annotations.Test)

Example 2 with PolicyExportManager

use of org.wso2.carbon.apimgt.rest.api.core.utils.PolicyExportManager in project carbon-apimgt by wso2.

the class ExportApiServiceImpl method exportPoliciesThrottleGet.

/**
 * Export throttle policies containing zip.
 *
 * @param accept  Accept header value
 * @param request msf4j request object
 * @return Response object
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response exportPoliciesThrottleGet(String accept, Request request) throws NotFoundException {
    String archiveName = "exported-policies";
    // files will be written to following directory
    String exportedPoliciesDirName = "exported-policies";
    // archive will be here at following location tmp directory
    String archiveDir = System.getProperty("java.io.tmpdir");
    if (log.isDebugEnabled()) {
        log.debug("Received export policies GET request ");
    }
    try {
        APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
        PolicyExportManager policyExportManager = new PolicyExportManager(apiMgtAdminService);
        // create archive and get the archive location
        String zippedFilePath = policyExportManager.createArchiveFromExecutionPlans(exportedPoliciesDirName, archiveDir, archiveName);
        APIFileUtils.deleteDirectory(exportedPoliciesDirName);
        File exportedApiArchiveFile = new File(zippedFilePath);
        Response.ResponseBuilder responseBuilder = Response.status(Response.Status.OK).entity(exportedApiArchiveFile);
        responseBuilder.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM).header("Content-Disposition", "attachment; filename=\"" + exportedApiArchiveFile.getName() + "\"");
        Response response = responseBuilder.build();
        return response;
    } catch (APIManagementException e) {
        String errorMessage = "Error while exporting policies";
        log.error(errorMessage, e);
        org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : Response(javax.ws.rs.core.Response) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyExportManager(org.wso2.carbon.apimgt.rest.api.core.utils.PolicyExportManager) File(java.io.File)

Aggregations

File (java.io.File)2 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)2 ArrayList (java.util.ArrayList)1 Response (javax.ws.rs.core.Response)1 Test (org.testng.annotations.Test)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)1 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)1 CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)1 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)1 PolicyExportManager (org.wso2.carbon.apimgt.rest.api.core.utils.PolicyExportManager)1