Search in sources :

Example 86 with APIPolicy

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

the class APIPublisherImplTestCase method testUpdateAPIWithContextChange.

@Test(description = "Test UpdateAPI with context changed")
public void testUpdateAPIWithContextChange() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.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);
    API.APIBuilder api = SampleTestObjectCreator.createDefaultAPI();
    String uuid = api.getId();
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api.build());
    Mockito.when(identityProvider.getRoleId(ADMIN_ROLE)).thenReturn(ADMIN_ROLE_ID);
    Mockito.when(identityProvider.getRoleId(DEVELOPER_ROLE)).thenReturn(DEVELOPER_ROLE_ID);
    Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
    apiPublisher.updateAPI(api.context("test"));
    Mockito.verify(apiDAO, Mockito.times(1)).updateAPI(uuid, api.context("test").build());
}
Also used : APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) 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) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) Test(org.testng.annotations.Test)

Example 87 with APIPolicy

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

the class APIMgtAdminServiceImplTestCase method testGetApiPolicyByUuid.

@Test(description = "Test getting API policy by UUID")
public void testGetApiPolicyByUuid() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
    APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
    Mockito.when(policyDAO.getApiPolicyByUuid(apiPolicy.getUuid())).thenReturn(apiPolicy);
    adminService.getApiPolicyByUuid(apiPolicy.getUuid());
    Mockito.verify(policyDAO, Mockito.times(1)).getApiPolicyByUuid(apiPolicy.getUuid());
    // Error path
    Mockito.when(policyDAO.getApiPolicyByUuid(apiPolicy.getUuid())).thenThrow(APIMgtDAOException.class);
    try {
        adminService.getApiPolicyByUuid(apiPolicy.getUuid());
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't retrieve API policy with id: " + apiPolicy.getUuid());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 88 with APIPolicy

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

the class APIMgtAdminServiceImplTestCase method testDeletePolicy.

@Test(description = "Test delete policy")
public void testDeletePolicy() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
    APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
    Mockito.when(policyDAO.getPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, apiPolicy.getPolicyName())).thenReturn(apiPolicy);
    adminService.deletePolicy(apiPolicy.getPolicyName(), APIMgtAdminService.PolicyLevel.api);
    Mockito.verify(policyDAO, Mockito.times(1)).getPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, apiPolicy.getPolicyName());
    Mockito.verify(policyDAO, Mockito.times(1)).deletePolicy(APIMgtAdminService.PolicyLevel.api, apiPolicy.getPolicyName());
    // Error path
    Mockito.doThrow(APIMgtDAOException.class).when(policyDAO).deletePolicy(APIMgtAdminService.PolicyLevel.api, apiPolicy.getPolicyName());
    try {
        adminService.deletePolicy(apiPolicy.getPolicyName(), APIMgtAdminService.PolicyLevel.api);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't delete policy with name: " + apiPolicy.getPolicyName() + ", level: " + APIMgtAdminService.PolicyLevel.api);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 89 with APIPolicy

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

the class APIMgtAdminServiceImplTestCase method testGetApiPolicy.

@Test(description = "Test getting API policy")
public void testGetApiPolicy() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
    APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
    Mockito.when(policyDAO.getApiPolicy(apiPolicy.getPolicyName())).thenReturn(apiPolicy);
    adminService.getApiPolicy(apiPolicy.getPolicyName());
    Mockito.verify(policyDAO, Mockito.times(1)).getApiPolicy(apiPolicy.getPolicyName());
    // Error path
    Mockito.when(policyDAO.getApiPolicy(apiPolicy.getPolicyName())).thenThrow(APIMgtDAOException.class);
    try {
        adminService.getApiPolicy(apiPolicy.getPolicyName());
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't retrieve API policy with name: " + apiPolicy.getPolicyName());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 90 with APIPolicy

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

the class PolicyExportManager method createArchiveFromExecutionPlans.

public String createArchiveFromExecutionPlans(String exportedPoliciesDirName, String archiveDir, String archiveName) throws APIManagementException {
    try {
        // retrieve all policies under each policy level
        List<APIPolicy> apiPolicies = apiMgtAdminService.getApiPolicies();
        List<ApplicationPolicy> applicationPolicies = apiMgtAdminService.getApplicationPolicies();
        List<SubscriptionPolicy> subscriptionPolicies = apiMgtAdminService.getSubscriptionPolicies();
        List<CustomPolicy> customPolicies = apiMgtAdminService.getCustomRules();
        // write all execution Plans/Siddhi Apps to exportPoliciesDirName directory
        String dirLocation = exportedPoliciesDirName + File.separator + EXPORT_POLICIES;
        APIFileUtils.createDirectory(dirLocation);
        if (!apiPolicies.isEmpty()) {
            for (Map<String, String> map : getApiPolicySiddhiApps(apiPolicies)) {
                prepareFile(dirLocation, map);
            }
        }
        if (!applicationPolicies.isEmpty()) {
            prepareFile(dirLocation, getAppPolicySiddhiApps(applicationPolicies));
        }
        if (!subscriptionPolicies.isEmpty()) {
            prepareFile(dirLocation, getSubscriptionPolicySiddhiApps(subscriptionPolicies));
        }
        if (!customPolicies.isEmpty()) {
            prepareFile(dirLocation, getCustomPolicySiddhiApps(customPolicies));
        }
        // create archive and get the archive location
        String zippedFilePath = createArchiveFromPolicies(exportedPoliciesDirName, archiveDir, archiveName);
        APIFileUtils.deleteDirectory(exportedPoliciesDirName);
        return zippedFilePath;
    } catch (APIManagementException e) {
        String errorMessage = "Error while exporting policies";
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e);
    }
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Aggregations

APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)87 Test (org.testng.annotations.Test)44 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)40 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)39 ArrayList (java.util.ArrayList)38 API (org.wso2.carbon.apimgt.core.models.API)33 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)30 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)27 HashMap (java.util.HashMap)25 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)22 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)22 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)22 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)20 HashSet (java.util.HashSet)19 Test (org.junit.Test)19 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)19 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 PreparedStatement (java.sql.PreparedStatement)15