Search in sources :

Example 86 with Subscription

use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.

the class APIStateChangeWorkflow method updateAPIStatusForWorkflowComplete.

private void updateAPIStatusForWorkflowComplete(String apiId, String status, String updatedBy, LocalDateTime time) throws APIManagementException {
    boolean requireReSubscriptions = false;
    boolean deprecateOlderVersion = false;
    try {
        API api = apiDAO.getAPI(apiId);
        API.APIBuilder apiBuilder = new API.APIBuilder(api);
        apiBuilder.lastUpdatedTime(time);
        apiBuilder.updatedBy(updatedBy);
        LifecycleState currentState = apiLifecycleManager.getLifecycleDataForState(apiBuilder.getLifecycleInstanceId(), apiBuilder.getLifeCycleStatus());
        apiBuilder.lifecycleState(currentState);
        if (APIMgtConstants.APILCWorkflowStatus.PENDING.toString().equals(api.getWorkflowStatus())) {
            apiBuilder.workflowStatus(APIMgtConstants.APILCWorkflowStatus.APPROVED.toString());
            apiDAO.updateAPIWorkflowStatus(apiId, APIMgtConstants.APILCWorkflowStatus.APPROVED);
        }
        List<CheckItemBean> list = currentState.getCheckItemBeanList();
        for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
            CheckItemBean checkItemBean = (CheckItemBean) iterator.next();
            if (APIMgtConstants.DEPRECATE_PREVIOUS_VERSIONS.equals(checkItemBean.getName())) {
                deprecateOlderVersion = checkItemBean.isValue();
            } else if (APIMgtConstants.REQUIRE_RE_SUBSCRIPTIONS.equals(checkItemBean.getName())) {
                requireReSubscriptions = checkItemBean.isValue();
            }
        }
        API originalAPI = apiBuilder.build();
        apiLifecycleManager.executeLifecycleEvent(api.getLifeCycleStatus(), status, apiBuilder.getLifecycleInstanceId(), updatedBy, originalAPI);
        if (deprecateOlderVersion) {
            if (StringUtils.isNotEmpty(api.getCopiedFromApiId())) {
                API oldAPI = apiDAO.getAPI(api.getCopiedFromApiId());
                if (oldAPI != null) {
                    API.APIBuilder previousAPI = new API.APIBuilder(oldAPI);
                    previousAPI.setLifecycleStateInfo(apiLifecycleManager.getLifecycleDataForState(previousAPI.getLifecycleInstanceId(), previousAPI.getLifeCycleStatus()));
                    if (APIUtils.validateTargetState(previousAPI.getLifecycleState(), APIStatus.DEPRECATED.getStatus())) {
                        apiLifecycleManager.executeLifecycleEvent(previousAPI.getLifeCycleStatus(), APIStatus.DEPRECATED.getStatus(), previousAPI.getLifecycleInstanceId(), updatedBy, previousAPI.build());
                    }
                }
            }
        }
        if (!requireReSubscriptions) {
            if (StringUtils.isNotEmpty(api.getCopiedFromApiId())) {
                List<Subscription> subscriptions = apiSubscriptionDAO.getAPISubscriptionsByAPI(api.getCopiedFromApiId());
                List<Subscription> subscriptionList = new ArrayList<>();
                for (Subscription subscription : subscriptions) {
                    if (api.getPolicies().contains(subscription.getPolicy())) {
                        if (!APIMgtConstants.SubscriptionStatus.ON_HOLD.equals(subscription.getStatus())) {
                            subscriptionList.add(new Subscription(UUID.randomUUID().toString(), subscription.getApplication(), subscription.getApi(), subscription.getPolicy()));
                        }
                    }
                    apiSubscriptionDAO.copySubscriptions(subscriptionList);
                }
            }
        }
        // publish API state change to gateway
        apiGateway.changeAPIState(originalAPI, status);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Couldn't change the status of api ID " + apiId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (LifecycleException e) {
        String errorMsg = "Couldn't change the status of api ID " + apiId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
    } catch (GatewayException e) {
        String message = "Error occurred while changing the state of api ID: " + apiId + " to " + status + "in gateway";
        log.error(message, e);
        throw new APIManagementException(message, ExceptionCodes.GATEWAY_EXCEPTION);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) LifecycleException(org.wso2.carbon.lcm.core.exception.LifecycleException) ArrayList(java.util.ArrayList) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) CheckItemBean(org.wso2.carbon.lcm.core.beans.CheckItemBean) Iterator(java.util.Iterator) API(org.wso2.carbon.apimgt.core.models.API) Subscription(org.wso2.carbon.apimgt.core.models.Subscription)

Example 87 with Subscription

use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.

the class SubscriptionDeletionWorkflow method completeWorkflow.

public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
    if (subscription == null) {
        // this is when complete method is executed through workflow rest api
        this.subscription = apiSubscriptionDAO.getAPISubscription(getWorkflowReference());
    }
    WorkflowResponse response = workflowExecutor.complete(this);
    setStatus(response.getWorkflowStatus());
    List<SubscriptionValidationData> subscriptionValidationDataList = null;
    if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("Subscription deletion workflow complete: Approved");
        }
        if (subscription.getApi() != null && subscription.getApplication() != null) {
            subscriptionValidationDataList = apiSubscriptionDAO.getAPISubscriptionsOfAPIForValidation(subscription.getApi().getContext(), subscription.getApi().getVersion(), subscription.getApplication().getId());
        }
        apiSubscriptionDAO.deleteAPISubscription(getWorkflowReference());
    } else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("Subscription deletion workflow complete: Rejected");
        }
    }
    updateWorkflowEntries(this);
    if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
        if (subscriptionValidationDataList != null && !subscriptionValidationDataList.isEmpty()) {
            apiGateway.deleteAPISubscription(subscriptionValidationDataList);
            if (log.isDebugEnabled()) {
                log.debug("Subscription for API : " + subscription.getApi().getName() + " with " + "application : " + subscription.getApplication().getName() + " has been successfully " + "removed from gateway");
            }
        }
    }
    return response;
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) SubscriptionValidationData(org.wso2.carbon.apimgt.core.models.SubscriptionValidationData)

Example 88 with Subscription

use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testUpdateSubscriptionPolicy.

@Test(description = "Update subscription policy")
public void testUpdateSubscriptionPolicy() throws APIManagementException {
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiSubscriptionDAO);
    apiPublisher.updateSubscriptionPolicy(SUB_ID, "test policy");
    Mockito.verify(apiSubscriptionDAO, Mockito.times(1)).updateSubscriptionPolicy(SUB_ID, "test policy");
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) Test(org.testng.annotations.Test)

Example 89 with Subscription

use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testUpdateSubscriptionPolicyException.

@Test(description = "Error when updating subscription policy", expectedExceptions = APIManagementException.class)
public void testUpdateSubscriptionPolicyException() throws APIManagementException {
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiSubscriptionDAO);
    Mockito.doThrow(new APIMgtDAOException("", new Throwable())).when(apiSubscriptionDAO).updateSubscriptionPolicy(SUB_ID, "test policy");
    apiPublisher.updateSubscriptionPolicy(SUB_ID, "test policy");
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) Test(org.testng.annotations.Test)

Example 90 with Subscription

use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testUpdateAPIStatusDeprecatePreviousVersionsAndNotRequireReSubscription.

@Test(description = "Update api status with deprecating previous versions and not require re-subscriptions")
public void testUpdateAPIStatusDeprecatePreviousVersionsAndNotRequireReSubscription() throws APIManagementException, LifecycleException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, applicationDAO, apiSubscriptionDAO, apiLifecycleManager, gatewaySourceGenerator, workflowDAO, gateway);
    API previousApi = SampleTestObjectCreator.createDefaultAPI().build();
    String previousApiUUID = previousApi.getId();
    String lifecycleIdPrevious = previousApi.getLifecycleInstanceId();
    String lcStatePrevious = previousApi.getLifeCycleStatus();
    LifecycleState previousLifecycleState = SampleTestObjectCreator.getMockLifecycleStateObject(lifecycleIdPrevious);
    List<AvailableTransitionBean> list = new ArrayList<>();
    AvailableTransitionBean bean = new AvailableTransitionBean("Deprecate", APIStatus.DEPRECATED.getStatus());
    list.add(bean);
    previousLifecycleState.setAvailableTransitionBeanList(list);
    Mockito.when(apiLifecycleManager.getLifecycleDataForState(lifecycleIdPrevious, lcStatePrevious)).thenReturn(previousLifecycleState);
    Mockito.when(apiLifecycleManager.executeLifecycleEvent(APIStatus.PUBLISHED.getStatus(), APIStatus.DEPRECATED.getStatus(), lifecycleIdPrevious, USER, previousApi)).thenReturn(previousLifecycleState);
    previousLifecycleState.setState(APIStatus.DEPRECATED.getStatus());
    API api = SampleTestObjectCreator.createDefaultAPI().copiedFromApiId(previousApiUUID).build();
    String uuid = api.getId();
    String lcState = api.getLifeCycleStatus();
    String lifecycleId = api.getLifecycleInstanceId();
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
    LifecycleState lifecycleState = SampleTestObjectCreator.getMockLifecycleStateObject(lifecycleId);
    Mockito.when(apiLifecycleManager.getLifecycleDataForState(lifecycleId, lcState)).thenReturn(lifecycleState);
    Mockito.when(apiLifecycleManager.executeLifecycleEvent(APIStatus.CREATED.getStatus(), APIStatus.PUBLISHED.getStatus(), lifecycleId, USER, api)).thenReturn(lifecycleState);
    lifecycleState.setState(APIStatus.PUBLISHED.getStatus());
    API.APIBuilder apiBuilder = new API.APIBuilder(api);
    apiBuilder.lifecycleState(lifecycleState);
    apiBuilder.updatedBy(USER);
    api = apiBuilder.build();
    Mockito.when(apiDAO.getAPI(previousApiUUID)).thenReturn(previousApi);
    Map<String, Boolean> checklist = new HashMap<>();
    checklist.put(APIMgtConstants.DEPRECATE_PREVIOUS_VERSIONS, true);
    Application application = SampleTestObjectCreator.createDefaultApplication();
    List<Subscription> subscriptions = new ArrayList<>();
    Subscription subscription = new Subscription(previousApiUUID, application, previousApi, new SubscriptionPolicy(TIER));
    subscriptions.add(subscription);
    Mockito.when(apiSubscriptionDAO.getAPISubscriptionsByAPI(previousApiUUID)).thenReturn(subscriptions);
    apiPublisher.updateAPIStatus(uuid, APIStatus.PUBLISHED.getStatus(), checklist);
    Mockito.verify(apiLifecycleManager, Mockito.times(1)).executeLifecycleEvent(APIStatus.CREATED.getStatus(), APIStatus.PUBLISHED.getStatus(), lifecycleId, USER, api);
}
Also used : AvailableTransitionBean(org.wso2.carbon.lcm.core.beans.AvailableTransitionBean) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)58 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)58 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)37 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)35 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)34 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)34 Application (org.wso2.carbon.apimgt.core.models.Application)30 API (org.wso2.carbon.apimgt.core.models.API)28 ArrayList (java.util.ArrayList)27 Test (org.junit.Test)22 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)22 Response (javax.ws.rs.core.Response)21 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)21 SQLException (java.sql.SQLException)20 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)18 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)17 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)15 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)15 Connection (java.sql.Connection)14 PreparedStatement (java.sql.PreparedStatement)14