Search in sources :

Example 56 with APIStore

use of org.wso2.carbon.apimgt.api.model.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();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) FileBasedApplicationImportExportManager(org.wso2.carbon.apimgt.rest.api.store.utils.FileBasedApplicationImportExportManager) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 57 with APIStore

use of org.wso2.carbon.apimgt.api.model.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();
    }
}
Also used : ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) FileBasedApplicationImportExportManager(org.wso2.carbon.apimgt.rest.api.store.utils.FileBasedApplicationImportExportManager) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 58 with APIStore

use of org.wso2.carbon.apimgt.api.model.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());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) Application(org.wso2.carbon.apimgt.core.models.Application) ApplicationDeletionWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationDeletionWorkflow)

Example 59 with APIStore

use of org.wso2.carbon.apimgt.api.model.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());
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) ByteArrayInputStream(java.io.ByteArrayInputStream) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 60 with APIStore

use of org.wso2.carbon.apimgt.api.model.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");
    }
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) Application(org.wso2.carbon.apimgt.core.models.Application) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

APIStore (org.wso2.carbon.apimgt.core.api.APIStore)226 Test (org.testng.annotations.Test)109 Test (org.junit.Test)98 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)96 BeforeTest (org.testng.annotations.BeforeTest)93 Response (javax.ws.rs.core.Response)90 Request (org.wso2.msf4j.Request)87 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)72 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)56 API (org.wso2.carbon.apimgt.core.models.API)51 HashMap (java.util.HashMap)49 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)49 ArrayList (java.util.ArrayList)47 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)47 Application (org.wso2.carbon.apimgt.core.models.Application)46 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)38 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)32 SQLException (java.sql.SQLException)31 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)31 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)29