Search in sources :

Example 46 with LifeCycle

use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testDeleteApiWithZeroSubscriptionsException.

@Test(description = "Error occurred while deleting API with zero subscriptions")
public void testDeleteApiWithZeroSubscriptionsException() throws APIManagementException, LifecycleException, SQLException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    String uuid = api.getId();
    Mockito.when(apiSubscriptionDAO.getSubscriptionCountByAPI(uuid)).thenReturn(0L);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    LabelDAO labelDao = Mockito.mock(LabelDAO.class);
    KeyManager keyManager = Mockito.mock(KeyManager.class);
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(USER, identityProvider, keyManager, apiDAO, null, apiSubscriptionDAO, null, apiLifecycleManager, labelDao, workflowDAO, null, null, null, gateway);
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
    // LifeCycleException
    Mockito.doThrow(LifecycleException.class).when(apiLifecycleManager).removeLifecycle(api.getLifecycleInstanceId());
    Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
    try {
        apiPublisher.deleteAPI(uuid);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while Disassociating the API with Lifecycle id " + uuid);
    }
    // ApiDAOException
    Mockito.doThrow(APIMgtDAOException.class).when(apiDAO).deleteAPI(uuid);
    try {
        apiPublisher.deleteAPI(uuid);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while deleting the API with id " + uuid);
    }
    // GatewayException
    Mockito.doThrow(GatewayException.class).when(gateway).deleteAPI(api);
    try {
        apiPublisher.deleteAPI(uuid);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while deleting API with id - " + uuid + " from gateway");
    }
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) KeyManager(org.wso2.carbon.apimgt.core.api.KeyManager) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 47 with LifeCycle

use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testGetAPILifeCycleData.

@Test(description = "Get api lifecycle data")
public void testGetAPILifeCycleData() throws APIManagementException, LifecycleException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gateway);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    String uuid = api.getId();
    String lifecycleId = api.getLifecycleInstanceId();
    String lcState = api.getLifeCycleStatus();
    LifecycleState bean = new LifecycleState();
    bean.setState(APIStatus.CREATED.getStatus());
    Mockito.when(apiDAO.getAPISummary(uuid)).thenReturn(api);
    Mockito.doReturn(bean).when(apiLifecycleManager).getLifecycleDataForState(lifecycleId, lcState);
    apiPublisher.getAPILifeCycleData(uuid);
}
Also used : APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) API(org.wso2.carbon.apimgt.core.models.API) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 48 with LifeCycle

use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.

the class ApiDAOImpl method changeLifeCycleStatus.

/**
 * Change the lifecycle status of a given API
 *
 * @param apiID  The UUID of the respective API
 * @param status The lifecycle status that the API must be set to
 * @throws APIMgtDAOException if error occurs while accessing data layer
 */
@Override
public void changeLifeCycleStatus(String apiID, String status) throws APIMgtDAOException {
    final String query = "UPDATE AM_API SET CURRENT_LC_STATUS = ?, LAST_UPDATED_TIME = ? WHERE UUID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            connection.setAutoCommit(false);
            statement.setString(1, status);
            statement.setTimestamp(2, Timestamp.valueOf(LocalDateTime.now()));
            statement.setString(3, apiID);
            statement.execute();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String msg = "changing Life Cycle Status for API: " + apiID + " to Status: " + status;
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String msg = "changing Life Cycle Status for API: " + apiID + " to Status: " + status;
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 49 with LifeCycle

use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.

the class APIPublisherImpl method updateCheckListItem.

/**
 * This method used to Update the lifecycle checklist of API
 *
 * @param apiId            UUID of the API
 * @param status           Current API lifecycle status.
 * @param checkListItemMap Check list item values.
 * @throws APIManagementException If failed to update checklist item values.
 */
@Override
public void updateCheckListItem(String apiId, String status, Map<String, Boolean> checkListItemMap) throws APIManagementException {
    API api = getApiDAO().getAPI(apiId);
    try {
        API.APIBuilder apiBuilder = new API.APIBuilder(api);
        apiBuilder.lastUpdatedTime(LocalDateTime.now());
        apiBuilder.updatedBy(getUsername());
        apiBuilder.lifecycleState(getApiLifecycleManager().getLifecycleDataForState(apiBuilder.getLifecycleInstanceId(), apiBuilder.getLifeCycleStatus()));
        for (Map.Entry<String, Boolean> checkListItem : checkListItemMap.entrySet()) {
            getApiLifecycleManager().checkListItemEvent(api.getLifecycleInstanceId(), api.getLifeCycleStatus(), checkListItem.getKey(), checkListItem.getValue());
        }
    } catch (LifecycleException e) {
        String errorMsg = "Couldn't get the lifecycle status of api ID " + apiId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
    }
}
Also used : LifecycleException(org.wso2.carbon.lcm.core.exception.LifecycleException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) Map(java.util.Map) HashMap(java.util.HashMap)

Example 50 with LifeCycle

use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.

the class ApiDAOImplIT method addAPIWithGivenData.

/**
 * This method adds an API with given information
 *
 * @param apiName                API name
 * @param apiVersion             API version
 * @param apiContext             API context
 * @param apiProvider            API provider
 * @param apiVisibility          API visibility
 * @param visibleRoles           roles that are eligible to consume the API
 * @param initialLifecycleStatus initial lifecycle status
 * @param description            API description
 * @param tags                   tag list for the API
 * @param uriTemplates           URI templates, i.e - resources
 * @param finalLifecycleStatus   final lifecycle status
 * @throws APIMgtDAOException if it fails to add the API
 */
private void addAPIWithGivenData(String apiName, String apiVersion, String apiContext, String apiProvider, API.Visibility apiVisibility, Set<String> visibleRoles, String initialLifecycleStatus, String description, Set<String> tags, Map<String, UriTemplate> uriTemplates, String finalLifecycleStatus) throws APIMgtDAOException {
    API.APIBuilder builder;
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    builder = SampleTestObjectCreator.createCustomAPI(apiName, apiVersion, apiContext);
    builder.provider(apiProvider);
    builder.createdBy(apiProvider);
    builder.visibility(apiVisibility);
    // visible roles should be added for restricted APIs
    if (apiVisibility != null && API.Visibility.RESTRICTED.toString().equalsIgnoreCase(apiVisibility.toString())) {
        builder.visibleRoles(visibleRoles);
    }
    builder.lifeCycleStatus(initialLifecycleStatus);
    builder.description(description);
    builder.tags(tags);
    builder.uriTemplates(uriTemplates);
    builder.endpoint(Collections.emptyMap());
    API api = builder.build();
    apiDAO.addAPI(api);
    apiDAO.changeLifeCycleStatus(api.getId(), finalLifecycleStatus);
}
Also used : CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO)

Aggregations

Test (org.testng.annotations.Test)20 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)20 API (org.wso2.carbon.apimgt.core.models.API)20 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)19 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)11 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)11 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)11 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)10 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)10 LifecycleException (org.wso2.carbon.lcm.core.exception.LifecycleException)10 JSONObject (org.json.simple.JSONObject)8 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)8 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)7 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)7 List (java.util.List)6 IOException (java.io.IOException)5 Map (java.util.Map)5 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)4