Search in sources :

Example 41 with LifecycleException

use of org.wso2.carbon.lcm.core.exception.LifecycleException in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testUpdateAPIStatusWhileAPINotAvailable.

@Test(description = "Update api status", expectedExceptions = { APIManagementException.class })
public void testUpdateAPIStatusWhileAPINotAvailable() throws APIManagementException, LifecycleException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    String uuid = api.getId();
    String lifecycleId = api.getLifecycleInstanceId();
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(null);
    Mockito.when(apiLifecycleManager.executeLifecycleEvent(APIStatus.CREATED.getStatus(), APIStatus.PUBLISHED.getStatus(), uuid, USER, api)).thenReturn(SampleTestObjectCreator.getMockLifecycleStateObject(lifecycleId));
    Mockito.doThrow(new APIMgtDAOException("Couldn't change the status of api ID " + uuid)).when(apiDAO).changeLifeCycleStatus(uuid, APIStatus.PUBLISHED.getStatus());
    APIGateway gateway = Mockito.mock(APIGateway.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gateway);
    apiPublisher.updateAPIStatus(uuid, APIStatus.PUBLISHED.getStatus(), Collections.emptyMap());
}
Also used : APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 42 with LifecycleException

use of org.wso2.carbon.lcm.core.exception.LifecycleException in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testGetLifeCycleEventsExceptionFindingApiSummaryResource.

@Test(description = "Exception finding APISummary Resource when getting lifecycle events list of an API", expectedExceptions = APIManagementException.class)
public void testGetLifeCycleEventsExceptionFindingApiSummaryResource() throws APIManagementException, LifecycleException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    Mockito.when(apiDAO.getAPISummary(API_ID)).thenThrow(new APIMgtDAOException("Couldn't find APISummary Resource for ID " + API_ID));
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO);
    apiPublisher.getLifeCycleEvents(API_ID);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 43 with LifecycleException

use of org.wso2.carbon.lcm.core.exception.LifecycleException in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testGetAPILifeCycleDataForNullAPI.

@Test(description = "Get api lifecycle data for a null api", expectedExceptions = APIManagementException.class)
public void testGetAPILifeCycleDataForNullAPI() throws APIManagementException, LifecycleException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO);
    Mockito.when(apiDAO.getAPISummary(API_ID)).thenThrow(new APIMgtDAOException("API with ID " + API_ID + " does not exist", ExceptionCodes.API_NOT_FOUND));
    apiPublisher.getAPILifeCycleData(API_ID);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 44 with LifecycleException

use of org.wso2.carbon.lcm.core.exception.LifecycleException 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 45 with LifecycleException

use of org.wso2.carbon.lcm.core.exception.LifecycleException in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testAddApiFromDefinitionFromUrlConnection.

@Test(description = "Add api from definition using httpUrlConnection")
public void testAddApiFromDefinitionFromUrlConnection() throws APIManagementException, LifecycleException, IOException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    LabelDAO labelDao = Mockito.mock(LabelDAO.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    HttpURLConnection httpURLConnection = Mockito.mock(HttpURLConnection.class);
    Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    KeyManager keyManager = Mockito.mock(KeyManager.class);
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
    APIPublisherImpl apiPublisher = getApiPublisherImpl(null, keyManager, apiDAO, null, null, policyDAO, apiLifecycleManager, labelDao, null, null, null, gatewaySourceGenerator, gateway);
    String def = SampleTestObjectCreator.apiDefinition;
    InputStream apiDefinition = new ByteArrayInputStream(def.getBytes());
    Mockito.when(httpURLConnection.getInputStream()).thenReturn(apiDefinition);
    Mockito.when(httpURLConnection.getResponseCode()).thenReturn(200);
    apiPublisher.addApiFromDefinition(httpURLConnection);
    Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) HttpURLConnection(java.net.HttpURLConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) KeyManager(org.wso2.carbon.apimgt.core.api.KeyManager) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Aggregations

ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)59 Test (org.testng.annotations.Test)58 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)56 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)55 API (org.wso2.carbon.apimgt.core.models.API)54 LifecycleState (org.wso2.carbon.lcm.core.impl.LifecycleState)32 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)31 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)21 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)20 HashMap (java.util.HashMap)17 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)15 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)14 LifecycleException (org.wso2.carbon.lcm.core.exception.LifecycleException)14 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)13 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)13 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)11 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)11 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)11 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)10 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)8