Search in sources :

Example 1 with APISubscriptionDAO

use of org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO in project carbon-apimgt by wso2.

the class DAOFactory method getAPISubscriptionDAO.

public static APISubscriptionDAO getAPISubscriptionDAO() throws APIMgtDAOException {
    APISubscriptionDAO apiSubscriptionDAO = null;
    try (Connection connection = DAOUtil.getConnection()) {
        String driverName = connection.getMetaData().getDriverName();
        if (driverName.contains(MYSQL) || driverName.contains(H2)) {
            apiSubscriptionDAO = new APISubscriptionDAOImpl();
        } else if (driverName.contains(DB2)) {
        } else if (driverName.contains(MS_SQL) || driverName.contains(MICROSOFT)) {
            apiSubscriptionDAO = new APISubscriptionDAOImpl();
        } else if (driverName.contains(POSTGRE)) {
            apiSubscriptionDAO = new APISubscriptionDAOImpl();
        } else if (driverName.contains(ORACLE)) {
            apiSubscriptionDAO = new APISubscriptionDAOImpl();
        } else {
            throw new APIMgtDAOException("Unhandled DB driver: " + driverName + " detected", ExceptionCodes.APIM_DAO_EXCEPTION);
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting APISubscriptionDAO", e);
    }
    setup();
    return apiSubscriptionDAO;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 2 with APISubscriptionDAO

use of org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testGetSubscribersOfProviderException.

@Test(description = "Exception when getting subscriptions for a provider's APIs", expectedExceptions = APIManagementException.class)
public void testGetSubscribersOfProviderException() throws APIManagementException {
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiSubscriptionDAO);
    Mockito.when(apiSubscriptionDAO.getAPISubscriptionsForUser(1, 2, USER)).thenThrow(new APIMgtDAOException("Unable to fetch subscriptions APIs of provider " + USER));
    apiPublisher.getSubscribersOfProvider(1, 2, USER);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) Test(org.testng.annotations.Test)

Example 3 with APISubscriptionDAO

use of org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testDeleteApiWhenUserHasNoDeletePermission.

@Test(description = "Delete API when the logged in user has no delete permission for the API")
public void testDeleteApiWhenUserHasNoDeletePermission() throws APIManagementException, LifecycleException, SQLException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
    API api = builder.build();
    String uuid = api.getId();
    Mockito.when(apiSubscriptionDAO.getSubscriptionCountByAPI(uuid)).thenReturn(0L);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    LabelDAO labelDao = Mockito.mock(LabelDAO.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(ALTERNATIVE_USER, identityProvider, apiDAO, apiSubscriptionDAO, apiLifecycleManager, gateway, labelDao);
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
    // Assuming the user role list retrieved from IS is null
    Mockito.when(identityProvider.getIdOfUser(ALTERNATIVE_USER)).thenReturn(USER_ID);
    Mockito.when(identityProvider.getRoleIdsOfUser(USER_ID)).thenReturn(null);
    Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
    try {
        apiPublisher.deleteAPI(uuid);
    } catch (APIManagementException ex) {
        Assert.assertEquals(ex.getMessage(), "The user " + ALTERNATIVE_USER + " does not have permission to delete the api " + api.getName());
    }
}
Also used : APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 4 with APISubscriptionDAO

use of org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testDeleteApiWithZeroSubscriptionsLifeCycleException.

@Test(description = "Error occurred while disassociating the API with Lifecycle when deleting the API", expectedExceptions = APIManagementException.class)
public void testDeleteApiWithZeroSubscriptionsLifeCycleException() 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();
    String lifecycleId = api.getLifecycleInstanceId();
    Mockito.when(apiSubscriptionDAO.getSubscriptionCountByAPI(uuid)).thenReturn(0L);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, apiSubscriptionDAO, apiLifecycleManager);
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
    Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.DEVELOPER_ROLE_ID)).thenReturn(DEVELOPER_ROLE);
    Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
    Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.ADMIN_ROLE_ID)).thenReturn(ADMIN_ROLE);
    Mockito.doThrow(new LifecycleException("Error occurred while Disassociating the API with Lifecycle id " + uuid)).when(apiLifecycleManager).removeLifecycle(lifecycleId);
    apiPublisher.deleteAPI(uuid);
}
Also used : APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) LifecycleException(org.wso2.carbon.lcm.core.exception.LifecycleException) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 5 with APISubscriptionDAO

use of org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testCreateNewAPIVersion.

@Test(description = "Create new  API version with valid APIID")
public void testCreateNewAPIVersion() throws APIManagementException, LifecycleException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    String uuid = api.getId();
    APIGateway gateway = Mockito.mock(APIGateway.class);
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiSubscriptionDAO, apiLifecycleManager, gateway);
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
    Mockito.when(apiSubscriptionDAO.getAPISubscriptionsByAPI(api.getId())).thenReturn(new ArrayList<>());
    Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
    String newUUid = apiPublisher.createNewAPIVersion(uuid, "2.0.0");
    Mockito.verify(apiDAO, Mockito.times(1)).getAPI(uuid);
    Mockito.verify(apiDAO, Mockito.times(0)).addAPI(api);
    Assert.assertNotEquals(uuid, newUUid);
}
Also used : APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) 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)

Aggregations

APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)59 Test (org.testng.annotations.Test)54 API (org.wso2.carbon.apimgt.core.models.API)30 Application (org.wso2.carbon.apimgt.core.models.Application)24 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)22 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)18 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)18 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)18 BeforeTest (org.testng.annotations.BeforeTest)14 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)14 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)14 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)12 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)11 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)10 SQLException (java.sql.SQLException)8 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)8 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)8 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)6 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)5 SubscriptionValidationData (org.wso2.carbon.apimgt.core.models.SubscriptionValidationData)5