use of org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI in project carbon-apimgt by wso2.
the class WSO2APIPublisherTestCase method testFailureWhileExportingAPI.
@Test
public void testFailureWhileExportingAPI() throws Exception {
// Error path - When exporting API failed
Mockito.when(APIImportExportUtil.getImportExportAPI()).thenReturn(importExportAPI);
PowerMockito.doThrow(new APIImportExportException("Error while exporting API")).when(importExportAPI).exportAPI(Matchers.anyString(), Matchers.anyString(), Matchers.anyString(), Matchers.anyString(), Matchers.anyString(), Matchers.anyBoolean(), Matchers.any(ExportFormat.class), Matchers.anyBoolean(), Matchers.anyBoolean(), Matchers.anyBoolean(), Matchers.anyString(), Matchers.anyString());
try {
wso2APIPublisher.publishToStore(api, store);
Assert.fail("APIManagement exception not thrown for error scenario");
} catch (APIManagementException e) {
String errorMsg = "Error while exporting API: " + api.getId().getApiName() + " version: " + api.getId().getVersion();
Assert.assertEquals(errorMsg, e.getMessage());
}
PowerMockito.doThrow(new UserStoreException("Error in getting the tenant id with tenant domain: " + tenantDomain + ".")).when(tenantManager).getTenantId(tenantDomain);
try {
wso2APIPublisher.publishToStore(api, store);
Assert.fail("APIManagement exception not thrown for error scenario");
} catch (APIManagementException e) {
String errorMsg = "Error while getting tenantId for tenant domain: " + tenantDomain + " when exporting API:" + api.getId().getApiName() + " version: " + api.getId().getVersion();
Assert.assertEquals(errorMsg, e.getMessage());
}
}
use of org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method importAPI.
/**
* Import an API by uploading an archive file. All relevant API data will be included upon the creation of
* the API. Depending on the choice of the user, provider of the imported API will be preserved or modified.
*
* @param fileInputStream Input stream from the REST request
* @param fileDetail File details as Attachment
* @param preserveProvider User choice to keep or replace the API provider
* @param overwrite Whether to update the API or not. This is used when updating already existing APIs.
* @return API import response
* @throws APIManagementException when error occurred while trying to import the API
*/
@Override
public Response importAPI(InputStream fileInputStream, Attachment fileDetail, Boolean preserveProvider, Boolean rotateRevision, Boolean overwrite, MessageContext messageContext) throws APIManagementException {
// Check whether to update. If not specified, default value is false.
overwrite = overwrite == null ? false : overwrite;
// Check if the URL parameter value is specified, otherwise the default value is true.
preserveProvider = preserveProvider == null || preserveProvider;
String organization = RestApiUtil.getValidatedOrganization(messageContext);
String[] tokenScopes = (String[]) PhaseInterceptorChain.getCurrentMessage().getExchange().get(RestApiConstants.USER_REST_API_SCOPES);
ImportExportAPI importExportAPI = APIImportExportUtil.getImportExportAPI();
importExportAPI.importAPI(fileInputStream, preserveProvider, rotateRevision, overwrite, tokenScopes, organization);
return Response.status(Response.Status.OK).entity("API imported successfully.").build();
}
use of org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI in project carbon-apimgt by wso2.
the class APIProviderImplTest method testDeleteAPIRevision.
/**
* This method tests deleting an API Revision
*
* @throws APIManagementException
*/
@Test
public void testDeleteAPIRevision() throws APIManagementException, APIPersistenceException {
ImportExportAPI importExportAPI = Mockito.mock(ImportExportAPI.class);
ArtifactSaver artifactSaver = Mockito.mock(ArtifactSaver.class);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, importExportAPI, gatewayArtifactsMgtDAO, artifactSaver);
APIIdentifier apiId = new APIIdentifier("admin", "PizzaShackAPI", "1.0.0", "63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
API api = new API(apiId);
api.setContext("/test");
api.setStatus(APIConstants.CREATED);
String apiPath = "/apimgt/applicationdata/provider/admin/PizzaShackAPI/1.0.0/api";
APIRevision apiRevision = new APIRevision();
apiRevision.setApiUUID("63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
apiRevision.setDescription("test description revision 1");
Mockito.when(apimgtDAO.getRevisionCountByAPI(Mockito.anyString())).thenReturn(0);
Mockito.when(apimgtDAO.getMostRecentRevisionId(Mockito.anyString())).thenReturn(0);
Mockito.when(APIUtil.getAPIIdentifierFromUUID(Mockito.anyString())).thenReturn(apiId);
Mockito.when(APIUtil.getAPIPath(apiId)).thenReturn(apiPath);
Mockito.when(APIUtil.getTenantConfig(Mockito.anyString())).thenReturn(new JSONObject());
PowerMockito.when(apiPersistenceInstance.addAPIRevision(any(Organization.class), Mockito.anyString(), Mockito.anyInt())).thenReturn("b55e0fc3-9829-4432-b99e-02056dc91838");
try {
apiProvider.addAPIRevision(apiRevision, superTenantDomain);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
Mockito.when(apimgtDAO.getRevisionByRevisionUUID(Mockito.anyString())).thenReturn(apiRevision);
PowerMockito.doNothing().when(apiPersistenceInstance).deleteAPIRevision(any(Organization.class), Mockito.anyString(), Mockito.anyString(), Mockito.anyInt());
try {
apiProvider.deleteAPIRevision("63e1e37e-a5b8-4be6-86a5-d6ae0749f131", "b55e0fc3-9829-4432-b99e-02056dc91838", superTenantDomain);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI in project carbon-apimgt by wso2.
the class APIProviderImplTest method testAddAPIRevision.
/**
* This method tests adding a new API Revision
*
* @throws APIManagementException
*/
@Test
public void testAddAPIRevision() throws APIManagementException, APIPersistenceException, APIImportExportException, ArtifactSynchronizerException {
ImportExportAPI importExportAPI = Mockito.mock(ImportExportAPI.class);
ArtifactSaver artifactSaver = Mockito.mock(ArtifactSaver.class);
Mockito.doNothing().when(artifactSaver).saveArtifact(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(File.class));
Mockito.when(GatewayArtifactsMgtDAO.getInstance()).thenReturn(gatewayArtifactsMgtDAO);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, importExportAPI, gatewayArtifactsMgtDAO, artifactSaver);
APIIdentifier apiId = new APIIdentifier("admin", "PizzaShackAPI", "1.0.0", "63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
API api = new API(apiId);
api.setContext("/test");
api.setStatus(APIConstants.CREATED);
String apiPath = "/apimgt/applicationdata/provider/admin/PizzaShackAPI/1.0.0/api";
APIRevision apiRevision = new APIRevision();
apiRevision.setApiUUID("63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
apiRevision.setDescription("test description revision 1");
Mockito.when(apimgtDAO.getRevisionCountByAPI(Mockito.anyString())).thenReturn(0);
Mockito.when(apimgtDAO.getMostRecentRevisionId(Mockito.anyString())).thenReturn(0);
Mockito.when(APIUtil.getAPIIdentifierFromUUID(Mockito.anyString())).thenReturn(apiId);
Mockito.when(APIUtil.getAPIPath(apiId)).thenReturn(apiPath);
PowerMockito.when(apiPersistenceInstance.addAPIRevision(any(Organization.class), Mockito.anyString(), Mockito.anyInt())).thenReturn("b55e0fc3-9829-4432-b99e-02056dc91838");
Mockito.when(APIUtil.getTenantConfig(Mockito.anyString())).thenReturn(new JSONObject());
try {
apiProvider.addAPIRevision(apiRevision, superTenantDomain);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
Aggregations