use of org.wso2.carbon.apimgt.core.api.APIStore in project carbon-apimgt by wso2.
the class ImportApiServiceImpl method importApplicationsPut.
@Override
public Response importApplicationsPut(InputStream fileInputStream, FileInfo fileDetail, Request request) throws NotFoundException {
APIStore consumer = null;
String username = RestApiUtil.getLoggedInUsername(request);
try {
consumer = RestApiUtil.getConsumer(RestApiUtil.getLoggedInUsername(request));
FileBasedApplicationImportExportManager importExportManager = new FileBasedApplicationImportExportManager(consumer, System.getProperty("java.io.tmpdir") + File.separator + "exported-app-archives-" + UUID.randomUUID().toString());
Application applicationDetails = importExportManager.importApplication(fileInputStream);
applicationDetails.setCreatedUser(username);
applicationDetails.setUpdatedUser(username);
Application updatedApplication = importExportManager.updateApplication(applicationDetails, username);
return Response.status(Response.Status.OK).entity(updatedApplication).build();
} catch (APIManagementException e) {
String errorMsg = "Error while importing the Applications";
log.error(errorMsg, e);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.core.api.APIStore in project carbon-apimgt by wso2.
the class ImportApiServiceImpl method importApplicationsPost.
/**
* Import an Application which has been exported to a zip file
*
* @param fileInputStream content stream of the zip file which contains exported Application
* @param fileDetail meta information of the zip file
* @param request msf4j request object
* @return Application that was imported
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response importApplicationsPost(InputStream fileInputStream, FileInfo fileDetail, Request request) throws NotFoundException {
APIStore consumer = null;
String username = RestApiUtil.getLoggedInUsername(request);
try {
consumer = RestApiUtil.getConsumer(username);
FileBasedApplicationImportExportManager importExportManager = new FileBasedApplicationImportExportManager(consumer, System.getProperty("java.io.tmpdir") + File.separator + "exported-app-archives-" + UUID.randomUUID().toString());
Application applicationDetails = importExportManager.importApplication(fileInputStream);
applicationDetails.setCreatedUser(username);
applicationDetails.setUpdatedUser(username);
ApplicationCreationResponse response = consumer.addApplication(applicationDetails);
return Response.status(Response.Status.OK).entity(response).build();
} catch (APIManagementException e) {
String errorMsg = "Error while importing the Applications";
log.error(errorMsg, e);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.core.api.APIStore in project carbon-apimgt by wso2.
the class APIStoreImpl method deleteApplication.
/**
* @see APIStore#deleteApplication(String)
*/
@Override
public WorkflowResponse deleteApplication(String appId) throws APIManagementException {
try {
if (appId == null) {
String message = "Application Id is not provided";
throw new APIManagementException(message, ExceptionCodes.PARAMETER_NOT_PROVIDED);
}
// get app info
Application application = getApplicationDAO().getApplication(appId);
if (application == null) {
String message = "Application cannot be found for id :" + appId;
throw new APIManagementException(message, ExceptionCodes.APPLICATION_NOT_FOUND);
}
// delete application creation pending tasks
cleanupPendingTaskForApplicationDeletion(application);
WorkflowExecutor removeApplicationWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION);
ApplicationDeletionWorkflow workflow = new ApplicationDeletionWorkflow(getApplicationDAO(), getWorkflowDAO(), getApiGateway());
workflow.setApplication(application);
workflow.setWorkflowType(APIMgtConstants.WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION);
workflow.setWorkflowReference(application.getId());
workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
workflow.setCreatedTime(LocalDateTime.now());
String workflowDescription = "Application [ " + application.getName() + " ] deletion request from - " + application.getName();
workflow.setWorkflowDescription(workflowDescription);
WorkflowResponse response = removeApplicationWFExecutor.execute(workflow);
workflow.setStatus(response.getWorkflowStatus());
if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
completeWorkflow(removeApplicationWFExecutor, workflow);
} else {
// add entry to workflow table if it is only in pending state
addWorkflowEntries(workflow);
}
return response;
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while deleting the application - " + appId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
use of org.wso2.carbon.apimgt.core.api.APIStore in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testUpdateCompositeApi.
@Test(description = "Update Composite API")
public void testUpdateCompositeApi() throws APIManagementException {
// Add a new Composite API
CompositeAPI.Builder apiBuilder = SampleTestObjectCreator.createUniqueCompositeAPI();
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
IdentityProvider idp = Mockito.mock(IdentityProvider.class);
APIStore apiStore = getApiStoreImpl(idp, null, apiDAO, apiSubscriptionDAO, gatewaySourceGenerator, apiGateway);
String ballerinaImpl = "Ballerina";
apiStore.addCompositeApi(apiBuilder);
CompositeAPI createdAPI = apiBuilder.build();
// Update existing Composite API
apiBuilder = SampleTestObjectCreator.createUniqueCompositeAPI();
apiBuilder.id(createdAPI.getId());
apiBuilder.name(createdAPI.getName());
apiBuilder.provider(createdAPI.getProvider());
apiBuilder.version(createdAPI.getVersion());
apiBuilder.context(createdAPI.getContext());
Mockito.when(apiDAO.getCompositeAPI(apiBuilder.getId())).thenReturn(createdAPI);
Mockito.when(apiDAO.getCompositeAPIGatewayConfig(apiBuilder.getId())).thenReturn(new ByteArrayInputStream(ballerinaImpl.getBytes(StandardCharsets.UTF_8)));
Mockito.when(gatewaySourceGenerator.getGatewayConfigFromSwagger(Matchers.anyString(), Matchers.anyString())).thenReturn(ballerinaImpl);
apiStore.updateCompositeApi(apiBuilder);
CompositeAPI updatedAPI = apiBuilder.build();
Assert.assertEquals(updatedAPI.getId(), createdAPI.getId());
Assert.assertEquals(updatedAPI.getName(), createdAPI.getName());
Assert.assertEquals(updatedAPI.getProvider(), createdAPI.getProvider());
Assert.assertEquals(updatedAPI.getVersion(), createdAPI.getVersion());
Assert.assertEquals(updatedAPI.getContext(), createdAPI.getContext());
}
use of org.wso2.carbon.apimgt.core.api.APIStore in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testUpdateApplication.
@Test(description = "Update an application")
public void testUpdateApplication() throws APIManagementException {
ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
APIStore apiStore = getApiStoreImpl(applicationDAO, policyDAO, workflowDAO, apiGateway);
Application existingApplication = SampleTestObjectCreator.createDefaultApplication();
String appUUID = existingApplication.getUuid();
existingApplication.setStatus(ApplicationStatus.APPLICATION_APPROVED);
Mockito.when(applicationDAO.getApplication(appUUID)).thenReturn(existingApplication);
// Updating the existing application
Application updatedApplication = SampleTestObjectCreator.createDefaultApplication();
updatedApplication.setDescription("updated description");
ApplicationPolicy applicationPolicy = SampleTestObjectCreator.createDefaultApplicationPolicy();
applicationPolicy.setPolicyName(TIER);
updatedApplication.setPolicy(applicationPolicy);
updatedApplication.setStatus(ApplicationStatus.APPLICATION_APPROVED);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, applicationPolicy.getPolicyName())).thenReturn(applicationPolicy);
apiStore.updateApplication(appUUID, updatedApplication);
Mockito.verify(applicationDAO, Mockito.times(1)).updateApplication(appUUID, updatedApplication);
// Error
// APIMgtDAOException
Mockito.doThrow(APIMgtDAOException.class).when(applicationDAO).updateApplication(appUUID, updatedApplication);
try {
apiStore.updateApplication(appUUID, updatedApplication);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while updating the application - " + appUUID);
}
// Error path
// When specified tier in the updated application is invalid
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, applicationPolicy.getPolicyName())).thenReturn(null);
try {
apiStore.updateApplication(appUUID, updatedApplication);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Specified tier " + applicationPolicy + " is invalid");
}
}
Aggregations