Search in sources :

Example 81 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.

the class ThreatProtectionDAOImplIT method testPolicyExists.

@Test
public void testPolicyExists() throws Exception {
    ThreatProtectionPolicy policy = SampleTestObjectCreator.createUniqueThreatProtectionPolicy();
    ThreatProtectionDAO dao = DAOFactory.getThreatProtectionDAO();
    dao.addPolicy(policy);
    Assert.assertEquals(dao.isPolicyExists(policy.getUuid()), true);
    ThreatProtectionPolicy policy2 = SampleTestObjectCreator.createUniqueThreatProtectionPolicy();
    Assert.assertEquals(dao.isPolicyExists(policy2.getUuid()), false);
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) ThreatProtectionDAO(org.wso2.carbon.apimgt.core.dao.ThreatProtectionDAO) Test(org.testng.annotations.Test)

Example 82 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.

the class ThreatProtectionDAOImplIT method testAddPolicy.

@Test
public void testAddPolicy() throws Exception {
    ThreatProtectionPolicy policy = SampleTestObjectCreator.createUniqueThreatProtectionPolicy();
    ThreatProtectionDAO dao = DAOFactory.getThreatProtectionDAO();
    dao.addPolicy(policy);
    ThreatProtectionPolicy fromDb = dao.getPolicy(policy.getUuid());
    Assert.assertEquals(fromDb.getUuid(), policy.getUuid());
    Assert.assertEquals(fromDb.getName(), policy.getName());
    Assert.assertEquals(fromDb.getType(), policy.getType());
    Assert.assertEquals(fromDb.getPolicy(), policy.getPolicy());
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) ThreatProtectionDAO(org.wso2.carbon.apimgt.core.dao.ThreatProtectionDAO) Test(org.testng.annotations.Test)

Example 83 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testAddApplicationNullPolicy.

@Test(description = "Add an application with null policy", expectedExceptions = APIManagementException.class)
public void testAddApplicationNullPolicy() throws Exception {
    ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    APIStore apiStore = getApiStoreImpl(applicationDAO, policyDAO, workflowDAO);
    Application application = new Application(APP_NAME, USER_NAME);
    application.setPolicy(new ApplicationPolicy(TIER));
    Mockito.when(applicationDAO.isApplicationNameExists(APP_NAME)).thenReturn(false);
    Mockito.when(policyDAO.getPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, TIER)).thenReturn(null);
    apiStore.addApplication(application);
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) 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 84 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testGetAPIWithNotAllowedLifecycleStates.

@Test(description = "Test get API with not allowed lifecycle states (Created, Maintenance, Retired)")
public void testGetAPIWithNotAllowedLifecycleStates() throws APIManagementException {
    String exceptionShouldThrowError = "Exception should be thrown when accessing an API in %s state";
    String matchedErrorMessage = "Attempt to access an API which is in a restricted state";
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    Policy policy = new SubscriptionPolicy(UUID, TIER);
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, TIER)).thenReturn(policy);
    APIStore apiStore = getApiStoreImpl(apiDAO, Mockito.mock(ApplicationDAO.class), Mockito.mock(APISubscriptionDAO.class), Mockito.mock(WorkflowDAO.class), Mockito.mock(APIGateway.class), policyDAO);
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
    String apiId = apiBuilder.getId();
    List<String> notAllowedStates = new ArrayList<String>() {

        {
            add(APIStatus.CREATED.getStatus());
            add(APIStatus.MAINTENANCE.getStatus());
            add(APIStatus.RETIRED.getStatus());
        }
    };
    for (String notAllowedState : notAllowedStates) {
        apiBuilder.lifeCycleStatus(notAllowedState);
        API api = apiBuilder.build();
        Mockito.when(apiDAO.getAPI(apiId)).thenReturn(api);
        try {
            apiStore.getAPIbyUUID(apiId);
            Assert.fail(exceptionShouldThrowError);
        } catch (APIManagementException e) {
            Assert.assertTrue(e.getMessage().contains(matchedErrorMessage));
        }
    }
}
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) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) ArrayList(java.util.ArrayList) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) 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 85 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testGetPolicy.

@Test(description = "Get policy given policy name and policy level")
public void testGetPolicy() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIStore apiStore = getApiStoreImpl(policyDAO);
    apiStore.getPolicy(APIMgtAdminService.PolicyLevel.application, POLICY_NAME);
    Mockito.verify(policyDAO, Mockito.times(1)).getPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, POLICY_NAME);
}
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)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)114 Test (org.testng.annotations.Test)106 ArrayList (java.util.ArrayList)96 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)79 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)73 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)73 Test (org.junit.Test)72 PreparedStatement (java.sql.PreparedStatement)68 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)64 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)63 SQLException (java.sql.SQLException)62 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)62 Connection (java.sql.Connection)58 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)58 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)54 ResultSet (java.sql.ResultSet)49 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)43 HashMap (java.util.HashMap)40 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)40 CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)40