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 ");
}
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();
}
}
Aggregations