Search in sources :

Example 66 with Workflow

use of org.wso2.carbon.apimgt.api.model.Workflow in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testResumeWorkflow.

@Test
public void testResumeWorkflow() throws Exception {
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper();
    apiConsumer.apiMgtDAO = apiMgtDAO;
    WorkflowDTO workflowDTO = new WorkflowDTO();
    workflowDTO.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    when(apiMgtDAO.retrieveWorkflow(Mockito.anyString())).thenReturn(workflowDTO);
    // Null input case
    assertNotNull(apiConsumer.resumeWorkflow(null));
    String[] args = { UUID.randomUUID().toString(), WorkflowStatus.CREATED.toString(), UUID.randomUUID().toString() };
    assertNotNull(apiConsumer.resumeWorkflow(args));
    Mockito.reset(apiMgtDAO);
    workflowDTO.setTenantDomain("wso2.com");
    when(apiMgtDAO.retrieveWorkflow(Mockito.anyString())).thenReturn(workflowDTO);
    JSONObject row = apiConsumer.resumeWorkflow(args);
    assertNotNull(row);
    Mockito.reset(apiMgtDAO);
    when(apiMgtDAO.retrieveWorkflow(Mockito.anyString())).thenThrow(APIManagementException.class);
    when(APIUtil.isStringArray(args)).thenReturn(true);
    row = apiConsumer.resumeWorkflow(args);
    assertEquals("Error while resuming the workflow. null", row.get("message"));
    // Workflow DAO null case
    Mockito.reset(apiMgtDAO);
    when(apiMgtDAO.retrieveWorkflow(Mockito.anyString())).thenReturn(null);
    row = apiConsumer.resumeWorkflow(args);
    assertNotNull(row);
    assertEquals(true, row.get("error"));
    assertEquals(500, row.get("statusCode"));
    // Invalid status test
    args[1] = "Invalid status";
    Mockito.reset(apiMgtDAO);
    when(apiMgtDAO.retrieveWorkflow(Mockito.anyString())).thenReturn(workflowDTO);
    row = apiConsumer.resumeWorkflow(args);
    assertEquals("Illegal argument provided. Valid values for status are APPROVED and REJECTED.", row.get("message"));
}
Also used : WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) SubscriptionWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO) JSONObject(org.json.simple.JSONObject) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 67 with Workflow

use of org.wso2.carbon.apimgt.api.model.Workflow in project carbon-apimgt by wso2.

the class UserPostSelfRegistrationHandler method executeUserRegistrationWorkflow.

/**
 * This method adds new role to the existing user roles
 * @param tenantDomain tenant domain extracted from the event
 * @param userName username extracted from the event
 * @throws org.wso2.carbon.identity.recovery.IdentityRecoveryServerException when unable to retrieve
 * userStoreManager instance
 */
private void executeUserRegistrationWorkflow(String tenantDomain, String userName) throws org.wso2.carbon.identity.recovery.IdentityRecoveryServerException {
    try {
        // Realm service is used for user management tasks
        RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
        UserStoreManager userStoreManager;
        try {
            userStoreManager = realmService.getTenantUserRealm(IdentityTenantUtil.getTenantId(tenantDomain)).getUserStoreManager();
        } catch (UserStoreException e) {
            throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_UNEXPECTED, userName, e);
        }
        // Start a tenant flow
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        carbonContext.setTenantId(IdentityTenantUtil.getTenantId(tenantDomain));
        carbonContext.setTenantDomain(tenantDomain);
        if (userStoreManager.isExistingUser(userName)) {
            List<String> roleList = asList(userStoreManager.getRoleListOfUser(userName));
            // User should have selfSignup role. Checking whether the user is in the new role
            if (roleList.contains(SELF_SIGNUP_ROLE) && !roleList.contains(SUBSCRIBER_ROLE)) {
                WorkflowExecutor userSignUpWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_USER_SIGNUP);
                // initiate a new signup workflow
                WorkflowDTO signUpWFDto = new WorkflowDTO();
                signUpWFDto.setWorkflowReference(userName);
                signUpWFDto.setStatus(WorkflowStatus.CREATED);
                signUpWFDto.setCreatedTime(System.currentTimeMillis());
                signUpWFDto.setTenantDomain(tenantDomain);
                signUpWFDto.setTenantId(IdentityTenantUtil.getTenantId(tenantDomain));
                signUpWFDto.setExternalWorkflowReference(userSignUpWFExecutor.generateUUID());
                signUpWFDto.setWorkflowType(WorkflowConstants.WF_TYPE_AM_USER_SIGNUP);
                signUpWFDto.setCallbackUrl(userSignUpWFExecutor.getCallbackURL());
                userSignUpWFExecutor.execute(signUpWFDto);
            }
        }
    } catch (UserStoreException | WorkflowException e) {
        throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_UNEXPECTED, userName, e);
    } finally {
        Utils.clearArbitraryProperties();
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) RealmService(org.wso2.carbon.user.core.service.RealmService) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) WorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager)

Example 68 with Workflow

use of org.wso2.carbon.apimgt.api.model.Workflow in project carbon-apimgt by wso2.

the class ApplicationCreationSimpleWorkflowExecutorTest method testFailureWhileExecutingApplicationDeletionWorkFlow.

@Test
public void testFailureWhileExecutingApplicationDeletionWorkFlow() throws APIManagementException {
    PowerMockito.doThrow(new APIManagementException("Error occurred while updating application status")).when(apiMgtDAO).updateApplicationStatus(Mockito.anyInt(), Mockito.anyString());
    try {
        // Test workflow execution when workflow status = 'CREATED'
        workflowDTO.setStatus(WorkflowStatus.CREATED);
        applicationCreationSimpleWorkflowExecutor.execute(workflowDTO);
        Assert.fail("Expected WorkflowException is not thrown while executing application deletion workflow");
    } catch (WorkflowException e) {
        Assert.assertTrue(e.getMessage().contains("Error occured when updating the status of the Application " + "creation process"));
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 69 with Workflow

use of org.wso2.carbon.apimgt.api.model.Workflow in project carbon-apimgt by wso2.

the class ApplicationDeletionSimpleWorkflowExecutorTest method testFailureWhileExecutingApplicationDeletionWorkFlow.

@Test
public void testFailureWhileExecutingApplicationDeletionWorkFlow() throws APIManagementException {
    // Test throwing WorkflowException with an error message
    Mockito.doThrow(new APIManagementException("Could not complete application deletion workflow")).when(apiMgtDAO).deleteApplication(application);
    try {
        applicationDeletionSimpleWorkflowExecutor.execute(workflowDTO);
        Assert.fail("Expected WorkflowException is not thrown when application deletion simple workflow" + " execution failed");
    } catch (WorkflowException e) {
        Assert.assertTrue(e.getMessage().contains("Could not complete application deletion workflow"));
    }
    // Test throwing WorkflowException without an empty error message
    Mockito.doThrow(APIManagementException.class).when(apiMgtDAO).deleteApplication(application);
    try {
        applicationDeletionSimpleWorkflowExecutor.execute(workflowDTO);
        Assert.fail("Expected WorkflowException is not thrown when application deletion simple workflow" + " execution failed");
    } catch (WorkflowException e) {
        Assert.assertTrue(e.getMessage().contains("Couldn't complete simple application deletion workflow for " + "application: " + application.getName()));
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 70 with Workflow

use of org.wso2.carbon.apimgt.api.model.Workflow in project carbon-apimgt by wso2.

the class ApplicationRegistrationWSWorkflowExecutorTest method testFailureToCompleteApplicationRegistrationWSWFWhenKeyGenerationFailed.

@Test
public void testFailureToCompleteApplicationRegistrationWSWFWhenKeyGenerationFailed() throws Exception {
    applicationRegistrationWSWorkflowExecutor.setUsername(adminUsername);
    applicationRegistrationWSWorkflowExecutor.setPassword(adminPassword.toCharArray());
    workflowDTO.setStatus(WorkflowStatus.APPROVED);
    PowerMockito.doThrow(new APIManagementException("Error occurred when updating the status of the Application " + "Registration process")).when(keyManager).createApplication((OAuthAppRequest) Mockito.anyObject());
    try {
        applicationRegistrationWSWorkflowExecutor.complete(workflowDTO);
        Assert.fail("Expected WorkflowException has not occurred while completing application registration " + "workflow");
    } catch (WorkflowException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred when updating the status of the Application " + "Registration process");
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 Test (org.junit.Test)35 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)35 Workflow (org.wso2.carbon.apimgt.core.workflow.Workflow)28 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)26 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)25 Test (org.testng.annotations.Test)24 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)22 WorkflowDTO (org.wso2.carbon.apimgt.impl.dto.WorkflowDTO)21 HashMap (java.util.HashMap)20 SubscriptionWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO)20 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)19 JSONObject (org.json.simple.JSONObject)17 ApplicationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO)17 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)16 ApplicationRegistrationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO)15 WorkflowExecutor (org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor)15 Application (org.wso2.carbon.apimgt.api.model.Application)14 ArrayList (java.util.ArrayList)12 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)11