Search in sources :

Example 11 with EventProcessorService

use of org.wso2.carbon.event.processor.core.EventProcessorService in project carbon-apimgt by wso2.

the class PolicyUtilTest method testUpdatePolicy_APPType.

@Test
public void testUpdatePolicy_APPType() throws ExecutionPlanConfigurationException, ExecutionPlanDependencyValidationException {
    Mockito.when(eventProcessorService.getActiveExecutionPlan(Mockito.anyString())).thenReturn("EXECUTION_PLAN");
    ApplicationPolicy policy = TestUtil.getPolicyAppLevel();
    ApplicationPolicyEvent policyEvent = new ApplicationPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_UPDATE.name(), -1234, policy.getTenantDomain(), policy.getId(), policy.getName(), policy.getDefaultLimit().getQuotaType());
    PolicyUtil.deployPolicy(policy, policyEvent);
    Mockito.verify(eventProcessorService, Mockito.times(1)).editActiveExecutionPlan(Mockito.anyString(), Mockito.anyString());
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy) ApplicationPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with EventProcessorService

use of org.wso2.carbon.event.processor.core.EventProcessorService in project carbon-apimgt by wso2.

the class PolicyUtilTest method testDeployAllPolicies.

@Test
public void testDeployAllPolicies() throws ExecutionPlanConfigurationException, ThrottlePolicyDeployerException, ExecutionPlanDependencyValidationException {
    ExecutionPlanConfigurationException executionPlanConfigurationException = Mockito.mock(ExecutionPlanConfigurationException.class);
    Mockito.when(eventProcessorService.getActiveExecutionPlan(Mockito.anyString())).thenThrow(executionPlanConfigurationException);
    Map<String, ExecutionPlanConfiguration> executionPlanConfigurationMap = new HashMap<>();
    ExecutionPlanConfiguration executionPlanConfiguration = Mockito.mock(ExecutionPlanConfiguration.class);
    executionPlanConfigurationMap.put("sample_policy", executionPlanConfiguration);
    Mockito.when(eventProcessorService.getAllActiveExecutionConfigurations()).thenReturn(executionPlanConfigurationMap);
    SubscriptionPolicyList subscriptionPolicyList = new SubscriptionPolicyList();
    SubscriptionPolicy subscriptionPolicy = TestUtil.getPolicySubLevel();
    List<SubscriptionPolicy> subscriptionPolicies = new ArrayList<>();
    subscriptionPolicies.add(subscriptionPolicy);
    subscriptionPolicyList.setList(subscriptionPolicies);
    Mockito.when(policyRetriever.getAllSubscriptionPolicies()).thenReturn(subscriptionPolicyList);
    ApplicationPolicyList applicationPolicyList = new ApplicationPolicyList();
    ApplicationPolicy applicationPolicy = TestUtil.getPolicyAppLevel();
    List<ApplicationPolicy> applicationPolicies = new ArrayList<>();
    applicationPolicies.add(applicationPolicy);
    applicationPolicyList.setList(applicationPolicies);
    Mockito.when(policyRetriever.getAllApplicationPolicies()).thenReturn(applicationPolicyList);
    ApiPolicyList apiPolicyList = new ApiPolicyList();
    ApiPolicy apiPolicy = TestUtil.getPolicyAPILevel();
    List<ApiPolicy> apiPolicies = new ArrayList<>();
    apiPolicies.add(apiPolicy);
    apiPolicyList.setList(apiPolicies);
    Mockito.when(policyRetriever.getAllApiPolicies()).thenReturn(apiPolicyList);
    GlobalPolicyList globalPolicyList = new GlobalPolicyList();
    GlobalPolicy globalPolicy = TestUtil.getPolicyGlobalLevel();
    List<GlobalPolicy> globalPolicies = new ArrayList<>();
    globalPolicies.add(globalPolicy);
    globalPolicyList.setList(globalPolicies);
    Mockito.when(policyRetriever.getAllGlobalPolicies()).thenReturn(globalPolicyList);
    PolicyUtil.deployAllPolicies();
    Mockito.verify(eventProcessorService, Mockito.times(5)).deployExecutionPlan(Mockito.anyString());
}
Also used : ApplicationPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicyList) HashMap(java.util.HashMap) GlobalPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy) ArrayList(java.util.ArrayList) ApiPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy) ExecutionPlanConfigurationException(org.wso2.carbon.event.processor.core.exception.ExecutionPlanConfigurationException) SubscriptionPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy) ExecutionPlanConfiguration(org.wso2.carbon.event.processor.core.ExecutionPlanConfiguration) ApplicationPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy) SubscriptionPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicyList) ApiPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicyList) GlobalPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicyList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with EventProcessorService

use of org.wso2.carbon.event.processor.core.EventProcessorService in project carbon-apimgt by wso2.

the class PolicyUtilTest method testDeletePolicy_APIType.

@Test
public void testDeletePolicy_APIType() throws ExecutionPlanConfigurationException {
    ApiPolicy policy = TestUtil.getPolicyAPILevel();
    List<Integer> deletedConditionGroupIds = new ArrayList<>();
    deletedConditionGroupIds.add(5);
    deletedConditionGroupIds.add(6);
    Mockito.when(eventProcessorService.getActiveExecutionPlan(policy.getTenantDomain() + "_" + PolicyConstants.POLICY_LEVEL_RESOURCE + "_" + policy.getName() + "_condition_5")).thenReturn("EXECUTION_PLAN");
    Mockito.when(eventProcessorService.getActiveExecutionPlan(policy.getTenantDomain() + "_" + PolicyConstants.POLICY_LEVEL_RESOURCE + "_" + policy.getName() + "_condition_6")).thenReturn("EXECUTION_PLAN");
    Mockito.when(eventProcessorService.getActiveExecutionPlan(policy.getTenantDomain() + "_" + PolicyConstants.POLICY_LEVEL_RESOURCE + "_" + policy.getName() + "_default")).thenReturn("EXECUTION_PLAN");
    APIPolicyEvent policyEvent = new APIPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_DELETE.name(), -1234, policy.getTenantDomain(), policy.getId(), policy.getName(), policy.getDefaultLimit().getQuotaType(), null, deletedConditionGroupIds);
    PolicyUtil.undeployPolicy(policyEvent);
    Mockito.verify(eventProcessorService, Mockito.times(3)).undeployActiveExecutionPlan(Mockito.anyString());
}
Also used : APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) ApiPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy) ArrayList(java.util.ArrayList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 14 with EventProcessorService

use of org.wso2.carbon.event.processor.core.EventProcessorService in project carbon-apimgt by wso2.

the class PolicyUtilTest method testAddPolicy_SubType.

@Test
public void testAddPolicy_SubType() throws ExecutionPlanConfigurationException, ExecutionPlanDependencyValidationException {
    ExecutionPlanConfigurationException executionPlanConfigurationException = Mockito.mock(ExecutionPlanConfigurationException.class);
    Mockito.when(eventProcessorService.getActiveExecutionPlan(Mockito.anyString())).thenThrow(executionPlanConfigurationException);
    SubscriptionPolicy policy = TestUtil.getPolicySubLevel();
    SubscriptionPolicyEvent policyEvent = new SubscriptionPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_CREATE.name(), -1234, policy.getTenantDomain(), policy.getId(), policy.getName(), policy.getDefaultLimit().getQuotaType(), policy.getRateLimitCount(), policy.getRateLimitTimeUnit(), policy.isStopOnQuotaReach(), policy.getGraphQLMaxDepth(), policy.getGraphQLMaxComplexity(), policy.getSubscriberCount());
    PolicyUtil.deployPolicy(policy, policyEvent);
    Mockito.verify(eventProcessorService, Mockito.times(1)).deployExecutionPlan(Mockito.anyString());
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy) ExecutionPlanConfigurationException(org.wso2.carbon.event.processor.core.exception.ExecutionPlanConfigurationException) SubscriptionPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with EventProcessorService

use of org.wso2.carbon.event.processor.core.EventProcessorService in project carbon-apimgt by wso2.

the class PolicyUtilTest method setUp.

@Before
public void setUp() throws Exception {
    System.setProperty("carbon.home", PolicyUtilTest.class.getResource("/").getFile());
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn("carbon.super");
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    eventProcessorService = Mockito.mock(EventProcessorService.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    Mockito.when(serviceReferenceHolder.getEventProcessorService()).thenReturn(eventProcessorService);
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class);
    String[] skipThrottlePolicies = { "skipPolicy1" };
    Mockito.when(throttleProperties.getSkipRedeployingPolicies()).thenReturn(skipThrottlePolicies);
    Mockito.when(apiManagerConfiguration.getThrottleProperties()).thenReturn(throttleProperties);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.VELOCITY_LOGGER)).thenReturn(null);
    Mockito.when(serviceReferenceHolder.getAPIMConfiguration()).thenReturn(apiManagerConfiguration);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    policyRetriever = Mockito.mock(PolicyRetriever.class);
    PowerMockito.whenNew(PolicyRetriever.class).withNoArguments().thenReturn(policyRetriever);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.throttle.policy.deployer.internal.ServiceReferenceHolder) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) EventProcessorService(org.wso2.carbon.event.processor.core.EventProcessorService) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) PolicyRetriever(org.wso2.carbon.apimgt.throttle.policy.deployer.PolicyRetriever) Before(org.junit.Before)

Aggregations

Test (org.junit.Test)13 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)13 ExecutionPlanConfigurationException (org.wso2.carbon.event.processor.core.exception.ExecutionPlanConfigurationException)9 ApiPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy)5 ApplicationPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy)5 GlobalPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy)5 SubscriptionPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy)5 ArrayList (java.util.ArrayList)4 APIPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent)4 EventProcessorService (org.wso2.carbon.event.processor.core.EventProcessorService)4 HashMap (java.util.HashMap)3 ApplicationPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent)3 GlobalPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent)3 SubscriptionPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent)3 Map (java.util.Map)2 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)2 ExecutionPlanConfiguration (org.wso2.carbon.event.processor.core.ExecutionPlanConfiguration)2 Before (org.junit.Before)1 ThrottleProperties (org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)1 APITemplateException (org.wso2.carbon.apimgt.impl.template.APITemplateException)1