Search in sources :

Example 1 with ApplicationCreationWorkflow

use of org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createWorkflow.

public static Workflow createWorkflow(String workflowReferenceID) throws APIMgtDAOException {
    Workflow workflow = new ApplicationCreationWorkflow(DAOFactory.getApplicationDAO(), DAOFactory.getWorkflowDAO(), null);
    workflow.setExternalWorkflowReference(workflowReferenceID);
    workflow.setStatus(WorkflowStatus.CREATED);
    workflow.setCreatedTime(LocalDateTime.now());
    workflow.setWorkflowType(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
    workflow.setWorkflowReference(UUID.randomUUID().toString());
    Map<String, String> properties = new HashMap<>();
    properties.put("property1", "value1");
    properties.put("property2", "value2");
    workflow.setAttributes(properties);
    return workflow;
}
Also used : HashMap(java.util.HashMap) Workflow(org.wso2.carbon.apimgt.core.workflow.Workflow) ApplicationCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow) ApplicationCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow)

Example 2 with ApplicationCreationWorkflow

use of org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow in project carbon-apimgt by wso2.

the class WorkflowMappingUtilTest method testToWorkflowDTO.

@Test(description = "Convert Workflow to WorkflowDTO")
public void testToWorkflowDTO() throws Exception {
    Workflow workflow1 = new ApplicationCreationWorkflow(null, null, null);
    workflow1.setStatus(WorkflowStatus.APPROVED);
    LocalDateTime date1 = LocalDateTime.now();
    workflow1.setCreatedTime(date1);
    workflow1.setWorkflowDescription("Description 1");
    workflow1.setWorkflowType(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
    String ref1 = UUID.randomUUID().toString();
    workflow1.setExternalWorkflowReference(ref1);
    WorkflowDTO dto = WorkflowMappingUtil.toWorkflowDTO(workflow1);
    Assert.assertEquals(dto.getDescription(), "Description 1", "Invalid description for workflow item 1");
    Assert.assertEquals(dto.getType(), WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION, "Invalid type for workflow item 1");
    Assert.assertEquals(dto.getWorkflowStatus(), WorkflowStatus.APPROVED.toString(), "Invalid status for workflow item 1");
    Assert.assertEquals(dto.getReferenceId(), ref1, "Invalid reference id for workflow item 1");
}
Also used : LocalDateTime(java.time.LocalDateTime) WorkflowDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.WorkflowDTO) SubscriptionCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow) ApplicationCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow) Workflow(org.wso2.carbon.apimgt.core.workflow.Workflow) ApplicationCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow) Test(org.testng.annotations.Test)

Example 3 with ApplicationCreationWorkflow

use of org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow in project carbon-apimgt by wso2.

the class APIStoreImpl method addApplication.

@Override
public ApplicationCreationResponse addApplication(Application application) throws APIManagementException {
    ApplicationCreationResponse applicationResponse = null;
    try {
        if (getApplicationDAO().isApplicationNameExists(application.getName())) {
            String message = "An application already exists with a duplicate name - " + application.getName();
            log.error(message);
            throw new APIMgtResourceAlreadyExistsException(message, ExceptionCodes.APPLICATION_ALREADY_EXISTS);
        }
        // Tier validation
        Policy tier = application.getPolicy();
        if (tier == null) {
            String message = "Tier name cannot be null - " + application.getName();
            log.error(message);
            throw new APIManagementException(message, ExceptionCodes.TIER_CANNOT_BE_NULL);
        } else {
            Policy policy = getPolicyDAO().getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, tier.getPolicyName());
            if (policy == null) {
                String message = "Specified tier " + tier.getPolicyName() + " is invalid";
                log.error(message);
                throw new APIManagementException(message, ExceptionCodes.TIER_CANNOT_BE_NULL);
            }
            application.setPolicy(policy);
        }
        // Generate UUID for application
        String generatedUuid = UUID.randomUUID().toString();
        application.setId(generatedUuid);
        String permissionString = application.getPermissionString();
        if (permissionString != null && !("").equals(permissionString)) {
            HashMap roleNamePermissionList;
            roleNamePermissionList = getAPIPermissionArray(permissionString);
            application.setPermissionMap(roleNamePermissionList);
        }
        application.setCreatedTime(LocalDateTime.now());
        getApplicationDAO().addApplication(application);
        WorkflowExecutor appCreationWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
        ApplicationCreationWorkflow workflow = new ApplicationCreationWorkflow(getApplicationDAO(), getWorkflowDAO(), getApiGateway());
        workflow.setApplication(application);
        workflow.setCreatedBy(getUsername());
        workflow.setWorkflowReference(application.getId());
        workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
        workflow.setCreatedTime(LocalDateTime.now());
        String workflowDescription = "Application [ " + application.getName() + " ] creation request from application creator - " + getUsername() + " with throttling tier - " + tier.getPolicyName() + "";
        workflow.setWorkflowDescription(workflowDescription);
        WorkflowResponse response = appCreationWFExecutor.execute(workflow);
        workflow.setStatus(response.getWorkflowStatus());
        if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
            completeWorkflow(appCreationWFExecutor, workflow);
        } else {
            getApplicationDAO().updateApplicationState(generatedUuid, APIMgtConstants.ApplicationStatus.APPLICATION_ONHOLD);
            addWorkflowEntries(workflow);
        }
        APIUtils.logDebug("successfully added application with appId " + application.getId(), log);
        applicationResponse = new ApplicationCreationResponse(application.getId(), response);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while creating the application - " + application.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (ParseException e) {
        String errorMsg = "Error occurred while parsing the permission json from swagger in application - " + application.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.SWAGGER_PARSE_EXCEPTION);
    } catch (WorkflowException e) {
        String errorMsg = "Error occurred in workflow";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.WORKFLOW_EXCEPTION);
    }
    return applicationResponse;
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) WorkflowException(org.wso2.carbon.apimgt.core.exception.WorkflowException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceAlreadyExistsException) ApplicationCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) ParseException(org.json.simple.parser.ParseException)

Example 4 with ApplicationCreationWorkflow

use of org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testCompleteApplicationWorkflowWithoutReference.

// End of exception testing
@Test(description = "Exception when completing application creation workflow without a reference", expectedExceptions = APIManagementException.class)
public void testCompleteApplicationWorkflowWithoutReference() throws Exception {
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
    APIStore apiStore = getApiStoreImpl(applicationDAO, workflowDAO);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    WorkflowExecutor executor = new DefaultWorkflowExecutor();
    Workflow workflow = new ApplicationCreationWorkflow(applicationDAO, workflowDAO, apiGateway);
    workflow.setWorkflowReference(null);
    apiStore.completeWorkflow(executor, workflow);
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) Workflow(org.wso2.carbon.apimgt.core.workflow.Workflow) ApplicationCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow) ApplicationUpdateWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationUpdateWorkflow) SubscriptionCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow) ApplicationCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) DefaultWorkflowExecutor(org.wso2.carbon.apimgt.core.workflow.DefaultWorkflowExecutor) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) DefaultWorkflowExecutor(org.wso2.carbon.apimgt.core.workflow.DefaultWorkflowExecutor) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 5 with ApplicationCreationWorkflow

use of org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testAddApplicationWorkflowReject.

@Test(description = "Test Application workflow rejection")
public void testAddApplicationWorkflowReject() throws APIManagementException {
    ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    Policy policy = Mockito.mock(Policy.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIStore apiStore = getApiStoreImpl(applicationDAO, policyDAO, workflowDAO, apiGateway);
    Application application = new Application(APP_NAME, USER_NAME);
    application.setPolicy(new ApplicationPolicy(TIER));
    application.setPermissionString("[{\"groupId\": \"testGroup\",\"permission\":[\"READ\",\"UPDATE\",\"DELETE\",\"SUBSCRIPTION\"]}]");
    Mockito.when(applicationDAO.isApplicationNameExists(APP_NAME)).thenReturn(false);
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, TIER)).thenReturn(policy);
    apiStore.addApplication(application);
    DefaultWorkflowExecutor executor = Mockito.mock(DefaultWorkflowExecutor.class);
    Workflow workflow = new ApplicationCreationWorkflow(applicationDAO, workflowDAO, apiGateway);
    workflow.setWorkflowReference(application.getId());
    WorkflowResponse response = new GeneralWorkflowResponse();
    response.setWorkflowStatus(WorkflowStatus.REJECTED);
    Mockito.when(executor.complete(workflow)).thenReturn(response);
    apiStore.completeWorkflow(executor, workflow);
    Mockito.verify(applicationDAO, Mockito.times(1)).updateApplicationState(application.getId(), "REJECTED");
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Workflow(org.wso2.carbon.apimgt.core.workflow.Workflow) ApplicationCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow) ApplicationUpdateWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationUpdateWorkflow) SubscriptionCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) DefaultWorkflowExecutor(org.wso2.carbon.apimgt.core.workflow.DefaultWorkflowExecutor) WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) ApplicationCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) 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

ApplicationCreationWorkflow (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow)7 Workflow (org.wso2.carbon.apimgt.core.workflow.Workflow)6 Test (org.testng.annotations.Test)4 SubscriptionCreationWorkflow (org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow)4 LocalDateTime (java.time.LocalDateTime)3 HashMap (java.util.HashMap)2 BeforeTest (org.testng.annotations.BeforeTest)2 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)2 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)2 WorkflowExecutor (org.wso2.carbon.apimgt.core.api.WorkflowExecutor)2 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)2 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)2 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)2 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)2 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)2 ApplicationUpdateWorkflow (org.wso2.carbon.apimgt.core.workflow.ApplicationUpdateWorkflow)2 DefaultWorkflowExecutor (org.wso2.carbon.apimgt.core.workflow.DefaultWorkflowExecutor)2 ArrayList (java.util.ArrayList)1 Response (javax.ws.rs.core.Response)1 ParseException (org.json.simple.parser.ParseException)1