Search in sources :

Example 1 with PolicyDAO

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

the class DAOFactory method getPolicyDAO.

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

Example 2 with PolicyDAO

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

the class ThrottlerUtil method addDefaultAdvancedThrottlePolicies.

/**
 * Deploy default throttle polices at startup
 *
 * @throws APIManagementException throws if any exception occured
 */
public static void addDefaultAdvancedThrottlePolicies() throws APIManagementException {
    int[] requestCount = new int[] { 50, 20, 10, Integer.MAX_VALUE };
    // Adding application level throttle policies
    String[] appPolicies = new String[] { ThrottleConstants.DEFAULT_APP_POLICY_FIFTY_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_TWENTY_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_TEN_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_UNLIMITED };
    String[] appPolicyDecs = new String[] { ThrottleConstants.DEFAULT_APP_POLICY_LARGE_DESC, ThrottleConstants.DEFAULT_APP_POLICY_MEDIUM_DESC, ThrottleConstants.DEFAULT_APP_POLICY_SMALL_DESC, ThrottleConstants.DEFAULT_APP_POLICY_UNLIMITED_DESC };
    PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
    String policyName;
    // Add application level throttle policies
    for (int i = 0; i < appPolicies.length; i++) {
        policyName = appPolicies[i];
        if (!isPolicyExist(APIMgtAdminService.PolicyLevel.application, policyName)) {
            ApplicationPolicy applicationPolicy = new ApplicationPolicy(policyName);
            applicationPolicy.setUuid(UUID.randomUUID().toString());
            applicationPolicy.setDisplayName(policyName);
            applicationPolicy.setDescription(appPolicyDecs[i]);
            applicationPolicy.setDeployed(true);
            QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
            RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCount[i]);
            defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
            defaultQuotaPolicy.setLimit(requestCountLimit);
            applicationPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
            policyDAO.addApplicationPolicy(applicationPolicy);
        }
    }
    // Adding Subscription level policies
    int[] requestCountSubPolicies = new int[] { 5000, 2000, 1000, 500, Integer.MAX_VALUE };
    String[] subPolicies = new String[] { ThrottleConstants.DEFAULT_SUB_POLICY_GOLD, ThrottleConstants.DEFAULT_SUB_POLICY_SILVER, ThrottleConstants.DEFAULT_SUB_POLICY_BRONZE, ThrottleConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED, ThrottleConstants.DEFAULT_SUB_POLICY_UNLIMITED };
    String[] subPolicyDecs = new String[] { ThrottleConstants.DEFAULT_SUB_POLICY_GOLD_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_SILVER_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_BRONZE_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_UNLIMITED_DESC };
    for (int i = 0; i < subPolicies.length; i++) {
        policyName = subPolicies[i];
        if (!isPolicyExist(APIMgtAdminService.PolicyLevel.subscription, policyName)) {
            SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(policyName);
            subscriptionPolicy.setUuid(UUID.randomUUID().toString());
            subscriptionPolicy.setDisplayName(policyName);
            subscriptionPolicy.setDescription(subPolicyDecs[i]);
            subscriptionPolicy.setDeployed(true);
            QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
            RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCountSubPolicies[i]);
            defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
            defaultQuotaPolicy.setLimit(requestCountLimit);
            subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
            subscriptionPolicy.setStopOnQuotaReach(true);
            subscriptionPolicy.setBillingPlan(ThrottleConstants.BILLING_PLAN_FREE);
            policyDAO.addSubscriptionPolicy(subscriptionPolicy);
        }
    }
    // Adding Resource level policies
    String[] apiPolicies = new String[] { ThrottleConstants.DEFAULT_API_POLICY_FIFTY_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_TWENTY_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_TEN_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_UNLIMITED };
    String[] apiPolicyDecs = new String[] { ThrottleConstants.DEFAULT_API_POLICY_ULTIMATE_DESC, ThrottleConstants.DEFAULT_API_POLICY_PLUS_DESC, ThrottleConstants.DEFAULT_API_POLICY_BASIC_DESC, ThrottleConstants.DEFAULT_API_POLICY_UNLIMITED_DESC };
    int[] requestCountApiPolicies = new int[] { 50000, 20000, 10000, Integer.MAX_VALUE };
    for (int i = 0; i < apiPolicies.length; i++) {
        policyName = apiPolicies[i];
        if (!isPolicyExist(APIMgtAdminService.PolicyLevel.api, policyName)) {
            APIPolicy apiPolicy = new APIPolicy(policyName);
            apiPolicy.setUuid(UUID.randomUUID().toString());
            apiPolicy.setDisplayName(policyName);
            apiPolicy.setDescription(apiPolicyDecs[i]);
            apiPolicy.setUserLevel(ThrottleConstants.API_POLICY_API_LEVEL);
            apiPolicy.setDeployed(true);
            QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
            RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCountApiPolicies[i]);
            defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
            defaultQuotaPolicy.setLimit(requestCountLimit);
            apiPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
            policyDAO.addApiPolicy(apiPolicy);
        }
    }
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO)

Example 3 with PolicyDAO

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

the class APIPublisherImplTestCase method testUpdateDescription.

@Test(description = "Test add api with production endpoint")
public void testUpdateDescription() throws APIManagementException {
    /**
     * this test method verify the API Add with correct API object get invoked correctly
     */
    Map<String, Endpoint> endpointMap = SampleTestObjectCreator.getMockEndpointMap();
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id(UUID.randomUUID().toString()).endpoint(endpointMap);
    apiBuilder.apiPermission("");
    apiBuilder.permissionMap(null);
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    Mockito.when(apiDAO.getAPI(apiBuilder.getId())).thenReturn(apiBuilder.build());
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, GOLD_TIER)).thenReturn(new SubscriptionPolicy(GOLD_TIER));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, SILVER_TIER)).thenReturn(new SubscriptionPolicy(SILVER_TIER));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, BRONZE_TIER)).thenReturn(new SubscriptionPolicy(BRONZE_TIER));
    APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO);
    Mockito.when(apiDAO.getEndpoint(endpointMap.get(APIMgtConstants.PRODUCTION_ENDPOINT).getId())).thenReturn(endpointMap.get(APIMgtConstants.PRODUCTION_ENDPOINT));
    Mockito.when(apiDAO.getEndpoint(endpointMap.get(APIMgtConstants.SANDBOX_ENDPOINT).getId())).thenReturn(endpointMap.get(APIMgtConstants.SANDBOX_ENDPOINT));
    Mockito.when(apiDAO.getEndpointByName(endpointMap.get(APIMgtConstants.PRODUCTION_ENDPOINT).getName())).thenReturn(endpointMap.get(APIMgtConstants.PRODUCTION_ENDPOINT));
    Mockito.when(apiDAO.getEndpointByName(endpointMap.get(APIMgtConstants.SANDBOX_ENDPOINT).getName())).thenReturn(endpointMap.get(APIMgtConstants.SANDBOX_ENDPOINT));
    Mockito.when(apiDAO.getApiSwaggerDefinition(apiBuilder.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
    apiBuilder.description("aaaaaa");
    apiPublisher.updateAPI(apiBuilder);
    Mockito.verify(apiDAO, Mockito.times(1)).updateAPI(apiBuilder.getId(), apiBuilder.build());
}
Also used : APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) 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) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 4 with PolicyDAO

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

the class APIPublisherImplTestCase method testAddApiWithRestrictedVisibility.

@Test(description = "Test add api with restricted visibility")
public void testAddApiWithRestrictedVisibility() throws APIManagementException, LifecycleException {
    Set<String> visibleRoles = new HashSet<>();
    visibleRoles.add(ADMIN_ROLE);
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(SampleTestObjectCreator.getMockEndpointMap()).visibility(API.Visibility.RESTRICTED).visibleRoles(visibleRoles);
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    LabelDAO labelDao = Mockito.mock(LabelDAO.class);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
    APIGateway gateway = Mockito.mock(APIGateway.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, GOLD_TIER)).thenReturn(new SubscriptionPolicy(GOLD_TIER));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, SILVER_TIER)).thenReturn(new SubscriptionPolicy(SILVER_TIER));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, BRONZE_TIER)).thenReturn(new SubscriptionPolicy(BRONZE_TIER));
    APIPublisherImpl apiPublisher = getApiPublisherImpl(null, apiDAO, null, null, policyDAO, apiLifecycleManager, labelDao, null, null, null, gatewaySourceGenerator, gateway);
    String endpointId = apiBuilder.getEndpoint().get("production").getId();
    Endpoint endpoint = new Endpoint.Builder().id(endpointId).name("testEndpoint").build();
    Mockito.when(apiDAO.getEndpoint(endpointId)).thenReturn(endpoint);
    apiPublisher.addAPI(apiBuilder);
    Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
    Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
Also used : APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) 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) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) 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) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 5 with PolicyDAO

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

the class APIPublisherImplTestCase method testUpdateAPIWithStatusUnchangedWhenContextExists.

@Test(description = "Test UpdateAPI with Status unchanged but context exist", expectedExceptions = APIManagementException.class)
public void testUpdateAPIWithStatusUnchangedWhenContextExists() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    API.APIBuilder api = SampleTestObjectCreator.createDefaultAPI();
    String uuid = api.getId();
    APIGateway gateway = Mockito.mock(APIGateway.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    Policy apiPolicy = new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY);
    apiPolicy.setUuid(UUID.randomUUID().toString());
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(apiPolicy);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO);
    Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api.build());
    Mockito.when(apiDAO.isAPIContextExists("testContext")).thenReturn(true);
    Mockito.when(identityProvider.getRoleId(ADMIN_ROLE)).thenReturn(ADMIN_ROLE_ID);
    Mockito.when(identityProvider.getRoleId(DEVELOPER_ROLE)).thenReturn(DEVELOPER_ROLE_ID);
    apiPublisher.updateAPI(api.context("testContext"));
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) API(org.wso2.carbon.apimgt.core.models.API) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) Test(org.testng.annotations.Test)

Aggregations

PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)106 Test (org.testng.annotations.Test)102 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)50 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)50 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)45 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)36 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)33 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)30 API (org.wso2.carbon.apimgt.core.models.API)28 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)27 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)24 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)24 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)23 BeforeTest (org.testng.annotations.BeforeTest)20 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)20 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)17 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)16 Application (org.wso2.carbon.apimgt.core.models.Application)15 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)14 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)13