Search in sources :

Example 6 with SubscriptionList

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionList in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImplTestCase method testSubscriptionsGetfromApplication.

@Test
public void testSubscriptionsGetfromApplication() throws APIManagementException, NotFoundException {
    TestUtil.printTestMethodName();
    String applicationId = UUID.randomUUID().toString();
    String subsID1 = UUID.randomUUID().toString();
    String subsID2 = UUID.randomUUID().toString();
    SubscriptionsApiServiceImpl subscriptionsApiService = new SubscriptionsApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
    Request request = TestUtil.getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    Application application = SampleTestObjectCreator.createDefaultApplication();
    List<Subscription> subscriptionList = new ArrayList<>();
    subscriptionList.add(SampleTestObjectCreator.createSubscription(subsID1));
    subscriptionList.add(SampleTestObjectCreator.createSubscription(subsID2));
    Mockito.when(apiStore.getApplicationByUuid(applicationId)).thenReturn(application);
    Mockito.when(apiStore.getAPISubscriptionsByApplication(application, ApiType.STANDARD)).thenReturn(subscriptionList);
    Response response = subscriptionsApiService.subscriptionsGet(null, applicationId, ApiType.STANDARD.toString(), 0, 10, null, request);
    Assert.assertEquals(200, response.getStatus());
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) ArrayList(java.util.ArrayList) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with SubscriptionList

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionList in project carbon-apimgt by wso2.

the class SubscriptionDataLoaderImpl method getSubscriptionById.

@Override
public Subscription getSubscriptionById(String apiId, String appId) throws DataLoadingException {
    String endPoint = APIConstants.SubscriptionValidationResources.SUBSCRIPTIONS + "?apiId=" + apiId + "&appId=" + appId;
    Subscription subscription = null;
    String responseString;
    try {
        responseString = invokeService(endPoint, null);
    } catch (IOException e) {
        String msg = "Error while executing the http client " + endPoint;
        log.error(msg, e);
        throw new DataLoadingException(msg, e);
    }
    if (responseString != null && !responseString.isEmpty()) {
        SubscriptionList list = new Gson().fromJson(responseString, SubscriptionList.class);
        if (list.getList() != null && !list.getList().isEmpty()) {
            subscription = list.getList().get(0);
        }
    }
    return subscription;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) SubscriptionList(org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionList) Gson(com.google.gson.Gson) IOException(java.io.IOException) Subscription(org.wso2.carbon.apimgt.keymgt.model.entity.Subscription)

Example 8 with SubscriptionList

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionList in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method subscriptionsGet.

@Override
public Response subscriptionsGet(String xWSO2Tenant, Integer apiId, Integer appId, String apiUUID, String applicationUUID, MessageContext messageContext) throws APIManagementException {
    Response result;
    SubscriptionValidationDAO subscriptionValidationDAO = new SubscriptionValidationDAO();
    List<Subscription> subscriptionList = new ArrayList<>();
    xWSO2Tenant = SubscriptionValidationDataUtil.validateTenantDomain(xWSO2Tenant, messageContext);
    if (StringUtils.isNotEmpty(applicationUUID) && StringUtils.isNotEmpty(apiUUID)) {
        Subscription subscription = subscriptionValidationDAO.getSubscription(apiUUID, applicationUUID);
        if (subscription != null) {
            subscriptionList.add(subscription);
        }
        result = Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionToSubscriptionListDTO(subscriptionList)).build();
    } else if (apiId != null && appId != null) {
        Subscription subscription = subscriptionValidationDAO.getSubscription(apiId, appId);
        if (subscription != null) {
            subscriptionList.add(subscription);
        }
        result = Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionToSubscriptionListDTO(subscriptionList)).build();
    } else if (StringUtils.isNotEmpty(xWSO2Tenant)) {
        result = Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionToSubscriptionListDTO(subscriptionValidationDAO.getAllSubscriptions(xWSO2Tenant))).build();
    } else {
        result = Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionToSubscriptionListDTO(subscriptionValidationDAO.getAllSubscriptions())).build();
    }
    return result;
}
Also used : Response(javax.ws.rs.core.Response) ArrayList(java.util.ArrayList) Subscription(org.wso2.carbon.apimgt.api.model.subscription.Subscription) SubscriptionValidationDAO(org.wso2.carbon.apimgt.impl.dao.SubscriptionValidationDAO)

Example 9 with SubscriptionList

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionList 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 10 with SubscriptionList

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionList in project carbon-apimgt by wso2.

the class APISubscriptionDAOImpl method createSubscriptionsWithApiInformationOnly.

private List<Subscription> createSubscriptionsWithApiInformationOnly(ResultSet rs) throws APIMgtDAOException {
    List<Subscription> subscriptionList = new ArrayList<>();
    try {
        Subscription subscription;
        while (rs.next()) {
            String subscriptionId = rs.getString("SUBS_UUID");
            String subscriptionTier = rs.getString("SUBS_POLICY");
            API.APIBuilder apiBuilder = new API.APIBuilder(rs.getString("API_PROVIDER"), rs.getString("API_NAME"), rs.getString("API_VERSION"));
            apiBuilder.id(rs.getString("API_ID"));
            apiBuilder.context(rs.getString("API_CONTEXT"));
            API api = apiBuilder.build();
            subscription = new Subscription(subscriptionId, null, api, new SubscriptionPolicy(subscriptionTier));
            subscription.setStatus(APIMgtConstants.SubscriptionStatus.valueOf(rs.getString("SUB_STATUS")));
            subscriptionList.add(subscription);
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "creating subscriptions api information", e);
    }
    return subscriptionList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) API(org.wso2.carbon.apimgt.core.models.API) Subscription(org.wso2.carbon.apimgt.core.models.Subscription)

Aggregations

ArrayList (java.util.ArrayList)10 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)10 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)4 SQLException (java.sql.SQLException)3 Response (javax.ws.rs.core.Response)3 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)2 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 API (org.wso2.carbon.apimgt.core.models.API)2 Application (org.wso2.carbon.apimgt.core.models.Application)2 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)2 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)2 SubscriptionListDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionListDTO)2 Request (org.wso2.msf4j.Request)2 Gson (com.google.gson.Gson)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1