Search in sources :

Example 26 with WorkflowDTO

use of org.wso2.carbon.apimgt.impl.dto.WorkflowDTO 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 27 with WorkflowDTO

use of org.wso2.carbon.apimgt.impl.dto.WorkflowDTO in project carbon-apimgt by wso2.

the class APIConsumerImpl method removeSubscription.

/**
 * Removes a subscription specified by SubscribedAPI object
 *
 * @param subscription SubscribedAPI object
 * @param organization Organization
 * @throws APIManagementException
 */
@Override
public void removeSubscription(SubscribedAPI subscription, String organization) throws APIManagementException {
    String uuid = subscription.getUUID();
    if (subscription != null) {
        Application application = subscription.getApplication();
        Identifier identifier = subscription.getApiId() != null ? subscription.getApiId() : subscription.getProductId();
        String userId = application.getSubscriber().getName();
        removeSubscription(identifier, userId, application.getId(), organization);
        if (log.isDebugEnabled()) {
            String appName = application.getName();
            String logMessage = "Identifier:  " + identifier.toString() + " subscription (uuid : " + uuid + ") removed from app " + appName;
            log.debug(logMessage);
        }
        // get the workflow state once the executor is executed.
        WorkflowDTO wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(application.getId()), WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION);
        int tenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
        String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
        // wfDTO is null when simple wf executor is used because wf state is not stored in the db and is always approved.
        if (wfDTO != null) {
            if (WorkflowStatus.APPROVED.equals(wfDTO.getStatus())) {
                SubscriptionEvent subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_DELETE.name(), tenantId, tenantDomain, subscription.getSubscriptionId(), subscription.getUUID(), identifier.getId(), identifier.getUUID(), application.getId(), application.getUUID(), identifier.getTier(), subscription.getSubStatus());
                APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
            }
        } else {
            SubscriptionEvent subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_DELETE.name(), tenantId, tenantDomain, subscription.getSubscriptionId(), subscription.getUUID(), identifier.getId(), identifier.getUUID(), application.getId(), application.getUUID(), identifier.getTier(), subscription.getSubStatus());
            APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
        }
    } else {
        throw new APIManagementException(String.format("Subscription for UUID:%s does not exist.", subscription.getUUID()));
    }
}
Also used : SubscriptionEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent) ApplicationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO) WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) ApplicationRegistrationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO) SubscriptionWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Identifier(org.wso2.carbon.apimgt.api.model.Identifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 28 with WorkflowDTO

use of org.wso2.carbon.apimgt.impl.dto.WorkflowDTO 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 29 with WorkflowDTO

use of org.wso2.carbon.apimgt.impl.dto.WorkflowDTO in project carbon-apimgt by wso2.

the class SubscriptionCreationSimpleWorkflowExecutorTest method testExecutingSubscriptionCreationWorkFlow.

@Test
public void testExecutingSubscriptionCreationWorkFlow() throws APIManagementException {
    WorkflowDTO workflowDTO = new WorkflowDTO();
    workflowDTO.setWorkflowReference("1");
    try {
        PowerMockito.doNothing().when(apiMgtDAO).updateSubscriptionStatus(1, "UNBLOCKED");
        Assert.assertNotNull(subscriptionCreationSimpleWorkflowExecutor.execute(workflowDTO));
    } catch (WorkflowException e) {
        Assert.fail("Unexpected WorkflowException occurred while executing Subscription creation simple workflow");
    }
}
Also used : WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 30 with WorkflowDTO

use of org.wso2.carbon.apimgt.impl.dto.WorkflowDTO in project carbon-apimgt by wso2.

the class UserSignUpSimpleWorkflowExecutorTest method testExecutingUserSignUpSimpleWorkflow.

@Test
public void testExecutingUserSignUpSimpleWorkflow() throws APIManagementException, UserStoreException {
    Map<String, Boolean> roleMap = new HashMap<String, Boolean>();
    roleMap.put(signUpRole, false);
    UserRegistrationConfigDTO userRegistrationConfigDTO = new UserRegistrationConfigDTO();
    userRegistrationConfigDTO.setAdminUserName("admin");
    userRegistrationConfigDTO.setAdminPassword("admin");
    userRegistrationConfigDTO.setRoles(roleMap);
    PowerMockito.when(SelfSignUpUtil.getSignupConfiguration(tenantDomain)).thenReturn(userRegistrationConfigDTO);
    PowerMockito.when(SelfSignUpUtil.getRoleNames(userRegistrationConfigDTO)).thenCallRealMethod();
    Mockito.when(userStoreManager.isExistingUser(username)).thenReturn(true);
    Mockito.when(userStoreManager.isExistingRole("Internal/" + signUpRole)).thenReturn(true);
    Mockito.doNothing().when(userStoreManager).updateRoleListOfUser(username, null, new String[] { "Internal/" + signUpRole });
    try {
        Assert.assertNotNull(userSignUpSimpleWorkflowExecutor.execute(workflowDTO));
    } catch (WorkflowException e) {
        Assert.fail("Unexpected WorkflowException has thrown while executing the user signup simple workflow");
    }
}
Also used : HashMap(java.util.HashMap) UserRegistrationConfigDTO(org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)51 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)51 WorkflowDTO (org.wso2.carbon.apimgt.impl.dto.WorkflowDTO)49 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)46 ApplicationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO)33 SubscriptionWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO)31 Application (org.wso2.carbon.apimgt.api.model.Application)25 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)22 ApplicationRegistrationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO)19 ServiceReferenceHolderMockCreator (org.wso2.carbon.apimgt.impl.ServiceReferenceHolderMockCreator)17 WorkflowExecutor (org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor)14 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)11 XMLStreamException (javax.xml.stream.XMLStreamException)10 JSONObject (org.json.simple.JSONObject)10 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)10 WorkflowException (org.wso2.carbon.apimgt.impl.workflow.WorkflowException)10 HashMap (java.util.HashMap)9 UserRegistrationConfigDTO (org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO)9 ServiceClient (org.apache.axis2.client.ServiceClient)7 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)7