Search in sources :

Example 91 with Policy

use of org.wso2.carbon.apimgt.core.models.policy.Policy in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testAddApplicationPermissionStringNull.

@Test(description = "Add an application with null permission String")
public void testAddApplicationPermissionStringNull() throws APIManagementException {
    ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    Policy policy = Mockito.mock(Policy.class);
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIStore apiStore = getApiStoreImpl(applicationDAO, policyDAO, workflowDAO, apiGateway);
    Application application = new Application(APP_NAME, USER_NAME);
    application.setPolicy(new ApplicationPolicy(TIER));
    application.setPermissionString(null);
    Mockito.when(applicationDAO.isApplicationNameExists(APP_NAME)).thenReturn(false);
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, TIER)).thenReturn(policy);
    ApplicationCreationResponse applicationResponse = apiStore.addApplication(application);
    String applicationUuid = applicationResponse.getApplicationUUID();
    Assert.assertNotNull(applicationUuid);
    Mockito.verify(applicationDAO, Mockito.times(1)).addApplication(application);
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) Application(org.wso2.carbon.apimgt.core.models.Application) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 92 with Policy

use of org.wso2.carbon.apimgt.core.models.policy.Policy in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testGetPoliciesException.

@Test(description = "Exception when getting all policies of a specific policy level", expectedExceptions = APIManagementException.class)
public void testGetPoliciesException() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIStore apiStore = getApiStoreImpl(policyDAO);
    Mockito.when(policyDAO.getPoliciesByLevel(APIMgtAdminService.PolicyLevel.application)).thenThrow(new APIMgtDAOException("Error occurred while retrieving policies for policy level - " + APPLICATION_POLICY_LEVEL, new SQLException()));
    apiStore.getPolicies(APIMgtAdminService.PolicyLevel.application);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 93 with Policy

use of org.wso2.carbon.apimgt.core.models.policy.Policy in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testGetPolicies.

@Test(description = "Get all policies of a specific policy level")
public void testGetPolicies() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIStore apiStore = getApiStoreImpl(policyDAO);
    apiStore.getPolicies(APIMgtAdminService.PolicyLevel.application);
    Mockito.verify(policyDAO, Mockito.times(1)).getPoliciesByLevel(APIMgtAdminService.PolicyLevel.application);
}
Also used : PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 94 with Policy

use of org.wso2.carbon.apimgt.core.models.policy.Policy in project carbon-apimgt by wso2.

the class ApplicationThrottlePolicyMappingUtilTest method testFromApplicationPolicyArrayToListDTO.

@Test(description = "Convert from Policy Array to DTO")
public void testFromApplicationPolicyArrayToListDTO() throws Exception {
    List<ApplicationPolicy> appPolicies = new ArrayList<>();
    ApplicationPolicy policy1 = SampleTestObjectCreator.createApplicationPolicyWithRequestLimit("Gold");
    ApplicationPolicy policy2 = SampleTestObjectCreator.createApplicationPolicyWithRequestLimit("Silver");
    appPolicies.add(policy1);
    appPolicies.add(policy2);
    ApplicationThrottlePolicyListDTO listDTO = ApplicationThrottlePolicyMappingUtil.fromApplicationPolicyArrayToListDTO(appPolicies);
    Assert.assertEquals(listDTO.getCount(), (Integer) appPolicies.size());
    Assert.assertNotNull(listDTO.getList().get(0).getPolicyName(), policy1.getPolicyName());
    Assert.assertEquals(listDTO.getList().get(0).getId(), policy1.getUuid());
    Assert.assertEquals(listDTO.getList().get(0).getDefaultLimit().getRequestCountLimit().getRequestCount().intValue(), ((RequestCountLimit) policy1.getDefaultQuotaPolicy().getLimit()).getRequestCount());
    Assert.assertEquals(listDTO.getList().get(0).getDisplayName(), policy1.getDisplayName());
    Assert.assertNotNull(listDTO.getList().get(1).getPolicyName(), policy2.getPolicyName());
    Assert.assertEquals(listDTO.getList().get(1).getId(), policy2.getUuid());
    Assert.assertEquals(listDTO.getList().get(1).getDefaultLimit().getRequestCountLimit().getRequestCount().intValue(), ((RequestCountLimit) policy2.getDefaultQuotaPolicy().getLimit()).getRequestCount());
    Assert.assertEquals(listDTO.getList().get(1).getDisplayName(), policy2.getDisplayName());
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) ArrayList(java.util.ArrayList) ApplicationThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ApplicationThrottlePolicyListDTO) Test(org.testng.annotations.Test)

Example 95 with Policy

use of org.wso2.carbon.apimgt.core.models.policy.Policy in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtilTestCase method fromRequestCountThrottleLimitDtoToQuotaPolicyTest.

@Test(description = "Convert Request Count Throttle Limit DTO to Quota Policy")
public void fromRequestCountThrottleLimitDtoToQuotaPolicyTest() throws Exception {
    ThrottleLimitDTO throttleLimitDTO = new ThrottleLimitDTO();
    throttleLimitDTO.setType(PolicyConstants.REQUEST_COUNT_LIMIT_TYPE);
    RequestCountLimitDTO requestCountLimitDTO = new RequestCountLimitDTO();
    requestCountLimitDTO.setRequestCount(100);
    throttleLimitDTO.setRequestCountLimit(requestCountLimitDTO);
    throttleLimitDTO.setTimeUnit("sec");
    throttleLimitDTO.setUnitTime(1);
    QuotaPolicy policy = CommonThrottleMappingUtil.fromDTOToQuotaPolicy(throttleLimitDTO);
    Assert.assertNotNull(policy);
    RequestCountLimit limit = (RequestCountLimit) policy.getLimit();
    Assert.assertNotNull(limit);
    assertEquals(limit.getRequestCount(), 100);
    assertEquals(limit.getTimeUnit(), "sec");
    assertEquals(limit.getUnitTime(), 1);
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)99 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)79 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)73 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)64 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)63 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)62 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)58 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)54 CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)40 ThreatProtectionPolicy (org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy)36 ArrayList (java.util.ArrayList)35 PreparedStatement (java.sql.PreparedStatement)32 SQLException (java.sql.SQLException)32 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)30 Connection (java.sql.Connection)27 API (org.wso2.carbon.apimgt.core.models.API)27 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)26 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)25 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)24 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)23