Search in sources :

Example 46 with ApiPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy in project carbon-apimgt by wso2.

the class ApplicationMappingUtilTestCase method testFromApplicationToDTO.

@Test
public void testFromApplicationToDTO() {
    String applicationID = UUID.randomUUID().toString();
    Application application1 = new Application("application1", "user1");
    application1.setId(applicationID);
    application1.setDescription("application 1");
    application1.setStatus("ACTIVE");
    application1.setPolicy(new APIPolicy("GOLD"));
    ApplicationMappingUtil applicationMappingUtil = new ApplicationMappingUtil();
    ApplicationDTO applicationDTO = applicationMappingUtil.fromApplicationToDTO(application1);
    Assert.assertEquals(applicationDTO.getApplicationId(), applicationID);
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Application(org.wso2.carbon.apimgt.core.models.Application) Test(org.junit.Test)

Example 47 with ApiPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy in project carbon-apimgt by wso2.

the class ApplicationMappingUtilTestCase method testFromApplicationsToDTO.

@Test
public void testFromApplicationsToDTO() {
    Application application1 = new Application("application1", "user1");
    application1.setId(UUID.randomUUID().toString());
    application1.setDescription("application 1");
    application1.setStatus("ACTIVE");
    application1.setPolicy(new APIPolicy("GOLD"));
    Application application2 = new Application("application2", "user1");
    application2.setId(UUID.randomUUID().toString());
    application2.setDescription("application 2");
    application2.setStatus("ACTIVE");
    application2.setPolicy(new APIPolicy("GOLD"));
    Application application3 = new Application("application3", "user1");
    application3.setId(UUID.randomUUID().toString());
    application3.setDescription("application 3");
    application3.setStatus("ACTIVE");
    application3.setPolicy(new APIPolicy("GOLD"));
    List<Application> applicationList = new ArrayList<>();
    applicationList.add(application1);
    applicationList.add(application2);
    applicationList.add(application3);
    ApplicationMappingUtil applicationMappingUtil = new ApplicationMappingUtil();
    ApplicationListDTO applicationListDTO = applicationMappingUtil.fromApplicationsToDTO(applicationList, 10, 0);
    Assert.assertEquals(applicationListDTO.getList().get(0).getName(), "application1");
}
Also used : ArrayList(java.util.ArrayList) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Application(org.wso2.carbon.apimgt.core.models.Application) ApplicationListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationListDTO) Test(org.junit.Test)

Example 48 with ApiPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testDeletePolicyByUuid.

@Test(description = "Test delete policy by UUID")
public void testDeletePolicyByUuid() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
    APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
    adminService.deletePolicyByUuid(apiPolicy.getUuid(), APIMgtAdminService.PolicyLevel.api);
    // Error path
    Mockito.doThrow(APIMgtDAOException.class).when(policyDAO).deletePolicyByUuid(APIMgtAdminService.PolicyLevel.api, apiPolicy.getUuid());
    try {
        adminService.deletePolicyByUuid(apiPolicy.getUuid(), APIMgtAdminService.PolicyLevel.api);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't delete policy with id: " + apiPolicy.getUuid() + ", 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 49 with ApiPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testUpdateAPIWithRestrictedVisibilityButDifferentContext.

@Test(description = "Test UpdateAPI with restricted visibility but different context")
public void testUpdateAPIWithRestrictedVisibilityButDifferentContext() throws APIManagementException {
    Set<String> visibleRoles = new HashSet<>();
    visibleRoles.add(ADMIN_ROLE);
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    API.APIBuilder api = SampleTestObjectCreator.createDefaultAPI().visibility(API.Visibility.RESTRICTED).visibleRoles(visibleRoles);
    String uuid = api.getId();
    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.getAPI(uuid)).thenReturn(api.build());
    Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
    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"));
    Mockito.verify(apiDAO, Mockito.times(1)).getAPI(uuid);
    Mockito.verify(apiDAO, Mockito.times(1)).isAPIContextExists(api.getContext());
    Mockito.verify(apiDAO, Mockito.times(1)).updateAPI(uuid, api.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) 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 50 with ApiPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testAddPolicy.

@Test(description = "Add policy")
public void testAddPolicy() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
    APIPolicy policy = SampleTestObjectCreator.createDefaultAPIPolicy();
    adminService.addApiPolicy(policy);
    Mockito.verify(policyDAO, Mockito.times(1)).addApiPolicy(policy);
    // Error path
    Mockito.doThrow(APIMgtDAOException.class).when(policyDAO).addApiPolicy(policy);
    try {
        adminService.addApiPolicy(policy);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't add API policy for uuid: " + policy.getUuid());
    }
}
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)

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