Search in sources :

Example 21 with ApiPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testUpdateApiEndpointUrl.

@Test(description = "Test add api with production endpoint")
public void testUpdateApiEndpointUrl() throws APIManagementException {
    /**
     * this test method verify the API Add with correct API object get invoked correctly
     */
    Endpoint endpoint1 = new Endpoint.Builder().id(UUID.randomUUID().toString()).endpointConfig("http://localhost").name("endpoint1").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build();
    Endpoint endpoint2 = new Endpoint.Builder().id(UUID.randomUUID().toString()).endpointConfig("http://localhost").name("endpoint2").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build();
    Map<String, Endpoint> endpointMap = new HashMap<>();
    endpointMap.put(APIMgtConstants.PRODUCTION_ENDPOINT, endpoint1);
    endpointMap.put(APIMgtConstants.SANDBOX_ENDPOINT, endpoint2);
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id(UUID.randomUUID().toString()).endpoint(endpointMap);
    apiBuilder.apiPermission("");
    apiBuilder.permissionMap(null);
    apiBuilder.policies(Collections.emptySet());
    apiBuilder.apiPolicy(null);
    ApiDAO apiDAO = Mockito.mock(ApiDAO.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);
    LabelDAO labelDao = Mockito.mock(LabelDAO.class);
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO, labelDao);
    Mockito.when(apiDAO.getEndpoint(endpoint1.getId())).thenReturn(endpoint1);
    Mockito.when(apiDAO.getEndpoint(endpoint2.getId())).thenReturn(endpoint2);
    Mockito.when(apiDAO.getEndpoint(endpoint1.getName())).thenReturn(endpoint1);
    Mockito.when(apiDAO.getEndpointByName(endpoint2.getName())).thenReturn(endpoint2);
    Endpoint endpoint3 = new Endpoint.Builder(endpoint2).maxTps(1200).build();
    Map<String, Endpoint> updatedEndpointMap = new HashMap<>(endpointMap);
    updatedEndpointMap.replace(APIMgtConstants.SANDBOX_ENDPOINT, endpoint3);
    apiBuilder.endpoint(updatedEndpointMap);
    Mockito.when(apiDAO.getApiSwaggerDefinition(apiBuilder.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
    apiPublisher.updateAPI(apiBuilder);
    Mockito.verify(apiDAO, Mockito.times(1)).updateAPI(apiBuilder.getId(), apiBuilder.build());
}
Also used : HashMap(java.util.HashMap) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) WorkflowExtensionsConfigBuilder(org.wso2.carbon.apimgt.core.workflow.WorkflowExtensionsConfigBuilder) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) 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) 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 22 with ApiPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testAddApi.

@Test(description = "Test add api with production endpoint")
public void testAddApi() throws APIManagementException, LifecycleException {
    /**
     * this test method verify the API Add with correct API object get invoked correctly
     */
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id("").endpoint(SampleTestObjectCreator.getMockEndpointMap());
    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);
    // Error path
    // When an APIMgtDAOException is being thrown when the API is created
    Mockito.doThrow(APIMgtDAOException.class).when(apiDAO).addAPI(apiBuilder.build());
    try {
        apiPublisher.addAPI(apiBuilder);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while creating the API - " + apiBuilder.getName());
    }
    // Error path
    // When an GatewayException is being thrown when an error occurred while adding API to the Gateway
    Mockito.doThrow(GatewayException.class).when(gateway).addAPI(apiBuilder.build());
    try {
        apiPublisher.addAPI(apiBuilder);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while adding API - " + apiBuilder.getName() + " to gateway");
    }
    // Error path
    // When an APITemplateException is being thrown when generating API configuration for API
    Mockito.when(gatewaySourceGenerator.getConfigStringFromTemplate(Mockito.any())).thenThrow(APITemplateException.class);
    try {
        apiPublisher.addAPI(apiBuilder);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error generating API configuration for API " + apiBuilder.getName());
    }
}
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) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) 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 23 with ApiPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testAddApiSandboxEndpoint.

@Test(description = "Test add api with sandbox endpoint")
public void testAddApiSandboxEndpoint() throws APIManagementException, LifecycleException {
    Map<String, Endpoint> endpointMap = new HashMap<>();
    Map<String, Endpoint> resourceEndpointMap = new HashMap<>();
    Map<String, UriTemplate> uriTemplateMap = new HashMap();
    UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
    uriTemplateBuilder.endpoint(resourceEndpointMap);
    uriTemplateBuilder.templateId("getApisApiIdGet");
    uriTemplateBuilder.uriTemplate("/apis/");
    uriTemplateBuilder.authType(APIMgtConstants.AUTH_APPLICATION_LEVEL_TOKEN);
    uriTemplateBuilder.policy(APIUtils.getDefaultAPIPolicy());
    uriTemplateBuilder.httpVerb("GET");
    uriTemplateMap.put("getApisApiIdGet", uriTemplateBuilder.build());
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id("").endpoint(endpointMap).uriTemplates(uriTemplateMap);
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
    String endpointId = String.valueOf(apiBuilder.getEndpoint().get("sandbox"));
    Endpoint endpoint = new Endpoint.Builder().id(endpointId).name("testEndpoint").build();
    Mockito.when(apiDAO.getEndpoint(endpointId)).thenReturn(endpoint);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    LabelDAO labelDao = Mockito.mock(LabelDAO.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);
    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 : HashMap(java.util.HashMap) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) 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) Test(org.testng.annotations.Test)

Example 24 with ApiPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultAPI.

public static API.APIBuilder createDefaultAPI() {
    Set<String> transport = new HashSet<>();
    transport.add(HTTP);
    transport.add(HTTPS);
    Set<String> tags = new HashSet<>();
    tags.add(TAG_CLIMATE);
    Set<Policy> policies = new HashSet<>();
    policies.add(goldSubscriptionPolicy);
    policies.add(silverSubscriptionPolicy);
    policies.add(bronzeSubscriptionPolicy);
    BusinessInformation businessInformation = new BusinessInformation();
    businessInformation.setBusinessOwner(NAME_BUSINESS_OWNER_1);
    businessInformation.setBusinessOwnerEmail(EMAIL_BUSINESS_OWNER_1);
    businessInformation.setTechnicalOwner(NAME_TECHNICAL_OWNER_1);
    businessInformation.setTechnicalOwnerEmail(EMAIL_TECHNICAL_OWNER_1);
    String permissionJson = "[{\"groupId\" : \"developer\", \"permission\" : " + "[\"READ\",\"UPDATE\"]},{\"groupId\" : \"admin\", \"permission\" : [\"READ\",\"UPDATE\"," + "\"DELETE\", \"MANAGE_SUBSCRIPTION\"]}]";
    Set<String> visibleRoles = new HashSet<>();
    visibleRoles.add("testRple");
    List<String> labels = new ArrayList<>();
    labels.add("testLabel");
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    corsConfiguration.setEnabled(true);
    corsConfiguration.setAllowMethods(Arrays.asList(APIMgtConstants.FunctionsConstants.GET, APIMgtConstants.FunctionsConstants.POST, APIMgtConstants.FunctionsConstants.DELETE));
    corsConfiguration.setAllowHeaders(Arrays.asList(ALLOWED_HEADER_AUTHORIZATION, ALLOWED_HEADER_CUSTOM));
    corsConfiguration.setAllowCredentials(true);
    corsConfiguration.setAllowOrigins(Arrays.asList("*"));
    Map<String, Endpoint> endpointMap = new HashMap<>();
    endpointMap.put("TestEndpoint", createMockEndpoint());
    API.APIBuilder apiBuilder = new API.APIBuilder(ADMIN, "WeatherAPI", API_VERSION).id(UUID.randomUUID().toString()).context("weather").description("Get Weather Info").lifeCycleStatus(APIStatus.CREATED.getStatus()).lifecycleInstanceId(UUID.randomUUID().toString()).endpoint(Collections.emptyMap()).wsdlUri("http://localhost:9443/echo?wsdl").isResponseCachingEnabled(false).cacheTimeout(60).isDefaultVersion(false).apiPolicy(unlimitedApiPolicy).transport(transport).tags(tags).policies(policies).visibility(API.Visibility.PUBLIC).visibleRoles(visibleRoles).businessInformation(businessInformation).corsConfiguration(corsConfiguration).createdTime(LocalDateTime.now()).createdBy(ADMIN).updatedBy(ADMIN).lastUpdatedTime(LocalDateTime.now()).apiPermission(permissionJson).uriTemplates(getMockUriTemplates()).apiDefinition(apiDefinition).workflowStatus(WORKFLOW_STATUS).labels(labels).endpoint(endpointMap);
    Map map = new HashMap();
    map.put(DEVELOPER_ROLE_ID, 6);
    map.put(ADMIN_ROLE_ID, 15);
    apiBuilder.permissionMap(map);
    return apiBuilder;
}
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) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 25 with ApiPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy in project carbon-apimgt by wso2.

the class AdvancedThrottlePolicyMappingUtil method fromAPIPolicyArrayToListDTO.

/**
 * Converts an array of Advanced Policy objects into a List DTO
 *
 * @param policies Array of Advanced Policies
 * @return A List DTO of converted Advanced Policies
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 * @throws UnsupportedThrottleConditionTypeException - If error occurs
 */
public static AdvancedThrottlePolicyListDTO fromAPIPolicyArrayToListDTO(List<APIPolicy> policies) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    AdvancedThrottlePolicyListDTO listDTO = new AdvancedThrottlePolicyListDTO();
    List<AdvancedThrottlePolicyDTO> advancedPolicyDTOs = new ArrayList<>();
    if (policies != null) {
        for (APIPolicy policy : policies) {
            advancedPolicyDTOs.add(fromAdvancedPolicyToDTO(policy));
        }
    }
    listDTO.setList(advancedPolicyDTOs);
    listDTO.setCount(advancedPolicyDTOs.size());
    return listDTO;
}
Also used : AdvancedThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyListDTO) ArrayList(java.util.ArrayList) AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO) 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